예제 #1
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();
        }
예제 #2
0
        public void Valid_Complex_Required_Option_Should_be_Parsed()
        {
            SimpleRequiredConfig     oConfig = new SimpleRequiredConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            string expected = "http://192.168.1.2/root-path/";

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"-required1 ""{expected}"" -required2 123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"-required1:""{expected}"" -required2:123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"/required1 ""{expected}"" /required2 123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"/required1:""{expected}"" /required2:123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);
        }
예제 #3
0
        public void Valid_Custom_DataType_Option_Shoud_Be_Parsed()
        {
            CustomDataTypeConfig oConfig = new CustomDataTypeConfig();

            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.RegisterCustomDataTypeHandler(typeof(Color), (name, value) =>
            {
                switch (value)
                {
                case "Red":
                    return(Color.Red);

                case "Yellow":
                    return(Color.Yellow);

                case "Green":
                    return(Color.Green);

                default:
                    return(Color.None);
                }
            },

                                                  (name, value, required) =>
            {
                return(new string[] { "None", "Red", "Yellow", "Green" }.Contains(value));
            });

            oParser.ParseArguments(oConfig, @"-color ""Green""");

            Assert.AreEqual(Color.Green, oConfig.Color);
        }
예제 #4
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();
        }
예제 #5
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();
        }
예제 #6
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();
        }
예제 #7
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");
        }
예제 #8
0
        public void Valid_String_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-s:""Example Name""");

            Assert.AreEqual("Example Name", oConfig.StringValueOption);
        }
예제 #9
0
        public void Valid_Char_Option_With_Single_Numeric_Value_Should_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "-c 1");

            Assert.AreEqual('1', oConfig.CharValueOption);
        }
예제 #10
0
        public void Valid_Char_Option_With_Uppercase_Value_Should_be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "-c A");

            Assert.AreEqual('A', oConfig.CharValueOption);
        }
예제 #11
0
        public void Valid_Int16_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-i16 32767");

            Assert.AreEqual(32767, oConfig.Int16ValueOption);
        }
예제 #12
0
        public void Valid_Int64_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-l 1234567890");

            Assert.AreEqual(1234567890, oConfig.Int64ValueOption);
        }
예제 #13
0
        public void Valid_UInt64_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-ui64 131072");

            Assert.AreEqual((ulong)131072, oConfig.UInt64ValueOption);
        }
예제 #14
0
        public void Valid_UInt32_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-ui32 65536");

            Assert.AreEqual((uint)65536, oConfig.UInt32ValueOption);
        }
예제 #15
0
        public void Valid_Bool_Option_With_AlternativeName_And_Alternative_Value_Should_be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "/? false");

            Assert.AreEqual(false, oConfig.ShowHelpOption);
        }
예제 #16
0
        public void Valid_Directory_Info_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-dir ""C:\Windows""");

            Assert.IsNotNull(oConfig.DirInfoValueOption);
        }
예제 #17
0
        public void Parse_Valid_UInt16_Long_Option()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-ui16 65535");

            Assert.AreEqual((ushort)65535, oConfig.UInt16ValueOption);
        }
예제 #18
0
        public void Valid_FileInfo_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-file ""C:\Windows\explorer.exe""");

            Assert.IsNotNull(oConfig.FileInfoValueOption);
        }
예제 #19
0
        public void Valid_DateTime_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"--date ""1970-04-01 10:43:28""");

            Assert.AreEqual(DateTime.Parse("1970-04-01 10:43:28"), oConfig.DateValueOption);
        }
예제 #20
0
        public void Invalid_UInt64_Option_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult actual = oParser.ParseArguments(oConfig, @"-ui64 -1");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
        }
예제 #21
0
        public void Valid_Required_Option_Should_Be_Parsed()
        {
            SimpleRequiredConfig     oConfig = new SimpleRequiredConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"/required1:""abc"" -required2:123");

            Assert.AreEqual("abc", oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);
        }
예제 #22
0
        public void Invalid_Uri_Option_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult oResult = oParser.ParseArguments(oConfig, $@"/uri ""C:\\Root\\Folder""");

            Assert.AreEqual(ResultStatus.Failure, oResult.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, oResult.Errors.First().ErrorType);
        }
예제 #23
0
        public void Valid_Parameters_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"""First Parameter Value"" ""Second Parameter Value""");

            Assert.AreEqual("First Parameter Value", oConfig.TextValueParameter1);
            Assert.AreEqual("Second Parameter Value", oConfig.TextValueParameter2);
        }
예제 #24
0
        public void Invalid_Char_Option_With_Long_Numeric_Value_Should_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult actual = oParser.ParseArguments(oConfig, "-c 123");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
        }
예제 #25
0
        public void Valid_Bool_Options_Should_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "--help -v:yes");

            Assert.AreEqual(true, oConfig.ShowHelpOption);
            Assert.AreEqual(true, oConfig.VerboseMessagesOption);
        }
예제 #26
0
        public void Invalid_Single_Option_With_Exceeding_Value_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
            ParseResult actual = oParser.ParseArguments(oConfig, $@"--single {3.402823E+39}");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
        }
예제 #27
0
        public void Valid_Uri_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            string      expected = "http://example.com/root/";
            ParseResult oResult  = oParser.ParseArguments(oConfig, $@"/uri ""{expected}""");

            Assert.AreEqual(ResultStatus.Success, oResult.Status);
            Assert.AreEqual(expected, oConfig.UriOptionValue.ToString());
        }
예제 #28
0
        public void Invalid_Bool_Option_With_AlternativeName_Should_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult actual = oParser.ParseArguments(oConfig, "/? Nein");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
            Assert.AreEqual($@"Value ""Nein"" is invalid for option ""?""", actual.Errors.First().Message);
        }
예제 #29
0
        public void Valid_SecureString_Option_Should_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            string value = "this is my secret";

            oParser.ParseArguments(oConfig, $@"-secure-string ""{value}""");

            Assert.AreEqual(value, oConfig.SecureStringOption.GetString());
        }
예제 #30
0
        public void Valid_Mixed_Arguments_With_Correct_Sequence_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"""First Parameter Value"" ""Second Parameter Value"" --verbose:yes -i:123");

            Assert.AreEqual("First Parameter Value", oConfig.TextValueParameter1);
            Assert.AreEqual("Second Parameter Value", oConfig.TextValueParameter2);
            Assert.AreEqual(true, oConfig.VerboseMessagesOption);
            Assert.AreEqual(123, oConfig.Int32ValueOption);
        }