Skip to content
Permalink
Browse files

Item - accept MessageBus as constructor param

  • Loading branch information...
bobbymcr committed Dec 15, 2018
1 parent a645543 commit da2ce6c962105cbed359be568d9adb9ec0fe0b9c
Showing with 103 additions and 66 deletions.
  1. +4 −1 core/src/Item.cs
  2. +27 −21 core/test/InventoryTest.cs
  3. +40 −24 core/test/ItemsTest.cs
  4. +23 −17 core/test/RoomTest.cs
  5. +5 −0 sample/src/Coin.cs
  6. +1 −1 sample/src/MainRoom.cs
  7. +3 −2 sample/src/Table.cs
@@ -8,8 +8,11 @@ namespace Adventure

public abstract class Item
{
protected Item()
private readonly MessageBus bus;

protected Item(MessageBus bus)
{
this.bus = bus;
}

public abstract string ShortDescription { get; }
@@ -34,7 +34,7 @@ public void ShowInventoryOneItem()
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem());
inv.Add("key", new TestItem(bus));
bus.Send(new ShowInventoryMessage());

messages.Should().Equal("You are carrying:", "a key");
@@ -49,8 +49,8 @@ public void ShowInventoryTwoItems()
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem());
inv.Add("coin", new TestItem2());
inv.Add("key", new TestItem(bus));
inv.Add("coin", new TestItem2(bus));
bus.Send(new ShowInventoryMessage());

messages.Should().Equal("You are carrying:", "a key", "a coin");
@@ -65,8 +65,8 @@ public void ShowInventoryAfterDispose()
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem());
inv.Add("coin", new TestItem2());
inv.Add("key", new TestItem(bus));
inv.Add("coin", new TestItem2(bus));
}

bus.Send(new ShowInventoryMessage());
@@ -83,8 +83,8 @@ public void ProcessCustomItemAction()
bus.Subscribe(subscriber);
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem());
inv.Add("coin", new TestItem2());
inv.Add("key", new TestItem(bus));
inv.Add("coin", new TestItem2(bus));

bus.Send(new SentenceMessage(new Word("flip", "FLIP"), new Word("coin", "COIN")));

@@ -101,8 +101,8 @@ public void ProcessCustomItemActionAfterDispose()
bus.Subscribe(subscriber);
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem());
inv.Add("coin", new TestItem2());
inv.Add("key", new TestItem(bus));
inv.Add("coin", new TestItem2(bus));
}

bus.Send(new SentenceMessage(new Word("flip", "FLIP"), new Word("coin", "COIN")));
@@ -119,8 +119,8 @@ public void ProcessLookItem()
using (Inventory inv = new Inventory(bus))
{
bus.Subscribe<LookItemMessage>(m => bus.Send(new OutputMessage($"I can't bear to look at {m.Noun}.")));
inv.Add("key", new TestItem());
inv.Add("coin", new TestItem2());
inv.Add("key", new TestItem(bus));
inv.Add("coin", new TestItem2(bus));

bus.Send(new LookItemMessage(new Word("key", "KEY")));

@@ -137,8 +137,8 @@ public void ProcessLookUnknown()
using (Inventory inv = new Inventory(bus))
{
bus.Subscribe<LookItemMessage>(m => bus.Send(new OutputMessage($"I can't bear to look at {m.Noun}.")));
inv.Add("key", new TestItem());
inv.Add("coin", new TestItem2());
inv.Add("key", new TestItem(bus));
inv.Add("coin", new TestItem2(bus));

bus.Send(new LookItemMessage(new Word("that", "THAT")));

@@ -155,8 +155,8 @@ public void ProcessLookAfterDispose()
using (Inventory inv = new Inventory(bus))
{
bus.Subscribe<LookItemMessage>(m => bus.Send(new OutputMessage($"I can't bear to look at {m.Noun}.")));
inv.Add("key", new TestItem());
inv.Add("coin", new TestItem2());
inv.Add("key", new TestItem(bus));
inv.Add("coin", new TestItem2(bus));
}

bus.Send(new LookItemMessage(new Word("key", "KEY")));
@@ -173,7 +173,7 @@ public void DropAllowedItem()
Items items = new Items(bus);
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem());
inv.Add("key", new TestItem(bus));
bus.Send(new DropItemMessage(items, new Word("drop", "THROW"), new Word("key", "KEY")));
bus.Send(new ShowInventoryMessage());

@@ -191,7 +191,7 @@ public void DropDisallowedItem()
Items items = new Items(bus);
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem(false));
inv.Add("key", new TestItem(bus, false));
bus.Send(new DropItemMessage(items, new Word("drop", "THROW"), new Word("key", "KEY")));
bus.Send(new ShowInventoryMessage());

