more component read tests
parent
7c787290eb
commit
5bd21c631e
|
@ -49,7 +49,9 @@ namespace Encompass
|
|||
|
||||
internal IEnumerable<KeyValuePair<Guid, TComponent>> GetActiveComponentsByType<TComponent>() where TComponent : struct, IComponent
|
||||
{
|
||||
return activeComponents[typeof(TComponent)].Select((id) => new KeyValuePair<Guid, TComponent>(id, (TComponent)IDToComponent[id]));
|
||||
return activeComponents.ContainsKey(typeof(TComponent)) ?
|
||||
activeComponents[typeof(TComponent)].Select((id) => new KeyValuePair<Guid, TComponent>(id, (TComponent)IDToComponent[id])) :
|
||||
Enumerable.Empty<KeyValuePair<Guid, TComponent>>();
|
||||
}
|
||||
|
||||
internal KeyValuePair<Guid, TComponent> GetActiveComponentByType<TComponent>() where TComponent : struct, IComponent
|
||||
|
|
|
@ -295,5 +295,67 @@ namespace Tests
|
|||
|
||||
Assert.Throws<IllegalMessageReadException>(() => world.Update(0.01f));
|
||||
}
|
||||
|
||||
static KeyValuePair<Guid, MockComponent> pairA;
|
||||
static KeyValuePair<Guid, MockComponent> pairB;
|
||||
|
||||
class SameValueComponentReadEngine : Engine
|
||||
{
|
||||
public override void Update(float dt)
|
||||
{
|
||||
var components = this.ReadComponents<MockComponent>();
|
||||
|
||||
pairA = components.First();
|
||||
pairB = components.Last();
|
||||
}
|
||||
}
|
||||
|
||||
// Tests that components with identical values should be distinguishable by ID
|
||||
[Test]
|
||||
public void SameValueComponents()
|
||||
{
|
||||
var worldBuilder = new WorldBuilder();
|
||||
worldBuilder.AddEngine<SameValueComponentReadEngine>();
|
||||
|
||||
MockComponent componentA;
|
||||
componentA.myInt = 20;
|
||||
componentA.myString = "hello";
|
||||
|
||||
MockComponent componentB;
|
||||
componentB.myInt = 20;
|
||||
componentB.myString = "hello";
|
||||
|
||||
var entity = worldBuilder.CreateEntity();
|
||||
entity.AddComponent(componentA);
|
||||
entity.AddComponent(componentB);
|
||||
|
||||
var world = worldBuilder.Build();
|
||||
world.Update(0.01f);
|
||||
|
||||
Assert.That(pairA, Is.Not.EqualTo(pairB));
|
||||
Assert.That(pairA.Value, Is.EqualTo(pairB.Value));
|
||||
}
|
||||
|
||||
static IEnumerable<KeyValuePair<Guid, MockComponent>> emptyComponentReadResult;
|
||||
|
||||
class ReadEmptyMockComponentsEngine : Engine
|
||||
{
|
||||
public override void Update(float dt)
|
||||
{
|
||||
emptyComponentReadResult = this.ReadComponents<MockComponent>();
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadComponentsOfTypeWhereNoneExist()
|
||||
{
|
||||
var worldBuilder = new WorldBuilder();
|
||||
worldBuilder.AddEngine<ReadEmptyMockComponentsEngine>();
|
||||
|
||||
var world = worldBuilder.Build();
|
||||
world.Update(0.01f);
|
||||
|
||||
Assert.That(emptyComponentReadResult, Is.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue