1
0

StaticFieldValidationTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Content.Shared.Tag;
  4. using Robust.Shared.GameObjects;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Reflection;
  7. using Robust.Shared.Serialization.Manager.Attributes;
  8. namespace Content.IntegrationTests.Tests.Linter;
  9. /// <summary>
  10. /// Verify that the yaml linter successfully validates static fields
  11. /// </summary>
  12. [TestFixture]
  13. public sealed class StaticFieldValidationTest
  14. {
  15. [Test]
  16. public async Task TestStaticFieldValidation()
  17. {
  18. await using var pair = await PoolManager.GetServerClient();
  19. var protoMan = pair.Server.ProtoMan;
  20. var protos = new Dictionary<Type, HashSet<string>>();
  21. foreach (var kind in protoMan.EnumeratePrototypeKinds())
  22. {
  23. var ids = protoMan.EnumeratePrototypes(kind).Select(x => x.ID).ToHashSet();
  24. protos.Add(kind, ids);
  25. }
  26. Assert.That(protoMan.ValidateStaticFields(typeof(StringValid), protos), Is.Empty);
  27. Assert.That(protoMan.ValidateStaticFields(typeof(StringArrayValid), protos), Is.Empty);
  28. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdValid), protos), Is.Empty);
  29. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdTValid), protos), Is.Empty);
  30. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdArrayValid), protos), Is.Empty);
  31. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdTArrayValid), protos), Is.Empty);
  32. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdTestValid), protos), Is.Empty);
  33. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdArrayValid), protos), Is.Empty);
  34. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdListValid), protos), Is.Empty);
  35. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdSetValid), protos), Is.Empty);
  36. Assert.That(protoMan.ValidateStaticFields(typeof(PrivateProtoIdArrayValid), protos), Is.Empty);
  37. Assert.That(protoMan.ValidateStaticFields(typeof(StringInvalid), protos), Has.Count.EqualTo(1));
  38. Assert.That(protoMan.ValidateStaticFields(typeof(StringArrayInvalid), protos), Has.Count.EqualTo(2));
  39. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdInvalid), protos), Has.Count.EqualTo(1));
  40. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdTInvalid), protos), Has.Count.EqualTo(1));
  41. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdArrayInvalid), protos), Has.Count.EqualTo(2));
  42. Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdTArrayInvalid), protos), Has.Count.EqualTo(2));
  43. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdTestInvalid), protos), Has.Count.EqualTo(1));
  44. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdArrayInvalid), protos), Has.Count.EqualTo(2));
  45. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdListInvalid), protos), Has.Count.EqualTo(2));
  46. Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdSetInvalid), protos), Has.Count.EqualTo(2));
  47. Assert.That(protoMan.ValidateStaticFields(typeof(PrivateProtoIdArrayInvalid), protos), Has.Count.EqualTo(2));
  48. await pair.CleanReturnAsync();
  49. }
  50. [TestPrototypes]
  51. private const string TestPrototypes = @"
  52. - type: entity
  53. id: StaticFieldTestEnt
  54. - type: Tag
  55. id: StaticFieldTestTag
  56. ";
  57. [Reflect(false)]
  58. private sealed class StringValid
  59. {
  60. [ValidatePrototypeId<TagPrototype>] public static string Tag = "StaticFieldTestTag";
  61. }
  62. [Reflect(false)]
  63. private sealed class StringInvalid
  64. {
  65. [ValidatePrototypeId<TagPrototype>] public static string Tag = string.Empty;
  66. }
  67. [Reflect(false)]
  68. private sealed class StringArrayValid
  69. {
  70. [ValidatePrototypeId<TagPrototype>] public static string[] Tag = ["StaticFieldTestTag", "StaticFieldTestTag"];
  71. }
  72. [Reflect(false)]
  73. private sealed class StringArrayInvalid
  74. {
  75. [ValidatePrototypeId<TagPrototype>] public static string[] Tag = [string.Empty, "StaticFieldTestTag", string.Empty];
  76. }
  77. [Reflect(false)]
  78. private sealed class EntProtoIdValid
  79. {
  80. public static EntProtoId Tag = "StaticFieldTestEnt";
  81. }
  82. [Reflect(false)]
  83. private sealed class EntProtoIdTValid
  84. {
  85. public static EntProtoId<TransformComponent> Tag = "StaticFieldTestEnt";
  86. }
  87. [Reflect(false)]
  88. private sealed class EntProtoIdInvalid
  89. {
  90. public static EntProtoId Tag = string.Empty;
  91. }
  92. [Reflect(false)]
  93. private sealed class EntProtoIdTInvalid
  94. {
  95. public static EntProtoId<TransformComponent> Tag = string.Empty;
  96. }
  97. [Reflect(false)]
  98. private sealed class EntProtoIdArrayValid
  99. {
  100. public static EntProtoId[] Tag = ["StaticFieldTestEnt", "StaticFieldTestEnt"];
  101. }
  102. [Reflect(false)]
  103. private sealed class EntProtoIdTArrayValid
  104. {
  105. public static EntProtoId<TransformComponent>[] Tag = ["StaticFieldTestEnt", "StaticFieldTestEnt"];
  106. }
  107. [Reflect(false)]
  108. private sealed class EntProtoIdArrayInvalid
  109. {
  110. public static EntProtoId[] Tag = [string.Empty, "StaticFieldTestEnt", string.Empty];
  111. }
  112. [Reflect(false)]
  113. private sealed class EntProtoIdTArrayInvalid
  114. {
  115. public static EntProtoId<TransformComponent>[] Tag = [string.Empty, "StaticFieldTestEnt", string.Empty];
  116. }
  117. [Reflect(false)]
  118. private sealed class ProtoIdTestValid
  119. {
  120. public static ProtoId<TagPrototype> Tag = "StaticFieldTestTag";
  121. }
  122. [Reflect(false)]
  123. private sealed class ProtoIdTestInvalid
  124. {
  125. public static ProtoId<TagPrototype> Tag = string.Empty;
  126. }
  127. [Reflect(false)]
  128. private sealed class ProtoIdArrayValid
  129. {
  130. public static ProtoId<TagPrototype>[] Tag = ["StaticFieldTestTag", "StaticFieldTestTag"];
  131. }
  132. [Reflect(false)]
  133. private sealed class ProtoIdArrayInvalid
  134. {
  135. public static ProtoId<TagPrototype>[] Tag = [string.Empty, "StaticFieldTestTag", string.Empty];
  136. }
  137. [Reflect(false)]
  138. private sealed class ProtoIdListValid
  139. {
  140. public static List<ProtoId<TagPrototype>> Tag = ["StaticFieldTestTag", "StaticFieldTestTag"];
  141. }
  142. [Reflect(false)]
  143. private sealed class ProtoIdListInvalid
  144. {
  145. public static List<ProtoId<TagPrototype>> Tag = [string.Empty, "StaticFieldTestTag", string.Empty];
  146. }
  147. [Reflect(false)]
  148. private sealed class ProtoIdSetValid
  149. {
  150. public static HashSet<ProtoId<TagPrototype>> Tag = ["StaticFieldTestTag", "StaticFieldTestTag"];
  151. }
  152. [Reflect(false)]
  153. private sealed class ProtoIdSetInvalid
  154. {
  155. public static HashSet<ProtoId<TagPrototype>> Tag = [string.Empty, "StaticFieldTestTag", string.Empty, " "];
  156. }
  157. [Reflect(false)]
  158. private sealed class PrivateProtoIdArrayValid
  159. {
  160. private static readonly ProtoId<TagPrototype>[] Tag = ["StaticFieldTestTag", "StaticFieldTestTag"];
  161. }
  162. [Reflect(false)]
  163. private sealed class PrivateProtoIdArrayInvalid
  164. {
  165. private static readonly ProtoId<TagPrototype>[] Tag = [string.Empty, "StaticFieldTestTag", string.Empty];
  166. }
  167. }