Beispiel #1
0
                public void ShortGlobalOption_ExplicitValue_InNextArgumentToken_BindsAsParsed()
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key           = "command",
                        GlobalOptions =
                        {
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool | b",
                                Property = ri => ri.Bool1
                            }
                        }
                    });

                    var runInfo = (TestRunInfo)builder.Build(new string[] { "command", "-b", "true" });

                    Assert.True(runInfo.Bool1);
                }
Beispiel #2
0
                public void ShortOption_ImplicitWithoutValue_BindsAsTrue()
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key     = "command",
                        Options =
                        {
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool | b",
                                Property = ri => ri.Bool1
                            }
                        }
                    });

                    var runInfo = (TestRunInfo)builder.Build(new string[] { "command", "-b" });

                    Assert.True(runInfo.Bool1);
                }
Beispiel #3
0
                public void ShortOption_ExplicitValue_InNextArgumentToken_BindsAsParsed()
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key     = "command",
                        Options =
                        {
                            new Option <TestRunInfo, int>
                            {
                                Key      = "int | i",
                                Property = ri => ri.Int1
                            }
                        }
                    });

                    var runInfo = (TestRunInfo)builder.Build(new string[] { "command", "-i", "99" });

                    Assert.Equal(99, runInfo.Int1);
                }
Beispiel #4
0
            public void StackedOption_MappedToNonBoolProperty_Throws_1(string option)
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key           = "command",
                        GlobalOptions =
                        {
                            new Option <TestRunInfo, int>
                            {
                                Key      = "int | i",
                                Property = ri => ri.Int1
                            },
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool1 | b",
                                Property = ri => ri.Bool1
                            },
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool2 | c",
                                Property = ri => ri.Bool2
                            }
                        }
                    });

                    builder.Build(new string[] { "command", option });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.InvalidStackedOption, processException.ErrorType);
                Assert.Equal(0, processException.CommandLevel);
            }
Beispiel #5
0
            public void InvalidValue_ForBoolOption_OnParseErrorUseMessage_IsSet_ThrowsExpectedMessage(
                string option, string invalidBoolValue)
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key           = "command",
                        GlobalOptions =
                        {
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool1 | b",
                                Property = ri => ri.Bool1,
                                OnParseErrorUseMessage = value => value + " from OnParseErrorUseMessage"
                            },
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool2 | c",
                                Property = ri => ri.Bool2,
                                OnParseErrorUseMessage = value => value + " from OnParseErrorUseMessage"
                            }
                        }
                    });

                    builder.Build(new string[] { "command", option, invalidBoolValue });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.ParserInvalidValue, processException.ErrorType);
                Assert.Equal(0, processException.CommandLevel);
                Assert.Equal(invalidBoolValue + " from OnParseErrorUseMessage", processException.Message);
            }
Beispiel #6
0
            public void InvalidValueProgramArgument_ForNonBoolOption_OnParseErrorUseMessage_IsSet_ThrowsExpectedMessage()
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key           = "command",
                        GlobalOptions =
                        {
                            new Option <TestRunInfo, int>
                            {
                                Key      = "int1 | i",
                                Property = ri => ri.Int1,
                                OnParseErrorUseMessage = value => value + " from OnParseErrorUseMessage"
                            }
                        },
                        SubCommands =
                        {
                            new SubCommand <TestRunInfo>
                            {
                                Key = "subcommand"
                            }
                        }
                    });

                    builder.Build(new string[] { "command", "subcommand", "--int1", "invalid" });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.ParserInvalidValue, processException.ErrorType);
                Assert.Equal(1, processException.CommandLevel);
                Assert.Equal("invalid from OnParseErrorUseMessage", processException.Message);
            }
                public void OnSuccessfulBuild_Invokes()
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(
                        new Command <TestRunInfo>
                    {
                        Key     = "command",
                        Options =
                        {
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool",
                                Property = ri => ri.Bool1
                            }
                        },
                        SubCommands =
                        {
                            new Command <TestRunInfo>
                            {
                                Key       = "subcommand",
                                Arguments =
                                {
                                    new PropertyArgument <TestRunInfo, int>
                                    {
                                        Property = ri => ri.Int1
                                    }
                                }
                            }
                        }
                    },
                        runInfo =>
                    {
                        Assert.True(runInfo.Bool1);
                        Assert.Equal(99, runInfo.Int1);
                    });

                    builder.Build(new string[] { "command", "--bool", "subcommand", "99" });
                }
            public void Processes_Stacked_Successfully()
            {
                RunInfoBuilder builder = GetBuilder();

                builder.Commands.AddDefault(new DefaultCommand <TestRunInfo>
                {
                    Options =
                    {
                        new Option <TestRunInfo, bool>
                        {
                            Key      = "bool1 | a",
                            Property = ri => ri.Bool1
                        },
                        new Option <TestRunInfo, bool>
                        {
                            Key      = "bool2 | b",
                            Property = ri => ri.Bool2
                        },
                        new Option <TestRunInfo, bool>
                        {
                            Key      = "bool3 | c",
                            Property = ri => ri.Bool3
                        },
                    }
                });

                assertFromArgs(new string[] { "-abc" });
                assertFromArgs(new string[] { "-abc", "true" });
                assertFromArgs(new string[] { "-abc=true" });

                void assertFromArgs(string[] args)
                {
                    var result = (TestRunInfo)builder.Build(args);

                    Assert.True(result.Bool1);
                    Assert.True(result.Bool2);
                    Assert.True(result.Bool3);
                }
            }
            public void SequenceArgument_Success()
            {
                RunInfoBuilder builder = GetBuilder();

                builder.Commands.AddDefault(new DefaultCommand <TestRunInfo>
                {
                    Arguments =
                    {
                        new SequenceArgument <TestRunInfo, int>
                        {
                            ListProperty = ri => ri.IntList1
                        }
                    }
                });

                var result = (TestRunInfo)builder.Build(new string[] { "1", "2", "3" });

                Assert.Equal(3, result.IntList1.Count);
                Assert.Equal(1, result.IntList1[0]);
                Assert.Equal(2, result.IntList1[1]);
                Assert.Equal(3, result.IntList1[2]);
            }
Beispiel #10
0
            public void EndResult_StopsFurtherProcessing()
            {
                RunInfoBuilder builder = GetBuilder();

                builder.Commands.AddDefault(
                    new DefaultCommand <TestRunInfo>
                {
                    OnMatched = ri => ProcessResult.End,
                    Options   =
                    {
                        new Option <TestRunInfo, bool>
                        {
                            Key      = "bool",
                            Property = ri => ri.Bool1
                        }
                    }
                });

                var runInfo = builder.Build(new string[] { "--bool" }) as TestRunInfo;

                Assert.False(runInfo?.Bool1);
            }
            public void OnSuccessfulBuild_Invokes()
            {
                RunInfoBuilder builder = GetBuilder();

                builder.Commands.AddDefault(
                    new DefaultCommand <TestRunInfo>
                {
                    Options =
                    {
                        new Option <TestRunInfo, bool>
                        {
                            Key      = "bool",
                            Property = ri => ri.Bool1
                        }
                    }
                },
                    runInfo =>
                {
                    Assert.True(runInfo.Bool1);
                });

                builder.Build(new string[] { "--bool" });
            }
            public void StringSubCommand_BindsCorrectly()
            {
                RunInfoBuilder builder = GetBuilder();

                var args = new string[]
                {
                    "ComplexCommand", "one", "33", "5", "6", "-s", "short_string", "--int=100", "--bool1",
                    "StringSubCommand", "short_string_from_sub", "two", "first", "last", "--bool2"
                };

                TestRunInfo result = (TestRunInfo)builder.Build(args);

                //Assert.Equal(2, result.Int1);
                Assert.Equal(33, result.Int2);
                Assert.Equal(11, result.Int3);
                //Assert.Equal("short_string", result.String1);
                Assert.Equal(100, result.Int1);                        // should override the fisrt arg "one"
                Assert.True(result.Bool1);
                Assert.Equal("short_string_from_sub", result.String1); // overrides top level command
                Assert.Equal("two", result.String2);
                Assert.Equal("firstlast", result.String3);
                Assert.True(result.Bool2);
            }
