示例#1
0
        public void EnterTwice()
        {
            Room room = new TestRoom(new MessageBus());

            room.Enter();

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

            act.Should().Throw <InvalidOperationException>().WithMessage("Cannot Enter again.");
        }
示例#2
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();
        }
示例#3
0
        public void EnterLeaveEnter()
        {
            MessageBus             bus        = new MessageBus();
            string                 lastOutput = null;
            Action <OutputMessage> subscriber = m => lastOutput = m.Text;

            bus.Subscribe(subscriber);
            Room room = new TestRoom(bus);

            room.Enter();
            room.Leave();
            room.Enter();
            bus.Send(new SentenceMessage(new Word("hello", "hello"), new Word("world", "world")));

            lastOutput.Should().Be("Hello, world!");
        }
示例#4
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.");
        }
示例#5
0
        public void LeaveTwice()
        {
            Room room = new TestRoom(new MessageBus());

            room.Enter();

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

            act.Should().Throw <InvalidOperationException>().WithMessage("Cannot Leave before Enter.");
        }
示例#6
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);
        }
示例#7
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.");
        }
示例#8
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.");
        }
示例#9
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.");
        }
示例#10
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);
        }
示例#11
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.");
        }
示例#12
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!");
        }
示例#13
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.");
        }
示例#14
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.");
        }
示例#15
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!");
        }
示例#16
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();
        }
示例#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.");
        }