fix null lookup bug in message manager

pull/5/head
Evan Hemsley 2019-06-21 17:23:52 -07:00
parent a2171e9915
commit fde3cfe9e3
2 changed files with 26 additions and 1 deletions

View File

@ -28,7 +28,9 @@ namespace Encompass
internal IEnumerable<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage
{
return messageTypeToMessages[typeof(TMessage)].Cast<TMessage>();
return messageTypeToMessages.ContainsKey(typeof(TMessage)) ?
messageTypeToMessages[typeof(TMessage)].Cast<TMessage>() :
Enumerable.Empty<TMessage>();
}
}
}

View File

@ -227,6 +227,29 @@ namespace Tests
}
}
static IEnumerable<MockMessage> emptyReadMessagesResult;
[Reads(typeof(MockMessage))]
class ReadMessagesWhenNoneExistEngine : Engine
{
public override void Update(float dt)
{
emptyReadMessagesResult = ReadMessages<MockMessage>();
}
}
[Test]
public void ReadMessagesWhenNoneHaveBeenEmitted()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<ReadMessagesWhenNoneExistEngine>();
var world = worldBuilder.Build();
world.Update(0.01f);
emptyReadMessagesResult.Should().BeEmpty();
}
[Test]
public void EmitUndeclaredMessage()
{