TagTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using Content.Shared.Tag;
  4. using Robust.Shared.GameObjects;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Utility;
  8. namespace Content.IntegrationTests.Tests.Tag
  9. {
  10. [TestFixture]
  11. [TestOf(typeof(TagComponent))]
  12. public sealed class TagTest
  13. {
  14. private const string TagEntityId = "TagTestDummy";
  15. // Register these three into the prototype manager
  16. private const string StartingTag = "StartingTagDummy";
  17. private const string AddedTag = "AddedTagDummy";
  18. private const string UnusedTag = "UnusedTagDummy";
  19. // Do not register this one
  20. private const string UnregisteredTag = "AAAAAAAAA";
  21. [TestPrototypes]
  22. private const string Prototypes = $@"
  23. - type: Tag
  24. id: {StartingTag}
  25. - type: Tag
  26. id: {AddedTag}
  27. - type: Tag
  28. id: {UnusedTag}
  29. - type: entity
  30. id: {TagEntityId}
  31. name: {TagEntityId}
  32. components:
  33. - type: Tag
  34. tags:
  35. - {StartingTag}";
  36. [Test]
  37. public async Task TagComponentTest()
  38. {
  39. await using var pair = await PoolManager.GetServerClient();
  40. var server = pair.Server;
  41. var sEntityManager = server.ResolveDependency<IEntityManager>();
  42. var sPrototypeManager = server.ResolveDependency<IPrototypeManager>();
  43. var entManager = server.ResolveDependency<IEntitySystemManager>();
  44. EntityUid sTagDummy = default;
  45. TagComponent sTagComponent = null!;
  46. Entity<TagComponent> sTagEntity = default;
  47. await server.WaitPost(() =>
  48. {
  49. sTagDummy = sEntityManager.SpawnEntity(TagEntityId, MapCoordinates.Nullspace);
  50. sTagComponent = sEntityManager.GetComponent<TagComponent>(sTagDummy);
  51. sTagEntity = new Entity<TagComponent>(sTagDummy, sTagComponent);
  52. });
  53. await server.WaitAssertion(() =>
  54. {
  55. var tagSystem = entManager.GetEntitySystem<TagSystem>();
  56. // Has one tag, the starting tag
  57. Assert.That(sTagComponent.Tags, Has.Count.EqualTo(1));
  58. sPrototypeManager.Index<TagPrototype>(StartingTag);
  59. Assert.Multiple(() =>
  60. {
  61. Assert.That(sTagComponent.Tags, Contains.Item(StartingTag));
  62. // Single
  63. Assert.That(tagSystem.HasTag(sTagDummy, StartingTag));
  64. Assert.That(tagSystem.HasTag(sTagComponent, StartingTag));
  65. // Any
  66. Assert.That(tagSystem.HasAnyTag(sTagDummy, StartingTag));
  67. Assert.That(tagSystem.HasAnyTag(sTagComponent, StartingTag));
  68. // All
  69. Assert.That(tagSystem.HasAllTags(sTagDummy, StartingTag));
  70. Assert.That(tagSystem.HasAllTags(sTagComponent, StartingTag));
  71. });
  72. // Does not have the added tag
  73. var addedTagPrototype = sPrototypeManager.Index<TagPrototype>(AddedTag);
  74. Assert.Multiple(() =>
  75. {
  76. Assert.That(sTagComponent.Tags, Does.Not.Contains(addedTagPrototype));
  77. // Single
  78. Assert.That(tagSystem.HasTag(sTagDummy, AddedTag), Is.False);
  79. Assert.That(tagSystem.HasTag(sTagComponent, AddedTag), Is.False);
  80. // Any
  81. Assert.That(tagSystem.HasAnyTag(sTagDummy, AddedTag), Is.False);
  82. Assert.That(tagSystem.HasAnyTag(sTagComponent, AddedTag), Is.False);
  83. // All
  84. Assert.That(tagSystem.HasAllTags(sTagDummy, AddedTag), Is.False);
  85. Assert.That(tagSystem.HasAllTags(sTagComponent, AddedTag), Is.False);
  86. });
  87. // Does not have the unused tag
  88. var unusedTagPrototype = sPrototypeManager.Index<TagPrototype>(UnusedTag);
  89. Assert.Multiple(() =>
  90. {
  91. Assert.That(sTagComponent.Tags, Does.Not.Contains(unusedTagPrototype));
  92. // Single
  93. Assert.That(tagSystem.HasTag(sTagDummy, UnusedTag), Is.False);
  94. Assert.That(tagSystem.HasTag(sTagComponent, UnusedTag), Is.False);
  95. // Any
  96. Assert.That(tagSystem.HasAnyTag(sTagDummy, UnusedTag), Is.False);
  97. Assert.That(tagSystem.HasAnyTag(sTagComponent, UnusedTag), Is.False);
  98. // All
  99. Assert.That(tagSystem.HasAllTags(sTagDummy, UnusedTag), Is.False);
  100. Assert.That(tagSystem.HasAllTags(sTagComponent, UnusedTag), Is.False);
  101. });
  102. // Throws when checking for an unregistered tag
  103. Assert.Throws<UnknownPrototypeException>(() =>
  104. {
  105. sPrototypeManager.Index<TagPrototype>(UnregisteredTag);
  106. });
  107. Assert.Multiple(() =>
  108. {
  109. // Cannot add the starting tag again
  110. Assert.That(tagSystem.AddTag(sTagEntity, StartingTag), Is.False);
  111. Assert.That(tagSystem.AddTags(sTagEntity, StartingTag, StartingTag), Is.False);
  112. Assert.That(tagSystem.AddTags(sTagEntity, new List<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.False);
  113. Assert.That(tagSystem.AddTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.False);
  114. // Has the starting tag
  115. Assert.That(tagSystem.HasTag(sTagComponent, StartingTag), Is.True);
  116. Assert.That(tagSystem.HasAllTags(sTagComponent, StartingTag, StartingTag), Is.True);
  117. Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
  118. Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
  119. Assert.That(tagSystem.HasAnyTag(sTagComponent, StartingTag, StartingTag), Is.True);
  120. Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
  121. Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, StartingTag }), Is.True);
  122. // Does not have the added tag yet
  123. Assert.That(tagSystem.HasTag(sTagComponent, AddedTag), Is.False);
  124. Assert.That(tagSystem.HasAllTags(sTagComponent, AddedTag, AddedTag), Is.False);
  125. Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
  126. Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
  127. Assert.That(tagSystem.HasAnyTag(sTagComponent, AddedTag, AddedTag), Is.False);
  128. Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
  129. Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
  130. // Has a combination of the two tags
  131. Assert.That(tagSystem.HasAnyTag(sTagComponent, StartingTag, AddedTag), Is.True);
  132. Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
  133. Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
  134. // Does not have both tags
  135. Assert.That(tagSystem.HasAllTags(sTagComponent, StartingTag, AddedTag), Is.False);
  136. Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
  137. Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
  138. // Cannot remove a tag that does not exist
  139. Assert.That(tagSystem.RemoveTag(sTagEntity, AddedTag), Is.False);
  140. Assert.That(tagSystem.RemoveTags(sTagEntity, AddedTag, AddedTag), Is.False);
  141. Assert.That(tagSystem.RemoveTags(sTagEntity, new List<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
  142. Assert.That(tagSystem.RemoveTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { AddedTag, AddedTag }), Is.False);
  143. });
  144. // Can add the new tag
  145. Assert.That(tagSystem.AddTag(sTagEntity, AddedTag), Is.True);
  146. Assert.Multiple(() =>
  147. {
  148. // Cannot add it twice
  149. Assert.That(tagSystem.AddTag(sTagEntity, AddedTag), Is.False);
  150. // Cannot add existing tags
  151. Assert.That(tagSystem.AddTags(sTagEntity, StartingTag, AddedTag), Is.False);
  152. Assert.That(tagSystem.AddTags(sTagEntity, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
  153. Assert.That(tagSystem.AddTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
  154. // Now has two tags
  155. Assert.That(sTagComponent.Tags, Has.Count.EqualTo(2));
  156. // Has both tags
  157. Assert.That(tagSystem.HasTag(sTagComponent, StartingTag), Is.True);
  158. Assert.That(tagSystem.HasTag(sTagComponent, AddedTag), Is.True);
  159. Assert.That(tagSystem.HasAllTags(sTagComponent, StartingTag, StartingTag), Is.True);
  160. Assert.That(tagSystem.HasAllTags(sTagComponent, AddedTag, StartingTag), Is.True);
  161. Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
  162. Assert.That(tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
  163. Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
  164. Assert.That(tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
  165. Assert.That(tagSystem.HasAnyTag(sTagComponent, StartingTag, AddedTag), Is.True);
  166. Assert.That(tagSystem.HasAnyTag(sTagComponent, AddedTag, StartingTag), Is.True);
  167. Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
  168. Assert.That(tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
  169. Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.True);
  170. Assert.That(tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { AddedTag, StartingTag }), Is.True);
  171. });
  172. Assert.Multiple(() =>
  173. {
  174. // Remove the existing starting tag
  175. Assert.That(tagSystem.RemoveTag(sTagEntity, StartingTag), Is.True);
  176. // Remove the existing added tag
  177. Assert.That(tagSystem.RemoveTags(sTagEntity, AddedTag, AddedTag), Is.True);
  178. });
  179. Assert.Multiple(() =>
  180. {
  181. // No tags left to remove
  182. Assert.That(tagSystem.RemoveTags(sTagEntity, new List<ProtoId<TagPrototype>> { StartingTag, AddedTag }), Is.False);
  183. // No tags left in the component
  184. Assert.That(sTagComponent.Tags, Is.Empty);
  185. });
  186. // It is run only in DEBUG build,
  187. // as the checks are performed only in DEBUG build.
  188. #if DEBUG
  189. // Has single
  190. Assert.Throws<DebugAssertException>(() => { tagSystem.HasTag(sTagDummy, UnregisteredTag); });
  191. Assert.Throws<DebugAssertException>(() => { tagSystem.HasTag(sTagComponent, UnregisteredTag); });
  192. // HasAny entityUid methods
  193. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, UnregisteredTag); });
  194. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, UnregisteredTag, UnregisteredTag); });
  195. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  196. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  197. // HasAny component methods
  198. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, UnregisteredTag); });
  199. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, UnregisteredTag, UnregisteredTag); });
  200. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  201. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAnyTag(sTagComponent, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  202. // HasAll entityUid methods
  203. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, UnregisteredTag); });
  204. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, UnregisteredTag, UnregisteredTag); });
  205. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  206. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  207. // HasAll component methods
  208. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, UnregisteredTag); });
  209. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, UnregisteredTag, UnregisteredTag); });
  210. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  211. Assert.Throws<DebugAssertException>(() => { tagSystem.HasAllTags(sTagComponent, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  212. // RemoveTag single
  213. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTag(sTagDummy, UnregisteredTag); });
  214. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTag(sTagEntity, UnregisteredTag); });
  215. // RemoveTags entityUid methods
  216. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, UnregisteredTag); });
  217. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, UnregisteredTag, UnregisteredTag); });
  218. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  219. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  220. // RemoveTags entity methods
  221. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, UnregisteredTag); });
  222. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, UnregisteredTag, UnregisteredTag); });
  223. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  224. Assert.Throws<DebugAssertException>(() => { tagSystem.RemoveTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  225. // AddTag single
  226. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTag(sTagDummy, UnregisteredTag); });
  227. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTag(sTagEntity, UnregisteredTag); });
  228. // AddTags entityUid methods
  229. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, UnregisteredTag); });
  230. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, UnregisteredTag, UnregisteredTag); });
  231. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  232. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagDummy, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  233. // AddTags entity methods
  234. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, UnregisteredTag); });
  235. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, UnregisteredTag, UnregisteredTag); });
  236. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, new List<ProtoId<TagPrototype>> { UnregisteredTag }); });
  237. Assert.Throws<DebugAssertException>(() => { tagSystem.AddTags(sTagEntity, new HashSet<ProtoId<TagPrototype>> { UnregisteredTag }); });
  238. #endif
  239. });
  240. await pair.CleanReturnAsync();
  241. }
  242. }
  243. }