Skip to content

Commit

Permalink
TextConsole - WriteAfterDispose
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Dec 7, 2018
1 parent cde1a38 commit cfd065d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/src/TextConsole.cs
Expand Up @@ -10,18 +10,20 @@ namespace Adventure
public sealed class TextConsole : IDisposable
{
private readonly MessageBus bus;
private readonly IDisposable sub;
private readonly IDisposable inputSub;
private readonly IDisposable outputSub;

public TextConsole(MessageBus bus, TextReader reader, TextWriter writer)
{
this.bus = bus;
this.sub = this.bus.Subscribe<InputRequestedMessage>(_ => this.ReadLine(reader));
this.bus.Subscribe<OutputMessage>(m => writer.WriteLine(m.Text));
this.inputSub = this.bus.Subscribe<InputRequestedMessage>(_ => this.ReadLine(reader));
this.outputSub = this.bus.Subscribe<OutputMessage>(m => writer.WriteLine(m.Text));
}

public void Dispose()
{
this.sub.Dispose();
this.inputSub.Dispose();
this.outputSub.Dispose();
}

private void ReadLine(TextReader reader)
Expand Down
17 changes: 17 additions & 0 deletions core/test/TextConsoleTest.cs
Expand Up @@ -83,5 +83,22 @@ public void WriteTwoLines()
sb.ToString().Should().Be("one" + Environment.NewLine + "two" + Environment.NewLine);
}
}

[Fact]
public void WriteAfterDispose()
{
MessageBus bus = new MessageBus();
StringBuilder sb = new StringBuilder();
using (StringWriter writer = new StringWriter(sb))
{
using (TextConsole console = new TextConsole(bus, TextReader.Null, writer))
{
}

bus.Send(new OutputMessage("one"));

sb.ToString().Should().BeEmpty();
}
}
}
}

0 comments on commit cfd065d

Please sign in to comment.