@@ -225,7 +225,7 @@ public void DropItemAfterDispose()
Items items = new Items(bus);
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem());
inv.Add("key", new TestItem(bus));
}

bus.Send(new DropItemMessage(items, new Word("drop", "THROW"), new Word("key", "KEY")));
@@ -242,7 +242,7 @@ public void TakeItem()
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
using (Inventory inv = new Inventory(bus))
{
bus.Send(new TakeItemMessage(new Word("take", "GRAB"), new Word("key", "KEY"), new TestItem()));
bus.Send(new TakeItemMessage(new Word("take", "GRAB"), new Word("key", "KEY"), new TestItem(bus)));
bus.Send(new ShowInventoryMessage());

messages.Should().Equal("You GRAB the KEY.", "You are carrying:", "a key");
@@ -257,7 +257,7 @@ public void TakeItemAfterDispose()
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Inventory inv = new Inventory(bus);
inv.Dispose();
bus.Send(new TakeItemMessage(new Word("take", "GRAB"), new Word("key", "KEY"), new TestItem()));
bus.Send(new TakeItemMessage(new Word("take", "GRAB"), new Word("key", "KEY"), new TestItem(bus)));

messages.Should().BeEmpty();
}
@@ -266,7 +266,8 @@ private sealed class TestItem : Item
{
private readonly bool canDrop;

public TestItem(bool canDrop = true)
public TestItem(MessageBus bus, bool canDrop = true)
: base(bus)
{
this.canDrop = canDrop;
}
@@ -289,6 +290,11 @@ protected override bool DropCore(MessageBus bus)

private sealed class TestItem2 : Item
{
public TestItem2(MessageBus bus)
: base(bus)
{
}

public override string ShortDescription => "a coin";

public override string LongDescription => throw new System.NotImplementedException();
@@ -15,30 +15,33 @@ public sealed class ItemsTest
[Fact]
public void AddOneItem()
{
Items items = new Items(new MessageBus());
MessageBus bus = new MessageBus();
Items items = new Items(bus);

Action act = () => items.Add("key", new TestItem());
Action act = () => items.Add("key", new TestItem(bus));

act.Should().NotThrow();
}

[Fact]
public void AddTwoItems()
{
Items items = new Items(new MessageBus());
MessageBus bus = new MessageBus();
Items items = new Items(bus);

items.Add("key", new TestItem());
Action act = () => items.Add("coin", new TestItem());
items.Add("key", new TestItem(bus));
Action act = () => items.Add("coin", new TestItem(bus));

act.Should().NotThrow();
}

[Fact]
public void TakeOneOfTwoItems()
{
Items items = new Items(new MessageBus());
items.Add("key", new TestItem());
TestItem coin = new TestItem();
MessageBus bus = new MessageBus();
Items items = new Items(bus);
items.Add("key", new TestItem(bus));
TestItem coin = new TestItem(bus);
items.Add("coin", coin);

Item taken = items.Take("coin");
@@ -49,10 +52,11 @@ public void TakeOneOfTwoItems()
[Fact]
public void TakeTwoItems()
{
Items items = new Items(new MessageBus());
TestItem key = new TestItem();
MessageBus bus = new MessageBus();
Items items = new Items(bus);
TestItem key = new TestItem(bus);
items.Add("key", key);
TestItem coin = new TestItem();
TestItem coin = new TestItem(bus);
items.Add("coin", coin);

Item takenCoin = items.Take("coin");
@@ -65,8 +69,9 @@ public void TakeTwoItems()
[Fact]
public void TakeItemAlreadyTaken()
{
Items items = new Items(new MessageBus());
items.Add("key", new TestItem());
MessageBus bus = new MessageBus();
Items items = new Items(bus);
items.Add("key", new TestItem(bus));

items.Take("key");
Item missing = items.Take("key");
@@ -87,10 +92,11 @@ public void TakeItemNotPresent()
[Fact]
public void DropItemAlreadyExists()
{
Items items = new Items(new MessageBus());
items.Add("key", new TestItem());
MessageBus bus = new MessageBus();
Items items = new Items(bus);
items.Add("key", new TestItem(bus));

Action act = () => items.Add("key", new TestItem());
Action act = () => items.Add("key", new TestItem(bus));

act.Should().Throw<InvalidOperationException>("Item 'key' already exists.");
}
@@ -102,7 +108,7 @@ public void DoCustomActionForItem()
List<string> messages = new List<string>();
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Items items = new Items(bus);
items.Add("ball", new TestItem());
items.Add("ball", new TestItem(bus));

items.Activate();
bus.Subscribe<SentenceMessage>(m => messages.Add($"Don't {m.Verb} the {m.Noun}"));
@@ -118,7 +124,7 @@ public void SkipCustomActionForItemNotPresent()
List<string> messages = new List<string>();
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Items items = new Items(bus);
items.Add("ball", new TestItem());
items.Add("ball", new TestItem(bus));

items.Activate();
bus.Subscribe<SentenceMessage>(m => messages.Add($"Don't {m.Verb} the {m.Noun}"));
@@ -134,7 +140,7 @@ public void SkipCustomActionForItemThatCannotHandleIt()
List<string> messages = new List<string>();
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Items items = new Items(bus);
items.Add("ball", new TestItem());
items.Add("ball", new TestItem(bus));

items.Activate();
bus.Subscribe<SentenceMessage>(m => messages.Add($"Don't {m.Verb} the {m.Noun}"));
@@ -150,7 +156,7 @@ public void SkipCustomActionForItemThatHasNoActions()
List<string> messages = new List<string>();
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Items items = new Items(bus);
items.Add("ball", new TestItemNoActions());
items.Add("ball", new TestItemNoActions(bus));

items.Activate();
bus.Subscribe<SentenceMessage>(m => messages.Add($"Don't {m.Verb} the {m.Noun}"));
@@ -166,7 +172,7 @@ public void SkipCustomActionBeforeActivation()
List<string> messages = new List<string>();
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Items items = new Items(bus);
items.Add("ball", new TestItem());
items.Add("ball", new TestItem(bus));

bus.Subscribe<SentenceMessage>(m => messages.Add($"Don't {m.Verb} the {m.Noun}"));
bus.Send(new SentenceMessage(new Word("throw", "TOSS"), new Word("ball", "BASEBALL")));
@@ -181,7 +187,7 @@ public void SkipCustomActionAfterDeactivation()
List<string> messages = new List<string>();
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Items items = new Items(bus);
items.Add("ball", new TestItem());
items.Add("ball", new TestItem(bus));

items.Activate();
items.Deactivate();
@@ -233,8 +239,8 @@ public void LookCountsItems()
MessageBus bus = new MessageBus();
Items items = new Items(bus);
items.Activate();
items.Add("one", new TestItem());
items.Add("two", new TestItem());
items.Add("one", new TestItem(bus));
items.Add("two", new TestItem(bus));

int count = items.Look("{0}");

@@ -243,13 +249,23 @@ public void LookCountsItems()

private sealed class TestItemNoActions : Item
{
public TestItemNoActions(MessageBus bus)
: base(bus)
{
}

public override string ShortDescription => "a dull item";

public override string LongDescription => "It's a very dull item.";
}

private sealed class TestItem : Item
{
public TestItem(MessageBus bus)
: base(bus)
{
}

public override string ShortDescription => "a test item";

public override string LongDescription => "It's a simple test item.";

0 comments on commit da2ce6c

Please sign in to comment.
You can’t perform that action at this time.