Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ private CliValueConverters()
return boolValue;
}

var valueConverted = InternalConvert(value, nullableType, optionAttribute);
var valueConverted = nullableType.IsEnum
? _enumConverter.Convert(value, nullableType, optionAttribute)
: InternalConvert(value, nullableType, optionAttribute);

if (valueConverted == ConverterAction.DoNothing)
{
return ConverterAction.DoNothing;
}

return valueConverted == null
? Activator.CreateInstance(targetType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,58 @@ public void Parse_EnumValue_PropertyIsSetCorrect(string argValue, TestEnum enumV
.Be(enumValue);
}

[Theory]
[InlineData("Ok", TestEnum.Ok)]
[InlineData("OK", TestEnum.OK)]
[InlineData("ok", null)]
[InlineData("", null)]
[InlineData("Default", TestEnum.Default)]
[InlineData("default", TestEnum.Default)]
[InlineData("Custom", TestEnum.Custom)]
[InlineData("CUSTOM", TestEnum.Custom)]
[InlineData("Failed", TestEnum.Failed)]
[InlineData("failED", TestEnum.Failed)]
[InlineData("None", TestEnum.None)]
[InlineData("nONe", TestEnum.None)]
public void Parse_NullableEnumValue_PropertyIsSetCorrect(string argValue, TestEnum? enumValue)
{
var args = new[] { "-ne", argValue };

var parser = new OptionParser(typeof(TestOptionWithEnum));

// Act
var option = parser.Parse(args) as TestOptionWithEnum;

// Assert
option
.Should()
.NotBeNull();

option.NullableEnumValue
.Should()
.Be(enumValue);
}

[Fact]
public void Parse_NotGivenNullableEnumValue_PropertyIsNull()
{
var args = Array.Empty<string>();

var parser = new OptionParser(typeof(TestOptionWithEnum));

// Act
var option = parser.Parse(args) as TestOptionWithEnum;

// Assert
option
.Should()
.NotBeNull();

option.NullableEnumValue
.Should()
.Be(null);
}

[Theory]
[InlineData("Hello", "World")]
[InlineData("World", "Hello")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ namespace CreativeCoders.SysConsole.Cli.Parsing.UnitTests.TestData;
public class TestOptionWithEnum
{
[OptionParameter('e', "enum")] public TestEnum EnumValue { get; set; }

[OptionParameter("ne", "nullableenum")]
public TestEnum? NullableEnumValue { get; set; }
}