more component read tests

pull/5/head
Evan Hemsley 2019-06-17 12:12:07 -07:00
parent 7c787290eb
commit 5bd21c631e
2 changed files with 65 additions and 1 deletions

View File

@ -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

View File

@ -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);
}
}
}