BadScript 2
Loading...
Searching...
No Matches
TypeConverterTests.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using Xunit;
5using FluentAssertions;
6using CSharpx;
8
10{
11 public class TypeConverterTests
12 {
14 {
15 ValueA = 1,
16 ValueB = 2
17 }
18
19 [Flags]
20 enum TestFlagEnum
21 {
22 ValueA = 0x1,
23 ValueB = 0x2
24 }
25
26 [Theory]
27 [MemberData(nameof(ChangeType_scalars_source))]
28 public void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult)
29 {
30 Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, false, CultureInfo.InvariantCulture, true);
31
32 if (expectFail)
33 {
34 result.MatchNothing().Should().BeTrue("should fail parsing");
35 }
36 else
37 {
38 result.MatchJust(out object 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}, //cause warning: Skipping test case with duplicate ID
61 // new object[] {"-1", typeof (uint), true, null}, //cause warning: Skipping test case with duplicate ID
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 new object[] {"ValueA", typeof (TestFlagEnum), false, TestFlagEnum.ValueA},
105 new object[] {"VALUEA", typeof (TestFlagEnum), false, TestFlagEnum.ValueA},
106 new object[] {"ValueB", typeof(TestFlagEnum), false, TestFlagEnum.ValueB},
107 new object[] {"ValueA,ValueB", typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
108 new object[] {"ValueA, ValueB", typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
109 new object[] {"VALUEA,ValueB", typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
110 new object[] {((int) TestFlagEnum.ValueA).ToString(), typeof (TestFlagEnum), false, TestFlagEnum.ValueA},
111 new object[] {((int) TestFlagEnum.ValueB).ToString(), typeof (TestFlagEnum), false, TestFlagEnum.ValueB},
112 new object[] {((int) (TestFlagEnum.ValueA | TestFlagEnum.ValueB)).ToString(), typeof (TestFlagEnum), false, TestFlagEnum.ValueA | TestFlagEnum.ValueB},
113 new object[] {((int) TestFlagEnum.ValueB + 2).ToString(), typeof (TestFlagEnum), true, null},
114 new object[] {((int) TestFlagEnum.ValueA - 1).ToString(), typeof (TestFlagEnum), true, null},
115
116
117 // Failed before #339
118 new object[] {"false", typeof (int), true, 0},
119 new object[] {"true", typeof (int), true, 0}
120 };
121 }
122 }
123
124 [Theory]
125 [MemberData(nameof(ChangeType_flagCounters_source))]
126 public void ChangeType_flagCounters(string[] testValue, Type destinationType, bool expectFail, object expectedResult)
127 {
128 Maybe<object> result = TypeConverter.ChangeType(testValue, destinationType, true, true, CultureInfo.InvariantCulture, true);
129
130 if (expectFail)
131 {
132 result.MatchNothing().Should().BeTrue("should fail parsing");
133 }
134 else
135 {
136 result.MatchJust(out object matchedValue).Should().BeTrue("should parse successfully");
137 Assert.Equal(matchedValue, expectedResult);
138 }
139 }
140
141 public static IEnumerable<object[]> ChangeType_flagCounters_source
142 {
143 get
144 {
145 return new[]
146 {
147 new object[] {new string[0], typeof (int), false, 0},
148 new object[] {new[] {"true"}, typeof (int), false, 1},
149 new object[] {new[] {"true", "true"}, typeof (int), false, 2},
150 new object[] {new[] {"true", "true", "true"}, typeof (int), false, 3},
151 new object[] {new[] {"true", "x"}, typeof (int), true, 0},
152 };
153 }
154 }
155
156 [Fact]
158 {
159 var values = new[] { "100", "200", "300", "400", "500" };
160 var result = TypeConverter.ChangeType(values, typeof(int), true, false, CultureInfo.InvariantCulture, true);
161 result.MatchJust(out var matchedValue).Should().BeTrue("should parse successfully");
162 Assert.Equal(500, matchedValue);
163
164 }
165 }
166}
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
bool MatchNothing()
Matches an empty value returning true.
Definition Maybe.cs:59
bool MatchJust(out T value)
Matches a value returning true and value itself via output parameter.
Definition Maybe.cs:49
static Maybe< object > ChangeType(IEnumerable< string > values, Type conversionType, bool scalar, bool isFlag, CultureInfo conversionCulture, bool ignoreValueCase)
static IEnumerable< object[]> ChangeType_scalars_source
void ChangeType_scalars(string testValue, Type destinationType, bool expectFail, object expectedResult)
void ChangeType_flagCounters(string[] testValue, Type destinationType, bool expectFail, object expectedResult)
static IEnumerable< object[]> ChangeType_flagCounters_source