Пример #1
0
        public void WhenParsingInput_AssertDirectResult()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('1', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false),

                new ConsoleKeyInfo('-', ConsoleKey.OemMinus, false, false, false),
                new ConsoleKeyInfo('t', ConsoleKey.T, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.ThinkWeHaveSomething.ShouldBeTrue();
            result.Command.Name.ShouldBe("Command1");
            result.FlaggedParameters.ShouldBeEmpty();
            result.ValuedParameters.ShouldBeEmpty();
            result.RemainingText.ShouldBe("-t");
        }
Пример #2
0
        public void WhenHittingEscapeOnCommand_AssertCommandNameCleared()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('\u0027', ConsoleKey.Escape, false, false, false),

                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.Command.ShouldBeNull();
            result.FlaggedParameters.ShouldBeEmpty();
            result.ValuedParameters.ShouldBeEmpty();
            result.RemainingText.ShouldBeEmpty();
        }
Пример #3
0
        public void WhenHittingEscapeOnParameterName_AssertParameterNameCleared()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('1', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false),

                new ConsoleKeyInfo('-', ConsoleKey.OemMinus, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('b', ConsoleKey.B, false, false, false),
                new ConsoleKeyInfo('c', ConsoleKey.C, false, false, false),
                new ConsoleKeyInfo('\u0027', ConsoleKey.Escape, false, false, false),

                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.Command.Name.ShouldBe("Command1");
            result.FlaggedParameters.ShouldBeEmpty();
            result.ValuedParameters.ShouldBeEmpty();
            result.RemainingText.ShouldBeEmpty();
        }
Пример #4
0
        public void WhenBackspacePressedManyTimes_AssertTextEmpty()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.RemainingText.ShouldBeEmpty();
        }
Пример #5
0
        public void WhenCommandEnteredAndTabbingForParam_AssertParamShowed()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('1', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false),

                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.ThinkWeHaveSomething.ShouldBeTrue();
            result.Command.Name.ShouldBe("Command1");
            result.FlaggedParameters.ShouldBeEmpty();
            result.ValuedParameters.Single().Parameter.Name.ShouldBe("Param1");
            result.RemainingText.ShouldBeEmpty();
        }
Пример #6
0
        public ICommandCache Get <T> (bool init = false) where T : class, IBaseCommand, new()
        {
            Type          type  = typeof(T);
            ICommandCache cache = caches.Retrieve(type, CreateCache <T>);

            return(cache);
        }
Пример #7
0
        public void Unregister <T> (string eventName) where T : class, IBaseCommand, new()
        {
            IList <ICommandCache> caches;

            if (eventMap.TryGetValue(eventName, out caches))
            {
                ICommandCache cache = commands.Get <T>();
                caches.Remove(cache);
            }
        }
Пример #8
0
        public void WhenHittingEnterWithNothingEnteredIntoLine_AssertNotValid()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.ThinkWeHaveSomething.ShouldBeFalse();
        }
Пример #9
0
        /// <summary>
        /// 构造函数
        /// </summary>
        protected RunDevice()
        {
            this.Tag            = null;
            this.IsRunDevice    = true;
            this.DevicePriority = DevicePriority.Normal;
            this._CommandCache  = new CommandCache.CommandCache();

            this._Timer = new Timer(1000)
            {
                AutoReset = true
            };
            this._Timer.Elapsed  += new ElapsedEventHandler(OnTimedEvent);
            this.IsRunTimer       = false;
            this.RunTimerInterval = 1000;
        }
Пример #10
0
        public void WhenParsingTabbedInputMultipleTimes_AssertTabResult()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('1', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false),

                new ConsoleKeyInfo('-', ConsoleKey.OemMinus, false, false, false),
                new ConsoleKeyInfo('p', ConsoleKey.T, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),

                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),

                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false), // Tab a lot to make sure the rollover works

                new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false),
                new ConsoleKeyInfo('v', ConsoleKey.V, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.ThinkWeHaveSomething.ShouldBeTrue();
            result.Command.Name.ShouldBe("Command1");
            result.FlaggedParameters.ShouldBeEmpty();
            result.ValuedParameters.Single().Parameter.Name.ShouldBe("Param3");
            result.ValuedParameters.Single().Value.ShouldBe("val");
            result.RemainingText.ShouldBeEmpty();
        }
