Skip to content

Commit bd67c24

Browse files
committed
Merge branch 'master' of https://github.com/Wind010/commandline into develop
2 parents 795dc7b + 5a4b077 commit bd67c24

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/CommandLine/Core/ReflectionExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace CommandLine.Core
1414
{
1515
static class ReflectionExtensions
1616
{
17+
public const string CannotSetValueToTargetInstance = "Cannot set value to target instance.";
18+
1719
public static IEnumerable<T> GetSpecifications<T>(this Type type, Func<PropertyInfo, T> selector)
1820
{
1921
return from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties())
@@ -91,6 +93,10 @@ public static IEnumerable<Error> SetProperties<T>(
9193

9294
private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specProp, T instance, object value)
9395
{
96+
Action<Exception> fail = inner => {
97+
throw new InvalidOperationException(CannotSetValueToTargetInstance, inner);
98+
};
99+
94100
try
95101
{
96102
specProp.Property.SetValue(instance, value, null);
@@ -104,6 +110,13 @@ private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specPro
104110
{
105111
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) };
106112
}
113+
catch(ArgumentException e)
114+
{
115+
var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e);
116+
fail(argEx);
117+
}
118+
119+
return instance;
107120
}
108121

109122
public static object CreateEmptyArray(this Type type)

src/CommandLine/Error.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public enum ErrorType
6565
/// Value of <see cref="CommandLine.SetValueExceptionError"/> type.
6666
/// </summary>
6767
SetValueExceptionError
68+
VersionRequestedError,
69+
/// <summary>
70+
/// Value of <see cref="CommandLine.InvalidAttributeConfigurationError"/> type.
71+
/// </summary>
72+
InvalidAttributeConfigurationError
6873
}
6974

7075
/// <summary>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
namespace CommandLine.Tests.Fakes
3+
{
4+
class Options_With_InvalidDefaults
5+
{
6+
// Default of string and integer type property will also throw.
7+
8+
[Option(Default = false)]
9+
public string FileName { get; set; }
10+
}
11+
}

tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,22 @@ public void Parse_TimeSpan()
11331133
// Teardown
11341134
}
11351135

1136-
public static IEnumerable<object[]> RequiredValueStringData
1136+
[Fact]
1137+
public void Build_DefaultBoolTypeString_ThrowsInvalidOperationException()
1138+
{
1139+
// Exercize system
1140+
Action test = () => InvokeBuild<Options_With_InvalidDefaults>(
1141+
new string[] { });
1142+
1143+
// Verify outcome
1144+
test.ShouldThrow<InvalidOperationException>()
1145+
.WithMessage(ReflectionExtensions.CannotSetValueToTargetInstance)
1146+
.WithInnerException<ArgumentException>()
1147+
.WithInnerMessage(InvalidAttributeConfigurationError.ErrorMessage);
1148+
}
1149+
1150+
1151+
public static IEnumerable<object> RequiredValueStringData
11371152
{
11381153
get
11391154
{

0 commit comments

Comments
 (0)