Files
RobustToolbox/Robust.UnitTesting/Shared/EntityLookupTest.cs
T
2021-12-03 15:53:10 +01:00

58 lines
2.4 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Robust.UnitTesting.Shared
{
[TestFixture, TestOf(typeof(IEntityLookup))]
public sealed class EntityLookupTest : RobustIntegrationTest
{
/// <summary>
/// Is the entity correctly removed / added to EntityLookup when anchored
/// </summary>
[Test]
public async Task TestAnchoring()
{
var server = StartServer();
await server.WaitIdleAsync();
var lookup = server.ResolveDependency<IEntityLookup>();
var entManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
await server.WaitAssertion(() =>
{
var mapId = mapManager.CreateMap();
var grid = mapManager.CreateGrid(mapId);
var theMapSpotBeingUsed = new Box2(Vector2.Zero, Vector2.One)
;
grid.SetTile(new Vector2i(), new Tile(1));
lookup.Update();
Assert.That(lookup.GetEntitiesIntersecting(mapId, theMapSpotBeingUsed).ToList().Count, Is.EqualTo(1));
// Setup and check it actually worked
var dummy = entManager.SpawnEntity(null, new MapCoordinates(Vector2.Zero, mapId));
lookup.Update();
Assert.That(lookup.GetEntitiesIntersecting(mapId, theMapSpotBeingUsed).ToList().Count, Is.EqualTo(2));
// When anchoring should still only be 1 entity.
IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(dummy).Anchored = true;
Assert.That(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(dummy).Anchored);
lookup.Update();
Assert.That(lookup.GetEntitiesIntersecting(mapId, theMapSpotBeingUsed).ToList().Count, Is.EqualTo(2));
// Even when unanchored should still be there
IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(dummy).Anchored = false;
lookup.Update();
Assert.That(lookup.GetEntitiesIntersecting(mapId, theMapSpotBeingUsed).ToList().Count, Is.EqualTo(2));
});
}
}
}