Пример #11
0
        public void WhenTabbingWithNoCommandEntered_AssertCommandInserted()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.ThinkWeHaveSomething.ShouldBeTrue();
            result.Command.Name.ShouldBe("Command1");
            result.FlaggedParameters.ShouldBeEmpty();
            result.ValuedParameters.ShouldBeEmpty();
            result.RemainingText.ShouldBeEmpty();
        }
Пример #12
0
        private static void InitCommands(ICommandCache commandCache)
        {
            string[] commandNames = new[] { "Help", "Exit", "Command1", "Command2", "Command3" };

            foreach (string commandName in commandNames)
            {
                ParameterOption[] parameters = new[] {
                    new ParameterOption("Param1", true, "Parameter 1"),
                    new ParameterOption("Param2", true, "Parameter 2"),
                    new ParameterOption("Param3", true, "Parameter 3"),

                    new ParameterOption("Flag1", false, "Flag 1"),
                    new ParameterOption("Flag2", false, "Flag 2"),
                };
                var command = new Command(commandName, '-', string.Empty, parameters);

                commandCache.AddCommand(command);
            }
        }
Пример #13
0
        public void WhenHittingTabButNothingToTabTo_AssertTextUnchanged()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('1', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false),

                new ConsoleKeyInfo('-', ConsoleKey.OemMinus, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),
                new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false),

                new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false),
                new ConsoleKeyInfo('v', ConsoleKey.V, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            var parser = new ConsoleCommandLineParser();
            ParseCommandLineResult result = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            result.ThinkWeHaveSomething.ShouldBeTrue();
            result.Command.Name.ShouldBe("Command1");
            result.FlaggedParameters.ShouldBeEmpty();
            result.ValuedParameters.Single().Parameter.Name.ShouldBe("a");
            result.ValuedParameters.Single().Value.ShouldBe("val");
            result.RemainingText.ShouldBeEmpty();
        }
