BadScript 2
Loading...
Searching...
No Matches
OptionMapperTests.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Collections.Generic;
5using System.Globalization;
6using System.Linq;
7#if PLATFORM_DOTNET
8using System.Reflection;
9#endif
10using Xunit;
11using CSharpx;
15
17{
18 public class OptionMapperTests
19 {
20 [Fact]
22 {
23 // Fixture setup
24 var tokenPartitions = new[]
25 {
26 new KeyValuePair<string, IEnumerable<string>>("x", new [] { "true" })
27 };
28 var specProps = new[]
29 {
31 new OptionSpecification("x", string.Empty, false, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(), '\0', Maybe.Nothing<object>(), string.Empty, string.Empty, new List<string>(), typeof(bool), TargetType.Switch, string.Empty),
32 typeof(Simple_Options).GetProperties().Single(p => p.Name.Equals("BoolValue", StringComparison.Ordinal)),
33 Maybe.Nothing<object>())
34 };
35
36 // Exercize system
37 var result = OptionMapper.MapValues(
38 specProps.Where(pt => pt.Specification.IsOption()),
39 tokenPartitions,
40 (vals, type, isScalar, isFlag) => TypeConverter.ChangeType(vals, type, isScalar, isFlag, CultureInfo.InvariantCulture, false),
41 StringComparer.Ordinal
42 );
43
44 // Verify outcome
45 Assert.NotNull(((Ok<IEnumerable<SpecificationProperty>, Error>)result).Success.Single(
46 a => a.Specification.IsOption()
47 && ((OptionSpecification)a.Specification).ShortName.Equals("x")
48 && (bool)((Just<object>)a.Value).Value));
49
50 // Teardown
51 }
52
53 [Fact]
55 {
56 var tokenPartitions = new[]
57 {
58 new KeyValuePair<string, IEnumerable<string>>("s", new[] { "string1" }),
59 new KeyValuePair<string, IEnumerable<string>>("shortandlong", new[] { "string2" }),
60 new KeyValuePair<string, IEnumerable<string>>("shortandlong", new[] { "string3" }),
61 new KeyValuePair<string, IEnumerable<string>>("s", new[] { "string4" }),
62 };
63
64 var specProps = new[]
65 {
67 new OptionSpecification("s", "shortandlong", false, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(), '\0', Maybe.Nothing<object>(), string.Empty, string.Empty, new List<string>(), typeof(string), TargetType.Scalar, string.Empty),
68 typeof(Simple_Options).GetProperties().Single(p => p.Name.Equals(nameof(Simple_Options.ShortAndLong), StringComparison.Ordinal)),
69 Maybe.Nothing<object>()),
70 };
71
72 var result = OptionMapper.MapValues(
73 specProps.Where(pt => pt.Specification.IsOption()),
74 tokenPartitions,
75 (vals, type, isScalar, isFlag) => TypeConverter.ChangeType(vals, type, isScalar, isFlag, CultureInfo.InvariantCulture, false),
76 StringComparer.Ordinal);
77
78 var property = result.SucceededWith().Single();
79 Assert.True(property.Specification.IsOption());
80 Assert.True(property.Value.MatchJust(out var stringVal));
81 Assert.Equal(tokenPartitions.Last().Value.Last(), stringVal);
82 }
83
84 [Fact]
86 {
87 var tokenPartitions = new[]
88 {
89 new KeyValuePair<string, IEnumerable<string>>("i", new [] { "1", "2" }),
90 new KeyValuePair<string, IEnumerable<string>>("i", new [] { "3" }),
91 new KeyValuePair<string, IEnumerable<string>>("i", new [] { "4", "5" }),
92 };
93 var specProps = new[]
94 {
96 new OptionSpecification("i", string.Empty, false, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(), '\0', Maybe.Nothing<object>(), string.Empty, string.Empty, new List<string>(), typeof(IEnumerable<int>), TargetType.Sequence, string.Empty),
97 typeof(Simple_Options).GetProperties().Single(p => p.Name.Equals(nameof(Simple_Options.IntSequence), StringComparison.Ordinal)),
98 Maybe.Nothing<object>())
99 };
100
101 var result = OptionMapper.MapValues(
102 specProps.Where(pt => pt.Specification.IsOption()),
103 tokenPartitions,
104 (vals, type, isScalar, isFlag) => TypeConverter.ChangeType(vals, type, isScalar, isFlag, CultureInfo.InvariantCulture, false),
105 StringComparer.Ordinal);
106
107 var property = result.SucceededWith().Single();
108 Assert.True(property.Specification.IsOption());
109 Assert.True(property.Value.MatchJust(out var sequence));
110
111 var expected = tokenPartitions.Aggregate(Enumerable.Empty<int>(), (prev, part) => prev.Concat(part.Value.Select(i => int.Parse(i))));
112 Assert.Equal(expected, sequence);
113 }
114 }
115}
Models a CSharpx.Maybe when contains a value.
Definition Maybe.cs:88
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
static Result< IEnumerable< SpecificationProperty >, Error > MapValues(IEnumerable< SpecificationProperty > propertyTuples, IEnumerable< KeyValuePair< string, IEnumerable< string > > > options, Func< IEnumerable< string >, Type, bool, bool, Maybe< object > > converter, StringComparer comparer)
static SpecificationProperty Create(Specification specification, PropertyInfo property, Maybe< object > value)
static Maybe< object > ChangeType(IEnumerable< string > values, Type conversionType, bool scalar, bool isFlag, CultureInfo conversionCulture, bool ignoreValueCase)
Base type of all errors.
Definition Error.cs:110
Represents the result of a successful computation.