Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 3dc4c53

Browse files
author
Jamie Brynes
authored
Add convenience method to the MockConnection (#1370)
1 parent 40e771f commit 3dc4c53

File tree

3 files changed

+59
-27
lines changed

3 files changed

+59
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- Produce code coverage reports in tests [#1359](https://github.com/spatialos/gdk-for-unity/pull/1359)
3131
- Replaced code generated ReferenceProviders with generic version. [#1358](https://github.com/spatialos/gdk-for-unity/pull/1358)
3232
- Refactor callbacks API. [#1348](https://github.com/spatialos/gdk-for-unity/pull/1348)
33+
- Added a convenience method to the `MockConnection` for removing entities and their components. [#1370](https://github.com/spatialos/gdk-for-unity/pull/1370)
3334

3435
## `0.3.5` - 2020-04-23
3536

test-project/Assets/EditmodeTests/Collections/ReferenceProviderTests.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ public void Disposed_world_cleans_provider()
1717
{
1818
mockWorld.Step(world =>
1919
{
20-
world.Connection.CreateEntity(entityId, new EntityTemplate());
21-
22-
world.Connection.AddComponent(entityId, Position.ComponentId,
23-
new Position.Update {Coords = Coordinates.Zero});
24-
25-
world.Connection.AddComponent(entityId, Metadata.ComponentId,
26-
new Metadata.Update {EntityType = "EntityWithReferenceComponent"});
20+
world.Connection.CreateEntity(entityId, GetTemplate());
2721
});
2822

2923
Assert.AreEqual(1, ReferenceProvider<string>.Count);
@@ -35,25 +29,30 @@ public void Disposed_world_cleans_provider()
3529
[Test]
3630
public void Removed_component_disposes_reference()
3731
{
32+
var template = GetTemplate();
3833
using (var mockWorld = MockWorld.Create(new MockWorld.Options()))
3934
{
40-
mockWorld.Step(world =>
41-
{
42-
world.Connection.CreateEntity(entityId, new EntityTemplate());
43-
44-
world.Connection.AddComponent(entityId, Position.ComponentId,
45-
new Position.Update {Coords = Coordinates.Zero});
46-
47-
world.Connection.AddComponent(entityId, Metadata.ComponentId,
48-
new Metadata.Update {EntityType = "EntityWithReferenceComponent"});
49-
}).Step(world =>
50-
{
51-
Assert.AreEqual(1, ReferenceProvider<string>.Count);
52-
world.Connection.RemoveComponent(entityId, Metadata.ComponentId);
53-
});
35+
mockWorld
36+
.Step(world =>
37+
{
38+
world.Connection.CreateEntity(entityId, template);
39+
})
40+
.Step(world =>
41+
{
42+
Assert.AreEqual(1, ReferenceProvider<string>.Count);
43+
world.Connection.RemoveEntityAndComponents(entityId, template);
44+
});
5445

5546
Assert.AreEqual(0, ReferenceProvider<string>.Count);
5647
}
5748
}
49+
50+
private EntityTemplate GetTemplate()
51+
{
52+
var template = new EntityTemplate();
53+
template.AddComponent(new Position.Snapshot(), "some-worker");
54+
template.AddComponent(new Metadata.Snapshot("AReferenceType"), "some-worker");
55+
return template;
56+
}
5857
}
5958
}

workers/unity/Packages/io.improbable.gdk.core/Worker/ConnectionHandlers/MockConnectionHandler.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class MockConnectionHandler : IConnectionHandler
3939

4040
public void CreateEntity(long entityId, EntityTemplate template)
4141
{
42-
var handler = new EntityTemplateDynamicHandler(template, entityId, currentDiff);
42+
var handler = new CreateEntityTemplateDynamicHandler(template, entityId, currentDiff);
4343
Dynamic.ForEachComponent(handler);
4444
}
4545

@@ -63,6 +63,14 @@ public void RemoveEntity(long entityId)
6363
currentDiff.RemoveEntity(entityId);
6464
}
6565

66+
public void RemoveEntityAndComponents(long entityId, EntityTemplate template)
67+
{
68+
var handler = new RemoveEntityTemplateDynamicHandler(template, entityId, currentDiff);
69+
Dynamic.ForEachComponent(handler);
70+
71+
RemoveEntity(entityId);
72+
}
73+
6674
public void RemoveComponent(long entityId, uint componentId)
6775
{
6876
currentDiff.RemoveComponent(entityId, componentId);
@@ -129,13 +137,13 @@ public bool IsConnected()
129137

130138
#endregion
131139

132-
private class EntityTemplateDynamicHandler : Dynamic.IHandler
140+
private class CreateEntityTemplateDynamicHandler : Dynamic.IHandler
133141
{
134-
private EntityTemplate template;
135-
private ViewDiff viewDiff;
136-
private long entityId;
142+
private readonly EntityTemplate template;
143+
private readonly long entityId;
144+
private readonly ViewDiff viewDiff;
137145

138-
public EntityTemplateDynamicHandler(EntityTemplate template, long entityId, ViewDiff viewDiff)
146+
public CreateEntityTemplateDynamicHandler(EntityTemplate template, long entityId, ViewDiff viewDiff)
139147
{
140148
this.template = template;
141149
this.viewDiff = viewDiff;
@@ -160,6 +168,30 @@ public void Accept<TUpdate, TSnapshot>(uint componentId, Dynamic.VTable<TUpdate,
160168
}
161169
}
162170

171+
private class RemoveEntityTemplateDynamicHandler : Dynamic.IHandler
172+
{
173+
private readonly EntityTemplate template;
174+
private readonly long entityId;
175+
private readonly ViewDiff viewDiff;
176+
177+
public RemoveEntityTemplateDynamicHandler(EntityTemplate template, long entityId, ViewDiff viewDiff)
178+
{
179+
this.template = template;
180+
this.entityId = entityId;
181+
this.viewDiff = viewDiff;
182+
}
183+
184+
public void Accept<TUpdate, TSnapshot>(uint componentId, Dynamic.VTable<TUpdate, TSnapshot> vtable)
185+
where TUpdate : struct, ISpatialComponentUpdate
186+
where TSnapshot : struct, ISpatialComponentSnapshot
187+
{
188+
if (template.HasComponent<TSnapshot>())
189+
{
190+
viewDiff.RemoveComponent(entityId, componentId);
191+
}
192+
}
193+
}
194+
163195
public void Dispose()
164196
{
165197
}

0 commit comments

Comments
 (0)