public void ProcessTakeUnavailableItem() { MessageBus bus = new MessageBus(); List <string> messages = new List <string>(); bus.Subscribe <OutputMessage>(m => messages.Add(m.Text)); Item actualItem = null; bus.Subscribe <TakeItemMessage>(m => { messages.Add($"You {m.Verb} the {m.Noun}!"); actualItem = m.Item; }); TestRoom room = new TestRoom(bus); room.Add("key", new TestKey(bus, false)); room.Add("coin", new TestCoin(bus)); room.Enter(); bus.Send(new SentenceMessage(new Word("take", "TAKE"), new Word("key", "KEY"))); bus.Send(new SentenceMessage(new Word("look", "LOOK"), new Word(string.Empty, string.Empty))); messages.Should().Equal( "You are in a test room.", "There is a key here.", "There is a coin here.", "I won't let you take this!", "You are in a test room.", "There is a key here.", "There is a coin here."); actualItem.Should().BeNull(); }
public void LeaveBeforeEnter() { Room room = new TestRoom(new MessageBus()); Action act = () => room.Leave(); act.Should().Throw <InvalidOperationException>().WithMessage("Cannot Leave before Enter."); }
public void EnterTwice() { Room room = new TestRoom(new MessageBus()); room.Enter(); Action act = () => room.Enter(); act.Should().Throw <InvalidOperationException>().WithMessage("Cannot Enter again."); }
public void RequestInventory() { MessageBus bus = new MessageBus(); int inv = 0; bus.Subscribe <ShowInventoryMessage>(_ => ++ inv); TestRoom room = new TestRoom(bus); room.Enter(); bus.Send(new SentenceMessage(new Word("inventory", "INV"), new Word(string.Empty, string.Empty))); inv.Should().Be(1); }
public void ProcessLookItemAfterLeave() { MessageBus bus = new MessageBus(); List <string> messages = new List <string>(); bus.Subscribe <OutputMessage>(m => messages.Add(m.Text)); Room room = new TestRoom(bus); room.Enter(); room.Leave(); bus.Send(new LookItemMessage(new Word("key", "KEY"))); messages.Should().Equal("You are in a test room."); }
public void RegisterSameVerbDifferentCase() { MessageBus bus = new MessageBus(); List <string> output = new List <string>(); Action <OutputMessage> subscriber = m => output.Add(m.Text); bus.Subscribe(subscriber); TestRoom room = new TestRoom(bus); room.Enter(); Action act = () => room.TestRegisterHello("HeLLO"); act.Should().Throw <InvalidOperationException>().WithMessage("The verb 'HeLLO' is already registered."); }
public void ProcessQuit() { MessageBus bus = new MessageBus(); string quitText = null; Action <EndOfGameMessage> subscriber = m => quitText = m.Text; bus.Subscribe(subscriber); Room room = new TestRoom(bus); room.Enter(); bus.Send(new SentenceMessage(new Word("quit", "QUIT"), new Word(string.Empty, string.Empty))); quitText.Should().Be("You quit. Game over."); }
private static void TestSend(Word verb, Word noun, string expectedOutput) { MessageBus bus = new MessageBus(); string lastOutput = null; Action <OutputMessage> subscriber = m => lastOutput = m.Text; bus.Subscribe(subscriber); Room room = new TestRoom(bus); room.Enter(); bus.Send(new SentenceMessage(verb, noun)); lastOutput.Should().Be(expectedOutput); }
public void UnsubscribeOnLeave() { MessageBus bus = new MessageBus(); Room room = new TestRoom(bus); string lastOutput = null; Action <OutputMessage> subscriber = m => lastOutput = m.Text; room.Enter(); room.Leave(); bus.Subscribe(subscriber); bus.Send(new SentenceMessage(new Word("hello", "hello"), new Word("world", "world"))); lastOutput.Should().BeNull(); }
public void ProcessLookItem() { MessageBus bus = new MessageBus(); string lastOutput = null; Action <OutputMessage> subscriber = m => lastOutput = m.Text; bus.Subscribe(subscriber); Room room = new TestRoom(bus); room.Add("key", new TestKey(bus)); room.Enter(); bus.Send(new SentenceMessage(new Word("look", "LOOK"), new Word("key", "KEY"))); lastOutput.Should().Be("It is solid gold."); }
public void ProcessGoDirection() { MessageBus bus = new MessageBus(); List <string> messages = new List <string>(); bus.Subscribe <OutputMessage>(m => messages.Add(m.Text)); bus.Subscribe <GoMessage>(m => messages.Add($"You go {m.Direction}!")); TestRoom room = new TestRoom(bus); room.Enter(); bus.Send(new SentenceMessage(new Word("go", "GO"), new Word("north", "NORTH"))); messages.Should().Equal( "You are in a test room.", "You go north!"); }
public void RemoveOneItemNotPresent() { MessageBus bus = new MessageBus(); List <string> output = new List <string>(); Action <OutputMessage> subscriber = m => output.Add(m.Text); bus.Subscribe(subscriber); TestRoom room = new TestRoom(bus); Item removed = room.Remove("key"); room.Enter(); output.Should().Equal( "You are in a test room."); removed.Should().BeNull(); }
public void ProcessRoomActionAfterLeave() { MessageBus bus = new MessageBus(); List <string> output = new List <string>(); bus.Subscribe <OutputMessage>(m => output.Add(m.Text)); Room room = new TestRoom(bus); room.Enter(); room.Leave(); bus.Send(new ActionMessage <Room>(r => r.Add("coin", new TestCoin(bus)))); room.Enter(); output.Should().Equal( "You are in a test room.", "You are in a test room."); }
public void ProcessRoomAction() { MessageBus bus = new MessageBus(); List <string> output = new List <string>(); bus.Subscribe <OutputMessage>(m => output.Add(m.Text)); Room room = new TestRoom(bus); room.Enter(); bus.Send(new ActionMessage <Room>(r => r.Add("coin", new TestCoin(bus)))); bus.Send(new SentenceMessage(new Word("look", "LOOK"), new Word(string.Empty, string.Empty))); output.Should().Equal( "You are in a test room.", "You are in a test room.", "There is a coin here."); }
public void ProcessUnknownCustomItemAction() { MessageBus bus = new MessageBus(); string lastOutput = null; Action <OutputMessage> subscriber = m => lastOutput = m.Text; bus.Subscribe(subscriber); TestRoom room = new TestRoom(bus); room.Add("key", new TestKey(bus)); room.Add("coin", new TestCoin(bus)); room.Enter(); bus.Send(new SentenceMessage(new Word("flip", "FLIP"), new Word("key", "KEY"))); lastOutput.Should().Be("You can't do that."); }
public void RegisterOnEnter() { MessageBus bus = new MessageBus(); List <string> output = new List <string>(); Action <OutputMessage> subscriber = m => output.Add(m.Text); bus.Subscribe(subscriber); Room room = new TestRoom(bus); room.Enter(); output.Should().ContainSingle().Which.Should().Be("You are in a test room."); bus.Send(new SentenceMessage(new Word("hello", "hello"), new Word("world", "world"))); output.Should().Contain("Hello, world!"); }
public void AddOneItem() { MessageBus bus = new MessageBus(); List <string> output = new List <string>(); Action <OutputMessage> subscriber = m => output.Add(m.Text); bus.Subscribe(subscriber); TestRoom room = new TestRoom(bus); room.Add("key", new TestKey(bus)); room.Enter(); output.Should().Equal( "You are in a test room.", "There is a key here."); }
public void ProcessCustomItemActionAfterLeave() { MessageBus bus = new MessageBus(); string lastOutput = null; Action <OutputMessage> subscriber = m => lastOutput = m.Text; bus.Subscribe(subscriber); TestRoom room = new TestRoom(bus); room.Add("key", new TestKey(bus)); room.Add("coin", new TestCoin(bus)); room.Enter(); room.Leave(); bus.Send(new SentenceMessage(new Word("flip", "FLIP"), new Word("coin", "COIN"))); lastOutput.Should().Be("There is a coin here."); }
public void ProcessLookItemUpperLevelSubscriber() { MessageBus bus = new MessageBus(); string lastOutput = null; bus.Subscribe <OutputMessage>(m => lastOutput = m.Text); Dictionary <string, Item> items = new Dictionary <string, Item>(); items.Add("key", new TestKey(bus)); bus.Subscribe <LookItemMessage>(m => { bus.Send(new OutputMessage(items[m.Noun.Primary].LongDescription)); return(true); }); Room room = new TestRoom(bus); room.Enter(); bus.Send(new SentenceMessage(new Word("look", "LOOK"), new Word("key", "KEY"))); lastOutput.Should().Be("It is solid gold."); }
public void DropOneItem() { MessageBus bus = new MessageBus(); Dictionary <string, Item> items = new Dictionary <string, Item>(); items["DROP/KEY"] = new TestKey(bus); bus.Subscribe <DropItemMessage>(m => m.Items.Add(m.Noun.Primary, items[$"{m.Verb}/{m.Noun}"])); List <string> output = new List <string>(); bus.Subscribe <OutputMessage>(m => output.Add(m.Text)); TestRoom room = new TestRoom(bus); room.Enter(); bus.Send(new SentenceMessage(new Word("drop", "DROP"), new Word("key", "KEY"))); bus.Send(new SentenceMessage(new Word("look", "LOOK"), new Word(string.Empty, string.Empty))); output.Should().Equal( "You are in a test room.", "You are in a test room.", "There is a key here."); }