Beispiel #13
0
            public void ParsedValues_SuccessfullyBind_ToListProperty()
            {
                RunInfoBuilder builder = GetBuilder();

                builder.Commands.Add(new Command <TestRunInfo>
                {
                    Key       = "command",
                    Arguments =
                    {
                        new SequenceArgument <TestRunInfo, int>
                        {
                            ListProperty = ri => ri.IntList1
                        }
                    }
                });

                var runInfo = (TestRunInfo)builder.Build(new string[] { "command", "1", "2", "3" });

                Assert.Equal(3, runInfo.IntList1.Count);
                Assert.Equal(1, runInfo.IntList1[0]);
                Assert.Equal(2, runInfo.IntList1[1]);
                Assert.Equal(3, runInfo.IntList1[2]);
            }
Beispiel #14
0
            public void NonBoolOption_MissingValueProgramArgument_Throws()
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key           = "command",
                        GlobalOptions =
                        {
                            new Option <TestRunInfo, int>
                            {
                                Key      = "int | i",
                                Property = ri => ri.Int1
                            }
                        },
                        SubCommands =
                        {
                            new SubCommand <TestRunInfo>
                            {
                                Key = "subcommand"
                            }
                        }
                    });

                    builder.Build(new string[] { "command", "subcommand", "--int" });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.ExpectedProgramArgument, processException.ErrorType);
                Assert.Equal(1, processException.CommandLevel);
            }
            public void ParsedArgumentValues_SuccessfullyBindToRunInfo()
            {
                RunInfoBuilder builder = GetBuilder();

                builder.Commands.Add(new Command <TestRunInfo>
                {
                    Key         = "command",
                    SubCommands =
                    {
                        new Command <TestRunInfo>
                        {
                            Key       = "subcommand",
                            Arguments =
                            {
                                new PropertyArgument <TestRunInfo, bool>
                                {
                                    Property = ri => ri.Bool1
                                },
                                new PropertyArgument <TestRunInfo, int>
                                {
                                    Property = ri => ri.Int1
                                },
                                new PropertyArgument <TestRunInfo, string>
                                {
                                    Property = ri => ri.String1
                                }
                            }
                        }
                    }
                });

                var runInfo = (TestRunInfo)builder.Build(new string[] { "command", "subcommand", "true", "99", "parsed" });

                Assert.True(runInfo.Bool1);
                Assert.Equal(99, runInfo.Int1);
                Assert.Equal("parsed", runInfo.String1);
            }
Beispiel #16
0
            // This tests that an invalid option program argument simply skips
            // the option stage and hits the invalid program argument stage.
            public void InvalidOption_TokenizeFail_Throws(string option)
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key         = "command",
                        SubCommands =
                        {
                            new SubCommand <TestRunInfo>
                            {
                                Key     = "subcommand",
                                Options =
                                {
                                    new Option <TestRunInfo, int>
                                    {
                                        Key      = "int | i",
                                        Property = ri => ri.Int1
                                    }
                                }
                            }
                        }
                    });

                    builder.Build(new string[] { "command", "subcommand", option });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.InvalidProgramArgument, processException.ErrorType);
                Assert.Equal(1, processException.CommandLevel);
            }
Beispiel #17
0
            public void ExpectedArgument_ButNoMore_ProgramArguments_Throws()
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key         = "command",
                        SubCommands =
                        {
                            new SubCommand <TestRunInfo>
                            {
                                Key       = "subcommand",
                                Arguments =
                                {
                                    new SequenceArgument <TestRunInfo, string>
                                    {
                                        ListProperty = ri => ri.StringList1
                                    }
                                }
                            }
                        }
                    });

                    builder.Build(new string[] { "command", "subcommand" });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.ExpectedProgramArgument, processException.ErrorType);
                Assert.Equal(1, processException.CommandLevel);
            }
Beispiel #18
0
            public void ProgramArguments_ContainsUnparseableValues_Throws(params string[] sequenceValues)
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key       = "command",
                        Arguments =
                        {
                            new SequenceArgument <TestRunInfo, int>
                            {
                                ListProperty           = ri => ri.IntList1,
                                OnParseErrorUseMessage = value => value + " from OnParseErrorUseMessage"
                            }
                        }
                    });

                    var programArgs = new List <string> {
                        "command"
                    };
                    programArgs.AddRange(sequenceValues);

                    builder.Build(programArgs.ToArray());
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.ParserInvalidValue, processException.ErrorType);
                Assert.Equal(0, processException.CommandLevel);
                Assert.Equal("a from OnParseErrorUseMessage", processException.Message);
            }
Beispiel #19
0
            public void SubCommandsConfigured_NoMoreArgs_Throws()
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key         = "command",
                        SubCommands =
                        {
                            new SubCommand <TestRunInfo>
                            {
                                Key         = "sub1",
                                SubCommands =
                                {
                                    new SubCommand <TestRunInfo>
                                    {
                                        Key = "sub2"
                                    }
                                }
                            }
                        }
                    });

                    builder.Build(new string[] { "command", "sub1" });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.ExpectedProgramArgument, processException.ErrorType);
                Assert.Equal(1, processException.CommandLevel);
            }
Beispiel #20
0
            public void SubCommandsConfigured_NextArg_DoestMatch_Throws()
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key         = "command",
                        SubCommands =
                        {
                            new SubCommand <TestRunInfo>
                            {
                                Key         = "sub1",
                                SubCommands =
                                {
                                    new SubCommand <TestRunInfo>
                                    {
                                        Key = "sub2"
                                    }
                                }
                            }
                        }
                    });

                    builder.Build(new string[] { "command", "sub1", "invalid_match" });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.InvalidSubCommand, processException.ErrorType);
                Assert.Equal(1, processException.CommandLevel);
            }
Beispiel #21
0
                public void ContinueResult_CorrectlySetsFlag()
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(
                        new Command <TestRunInfo>
                    {
                        Key         = "command",
                        SubCommands =
                        {
                            new SubCommand <TestRunInfo>
                            {
                                Key       = "subcommand",
                                OnMatched = ri =>
                                {
                                    ri.String1 = "SetFromOnMatched";

                                    return(ProcessResult.Continue);
                                },
                                Options =
                                {
                                    new Option <TestRunInfo, bool>
                                    {
                                        Key      = "bool",
                                        Property = ri => ri.Bool1
                                    }
                                }
                            }
                        }
                    });

                    var runInfo = builder.Build(new string[] { "command", "subcommand", "--bool" }) as TestRunInfo;

                    Assert.True(runInfo?.Bool1);
                    Assert.Equal("SetFromOnMatched", runInfo?.String1);
                }
Beispiel #22
0
                public void FullOption_OnProcessInvokes_IfSet()
                {
                    RunInfoBuilder builder = GetBuilder();

                    bool invoked = false;

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key         = "command",
                        SubCommands =
                        {
                            new Command <TestRunInfo>
                            {
                                Key     = "subcommand",
                                Options =
                                {
                                    new Option <TestRunInfo, int>
                                    {
                                        Key      = "int | i",
                                        Property = ri => ri.Int1,
                                        OnParsed = arg =>
                                        {
                                            invoked = true;
                                            return(ProcessResult.Continue);
                                        }
                                    }
                                }
                            }
                        }
                    });

                    var runInfo = (TestRunInfo)builder.Build(new string[] { "command", "subcommand", "--int", "99" });

                    Assert.Equal(99, runInfo.Int1);
                    Assert.True(invoked);
                }
Beispiel #23
0
            public void InvalidValue_ForBoolOption_NotParseable_Throws(
                string option, string invalidBoolValue)
            {
                Action testCode = () =>
                {
                    RunInfoBuilder builder = GetBuilder();

                    builder.Commands.Add(new Command <TestRunInfo>
                    {
                        Key           = "command",
                        GlobalOptions =
                        {
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool1 | b",
                                Property = ri => ri.Bool1
                            },
                            new Option <TestRunInfo, bool>
                            {
                                Key      = "bool2 | c",
                                Property = ri => ri.Bool2
                            }
                        }
                    });

                    builder.Build(new string[] { "command", option, invalidBoolValue });
                };

                Exception exception = Record.Exception(testCode);

                var processException = exception as ProcessException;

                Assert.NotNull(processException);
                Assert.Equal(ProcessError.ParserInvalidValue, processException.ErrorType);
                Assert.Equal(0, processException.CommandLevel);
            }