Permalink
Browse files

TextConsole - Run(x) -> NewLoop() + InputLoop.Run(x)

  • Loading branch information...
bobbymcr committed Dec 4, 2018
1 parent 1dfb74d commit 27e941527cb1182964cc4af9cb0ab16393099628
Showing with 23 additions and 18 deletions.
  1. +2 −7 core/src/TextConsole.cs
  2. +19 −10 core/test/TextConsoleTest.cs
  3. +2 −1 sample/src/Game.cs
@@ -4,9 +4,7 @@

namespace Adventure
{
using System;
using System.IO;
using System.Threading;

public sealed class TextConsole
{
@@ -21,12 +19,9 @@ public TextConsole(MessageBus bus, TextReader reader, TextWriter writer)
this.writer = writer;
}

public void Run(CancellationToken token)
public InputLoop NewLoop()
{
using (InputLoop loop = new InputLoop(this.bus, this.ReadLine, m => this.writer.WriteLine(m.Text)))
{
loop.Run(token);
}
return new InputLoop(this.bus, this.ReadLine, m => this.writer.WriteLine(m.Text));
}

private bool ReadLine()
@@ -17,10 +17,12 @@ public class TextConsoleTest
public void RunNoInput()
{
TextConsole con = new TextConsole(new MessageBus(), TextReader.Null, TextWriter.Null);
using (InputLoop loop = con.NewLoop())
{
Action act = () => loop.Run(CancellationToken.None);

Action act = () => con.Run(CancellationToken.None);

act.Should().NotThrow();
act.Should().NotThrow();
}
}

[Fact]
@@ -31,21 +33,26 @@ public void ReadsOneInputProducesOneOutput()
StringWriter writer = new StringWriter(output);
bus.Subscribe<InputMessage>(m => bus.Send(new OutputMessage($"I saw '{m.Line}'")));
TextConsole con = new TextConsole(bus, new StringReader("one line"), writer);
using (InputLoop loop = con.NewLoop())
{
loop.Run(CancellationToken.None);

con.Run(CancellationToken.None);

output.ToString().Should().Be("I saw 'one line'\r\n");
output.ToString().Should().Be("I saw 'one line'\r\n");
}
}

[Fact]
public void ProducesNoOutputAfterRun()
public void ProducesNoOutputAfterDispose()
{
MessageBus bus = new MessageBus();
StringBuilder output = new StringBuilder();
StringWriter writer = new StringWriter(output);
TextConsole con = new TextConsole(bus, TextReader.Null, writer);
using (InputLoop loop = con.NewLoop())
{
loop.Run(CancellationToken.None);
}

con.Run(CancellationToken.None);
bus.Send(new OutputMessage("do not print this"));

output.ToString().Should().BeEmpty();
@@ -69,8 +76,10 @@ public void BreaksOutOfLoopAfterCancellation()
bus.Subscribe(subscriber);
string[] lines = new string[] { "start", "cancel", "too late" };
TextConsole con = new TextConsole(bus, new StringReader(string.Join(Environment.NewLine, lines)), TextWriter.Null);

con.Run(cts.Token);
using (InputLoop loop = con.NewLoop())
{
loop.Run(cts.Token);
}

calls.Should().Be(2);
}
@@ -24,10 +24,11 @@ public void Run()
{
using (CancellationTokenSource cts = new CancellationTokenSource())
using (new SentenceParser(this.bus, this.words))
using (InputLoop loop = this.console.NewLoop())
{
Room room = new MainRoom(this.bus, cts);
room.Enter();
this.console.Run(cts.Token);
loop.Run(cts.Token);
}
}

0 comments on commit 27e9415

Please sign in to comment.