From fde3cfe9e36b499afe31c04aaee953090df81ba0 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Fri, 21 Jun 2019 17:23:52 -0700 Subject: [PATCH] fix null lookup bug in message manager --- encompass-cs/MessageManager.cs | 4 +++- test/EngineTest.cs | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/encompass-cs/MessageManager.cs b/encompass-cs/MessageManager.cs index d22f2c0..b6a758d 100644 --- a/encompass-cs/MessageManager.cs +++ b/encompass-cs/MessageManager.cs @@ -28,7 +28,9 @@ namespace Encompass internal IEnumerable GetMessagesByType() where TMessage : struct, IMessage { - return messageTypeToMessages[typeof(TMessage)].Cast(); + return messageTypeToMessages.ContainsKey(typeof(TMessage)) ? + messageTypeToMessages[typeof(TMessage)].Cast() : + Enumerable.Empty(); } } } diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 069c49f..c38a0ae 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -227,6 +227,29 @@ namespace Tests } } + static IEnumerable emptyReadMessagesResult; + [Reads(typeof(MockMessage))] + class ReadMessagesWhenNoneExistEngine : Engine + { + public override void Update(float dt) + { + emptyReadMessagesResult = ReadMessages(); + } + } + + [Test] + public void ReadMessagesWhenNoneHaveBeenEmitted() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(); + + var world = worldBuilder.Build(); + + world.Update(0.01f); + + emptyReadMessagesResult.Should().BeEmpty(); + } + [Test] public void EmitUndeclaredMessage() {