Skip to content

Commit

Permalink
TextConsole - Run(x) -> NewLoop() + InputLoop.Run(x)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Dec 4, 2018
1 parent 1dfb74d commit 27e9415
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
9 changes: 2 additions & 7 deletions core/src/TextConsole.cs
Expand Up @@ -4,9 +4,7 @@

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

public sealed class TextConsole
{
Expand All @@ -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()
Expand Down
29 changes: 19 additions & 10 deletions core/test/TextConsoleTest.cs
Expand Up @@ -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]
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion sample/src/Game.cs
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 27e9415

Please sign in to comment.