Пример #14
0
        public void WhenUsingDownArrowForPastResultsIsPressedALot_AssertDirectResult()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('1', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            //Type in Command1 10 times, meaning the entire history is filled with this one
            var parser = new ConsoleCommandLineParser();

            for (int i = 0; i < 10; i++)
            {
                parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);
                _commandingConsoleSubstitute.ResetNextKeyIndex();
            }

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('2', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            //Type in Command2 one time, so only the first command in history buffer is Command2 and the rest are Command1
            for (int i = 0; i < 1; i++)
            {
                parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);
                _commandingConsoleSubstitute.ResetNextKeyIndex();
            }

            //Hit up arrow key 9 times. Meaning skip over the most recent Command2 and get Command1
            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                //Add a few misc characters to prove they're removed when using up arrow
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),

                //Down arrow 10 times so it goes past the history buffer
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),

                //Down arrow 10 more times to go to the only instance of Command2 in the history buffer. The one at the front. Index 0
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            ParseCommandLineResult finalResult = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            finalResult.ThinkWeHaveSomething.ShouldBeTrue();
            finalResult.Command.Name.ShouldBe("Command2");
        }
        public ParseCommandLineResult ParseCommandLine(string userTextSoFar, ICommandCache commandsCache)
        {
            //Steps:
            // - Check if command name has been set yet by searching string for a space
            // - If no space, try to find a command that matches the already existing name
            // - If has space, try to find command and then try to use parameter

            var sanitizedText = SanitizeUserText(userTextSoFar);
            var spaceIndex    = sanitizedText.IndexOf(SpaceChar);

            if (!SpaceCharacterFound(spaceIndex))
            {
                //Don't have a command set yet, so try to find one that matches
                var possibleCommandNames = commandsCache.PossibleCommands
                                           .Where(x => x.Name.StartsWith(sanitizedText, StringComparison.OrdinalIgnoreCase))
                                           .Select(x => x.Name)
                                           .OrderBy(x => x);

                var existingCommand = commandsCache.PossibleCommands
                                      .FirstOrDefault(command => command.Name.Equals(sanitizedText, StringComparison.OrdinalIgnoreCase));

                var remainingText = sanitizedText;
                if (existingCommand != null)
                {
                    remainingText = string.Empty;
                }

                return(new ParseCommandLineResult(existingCommand, null, null, remainingText, possibleCommandNames));
            }
            else
            {
                //Guess we know the command
                var commandName  = sanitizedText.Substring(0, spaceIndex);
                var knownCommand = commandsCache
                                   .PossibleCommands
                                   .FirstOrDefault(x => string.Equals(x.Name, commandName, StringComparison.OrdinalIgnoreCase));

                if (knownCommand == null)
                {
                    //Didn't find the command
                    return(ParseCommandLineResult.CouldNotParseCommand);
                }
                else
                {
                    var textAfterCommand = sanitizedText.Substring(spaceIndex + 1);
                    var parameters       = SplitParameters(textAfterCommand);

                    if (parameters.Any())
                    {
                        return(ParseCommandLineResultFromParameters(knownCommand, parameters));
                    }
                    else
                    {
                        return(new ParseCommandLineResult(knownCommand,
                                                          new List <ParameterOption>(),
                                                          new List <(ParameterOption Param, string Value)>(),
                                                          string.Empty,
                                                          new List <string>()));
                    }
                }
            }
        }
Пример #16
0
        public void WhenUsingPastResultsDownArrow_AssertDirectResult()
        {
            ICommandCache commandCache = GenerateCommandCache();

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('1', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            //Type in Command1 10 times, meaning the entire history is filled with this one
            var parser = new ConsoleCommandLineParser();

            for (int i = 0; i < 10; i++)
            {
                parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);
                _commandingConsoleSubstitute.ResetNextKeyIndex();
            }

            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                new ConsoleKeyInfo('C', ConsoleKey.C, true, false, false),
                new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('m', ConsoleKey.M, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('n', ConsoleKey.N, false, false, false),
                new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false),
                new ConsoleKeyInfo('2', ConsoleKey.NumPad1, false, false, false),
                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            //Type in Command2 3 times, so only the very last item is Command1
            for (int i = 0; i < 3; i++)
            {
                parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);
                _commandingConsoleSubstitute.ResetNextKeyIndex();
            }

            //Hit up down key 8 times. Meaning start looking at the end of the history buffer and end of on the last instance of Command2 typed in
            _commandingConsoleSubstitute.SetConsoleKeyInfoOrder(new[]
            {
                //Add a few misc characters to prove they're removed when using up arrow
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),
                new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false),

                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),
                new ConsoleKeyInfo(' ', ConsoleKey.DownArrow, true, false, false),

                new ConsoleKeyInfo('\n', ConsoleKey.Enter, false, false, false),
            });

            ParseCommandLineResult finalResult = parser.ParseCommandLine(commandCache, _commandingConsoleSubstitute);

            finalResult.ThinkWeHaveSomething.ShouldBeTrue();
            finalResult.Command.Name.ShouldBe("Command2");
        }
Пример #17
0
        public void Register <T> (string eventName) where T : class, IBaseCommand, new()
        {
            ICommandCache cache = commands.Get <T>();

            eventMap.Retrieve(eventName, CreateList).Add(cache);
        }
 public InventoryDialogService(IInventoryStorage storage, IInputParserService parser, ICommandCache commandCache)
 {
     this.storage      = storage;
     this.parser       = parser;
     this.commandCache = commandCache;
 }