-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for FlagCounter and implement it
Now things like -vvv for triple-verbose can be done in user code.
- Loading branch information
Showing
8 changed files
with
184 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/CommandLine.Tests/Fakes/Options_With_FlagCounter_Switches.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. | ||
|
||
namespace CommandLine.Tests.Fakes | ||
{ | ||
public class Options_With_FlagCounter_Switches | ||
{ | ||
[Option('v', FlagCounter=true)] | ||
public int Verbose { get; set; } | ||
|
||
[Option('s', FlagCounter=true)] | ||
public int Silent { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 116 additions & 114 deletions
230
tests/CommandLine.Tests/Unit/Core/TypeConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Xunit; | ||
using FluentAssertions; | ||
using CSharpx; | ||
using CommandLine.Core; | ||
|
||
namespace CommandLine.Tests.Unit.Core | ||
{ | ||
public class TypeConverterTests | ||
{ | ||
enum TestEnum | ||
{ | ||
ValueA = 1, | ||
ValueB = 2 | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ChangeType_scalars_source))] | ||
public void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult) | ||
{ | ||
Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, CultureInfo.InvariantCulture, true); | ||
|
||
if (expectFail) | ||
{ | ||
result.MatchNothing().Should().BeTrue("should fail parsing"); | ||
} | ||
else | ||
{ | ||
result.MatchJust(out object matchedValue).Should().BeTrue("should parse successfully"); | ||
Assert.Equal(matchedValue, expectedResult); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ChangeType_Scalar_LastOneWins() | ||
{ | ||
var values = new[] { "100", "200", "300", "400", "500" }; | ||
var result = TypeConverter.ChangeType(values, typeof(int), true, CultureInfo.InvariantCulture, true); | ||
result.MatchJust(out var matchedValue).Should().BeTrue("should parse successfully"); | ||
Assert.Equal(500, matchedValue); | ||
|
||
} | ||
|
||
public static IEnumerable<object[]> ChangeType_scalars_source | ||
{ | ||
get | ||
{ | ||
return new[] | ||
{ | ||
new object[] {"1", typeof (int), false, 1}, | ||
new object[] {"0", typeof (int), false, 0}, | ||
new object[] {"-1", typeof (int), false, -1}, | ||
new object[] {"abcd", typeof (int), true, null}, | ||
new object[] {"1.0", typeof (int), true, null}, | ||
new object[] {int.MaxValue.ToString(), typeof (int), false, int.MaxValue}, | ||
new object[] {int.MinValue.ToString(), typeof (int), false, int.MinValue}, | ||
new object[] {((long) int.MaxValue + 1).ToString(), typeof (int), true, null}, | ||
new object[] {((long) int.MinValue - 1).ToString(), typeof (int), true, null}, | ||
|
||
new object[] {"1", typeof (uint), false, (uint) 1}, | ||
// new object[] {"0", typeof (uint), false, (uint) 0}, //cause warning: Skipping test case with duplicate ID | ||
// new object[] {"-1", typeof (uint), true, null}, //cause warning: Skipping test case with duplicate ID | ||
new object[] {uint.MaxValue.ToString(), typeof (uint), false, uint.MaxValue}, | ||
new object[] {uint.MinValue.ToString(), typeof (uint), false, uint.MinValue}, | ||
new object[] {((long) uint.MaxValue + 1).ToString(), typeof (uint), true, null}, | ||
new object[] {((long) uint.MinValue - 1).ToString(), typeof (uint), true, null}, | ||
|
||
new object[] {"true", typeof (bool), false, true}, | ||
new object[] {"True", typeof (bool), false, true}, | ||
new object[] {"TRUE", typeof (bool), false, true}, | ||
new object[] {"false", typeof (bool), false, false}, | ||
new object[] {"False", typeof (bool), false, false}, | ||
new object[] {"FALSE", typeof (bool), false, false}, | ||
new object[] {"abcd", typeof (bool), true, null}, | ||
new object[] {"0", typeof (bool), true, null}, | ||
new object[] {"1", typeof (bool), true, null}, | ||
|
||
new object[] {"1.0", typeof (float), false, 1.0f}, | ||
new object[] {"0.0", typeof (float), false, 0.0f}, | ||
new object[] {"-1.0", typeof (float), false, -1.0f}, | ||
new object[] {"abcd", typeof (float), true, null}, | ||
|
||
new object[] {"1.0", typeof (double), false, 1.0}, | ||
new object[] {"0.0", typeof (double), false, 0.0}, | ||
new object[] {"-1.0", typeof (double), false, -1.0}, | ||
new object[] {"abcd", typeof (double), true, null}, | ||
|
||
new object[] {"1.0", typeof (decimal), false, 1.0m}, | ||
new object[] {"0.0", typeof (decimal), false, 0.0m}, | ||
new object[] {"-1.0", typeof (decimal), false, -1.0m}, | ||
new object[] {"-1.123456", typeof (decimal), false, -1.123456m}, | ||
new object[] {"abcd", typeof (decimal), true, null}, | ||
|
||
new object[] {"", typeof (string), false, ""}, | ||
new object[] {"abcd", typeof (string), false, "abcd"}, | ||
|
||
new object[] {"ValueA", typeof (TestEnum), false, TestEnum.ValueA}, | ||
new object[] {"VALUEA", typeof (TestEnum), false, TestEnum.ValueA}, | ||
new object[] {"ValueB", typeof(TestEnum), false, TestEnum.ValueB}, | ||
new object[] {((int) TestEnum.ValueA).ToString(), typeof (TestEnum), false, TestEnum.ValueA}, | ||
new object[] {((int) TestEnum.ValueB).ToString(), typeof (TestEnum), false, TestEnum.ValueB}, | ||
new object[] {((int) TestEnum.ValueB + 1).ToString(), typeof (TestEnum), true, null}, | ||
new object[] {((int) TestEnum.ValueA - 1).ToString(), typeof (TestEnum), true, null}, | ||
|
||
// Failed before #339 | ||
new object[] {"false", typeof (int), true, 0}, | ||
new object[] {"true", typeof (int), true, 0} | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Xunit; | ||
using FluentAssertions; | ||
using CSharpx; | ||
using CommandLine.Core; | ||
|
||
namespace CommandLine.Tests.Unit.Core | ||
{ | ||
public class TypeConverterTests | ||
{ | ||
enum TestEnum | ||
{ | ||
ValueA = 1, | ||
ValueB = 2 | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ChangeType_scalars_source))] | ||
public void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult) | ||
{ | ||
Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, false, CultureInfo.InvariantCulture, true); | ||
|
||
if (expectFail) | ||
{ | ||
result.MatchNothing().Should().BeTrue("should fail parsing"); | ||
} | ||
else | ||
{ | ||
result.MatchJust(out object matchedValue).Should().BeTrue("should parse successfully"); | ||
Assert.Equal(matchedValue, expectedResult); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ChangeType_Scalar_LastOneWins() | ||
{ | ||
var values = new[] { "100", "200", "300", "400", "500" }; | ||
var result = TypeConverter.ChangeType(values, typeof(int), true, false, CultureInfo.InvariantCulture, true); | ||
result.MatchJust(out var matchedValue).Should().BeTrue("should parse successfully"); | ||
Assert.Equal(500, matchedValue); | ||
|
||
} | ||
|
||
// TODO: Write test for TypeConverter.ChangeType when isFlag = true | ||
|
||
public static IEnumerable<object[]> ChangeType_scalars_source | ||
{ | ||
get | ||
{ | ||
return new[] | ||
{ | ||
new object[] {"1", typeof (int), false, 1}, | ||
new object[] {"0", typeof (int), false, 0}, | ||
new object[] {"-1", typeof (int), false, -1}, | ||
new object[] {"abcd", typeof (int), true, null}, | ||
new object[] {"1.0", typeof (int), true, null}, | ||
new object[] {int.MaxValue.ToString(), typeof (int), false, int.MaxValue}, | ||
new object[] {int.MinValue.ToString(), typeof (int), false, int.MinValue}, | ||
new object[] {((long) int.MaxValue + 1).ToString(), typeof (int), true, null}, | ||
new object[] {((long) int.MinValue - 1).ToString(), typeof (int), true, null}, | ||
|
||
new object[] {"1", typeof (uint), false, (uint) 1}, | ||
// new object[] {"0", typeof (uint), false, (uint) 0}, //cause warning: Skipping test case with duplicate ID | ||
// new object[] {"-1", typeof (uint), true, null}, //cause warning: Skipping test case with duplicate ID | ||
new object[] {uint.MaxValue.ToString(), typeof (uint), false, uint.MaxValue}, | ||
new object[] {uint.MinValue.ToString(), typeof (uint), false, uint.MinValue}, | ||
new object[] {((long) uint.MaxValue + 1).ToString(), typeof (uint), true, null}, | ||
new object[] {((long) uint.MinValue - 1).ToString(), typeof (uint), true, null}, | ||
|
||
new object[] {"true", typeof (bool), false, true}, | ||
new object[] {"True", typeof (bool), false, true}, | ||
new object[] {"TRUE", typeof (bool), false, true}, | ||
new object[] {"false", typeof (bool), false, false}, | ||
new object[] {"False", typeof (bool), false, false}, | ||
new object[] {"FALSE", typeof (bool), false, false}, | ||
new object[] {"abcd", typeof (bool), true, null}, | ||
new object[] {"0", typeof (bool), true, null}, | ||
new object[] {"1", typeof (bool), true, null}, | ||
|
||
new object[] {"1.0", typeof (float), false, 1.0f}, | ||
new object[] {"0.0", typeof (float), false, 0.0f}, | ||
new object[] {"-1.0", typeof (float), false, -1.0f}, | ||
new object[] {"abcd", typeof (float), true, null}, | ||
|
||
new object[] {"1.0", typeof (double), false, 1.0}, | ||
new object[] {"0.0", typeof (double), false, 0.0}, | ||
new object[] {"-1.0", typeof (double), false, -1.0}, | ||
new object[] {"abcd", typeof (double), true, null}, | ||
|
||
new object[] {"1.0", typeof (decimal), false, 1.0m}, | ||
new object[] {"0.0", typeof (decimal), false, 0.0m}, | ||
new object[] {"-1.0", typeof (decimal), false, -1.0m}, | ||
new object[] {"-1.123456", typeof (decimal), false, -1.123456m}, | ||
new object[] {"abcd", typeof (decimal), true, null}, | ||
|
||
new object[] {"", typeof (string), false, ""}, | ||
new object[] {"abcd", typeof (string), false, "abcd"}, | ||
|
||
new object[] {"ValueA", typeof (TestEnum), false, TestEnum.ValueA}, | ||
new object[] {"VALUEA", typeof (TestEnum), false, TestEnum.ValueA}, | ||
new object[] {"ValueB", typeof(TestEnum), false, TestEnum.ValueB}, | ||
new object[] {((int) TestEnum.ValueA).ToString(), typeof (TestEnum), false, TestEnum.ValueA}, | ||
new object[] {((int) TestEnum.ValueB).ToString(), typeof (TestEnum), false, TestEnum.ValueB}, | ||
new object[] {((int) TestEnum.ValueB + 1).ToString(), typeof (TestEnum), true, null}, | ||
new object[] {((int) TestEnum.ValueA - 1).ToString(), typeof (TestEnum), true, null}, | ||
|
||
// Failed before #339 | ||
new object[] {"false", typeof (int), true, 0}, | ||
new object[] {"true", typeof (int), true, 0} | ||
}; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.