Skip to content

Commit 1eddb5d

Browse files
committed
Simplified tests for Populate() issue with no operand attributes and resolved the error.
1 parent e3ab89c commit 1eddb5d

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

Utility.CommandLine.Arguments.Tests/Arguments.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -554,21 +554,9 @@ public class TestClassWithNoProperties
554554
/// containing a single argument/value pair, and with a Type containing no properties.
555555
/// </summary>
556556
[Fact]
557-
public void PopulateArguments()
558-
{
559-
Exception ex = Record.Exception(() => CommandLine.Arguments.Populate(GetType(), "--hello world"));
560-
561-
Assert.Null(ex);
562-
}
563-
564-
/// <summary>
565-
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
566-
/// containing two operands, and with a Type containing no properties.
567-
/// </summary>
568-
[Fact]
569-
public void PopulateOperands()
557+
public void Populate()
570558
{
571-
Exception ex = Record.Exception(() => CommandLine.Arguments.Populate(GetType(), "one two"));
559+
Exception ex = Record.Exception(() => CommandLine.Arguments.Populate(GetType(), "--hello world one two"));
572560

573561
Assert.Null(ex);
574562
}

Utility.CommandLine.Arguments/Arguments.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,18 @@ public static void Populate(Type type, Arguments arguments)
312312

313313
PropertyInfo operandsProperty = GetOperandsProperty(type);
314314

315-
if (operandsProperty.PropertyType.IsAssignableFrom(typeof(List<string>)))
315+
// check to ensure the target class has a property marked with the Operands attribute; if not GetOperandsProperty()
316+
// will return null.
317+
if (operandsProperty != default(PropertyInfo))
316318
{
317-
operandsProperty.SetValue(null, arguments.OperandList);
318-
}
319-
else
320-
{
321-
operandsProperty.SetValue(null, arguments.OperandList.ToArray());
319+
if (operandsProperty.PropertyType.IsAssignableFrom(typeof(List<string>)))
320+
{
321+
operandsProperty.SetValue(null, arguments.OperandList);
322+
}
323+
else
324+
{
325+
operandsProperty.SetValue(null, arguments.OperandList.ToArray());
326+
}
322327
}
323328
}
324329

@@ -458,7 +463,7 @@ private static PropertyInfo GetOperandsProperty(Type type)
458463
.Any(a => a.AttributeType.Name == typeof(OperandsAttribute).Name))
459464
.FirstOrDefault();
460465

461-
if (property.PropertyType != typeof(string[]) && property.PropertyType != typeof(List<string>))
466+
if (property != default(PropertyInfo) && property.PropertyType != typeof(string[]) && property.PropertyType != typeof(List<string>))
462467
{
463468
throw new InvalidCastException("The target for the Operands attribute must be of string[] or List<string>.");
464469
}

0 commit comments

Comments
 (0)