Skip to content

Commit ee2b897

Browse files
committed
add additional tests, remove deprecated check for list value
1 parent 2480c4b commit ee2b897

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

src/Utility.CommandLine.Arguments/Arguments.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,16 +430,7 @@ public static void Populate(Type type, Arguments arguments, bool clearExistingVa
430430
}
431431
else if (propertyType.IsArray || (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>)))
432432
{
433-
// if the property is an array or list, convert the value to an array or list of the matching type. start
434-
// by converting atomic values to a list containing a single value, just to simplify processing.
435-
if (valueIsList)
436-
{
437-
convertedValue = value;
438-
}
439-
else
440-
{
441-
convertedValue = new List<object>(new object[] { value });
442-
}
433+
convertedValue = value;
443434

444435
// next, create a list with the same type as the target property
445436
Type valueType;

tests/Utility.CommandLine.Arguments.Tests/ArgumentsTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,76 @@ public void Value_Is_Replaced_Given_Multiple_Long()
888888
Assert.Equal("2", dict["bb"]);
889889
}
890890

891+
[Fact]
892+
public void Value_Is_Replaced_Given_Multiple_Long_No_Type()
893+
{
894+
var list = new List<KeyValuePair<string, string>>();
895+
list.Add(new KeyValuePair<string, string>("bb", "1"));
896+
list.Add(new KeyValuePair<string, string>("bb", "2"));
897+
898+
var a = Arguments.Parse("--bb 1 --bb 2");
899+
var dict = a.ArgumentDictionary;
900+
901+
Assert.Single(dict);
902+
Assert.Equal("2", dict["bb"]);
903+
904+
Assert.Equal(2, a.ArgumentList.Count);
905+
Assert.True(a.ArgumentList.Where(r => r.Key == "bb" && r.Value == "1").Any());
906+
Assert.True(a.ArgumentList.Where(r => r.Key == "bb" && r.Value == "2").Any());
907+
}
908+
909+
[Fact]
910+
public void ArgumentList_Retains_Replaced_Arguments()
911+
{
912+
var list = new List<KeyValuePair<string, string>>();
913+
list.Add(new KeyValuePair<string, string>("bb", "1"));
914+
list.Add(new KeyValuePair<string, string>("bb", "2"));
915+
916+
var a = Arguments.Parse("--bb 1 --bb 2");
917+
918+
Assert.Equal(2, a.ArgumentList.Count);
919+
Assert.True(a.ArgumentList.Where(r => r.Key == "bb" && r.Value == "1").Any());
920+
Assert.True(a.ArgumentList.Where(r => r.Key == "bb" && r.Value == "2").Any());
921+
}
922+
923+
[Fact]
924+
public void Arguments_Returns_ArgumentList_Elements_Via_Indexer()
925+
{
926+
var list = new List<KeyValuePair<string, string>>();
927+
list.Add(new KeyValuePair<string, string>("bb", "1"));
928+
list.Add(new KeyValuePair<string, string>("bb", "2"));
929+
930+
var a = Arguments.Parse("--bb 1 --bb 2");
931+
932+
Assert.Equal("1", a[0]);
933+
Assert.Equal("2", a[1]);
934+
}
935+
936+
[Fact]
937+
public void Arguments_Sets_TargetType_Given_Type()
938+
{
939+
var list = new List<KeyValuePair<string, string>>();
940+
list.Add(new KeyValuePair<string, string>("bb", "1"));
941+
list.Add(new KeyValuePair<string, string>("bb", "2"));
942+
943+
var a = Arguments.Parse("--bb 1 --bb 2", GetType());
944+
945+
Assert.NotNull(a.TargetType);
946+
Assert.Equal(GetType(), a.TargetType);
947+
}
948+
949+
[Fact]
950+
public void Arguments_Does_Not_Set_TargetType_Given_No_Type()
951+
{
952+
var list = new List<KeyValuePair<string, string>>();
953+
list.Add(new KeyValuePair<string, string>("bb", "1"));
954+
list.Add(new KeyValuePair<string, string>("bb", "2"));
955+
956+
var a = Arguments.Parse("--bb 1 --bb 2");
957+
958+
Assert.Null(a.TargetType);
959+
}
960+
891961
[Fact]
892962
public void Value_Is_Replaced_Given_Mixed_Args_Long_First()
893963
{

0 commit comments

Comments
 (0)