public void Parse_CustomFormatSettings_ShouldPassCustomFormatToValueParser()
        {
            var valueParser = A.Fake <ValueParser>(ob => ob.CallsBaseMethods());
            var parser      = new GuidListOptionParser(typeof(DataTypesCommandOptions).GetProperty("Guids"), "guids");

            parser.ValueParser = valueParser;
            parser.GuidFormat  = "X";

            var tokens = new List <Token>
            {
                new OptionToken("guids")
                {
                    OptionValues = { "{0x13d02a84,0x84f7,0x4a2d,{0x8f,0x09,0x2f,0x96,0xde,0xfb,0x8c,0x79}}" }
                }
            };
            var parseResult             = new ParseResult();
            var dataTypesCommandOptions = new DataTypesCommandOptions();

            parseResult.CommandOptions = dataTypesCommandOptions;

            parser.Parse(tokens, parseResult);

            Guid guid;

            A.CallTo(() => valueParser.TryParseGuid("{0x13d02a84,0x84f7,0x4a2d,{0x8f,0x09,0x2f,0x96,0xde,0xfb,0x8c,0x79}}", "X", out guid)).MustHaveHappened();
        }
        public void Parse_OptionNotPresent_ShouldAssignDefaultValueToTargetProperty()
        {
            var parser = new GuidListOptionParser(typeof(DataTypesCommandOptions).GetProperty("Guids"), "guids");

            parser.OptionDefaultValue = new List <Guid> {
                new Guid("13d02a84-84f7-4a2d-8f09-2f96defb8c79"), new Guid("9e6a5202-102f-4eb9-a217-0a58f4db40b6")
            };

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

            parseResult.CommandOptions = dataTypesCommandOptions;

            parser.Parse(tokens, parseResult);

            dataTypesCommandOptions.Guids.Should().BeEquivalentTo(new Guid("13d02a84-84f7-4a2d-8f09-2f96defb8c79"), new Guid("9e6a5202-102f-4eb9-a217-0a58f4db40b6"));
        }
        public void Parse_RequiredOptionMissing_ShouldAddError()
        {
            var parser = new GuidListOptionParser(typeof(DataTypesCommandOptions).GetProperty("Guids"), "guids");

            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("guids");
            error.GetErrorMessage().Should().Be("The option --guids is required.");
        }
        public void Parse_ValidValue_ShouldParseAndAssignValueToTargetProperty()
        {
            var valueParser = A.Fake <ValueParser>();
            var parser      = new GuidListOptionParser(typeof(DataTypesCommandOptions).GetProperty("Guids"), "guids");

            parser.ValueParser = valueParser;

            var tokens = new List <Token>
            {
                new OptionToken("guids")
                {
                    OptionValues = { "13d02a84-84f7-4a2d-8f09-2f96defb8c79", "9e6a5202-102f-4eb9-a217-0a58f4db40b6" }
                }
            };
            var parseResult             = new ParseResult();
            var dataTypesCommandOptions = new DataTypesCommandOptions();

            parseResult.CommandOptions = dataTypesCommandOptions;

            Guid guid;

            A.CallTo(() => valueParser.TryParseGuid("13d02a84-84f7-4a2d-8f09-2f96defb8c79", null, out guid))
            .Returns(true)
            .AssignsOutAndRefParameters(new Guid("13d02a84-84f7-4a2d-8f09-2f96defb8c79"));

            A.CallTo(() => valueParser.TryParseGuid("9e6a5202-102f-4eb9-a217-0a58f4db40b6", null, out guid))
            .Returns(true)
            .AssignsOutAndRefParameters(new Guid("9e6a5202-102f-4eb9-a217-0a58f4db40b6"));

            parser.Parse(tokens, parseResult);

            dataTypesCommandOptions.Guids.Should().BeEquivalentTo(new Guid("13d02a84-84f7-4a2d-8f09-2f96defb8c79"), new Guid("9e6a5202-102f-4eb9-a217-0a58f4db40b6"));

            A.CallTo(() => valueParser.TryParseGuid("13d02a84-84f7-4a2d-8f09-2f96defb8c79", null, out guid)).MustHaveHappened();
            A.CallTo(() => valueParser.TryParseGuid("9e6a5202-102f-4eb9-a217-0a58f4db40b6", null, out guid)).MustHaveHappened();
        }
        public void Parse_OptionValueMissing_ShouldAddError()
        {
            var parser = new GuidListOptionParser(typeof(DataTypesCommandOptions).GetProperty("Guids"), "guids");

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

            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("guids");
            error.GetErrorMessage().Should().Be("The option --guids requires a value, but no value was specified.");
        }
        public void Parse_InvalidValue_ShouldAddError()
        {
            var valueParser = A.Fake <ValueParser>();
            var parser      = new GuidListOptionParser(typeof(DataTypesCommandOptions).GetProperty("Guids"), "guids");

            parser.ValueParser = valueParser;

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

            parseResult.CommandOptions = new DataTypesCommandOptions();

            Guid guid;

            A.CallTo(() => valueParser.TryParseGuid("NotAGuid", null, out guid))
            .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("guids");
            error.InvalidOptionValue.Should().Be("NotAGuid");
            error.ExpectedValueFormat.Should().Be("A valid Guid");
            error.GetErrorMessage().Should().Be("The value 'NotAGuid' of the option --guids has an invalid format. The expected format is: A valid Guid.");
        }