コード例 #1
0
        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();
        }
コード例 #2
0
        public void LeaveBeforeEnter()
        {
            Room room = new TestRoom(new MessageBus());

            Action act = () => room.Leave();

            act.Should().Throw <InvalidOperationException>().WithMessage("Cannot Leave before Enter.");
        }
コード例 #3
0
        public void EnterTwice()
        {
            Room room = new TestRoom(new MessageBus());

            room.Enter();

            Action act = () => room.Enter();

            act.Should().Throw <InvalidOperationException>().WithMessage("Cannot Enter again.");
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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.");
        }
コード例 #6
0
        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.");
        }
コード例 #7
0
        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.");
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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();
        }
コード例 #10
0
        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.");
        }
コード例 #11
0
        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!");
        }
コード例 #12
0
        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();
        }
コード例 #13
0
        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.");
        }
コード例 #14
0
        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.");
        }
コード例 #15
0
        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.");
        }
コード例 #16
0
        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!");
        }
コード例 #17
0
        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.");
        }
コード例 #18
0
        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.");
        }
コード例 #19
0
        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.");
        }
コード例 #20
0
        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.");
        }