|
| 1 | +using JetBrains.Annotations; |
| 2 | +using NUnit.Framework; |
| 3 | +using Robust.Shared.GameObjects; |
| 4 | +using Robust.Shared.IoC; |
| 5 | +using Robust.Shared.Prototypes; |
| 6 | +using Robust.Shared.Serialization.Manager; |
| 7 | +using Robust.Shared.Serialization.Manager.Attributes; |
| 8 | + |
| 9 | +namespace Robust.UnitTesting.Shared.Prototypes; |
| 10 | + |
| 11 | +[UsedImplicitly] |
| 12 | +[TestFixture] |
| 13 | +internal sealed partial class PrototypeVariantizationTest : OurRobustUnitTest |
| 14 | +{ |
| 15 | + private const string TestProtoId = "TestPrototype"; |
| 16 | + private const string TestProtoVariantAId = "TestPrototypeVariantA"; |
| 17 | + private const string TestProtoVariantBId = "TestPrototypeVariantB"; |
| 18 | + |
| 19 | + private IPrototypeManager protoManager = default!; |
| 20 | + |
| 21 | + protected override Type[] ExtraComponents => new[] { typeof(PrototypeVariantizationTestComponent) }; |
| 22 | + |
| 23 | + [OneTimeSetUp] |
| 24 | + public void Setup() |
| 25 | + { |
| 26 | + IoCManager.Resolve<ISerializationManager>().Initialize(); |
| 27 | + protoManager = IoCManager.Resolve<IPrototypeManager>(); |
| 28 | + protoManager.Initialize(); |
| 29 | + protoManager.LoadString(DOCUMENT, changed: new()); |
| 30 | + protoManager.ResolveResults(); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Tests that the prototypes defined in the test YAML document, as well as their expected variants, |
| 35 | + /// are properly generated and can be resolved from the prototype manager. |
| 36 | + /// </summary> |
| 37 | + [Test] |
| 38 | + public void TestPrototypesExist() |
| 39 | + { |
| 40 | + Assert.Multiple(() => |
| 41 | + { |
| 42 | + Assert.That(protoManager.Resolve<EntityPrototype>(TestProtoId, out var _)); |
| 43 | + Assert.That(protoManager.Resolve<EntityPrototype>(TestProtoVariantAId, out _)); |
| 44 | + Assert.That(protoManager.Resolve<EntityPrototype>(TestProtoVariantBId, out _)); |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Tests that the value modifications defined in the test YAML document are properly applied to all variants. |
| 50 | + /// </summary> |
| 51 | + [Test] |
| 52 | + public void TestValueModification() |
| 53 | + { |
| 54 | + // Original |
| 55 | + Assert.Multiple(() => |
| 56 | + { |
| 57 | + var testProto = protoManager.Index<EntityPrototype>(TestProtoId); |
| 58 | + Assert.That(testProto.Components, Contains.Key("PrototypeVariantizationTest")); |
| 59 | + |
| 60 | + var comp = testProto.Components["PrototypeVariantizationTest"].Component as PrototypeVariantizationTestComponent; |
| 61 | + Assert.That(comp, Is.Not.Null); |
| 62 | + Assert.That(comp!.NumValue, Is.EqualTo(1)); |
| 63 | + Assert.That(comp!.EnumValue, Is.EqualTo(PrototypeVariantizationTestEnum.First)); |
| 64 | + Assert.That(comp!.StringValue, Is.EqualTo("string-1")); |
| 65 | + Assert.That(comp!.StringArray.SequenceEqual(["el-1"])); |
| 66 | + |
| 67 | + Assert.That(comp!.StringDict.TryGetValue("key1", out var value1)); |
| 68 | + Assert.That(value1, Is.EqualTo("string-1a")); |
| 69 | + Assert.That(comp!.StringDict.TryGetValue("key2", out var value2)); |
| 70 | + Assert.That(value2, Is.EqualTo("string-1b")); |
| 71 | + |
| 72 | + Assert.That(comp!.RecursiveRecord?.Child?.Child?.Value, Is.EqualTo("child-1")); |
| 73 | + }); |
| 74 | + |
| 75 | + // First variant |
| 76 | + Assert.Multiple(() => |
| 77 | + { |
| 78 | + var testProtoVariantA = protoManager.Index<EntityPrototype>(TestProtoVariantAId); |
| 79 | + Assert.That(testProtoVariantA.Components, Contains.Key("PrototypeVariantizationTest")); |
| 80 | + |
| 81 | + var compVariantA = testProtoVariantA.Components["PrototypeVariantizationTest"].Component as PrototypeVariantizationTestComponent; |
| 82 | + Assert.That(compVariantA, Is.Not.Null); |
| 83 | + Assert.That(compVariantA!.NumValue, Is.EqualTo(1.5f)); |
| 84 | + Assert.That(compVariantA!.EnumValue, Is.EqualTo(PrototypeVariantizationTestEnum.Second)); |
| 85 | + Assert.That(compVariantA!.StringValue, Is.EqualTo("string-2")); |
| 86 | + Assert.That(compVariantA!.StringArray.SequenceEqual(["el-1", "el-2"])); |
| 87 | + |
| 88 | + Assert.That(compVariantA!.StringDict.TryGetValue("key1", out var value1)); |
| 89 | + Assert.That(value1, Is.EqualTo("string-2a")); |
| 90 | + Assert.That(compVariantA!.StringDict.TryGetValue("key2", out var value2)); |
| 91 | + Assert.That(value2, Is.EqualTo("string-2b")); |
| 92 | + |
| 93 | + Assert.That(compVariantA!.RecursiveRecord?.Child?.Child?.Value, Is.EqualTo("child-2")); |
| 94 | + }); |
| 95 | + |
| 96 | + // Second variant |
| 97 | + Assert.Multiple(() => |
| 98 | + { |
| 99 | + var testProtoVariantB = protoManager.Index<EntityPrototype>(TestProtoVariantBId); |
| 100 | + Assert.That(testProtoVariantB.Components, Contains.Key("PrototypeVariantizationTest")); |
| 101 | + |
| 102 | + var compVariantB = testProtoVariantB.Components["PrototypeVariantizationTest"].Component as PrototypeVariantizationTestComponent; |
| 103 | + Assert.That(compVariantB, Is.Not.Null); |
| 104 | + Assert.That(compVariantB!.NumValue, Is.EqualTo(2)); |
| 105 | + Assert.That(compVariantB!.EnumValue, Is.EqualTo(PrototypeVariantizationTestEnum.Third)); |
| 106 | + Assert.That(compVariantB!.StringValue, Is.EqualTo("string-3")); |
| 107 | + Assert.That(compVariantB!.StringArray.SequenceEqual(["el-1", "el-2", "el-3"])); |
| 108 | + |
| 109 | + Assert.That(compVariantB!.StringDict.TryGetValue("key1", out var value1)); |
| 110 | + Assert.That(value1, Is.EqualTo("string-3a")); |
| 111 | + Assert.That(compVariantB!.StringDict.TryGetValue("key2", out var value2)); |
| 112 | + Assert.That(value2, Is.EqualTo("string-3b")); |
| 113 | + |
| 114 | + Assert.That(compVariantB!.RecursiveRecord?.Child?.Child?.Value, Is.EqualTo("child-3")); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + const string DOCUMENT = $@" |
| 119 | +- type: entity |
| 120 | + id: !type:CreateVariants |
| 121 | + values: [ TestPrototype, TestPrototypeVariantA, TestPrototypeVariantB ] |
| 122 | + components: |
| 123 | + - type: PrototypeVariantizationTest |
| 124 | + numValue: !type:CreateVariants |
| 125 | + values: [ 1, 1.5, 2 ] |
| 126 | + enumValue: !type:CreateVariants |
| 127 | + values: [ 0, 1, 2 ] |
| 128 | + stringValue: !type:CreateVariants |
| 129 | + values: [ string-1, string-2, string-3 ] |
| 130 | + stringArray: !type:CreateVariants |
| 131 | + sequences: [ [ el-1 ], [ el-1, el-2 ], [ el-1, el-2, el-3 ] ] |
| 132 | + stringDict: |
| 133 | + key1: !type:CreateVariants |
| 134 | + values: [ string-1a, string-2a, string-3a ] |
| 135 | + key2: !type:CreateVariants |
| 136 | + values: [ string-1b, string-2b, string-3b ] |
| 137 | + recursiveRecord: !type:RecursionTestRecord |
| 138 | + child: |
| 139 | + child: |
| 140 | + value: !type:CreateVariants |
| 141 | + values: [ child-1, child-2, child-3 ] |
| 142 | +"; |
| 143 | +} |
| 144 | + |
| 145 | +internal sealed partial class PrototypeVariantizationTestComponent : Component |
| 146 | +{ |
| 147 | + [DataField] public float NumValue = -1; |
| 148 | + [DataField] public PrototypeVariantizationTestEnum EnumValue = PrototypeVariantizationTestEnum.Invalid; |
| 149 | + [DataField] public string StringValue = string.Empty; |
| 150 | + [DataField] public string[] StringArray = Array.Empty<string>(); |
| 151 | + [DataField] public Dictionary<string, string> StringDict = new(); |
| 152 | + [DataField] public RecursionTestRecord RecursiveRecord = new(); |
| 153 | +} |
| 154 | + |
| 155 | +[DataDefinition] |
| 156 | +internal sealed partial record RecursionTestRecord |
| 157 | +{ |
| 158 | + [DataField] public RecursionTestRecord? Child = null; |
| 159 | + [DataField] public string Value = string.Empty; |
| 160 | +} |
| 161 | + |
| 162 | +public enum PrototypeVariantizationTestEnum : int |
| 163 | +{ |
| 164 | + Invalid = -1, |
| 165 | + First = 0, |
| 166 | + Second = 1, |
| 167 | + Third = 2, |
| 168 | +} |
| 169 | + |
0 commit comments