Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Wind010/commandline into …
Browse files Browse the repository at this point in the history
…develop
  • Loading branch information
ericnewton76 committed Jan 27, 2019
2 parents 795dc7b + 5a4b077 commit bd67c24
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/CommandLine/Core/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace CommandLine.Core
{
static class ReflectionExtensions
{
public const string CannotSetValueToTargetInstance = "Cannot set value to target instance.";

public static IEnumerable<T> GetSpecifications<T>(this Type type, Func<PropertyInfo, T> selector)
{
return from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties())
Expand Down Expand Up @@ -91,6 +93,10 @@ public static IEnumerable<Error> SetProperties<T>(

private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specProp, T instance, object value)
{
Action<Exception> fail = inner => {
throw new InvalidOperationException(CannotSetValueToTargetInstance, inner);
};

try
{
specProp.Property.SetValue(instance, value, null);
Expand All @@ -104,6 +110,13 @@ private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specPro
{
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) };
}
catch(ArgumentException e)
{
var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e);
fail(argEx);
}

return instance;
}

public static object CreateEmptyArray(this Type type)
Expand Down
5 changes: 5 additions & 0 deletions src/CommandLine/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public enum ErrorType
/// Value of <see cref="CommandLine.SetValueExceptionError"/> type.
/// </summary>
SetValueExceptionError
VersionRequestedError,
/// <summary>
/// Value of <see cref="CommandLine.InvalidAttributeConfigurationError"/> type.
/// </summary>
InvalidAttributeConfigurationError
}

/// <summary>
Expand Down
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: 16 additions & 1 deletion tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,22 @@ public void Parse_TimeSpan()
// Teardown
}

public static IEnumerable<object[]> RequiredValueStringData
[Fact]
public void Build_DefaultBoolTypeString_ThrowsInvalidOperationException()
{
// Exercize system
Action test = () => InvokeBuild<Options_With_InvalidDefaults>(
new string[] { });

// Verify outcome
test.ShouldThrow<InvalidOperationException>()
.WithMessage(ReflectionExtensions.CannotSetValueToTargetInstance)
.WithInnerException<ArgumentException>()
.WithInnerMessage(InvalidAttributeConfigurationError.ErrorMessage);
}


public static IEnumerable<object> RequiredValueStringData
{
get
{
Expand Down

0 comments on commit bd67c24

Please sign in to comment.