|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Globalization; |
4 | | -using Xunit; |
5 | | -using FluentAssertions; |
6 | | -using CSharpx; |
7 | | -using CommandLine.Core; |
8 | | - |
9 | | -namespace CommandLine.Tests.Unit.Core |
10 | | -{ |
11 | | - public class TypeConverterTests |
12 | | - { |
13 | | - enum TestEnum |
14 | | - { |
15 | | - ValueA = 1, |
16 | | - ValueB = 2 |
17 | | - } |
18 | | - |
19 | | - [Theory] |
20 | | - [MemberData(nameof(ChangeType_scalars_source))] |
21 | | - public void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult) |
22 | | - { |
23 | | - Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, CultureInfo.InvariantCulture, true); |
24 | | - |
25 | | - if (expectFail) |
26 | | - { |
27 | | - result.MatchNothing().Should().BeTrue("should fail parsing"); |
28 | | - } |
29 | | - else |
30 | | - { |
31 | | - result.MatchJust(out object matchedValue).Should().BeTrue("should parse successfully"); |
32 | | - Assert.Equal(matchedValue, expectedResult); |
33 | | - } |
34 | | - } |
35 | | - |
36 | | - [Fact] |
37 | | - public void ChangeType_Scalar_LastOneWins() |
38 | | - { |
39 | | - var values = new[] { "100", "200", "300", "400", "500" }; |
40 | | - var result = TypeConverter.ChangeType(values, typeof(int), true, CultureInfo.InvariantCulture, true); |
41 | | - result.MatchJust(out var matchedValue).Should().BeTrue("should parse successfully"); |
42 | | - Assert.Equal(500, matchedValue); |
43 | | - |
44 | | - } |
45 | | - |
46 | | - public static IEnumerable<object[]> ChangeType_scalars_source |
47 | | - { |
48 | | - get |
49 | | - { |
50 | | - return new[] |
51 | | - { |
52 | | - new object[] {"1", typeof (int), false, 1}, |
53 | | - new object[] {"0", typeof (int), false, 0}, |
54 | | - new object[] {"-1", typeof (int), false, -1}, |
55 | | - new object[] {"abcd", typeof (int), true, null}, |
56 | | - new object[] {"1.0", typeof (int), true, null}, |
57 | | - new object[] {int.MaxValue.ToString(), typeof (int), false, int.MaxValue}, |
58 | | - new object[] {int.MinValue.ToString(), typeof (int), false, int.MinValue}, |
59 | | - new object[] {((long) int.MaxValue + 1).ToString(), typeof (int), true, null}, |
60 | | - new object[] {((long) int.MinValue - 1).ToString(), typeof (int), true, null}, |
61 | | - |
62 | | - new object[] {"1", typeof (uint), false, (uint) 1}, |
63 | | - // new object[] {"0", typeof (uint), false, (uint) 0}, //cause warning: Skipping test case with duplicate ID |
64 | | - // new object[] {"-1", typeof (uint), true, null}, //cause warning: Skipping test case with duplicate ID |
65 | | - new object[] {uint.MaxValue.ToString(), typeof (uint), false, uint.MaxValue}, |
66 | | - new object[] {uint.MinValue.ToString(), typeof (uint), false, uint.MinValue}, |
67 | | - new object[] {((long) uint.MaxValue + 1).ToString(), typeof (uint), true, null}, |
68 | | - new object[] {((long) uint.MinValue - 1).ToString(), typeof (uint), true, null}, |
69 | | - |
70 | | - new object[] {"true", typeof (bool), false, true}, |
71 | | - new object[] {"True", typeof (bool), false, true}, |
72 | | - new object[] {"TRUE", typeof (bool), false, true}, |
73 | | - new object[] {"false", typeof (bool), false, false}, |
74 | | - new object[] {"False", typeof (bool), false, false}, |
75 | | - new object[] {"FALSE", typeof (bool), false, false}, |
76 | | - new object[] {"abcd", typeof (bool), true, null}, |
77 | | - new object[] {"0", typeof (bool), true, null}, |
78 | | - new object[] {"1", typeof (bool), true, null}, |
79 | | - |
80 | | - new object[] {"1.0", typeof (float), false, 1.0f}, |
81 | | - new object[] {"0.0", typeof (float), false, 0.0f}, |
82 | | - new object[] {"-1.0", typeof (float), false, -1.0f}, |
83 | | - new object[] {"abcd", typeof (float), true, null}, |
84 | | - |
85 | | - new object[] {"1.0", typeof (double), false, 1.0}, |
86 | | - new object[] {"0.0", typeof (double), false, 0.0}, |
87 | | - new object[] {"-1.0", typeof (double), false, -1.0}, |
88 | | - new object[] {"abcd", typeof (double), true, null}, |
89 | | - |
90 | | - new object[] {"1.0", typeof (decimal), false, 1.0m}, |
91 | | - new object[] {"0.0", typeof (decimal), false, 0.0m}, |
92 | | - new object[] {"-1.0", typeof (decimal), false, -1.0m}, |
93 | | - new object[] {"-1.123456", typeof (decimal), false, -1.123456m}, |
94 | | - new object[] {"abcd", typeof (decimal), true, null}, |
95 | | - |
96 | | - new object[] {"", typeof (string), false, ""}, |
97 | | - new object[] {"abcd", typeof (string), false, "abcd"}, |
98 | | - |
99 | | - new object[] {"ValueA", typeof (TestEnum), false, TestEnum.ValueA}, |
100 | | - new object[] {"VALUEA", typeof (TestEnum), false, TestEnum.ValueA}, |
101 | | - new object[] {"ValueB", typeof(TestEnum), false, TestEnum.ValueB}, |
102 | | - new object[] {((int) TestEnum.ValueA).ToString(), typeof (TestEnum), false, TestEnum.ValueA}, |
103 | | - new object[] {((int) TestEnum.ValueB).ToString(), typeof (TestEnum), false, TestEnum.ValueB}, |
104 | | - new object[] {((int) TestEnum.ValueB + 1).ToString(), typeof (TestEnum), true, null}, |
105 | | - new object[] {((int) TestEnum.ValueA - 1).ToString(), typeof (TestEnum), true, null}, |
106 | | - |
107 | | - // Failed before #339 |
108 | | - new object[] {"false", typeof (int), true, 0}, |
109 | | - new object[] {"true", typeof (int), true, 0} |
110 | | - }; |
111 | | - } |
112 | | - } |
113 | | - } |
114 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using Xunit; |
| 5 | +using FluentAssertions; |
| 6 | +using CSharpx; |
| 7 | +using CommandLine.Core; |
| 8 | + |
| 9 | +namespace CommandLine.Tests.Unit.Core |
| 10 | +{ |
| 11 | + public class TypeConverterTests |
| 12 | + { |
| 13 | + enum TestEnum |
| 14 | + { |
| 15 | + ValueA = 1, |
| 16 | + ValueB = 2 |
| 17 | + } |
| 18 | + |
| 19 | + [Theory] |
| 20 | + [MemberData(nameof(ChangeType_scalars_source))] |
| 21 | + public void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult) |
| 22 | + { |
| 23 | + Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, false, CultureInfo.InvariantCulture, true); |
| 24 | + |
| 25 | + if (expectFail) |
| 26 | + { |
| 27 | + result.MatchNothing().Should().BeTrue("should fail parsing"); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + result.MatchJust(out object matchedValue).Should().BeTrue("should parse successfully"); |
| 32 | + Assert.Equal(matchedValue, expectedResult); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void ChangeType_Scalar_LastOneWins() |
| 38 | + { |
| 39 | + var values = new[] { "100", "200", "300", "400", "500" }; |
| 40 | + var result = TypeConverter.ChangeType(values, typeof(int), true, false, CultureInfo.InvariantCulture, true); |
| 41 | + result.MatchJust(out var matchedValue).Should().BeTrue("should parse successfully"); |
| 42 | + Assert.Equal(500, matchedValue); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + // TODO: Write test for TypeConverter.ChangeType when isFlag = true |
| 47 | + |
| 48 | + public static IEnumerable<object[]> ChangeType_scalars_source |
| 49 | + { |
| 50 | + get |
| 51 | + { |
| 52 | + return new[] |
| 53 | + { |
| 54 | + new object[] {"1", typeof (int), false, 1}, |
| 55 | + new object[] {"0", typeof (int), false, 0}, |
| 56 | + new object[] {"-1", typeof (int), false, -1}, |
| 57 | + new object[] {"abcd", typeof (int), true, null}, |
| 58 | + new object[] {"1.0", typeof (int), true, null}, |
| 59 | + new object[] {int.MaxValue.ToString(), typeof (int), false, int.MaxValue}, |
| 60 | + new object[] {int.MinValue.ToString(), typeof (int), false, int.MinValue}, |
| 61 | + new object[] {((long) int.MaxValue + 1).ToString(), typeof (int), true, null}, |
| 62 | + new object[] {((long) int.MinValue - 1).ToString(), typeof (int), true, null}, |
| 63 | + |
| 64 | + new object[] {"1", typeof (uint), false, (uint) 1}, |
| 65 | + // new object[] {"0", typeof (uint), false, (uint) 0}, //cause warning: Skipping test case with duplicate ID |
| 66 | + // new object[] {"-1", typeof (uint), true, null}, //cause warning: Skipping test case with duplicate ID |
| 67 | + new object[] {uint.MaxValue.ToString(), typeof (uint), false, uint.MaxValue}, |
| 68 | + new object[] {uint.MinValue.ToString(), typeof (uint), false, uint.MinValue}, |
| 69 | + new object[] {((long) uint.MaxValue + 1).ToString(), typeof (uint), true, null}, |
| 70 | + new object[] {((long) uint.MinValue - 1).ToString(), typeof (uint), true, null}, |
| 71 | + |
| 72 | + new object[] {"true", typeof (bool), false, true}, |
| 73 | + new object[] {"True", typeof (bool), false, true}, |
| 74 | + new object[] {"TRUE", typeof (bool), false, true}, |
| 75 | + new object[] {"false", typeof (bool), false, false}, |
| 76 | + new object[] {"False", typeof (bool), false, false}, |
| 77 | + new object[] {"FALSE", typeof (bool), false, false}, |
| 78 | + new object[] {"abcd", typeof (bool), true, null}, |
| 79 | + new object[] {"0", typeof (bool), true, null}, |
| 80 | + new object[] {"1", typeof (bool), true, null}, |
| 81 | + |
| 82 | + new object[] {"1.0", typeof (float), false, 1.0f}, |
| 83 | + new object[] {"0.0", typeof (float), false, 0.0f}, |
| 84 | + new object[] {"-1.0", typeof (float), false, -1.0f}, |
| 85 | + new object[] {"abcd", typeof (float), true, null}, |
| 86 | + |
| 87 | + new object[] {"1.0", typeof (double), false, 1.0}, |
| 88 | + new object[] {"0.0", typeof (double), false, 0.0}, |
| 89 | + new object[] {"-1.0", typeof (double), false, -1.0}, |
| 90 | + new object[] {"abcd", typeof (double), true, null}, |
| 91 | + |
| 92 | + new object[] {"1.0", typeof (decimal), false, 1.0m}, |
| 93 | + new object[] {"0.0", typeof (decimal), false, 0.0m}, |
| 94 | + new object[] {"-1.0", typeof (decimal), false, -1.0m}, |
| 95 | + new object[] {"-1.123456", typeof (decimal), false, -1.123456m}, |
| 96 | + new object[] {"abcd", typeof (decimal), true, null}, |
| 97 | + |
| 98 | + new object[] {"", typeof (string), false, ""}, |
| 99 | + new object[] {"abcd", typeof (string), false, "abcd"}, |
| 100 | + |
| 101 | + new object[] {"ValueA", typeof (TestEnum), false, TestEnum.ValueA}, |
| 102 | + new object[] {"VALUEA", typeof (TestEnum), false, TestEnum.ValueA}, |
| 103 | + new object[] {"ValueB", typeof(TestEnum), false, TestEnum.ValueB}, |
| 104 | + new object[] {((int) TestEnum.ValueA).ToString(), typeof (TestEnum), false, TestEnum.ValueA}, |
| 105 | + new object[] {((int) TestEnum.ValueB).ToString(), typeof (TestEnum), false, TestEnum.ValueB}, |
| 106 | + new object[] {((int) TestEnum.ValueB + 1).ToString(), typeof (TestEnum), true, null}, |
| 107 | + new object[] {((int) TestEnum.ValueA - 1).ToString(), typeof (TestEnum), true, null}, |
| 108 | + |
| 109 | + // Failed before #339 |
| 110 | + new object[] {"false", typeof (int), true, 0}, |
| 111 | + new object[] {"true", typeof (int), true, 0} |
| 112 | + }; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments