]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
SpawnAndDeleteEntityCountTest improvements (#34069)
authorTayrtahn <tayrtahn@gmail.com>
Thu, 17 Apr 2025 11:43:28 +0000 (07:43 -0400)
committerGitHub <noreply@github.com>
Thu, 17 Apr 2025 11:43:28 +0000 (21:43 +1000)
* Minor improvements to SpawnAndDeleteEntityCountTest Assert messages

* * Now lists added/removed entities on test failure.
* Wrapped entity loop in MultipleAsync so all failing ents are reported.
* Removed Multiple wrappers on client/server pairs, as they made result formatting ugly.

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Content.IntegrationTests/Tests/EntityTest.cs

index 26365d8d96ffcffe762a17a37fe02fc59a1d590a..3992272b5b726e1e4852b78db4eafadc88e82589 100644 (file)
@@ -1,6 +1,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Numerics;
+using System.Text;
 using Robust.Shared;
 using Robust.Shared.Audio.Components;
 using Robust.Shared.Configuration;
@@ -270,68 +271,79 @@ namespace Content.IntegrationTests.Tests
 
             // We consider only non-audio entities, as some entities will just play sounds when they spawn.
             int Count(IEntityManager ent) =>  ent.EntityCount - ent.Count<AudioComponent>();
+            IEnumerable<EntityUid> Entities(IEntityManager entMan) => entMan.GetEntities().Where(entMan.HasComponent<AudioComponent>);
 
-            foreach (var protoId in protoIds)
+            await Assert.MultipleAsync(async () =>
             {
-                // TODO fix ninja
-                // Currently ninja fails to equip their own loadout.
-                if (protoId == "MobHumanSpaceNinja")
-                    continue;
-
-                var count = Count(server.EntMan);
-                var clientCount = Count(client.EntMan);
-                EntityUid uid = default;
-                await server.WaitPost(() => uid = server.EntMan.SpawnEntity(protoId, coords));
-                await pair.RunTicksSync(3);
-
-                // If the entity deleted itself, check that it didn't spawn other entities
-                if (!server.EntMan.EntityExists(uid))
+                foreach (var protoId in protoIds)
                 {
-                    if (Count(server.EntMan) != count)
-                    {
-                        Assert.Fail($"Server prototype {protoId} failed on deleting itself");
-                    }
+                    // TODO fix ninja
+                    // Currently ninja fails to equip their own loadout.
+                    if (protoId == "MobHumanSpaceNinja")
+                        continue;
 
-                    if (Count(client.EntMan) != clientCount)
+                    var count = Count(server.EntMan);
+                    var clientCount = Count(client.EntMan);
+                    var serverEntities = new HashSet<EntityUid>(Entities(server.EntMan));
+                    var clientEntities = new HashSet<EntityUid>(Entities(client.EntMan));
+                    EntityUid uid = default;
+                    await server.WaitPost(() => uid = server.EntMan.SpawnEntity(protoId, coords));
+                    await pair.RunTicksSync(3);
+
+                    // If the entity deleted itself, check that it didn't spawn other entities
+                    if (!server.EntMan.EntityExists(uid))
                     {
-                        Assert.Fail($"Client prototype {protoId} failed on deleting itself\n" +
-                                    $"Expected {clientCount} and found {Count(client.EntMan)}.\n" +
-                                    $"Server was {count}.");
+                        Assert.That(Count(server.EntMan), Is.EqualTo(count), $"Server prototype {protoId} failed on deleting itself\n" +
+                            BuildDiffString(serverEntities, Entities(server.EntMan), server.EntMan));
+                        Assert.That(Count(client.EntMan), Is.EqualTo(clientCount), $"Client prototype {protoId} failed on deleting itself\n" +
+                            $"Expected {clientCount} and found {client.EntMan.EntityCount}.\n" +
+                            $"Server count was {count}.\n" +
+                            BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan));
+                        continue;
                     }
-                    continue;
-                }
 
-                // Check that the number of entities has increased.
-                if (Count(server.EntMan) <= count)
-                {
-                    Assert.Fail($"Server prototype {protoId} failed on spawning as entity count didn't increase");
-                }
-
-                if (Count(client.EntMan) <= clientCount)
-                {
-                    Assert.Fail($"Client prototype {protoId} failed on spawning as entity count didn't increase" +
-                                $"Expected at least {clientCount} and found {Count(client.EntMan)}. " +
-                                $"Server was {count}");
+                    // Check that the number of entities has increased.
+                    Assert.That(Count(server.EntMan), Is.GreaterThan(count), $"Server prototype {protoId} failed on spawning as entity count didn't increase\n" +
+                        BuildDiffString(serverEntities, Entities(server.EntMan), server.EntMan));
+                    Assert.That(Count(client.EntMan), Is.GreaterThan(clientCount), $"Client prototype {protoId} failed on spawning as entity count didn't increase\n" +
+                        $"Expected at least {clientCount} and found {client.EntMan.EntityCount}. " +
+                        $"Server count was {count}.\n" +
+                        BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan));
+
+                    await server.WaitPost(() => server.EntMan.DeleteEntity(uid));
+                    await pair.RunTicksSync(3);
+
+                    // Check that the number of entities has gone back to the original value.
+                    Assert.That(Count(server.EntMan), Is.EqualTo(count), $"Server prototype {protoId} failed on deletion: count didn't reset properly\n" +
+                        BuildDiffString(serverEntities, Entities(server.EntMan), server.EntMan));
+                    Assert.That(client.EntMan.EntityCount, Is.EqualTo(clientCount), $"Client prototype {protoId} failed on deletion: count didn't reset properly:\n" +
+                        $"Expected {clientCount} and found {client.EntMan.EntityCount}.\n" +
+                        $"Server count was {count}.\n" +
+                        BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan));
                 }
+            });
 
-                await server.WaitPost(() => server.EntMan.DeleteEntity(uid));
-                await pair.RunTicksSync(3);
-
-                // Check that the number of entities has gone back to the original value.
-                if (Count(server.EntMan) != count)
-                {
-                    Assert.Fail($"Server prototype {protoId} failed on deletion count didn't reset properly");
-                }
+            await pair.CleanReturnAsync();
+        }
 
-                if (Count(client.EntMan) != clientCount)
-                {
-                    Assert.Fail($"Client prototype {protoId} failed on deletion count didn't reset properly:\n" +
-                                $"Expected {clientCount} and found {Count(client.EntMan)}.\n" +
-                                $"Server was {count}.");
-                }
+        private static string BuildDiffString(IEnumerable<EntityUid> oldEnts, IEnumerable<EntityUid> newEnts, IEntityManager entMan)
+        {
+            var sb = new StringBuilder();
+            var addedEnts = newEnts.Except(oldEnts);
+            var removedEnts = oldEnts.Except(newEnts);
+            if (addedEnts.Any())
+                sb.AppendLine("Listing new entities:");
+            foreach (var addedEnt in addedEnts)
+            {
+                sb.AppendLine(entMan.ToPrettyString(addedEnt));
             }
-
-            await pair.CleanReturnAsync();
+            if (removedEnts.Any())
+                sb.AppendLine("Listing removed entities:");
+            foreach (var removedEnt in removedEnts)
+            {
+                sb.AppendLine("\t" + entMan.ToPrettyString(removedEnt));
+            }
+            return sb.ToString();
         }
 
         private static bool HasRequiredDataField(Component component)