Permalink
Browse files

SentenceParser - split line into verb and noun

  • Loading branch information...
bobbymcr committed Nov 29, 2018
1 parent b1e140d commit 6821520bd9ab114c33ab1594d2514c4e883768a5
Showing with 19 additions and 10 deletions.
  1. +6 −5 core/src/SentenceMessage.cs
  2. +9 −1 core/src/SentenceParser.cs
  3. +4 −4 core/test/SentenceParserTest.cs
@@ -6,13 +6,14 @@ namespace Adventure
{
public sealed class SentenceMessage
{
private readonly string line;

public SentenceMessage(string line)
public SentenceMessage(string verb, string noun)
{
this.line = line;
this.Verb = verb;
this.Noun = noun;
}

public override string ToString() => this.line;
public string Verb { get; }

public string Noun { get; }
}
}
@@ -16,7 +16,15 @@ public SentenceParser(MessageBus bus)

private void ProcessInput(string line)
{
this.bus.Send(new SentenceMessage(line));
string[] parts = line.Split(' ');
if (parts.Length == 1)
{
this.bus.Send(new SentenceMessage(parts[0], string.Empty));
}
else
{
this.bus.Send(new SentenceMessage(parts[0], parts[1]));
}
}
}
}
@@ -16,27 +16,27 @@ public void SendOneWord()
{
MessageBus bus = new MessageBus();
List<string> sentences = new List<string>();
Action<SentenceMessage> onSentence = m => sentences.Add(m.ToString());
Action<SentenceMessage> onSentence = m => sentences.Add(m.Verb + ":" + m.Noun);
bus.Subscribe(onSentence);
SentenceParser parser = new SentenceParser(bus);

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

sentences.Should().ContainSingle().Which.Should().Be("one");
sentences.Should().ContainSingle().Which.Should().Be("one:");
}

[Fact]
public void SendTwoWords()
{
MessageBus bus = new MessageBus();
List<string> sentences = new List<string>();
Action<SentenceMessage> onSentence = m => sentences.Add(m.ToString());
Action<SentenceMessage> onSentence = m => sentences.Add(m.Verb + ":" + m.Noun);
bus.Subscribe(onSentence);
SentenceParser parser = new SentenceParser(bus);

bus.Send(new InputMessage("one two"));

sentences.Should().ContainSingle().Which.Should().Be("one two");
sentences.Should().ContainSingle().Which.Should().Be("one:two");
}
}
}

0 comments on commit 6821520

Please sign in to comment.