Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4fa8ddf

Browse files
authored
Merge pull request #358 from anthonylangsworth/type_conversion
Fix for invalid cast when specifying true or false for int option (#339)
2 parents ae88004 + 8b31892 commit 4fa8ddf

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

‎.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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
enum TestEnum
19+
{
20+
ValueA = 1,
21+
ValueB = 2
22+
}
23+
24+
[Theory]
25+
[MemberData("ChangeType_scalars_source")]
26+
public void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult)
27+
{
28+
Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, CultureInfo.InvariantCulture, true);
29+
30+
if (expectFail)
31+
{
32+
result.MatchNothing().Should().BeTrue("should fail parsing");
33+
}
34+
else
35+
{
36+
object matchedValue;
37+
38+
result.MatchJust(out matchedValue).Should().BeTrue("should parse successfully");
39+
Assert.Equal(matchedValue, expectedResult);
40+
}
41+
}
42+
43+
public static IEnumerable<object[]> ChangeType_scalars_source
44+
{
45+
get
46+
{
47+
return new[]
48+
{
49+
new object[] {"1", typeof (int), false, 1},
50+
new object[] {"0", typeof (int), false, 0},
51+
new object[] {"-1", typeof (int), false, -1},
52+
new object[] {"abcd", typeof (int), true, null},
53+
new object[] {"1.0", typeof (int), true, null},
54+
new object[] {int.MaxValue.ToString(), typeof (int), false, int.MaxValue},
55+
new object[] {int.MinValue.ToString(), typeof (int), false, int.MinValue},
56+
new object[] {((long) int.MaxValue + 1).ToString(), typeof (int), true, null},
57+
new object[] {((long) int.MinValue - 1).ToString(), typeof (int), true, null},
58+
59+
new object[] {"1", typeof (uint), false, (uint) 1},
60+
new object[] {"0", typeof (uint), false, (uint) 0},
61+
new object[] {"-1", typeof (uint), true, null},
62+
new object[] {uint.MaxValue.ToString(), typeof (uint), false, uint.MaxValue},
63+
new object[] {uint.MinValue.ToString(), typeof (uint), false, uint.MinValue},
64+
new object[] {((long) uint.MaxValue + 1).ToString(), typeof (uint), true, null},
65+
new object[] {((long) uint.MinValue - 1).ToString(), typeof (uint), true, null},
66+
67+
new object[] {"true", typeof (bool), false, true},
68+
new object[] {"True", typeof (bool), false, true},
69+
new object[] {"TRUE", typeof (bool), false, true},
70+
new object[] {"false", typeof (bool), false, false},
71+
new object[] {"False", typeof (bool), false, false},
72+
new object[] {"FALSE", typeof (bool), false, false},
73+
new object[] {"abcd", typeof (bool), true, null},
74+
new object[] {"0", typeof (bool), true, null},
75+
new object[] {"1", typeof (bool), true, null},
76+
77+
new object[] {"1.0", typeof (float), false, 1.0f},
78+
new object[] {"0.0", typeof (float), false, 0.0f},
79+
new object[] {"-1.0", typeof (float), false, -1.0f},
80+
new object[] {"abcd", typeof (float), true, null},
81+
82+
new object[] {"1.0", typeof (double), false, 1.0},
83+
new object[] {"0.0", typeof (double), false, 0.0},
84+
new object[] {"-1.0", typeof (double), false, -1.0},
85+
new object[] {"abcd", typeof (double), true, null},
86+
87+
new object[] {"1.0", typeof (decimal), false, 1.0m},
88+
new object[] {"0.0", typeof (decimal), false, 0.0m},
89+
new object[] {"-1.0", typeof (decimal), false, -1.0m},
90+
new object[] {"-1.123456", typeof (decimal), false, -1.123456m},
91+
new object[] {"abcd", typeof (decimal), true, null},
92+
93+
new object[] {"", typeof (string), false, ""},
94+
new object[] {"abcd", typeof (string), false, "abcd"},
95+
96+
new object[] {"ValueA", typeof (TestEnum), false, TestEnum.ValueA},
97+
new object[] {"VALUEA", typeof (TestEnum), false, TestEnum.ValueA},
98+
new object[] {"ValueB", typeof(TestEnum), false, TestEnum.ValueB},
99+
new object[] {((int) TestEnum.ValueA).ToString(), typeof (TestEnum), false, TestEnum.ValueA},
100+
new object[] {((int) TestEnum.ValueB).ToString(), typeof (TestEnum), false, TestEnum.ValueB},
101+
new object[] {((int) TestEnum.ValueB + 1).ToString(), typeof (TestEnum), true, null},
102+
new object[] {((int) TestEnum.ValueA - 1).ToString(), typeof (TestEnum), true, null},
103+
104+
// Failed before #339
105+
new object[] {"false", typeof (int), true, 0},
106+
new object[] {"true", typeof (int), true, 0}
107+
};
108+
}
109+
}
110+
}
111+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /