Beispiel #1
0
        public void Parse_MoreThanOneOptionValue_ShouldAddError()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            var tokens = new List <Token>
            {
                new OptionToken("enum")
                {
                    OptionValues = { "Debug", "Trace" }
                }
            };

            var parseResult = new ParseResult();

            parseResult.CommandOptions = new DataTypesCommandOptions();

            parser.Parse(tokens, parseResult);

            parseResult.HasErrors.Should().BeTrue();
            parseResult.Errors.Should().HaveCount(1);
            parseResult.Errors[0].Should().BeOfType <OptionMultipleValuesError>();

            var error = (OptionMultipleValuesError)parseResult.Errors[0];

            error.OptionName.Should().Be("enum");
            error.GetErrorMessage().Should().Be("Multiple values are given for the option --enum, but the option expects a single value.");
        }
Beispiel #2
0
        public void Parse_ValidValues_ShouldParseAndAssignToTargetProperty()
        {
            var valueParser = A.Fake <ValueParser>();
            var parser      = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "logLevel");

            parser.ValueParser = valueParser;

            var tokens = new List <Token>
            {
                new OptionToken("logLevel")
                {
                    OptionValues = { "Debug" }
                }
            };
            var parseResult             = new ParseResult();
            var dataTypesCommandOptions = new DataTypesCommandOptions();

            parseResult.CommandOptions = dataTypesCommandOptions;

            LogLevel logLevel;

            A.CallTo(() => valueParser.TryParseEnum("Debug", out logLevel))
            .Returns(true)
            .AssignsOutAndRefParameters(LogLevel.Debug);

            parser.Parse(tokens, parseResult);

            parseResult.HasErrors.Should().BeFalse();
            parseResult.CommandOptions.Should().BeOfType <DataTypesCommandOptions>();

            dataTypesCommandOptions.Enum.Should().Be(LogLevel.Debug);

            A.CallTo(() => valueParser.TryParseEnum("Debug", out logLevel)).MustHaveHappened();
        }
Beispiel #3
0
        public void GetHelpText_ShouldReturnSpecifiedHelpText()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.OptionHelp = "Help text for option enum.";

            parser.GetHelpText().Should().Be(@"Help text for option enum. Possible values: Trace, Debug, Info, Error.");
        }
Beispiel #4
0
        public void Parse_OptionNotPresent_ShouldAssignDefaultValueToTargetProperty()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "logLevel");

            parser.OptionDefaultValue = LogLevel.Error;

            var tokens                  = new List <Token>();
            var parseResult             = new ParseResult();
            var dataTypesCommandOptions = new DataTypesCommandOptions();

            parseResult.CommandOptions = dataTypesCommandOptions;

            parser.Parse(tokens, parseResult);

            dataTypesCommandOptions.Enum.Should().Be(LogLevel.Error);
        }
Beispiel #5
0
        public void GetHelpText_EnumValuesHelpPresent_ShouldReturnSpecifiedHelpText()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.OptionHelp = "Help text for option enum.";
            parser.EnumValuesHelps.Add(LogLevel.Trace, "Trace help.");
            parser.EnumValuesHelps.Add(LogLevel.Debug, "Debug help.");
            parser.EnumValuesHelps.Add(LogLevel.Info, "Info help.");
            parser.EnumValuesHelps.Add(LogLevel.Error, "Error help.");

            parser.GetHelpText().Should().Be(@"Help text for option enum. Possible values: Trace, Debug, Info, Error.
Trace: Trace help.
Debug: Debug help.
Info: Info help.
Error: Error help.
");
        }
Beispiel #6
0
        public void Parse_RequiredOptionMissing_ShouldAddError()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.IsOptionRequired = true;

            var tokens      = new List <Token>();
            var parseResult = new ParseResult();

            parseResult.CommandOptions = new DataTypesCommandOptions();

            parser.Parse(tokens, parseResult);

            parseResult.HasErrors.Should().BeTrue();
            parseResult.Errors.Should().HaveCount(1);
            parseResult.Errors[0].Should().BeOfType <OptionMissingError>();

            var error = (OptionMissingError)parseResult.Errors[0];

            error.OptionName.Should().Be("enum");
            error.GetErrorMessage().Should().Be("The option --enum is required.");
        }
Beispiel #7
0
        public void Parse_OptionValueMissing_ShouldAddError()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            var tokens = new List <Token>
            {
                new OptionToken("enum")
            };

            var parseResult = new ParseResult();

            parseResult.CommandOptions = new DataTypesCommandOptions();

            parser.Parse(tokens, parseResult);

            parseResult.HasErrors.Should().BeTrue();
            parseResult.Errors.Should().HaveCount(1);
            parseResult.Errors[0].Should().BeOfType <OptionValueMissingError>();

            var error = (OptionValueMissingError)parseResult.Errors[0];

            error.OptionName.Should().Be("enum");
            error.GetErrorMessage().Should().Be("The option --enum requires a value, but no value was specified.");
        }
Beispiel #8
0
        public void Parse_InvalidValue_ShouldAddError()
        {
            var valueParser = A.Fake <ValueParser>();
            var parser      = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "logLevel");

            parser.ValueParser = valueParser;

            var tokens = new List <Token>
            {
                new OptionToken("logLevel")
                {
                    OptionValues = { "NonExistentLogLevel" }
                }
            };
            var parseResult = new ParseResult();

            parseResult.CommandOptions = new DataTypesCommandOptions();

            LogLevel logLevel;

            A.CallTo(() => valueParser.TryParseEnum("NonExistentLogLevel", out logLevel))
            .Returns(false);

            parser.Parse(tokens, parseResult);

            parseResult.HasErrors.Should().BeTrue();
            parseResult.Errors.Should().HaveCount(1);
            parseResult.Errors[0].Should().BeOfType <OptionValueInvalidFormatError>();

            var error = (OptionValueInvalidFormatError)parseResult.Errors[0];

            error.OptionName.Should().Be("logLevel");
            error.InvalidOptionValue.Should().Be("NonExistentLogLevel");
            error.ExpectedValueFormat.Should().Be("One of the valid values (see help)");
            error.GetErrorMessage().Should().Be("The value 'NonExistentLogLevel' of the option --logLevel has an invalid format. The expected format is: One of the valid values (see help).");
        }
Beispiel #9
0
 /// <summary>
 /// Initializes a new instance of this class.
 /// </summary>
 /// <param name="commandParser">The parser for the command the option belongs to.</param>
 /// <param name="optionParser">The parser for the option.</param>
 /// <exception cref="ArgumentNullException"><paramref name="commandParser" /> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="optionParser" /> is null.</exception>
 public EnumOptionSetup(CommandParser <TCommandOptions> commandParser, EnumOptionParser <TEnum> optionParser) : base(commandParser, optionParser)
 {
 }
Beispiel #10
0
        public void OptionName_ShouldReturnNameSpecifiedViaConstructor()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.OptionName.Should().Be("enum");
        }
Beispiel #11
0
        public void OptionDefaultValue_Initially_ShouldReturnDefaultOfEnum()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.OptionDefaultValue.Should().Be(default(LogLevel));
        }
Beispiel #12
0
        public void IsOptionRequired_Initially_ShouldReturnFalse()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.IsOptionRequired.Should().BeFalse();
        }
Beispiel #13
0
        public void TargetProperty_ShouldReturnPropertySpecifiedViaConstructor()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.TargetProperty.Should().BeSameAs(typeof(DataTypesCommandOptions).GetProperty("Enum"));
        }
Beispiel #14
0
        public void OptionType_ShouldReturnSingleValueOption()
        {
            var parser = new EnumOptionParser <LogLevel>(typeof(DataTypesCommandOptions).GetProperty("Enum"), "enum");

            parser.OptionType.Should().Be(OptionType.SingleValueOption);
        }