Skip to content

Commit

Permalink
Merge PR 314 (Wind010)
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-hassan committed Apr 27, 2019
1 parent 92869e2 commit 4d69a38
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/CommandLine/Core/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,18 @@ private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specPro
{
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e.InnerException, value) };
}
catch (ArgumentException e)
{
var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e);

return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), argEx, value) };
}

catch (Exception e)
{
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) };
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) };
}

}

public static object CreateEmptyArray(this Type type)
Expand Down Expand Up @@ -202,4 +210,4 @@ public static bool IsPrimitiveEx(this Type type)
|| Convert.GetTypeCode(type) != TypeCode.Object;
}
}
}
}
22 changes: 20 additions & 2 deletions src/CommandLine/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public enum ErrorType
/// <summary>
/// Value of <see cref="CommandLine.SetValueExceptionError"/> type.
/// </summary>
SetValueExceptionError

SetValueExceptionError,
/// <summary>
/// Value of <see cref="CommandLine.InvalidAttributeConfigurationError"/> type.
/// </summary>
InvalidAttributeConfigurationError

}

/// <summary>
Expand Down Expand Up @@ -506,6 +512,18 @@ public object Value
{
get { return value; }
}
}

/// <summary>
/// Models an error generated when an invalid token is detected.
/// </summary>
public sealed class InvalidAttributeConfigurationError : Error
{
public const string ErrorMessage = "Check if Option or Value attribute values are set properly for the given type.";

internal InvalidAttributeConfigurationError()
: base(ErrorType.InvalidAttributeConfigurationError, true)
{
}
}
}
}
11 changes: 11 additions & 0 deletions tests/CommandLine.Tests/Fakes/Options_With_InvalidDefaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace CommandLine.Tests.Fakes
{
class Options_With_InvalidDefaults
{
// Default of string and integer type property will also throw.

[Option(Default = false)]
public string FileName { get; set; }
}
}
17 changes: 17 additions & 0 deletions tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,22 @@ public void Parse_option_with_exception_thrown_from_setter_generates_SetValueExc
// Teardown
}

[Fact]
public void Parse_default_bool_type_string_SetValueExceptionError()
{
// Fixture setup
string name = nameof(Options_With_InvalidDefaults.FileName).ToLower();
var expectedResult = new[] { new SetValueExceptionError(new NameInfo("", name),
new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage), "bad") };

// Exercize system
var result = InvokeBuild<Options_With_InvalidDefaults>(
new[] { name, "bad" });

// Verify outcome
((NotParsed<Options_With_InvalidDefaults>)result).Errors.Should().BeEquivalentTo(expectedResult);
}


[Theory]
[InlineData(new[] { "--stringvalue", "x-" }, "x-")]
Expand Down Expand Up @@ -1133,6 +1149,7 @@ public void Parse_TimeSpan()
// Teardown
}


[Fact]
public void OptionClass_IsImmutable_HasNoCtor()
{
Expand Down

0 comments on commit 4d69a38

Please sign in to comment.