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;
// 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)