Exemplo n.º 1
0
        public void AmbiguousTwoWordThrows()
        {
            ParserCommand command1 = new ParserCommand("add");
            ParserCommand command2 = new ParserCommand("add file");

            // ReSharper disable once ObjectCreationAsStatement
            Should.Throw<ParserConfigurationException>(() => new CommandMapper(new[] { command1, command2 }));
        }
Exemplo n.º 2
0
        public void InvalidFirstWordRejected()
        {
            ParserCommand command1 = new ParserCommand("add file");
            ParserCommand command2 = new ParserCommand("add comment");
            var mapper = new CommandMapper(new[] { command1, command2 });
            mapper.Advance("remove");

            mapper.State.ShouldBe(MapperState.Rejected);
        }
Exemplo n.º 3
0
        public void CanFindOneAndOnly()
        {
            ParserCommand command = new ParserCommand("add");
            var mapper = new CommandMapper(new[] { command });
            mapper.Advance("add");

            mapper.State.ShouldBe(MapperState.Accepted);
            mapper.Command.ShouldBe(command);
        }
Exemplo n.º 4
0
        public void CanFindSecondOfTwo()
        {
            ParserCommand command1 = new ParserCommand("add");
            ParserCommand command2 = new ParserCommand("remove");
            var mapper = new CommandMapper(new[] { command1, command2 });
            mapper.Advance("remove");

            mapper.State.ShouldBe(MapperState.Accepted);
            mapper.Command.ShouldBe(command2);
        }
Exemplo n.º 5
0
        public ParserCommand AddCommand(string commandText)
        {
            if (_commands.Any(x => x.Text == commandText))
            {
                throw new ParserConfigurationException("Cannot add duplicate command '{0}'.", commandText);
            }

            var command = new ParserCommand(commandText);

            _commands.Add(command);

            return command;
        }
Exemplo n.º 6
0
        public void CanFindTwoWordCommand()
        {
            ParserCommand command1 = new ParserCommand("add file");
            ParserCommand command2 = new ParserCommand("add comment");
            var mapper = new CommandMapper(new[] { command1, command2 });
            mapper.State.ShouldBe(MapperState.Initial);

            mapper.Advance("add");
            mapper.State.ShouldBe(MapperState.Pending);

            mapper.Advance("comment");
            mapper.State.ShouldBe(MapperState.Accepted);
            mapper.Command.ShouldBe(command2);
        }
Exemplo n.º 7
0
        private void AddCommand(ParserCommand command)
        {
            AddCommand(command.Text, command);

            foreach (var alias in command.Aliases)
            {
                AddCommand(alias, command);
            }
        }
Exemplo n.º 8
0
            public void Add(Queue<string> words, ParserCommand command)
            {
                string word = words.Dequeue();
                if (words.Count == 0)
                {
                    if (_entries.ContainsKey(word))
                    {
                        throw new ParserConfigurationException("Ambiguous command: {0}", word);
                    }

                    _commands.Add(word, command);
                }
                else
                {
                    if (_commands.ContainsKey(word))
                    {
                        throw new ParserConfigurationException("Ambiguous command: {0}", word);
                    }

                    Entry child;
                    if (!_entries.TryGetValue(word, out child))
                    {
                        child = new Entry();
                        _entries.Add(word, child);
                    }

                    child.Add(words, command);
                }
            }
Exemplo n.º 9
0
        private void AddCommand(string commandText, ParserCommand command)
        {
            Queue<string> queue = new Queue<string>();
            foreach (var s in commandText.Split(' '))
            {
                queue.Enqueue(s);
            }

            _root.Add(queue, command);
        }
Exemplo n.º 10
0
        public void InvalidSecondWordRejected()
        {
            ParserCommand command1 = new ParserCommand("add file");
            ParserCommand command2 = new ParserCommand("add comment");
            var mapper = new CommandMapper(new[] { command1, command2 });
            mapper.Advance("add");
            mapper.State.ShouldBe(MapperState.Pending);

            mapper.Advance("junk");
            mapper.State.ShouldBe(MapperState.Rejected);
        }