|
| 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 | + [Theory] |
| 19 | + [MemberData("ChangeType_scalars_source")] |
| 20 | + public void ChangeType_scalars(string testValue, Type destinationType, CultureInfo culture, bool expectFail, object expectedResult) |
| 21 | + { |
| 22 | + Maybe<object> result = TypeConverter.ChangeType(new[] {testValue}, destinationType, true, culture, true); |
| 23 | + |
| 24 | + if (expectFail) |
| 25 | + { |
| 26 | + result.MatchNothing().Should().BeTrue(); |
| 27 | + } |
| 28 | + else |
| 29 | + { |
| 30 | + object matchedValue; |
| 31 | + |
| 32 | + result.MatchJust(out matchedValue).Should().BeTrue(); |
| 33 | + Assert.Equal(matchedValue, expectedResult); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static IEnumerable<object[]> ChangeType_scalars_source |
| 38 | + { |
| 39 | + get |
| 40 | + { |
| 41 | + yield return new object[] { "1", typeof (int), CultureInfo.InvariantCulture, false, 1}; |
| 42 | + yield return new object[] { "0", typeof (int), CultureInfo.InvariantCulture, false, 0 }; |
| 43 | + yield return new object[] { "-1", typeof (int), CultureInfo.InvariantCulture, false, -1 }; |
| 44 | + yield return new object[] { "1.0", typeof (int), CultureInfo.InvariantCulture, true, null }; |
| 45 | + yield return new object[] { "1.0", typeof(float), CultureInfo.InvariantCulture, false, 1.0f }; |
| 46 | + yield return new object[] { "0.0", typeof (float), CultureInfo.InvariantCulture, false, 0.0f}; |
| 47 | + yield return new object[] { "-1.0", typeof (float), CultureInfo.InvariantCulture, false, -1.0f}; |
| 48 | + yield return new object[] { "1.0", typeof(double), CultureInfo.InvariantCulture, false, 1.0 }; |
| 49 | + yield return new object[] { "0.0", typeof (double), CultureInfo.InvariantCulture, false, 0.0}; |
| 50 | + yield return new object[] { "-1.0", typeof(double), CultureInfo.InvariantCulture, false, -1.0 }; |
| 51 | + yield return new object[] { "1.0", typeof(decimal), CultureInfo.InvariantCulture, false, 1.0m }; |
| 52 | + yield return new object[] { "0.0", typeof(decimal), CultureInfo.InvariantCulture, false, 0.0m }; |
| 53 | + yield return new object[] { "-1.0", typeof(decimal), CultureInfo.InvariantCulture, false, -1.0m }; |
| 54 | + yield return new object[] { "-1.123456", typeof(decimal), CultureInfo.InvariantCulture, false, -1.123456m }; |
| 55 | + yield return new object[] { "true", typeof(bool), CultureInfo.InvariantCulture, false, true }; |
| 56 | + yield return new object[] { "false", typeof (bool), CultureInfo.InvariantCulture, false, false }; |
| 57 | + yield return new object[] { "", typeof(string), CultureInfo.InvariantCulture, false, "" }; |
| 58 | + yield return new object[] { "abcd", typeof(int), CultureInfo.InvariantCulture, true, "abcd" }; |
| 59 | + yield return new object[] { "abcd", typeof(string), CultureInfo.InvariantCulture, false, "abcd" }; |
| 60 | + |
| 61 | + // Failed before change |
| 62 | + yield return new object[] { "false", typeof(int), CultureInfo.InvariantCulture, true, 0 }; |
| 63 | + yield return new object[] { "true", typeof(int), CultureInfo.InvariantCulture, true, 0 }; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments