Skip to content

Commit 2b63108

Browse files
Fix invalid cast coverting bool to expected int
Add unit tests for TypeConverter for scalars (only). Add DotSettings.user to .gitignore.
1 parent a622376 commit 2b63108

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ artifacts/*
4141
*.nuget.targets
4242
*.lock.json
4343
*.nuget.props
44+
*.DotSettings.user
4445

src/CommandLine/Core/TypeConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static Result<object, Exception> ChangeTypeScalarImpl(string value, Type
8484
return (value == null) ? empty() : withValue();
8585
};
8686

87-
return value.IsBooleanString()
87+
return value.IsBooleanString() && conversionType == typeof(bool)
8888
? value.ToBoolean() : conversionType.GetTypeInfo().IsEnum
8989
? value.ToEnum(conversionType, ignoreValueCase) : safeChangeType();
9090
};

tests/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
<SubType>Code</SubType>
113113
</Compile>
114114
<Compile Include="Unit\Core\TokenTests.cs" />
115+
<Compile Include="Unit\Core\TypeConverterTests.cs" />
115116
<Compile Include="Unit\Infrastructure\FSharpOptionHelperTests.cs" />
116117
<Compile Include="Unit\Core\ReflectionExtensions.cs" />
117118
<Compile Include="Unit\ParserResultExtensionsTests.cs" />
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.CodeDom;
3+
using System.Collections.Generic;
4+
using System.Dynamic;
5+
using System.Globalization;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using CommandLine.Core;
10+
using CSharpx;
11+
using FluentAssertions;
12+
using Xunit;
13+
14+
namespace CommandLine.Tests.Unit.Core
15+
{
16+
public class TypeConverterTests
17+
{
18+
[Theory]
19+
[MemberData("ChangeType_scalars_source")]
20+
public void ChangeType_scalars(string testValue, Type destinationType, CultureInfo culture, bool expectFail, object expectedResult)
21+
{
22+
Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, culture, true);
23+
24+
if (expectFail)
25+
{
26+
result.MatchNothing().Should().BeTrue();
27+
}
28+
else
29+
{
30+
object matchedValue;
31+
32+
result.MatchJust(out matchedValue).Should().BeTrue();
33+
Assert.Equal(matchedValue, expectedResult);
34+
}
35+
}
36+
37+
public static IEnumerable<object[]> ChangeType_scalars_source
38+
{
39+
get
40+
{
41+
yield return new object[] { "1", typeof (int), CultureInfo.InvariantCulture, false, 1};
42+
yield return new object[] { "0", typeof (int), CultureInfo.InvariantCulture, false, 0 };
43+
yield return new object[] { "-1", typeof (int), CultureInfo.InvariantCulture, false, -1 };
44+
yield return new object[] { "1.0", typeof (int), CultureInfo.InvariantCulture, true, null };
45+
yield return new object[] { "1.0", typeof(float), CultureInfo.InvariantCulture, false, 1.0f };
46+
yield return new object[] { "0.0", typeof (float), CultureInfo.InvariantCulture, false, 0.0f};
47+
yield return new object[] { "-1.0", typeof (float), CultureInfo.InvariantCulture, false, -1.0f};
48+
yield return new object[] { "1.0", typeof(double), CultureInfo.InvariantCulture, false, 1.0 };
49+
yield return new object[] { "0.0", typeof (double), CultureInfo.InvariantCulture, false, 0.0};
50+
yield return new object[] { "-1.0", typeof(double), CultureInfo.InvariantCulture, false, -1.0 };
51+
yield return new object[] { "1.0", typeof(decimal), CultureInfo.InvariantCulture, false, 1.0m };
52+
yield return new object[] { "0.0", typeof(decimal), CultureInfo.InvariantCulture, false, 0.0m };
53+
yield return new object[] { "-1.0", typeof(decimal), CultureInfo.InvariantCulture, false, -1.0m };
54+
yield return new object[] { "-1.123456", typeof(decimal), CultureInfo.InvariantCulture, false, -1.123456m };
55+
yield return new object[] { "true", typeof(bool), CultureInfo.InvariantCulture, false, true };
56+
yield return new object[] { "false", typeof (bool), CultureInfo.InvariantCulture, false, false };
57+
yield return new object[] { "", typeof(string), CultureInfo.InvariantCulture, false, "" };
58+
yield return new object[] { "abcd", typeof(int), CultureInfo.InvariantCulture, true, "abcd" };
59+
yield return new object[] { "abcd", typeof(string), CultureInfo.InvariantCulture, false, "abcd" };
60+
61+
// Failed before change
62+
yield return new object[] { "false", typeof(int), CultureInfo.InvariantCulture, true, 0 };
63+
yield return new object[] { "true", typeof(int), CultureInfo.InvariantCulture, true, 0 };
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)