|
| 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