BadScript 2
Loading...
Searching...
No Matches
TokenizerTests.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.Linq;
6using Xunit;
7using FluentAssertions;
8using CSharpx;
12
14{
15 public class TokenizerTests
16 {
17 [Fact]
19 {
20 // Fixture setup
21 var expectedTokens = new[] { Token.Name("i"), Token.Value("10"), Token.Name("string-seq"),
22 Token.Value("aaa"), Token.Value("bb"), Token.Value("cccc"), Token.Name("switch") };
23 var specs = new[] { new OptionSpecification(string.Empty, "string-seq",
24 false, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(), ',', null, string.Empty, string.Empty, new List<string>(), typeof(IEnumerable<string>), TargetType.Sequence, string.Empty)};
25
26 // Exercize system
27 var result =
29 Result.Succeed(
30 Enumerable.Empty<Token>().Concat(new[] { Token.Name("i"), Token.Value("10"),
31 Token.Name("string-seq"), Token.Value("aaa,bb,cccc"), Token.Name("switch") }),
32 Enumerable.Empty<Error>()),
33 optionName => NameLookup.HavingSeparator(optionName, specs, StringComparer.Ordinal));
34 // Verify outcome
35 ((Ok<IEnumerable<Token>, Error>)result).Success.Should().BeEquivalentTo(expectedTokens);
36
37 // Teardown
38 }
39
40 [Fact]
42 {
43 // Fixture setup
44 var expectedTokens = new[] { Token.Name("x"), Token.Name("string-seq"),
45 Token.Value("aaa"), Token.Value("bb"), Token.Value("cccc"), Token.Name("switch") };
46 var specs = new[] { new OptionSpecification(string.Empty, "string-seq",
47 false, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(), ',', null, string.Empty, string.Empty, new List<string>(), typeof(IEnumerable<string>), TargetType.Sequence, string.Empty)};
48
49 // Exercize system
50 var result =
52 Result.Succeed(
53 Enumerable.Empty<Token>().Concat(new[] { Token.Name("x"),
54 Token.Name("string-seq"), Token.Value("aaa,bb,cccc"), Token.Name("switch") }),
55 Enumerable.Empty<Error>()),
56 optionName => NameLookup.HavingSeparator(optionName, specs, StringComparer.Ordinal));
57
58 // Verify outcome
59 ((Ok<IEnumerable<Token>, Error>)result).Success.Should().BeEquivalentTo(expectedTokens);
60
61 // Teardown
62 }
63
64 [Fact]
66 {
67 // Fixture setup
68 var expectedTokens = new[] {
69 Token.Name("x"), Token.Name("string-seq"), Token.Value("value0", true), Token.Value("bb"),
70 Token.Name("switch") };
71 Func<string, bool> nameLookup =
72 name => name.Equals("x") || name.Equals("string-seq") || name.Equals("switch");
73
74 // Exercize system
75 var result =
77 //Result.Succeed(
78 Enumerable.Empty<Token>()
79 .Concat(
80 new[] {
81 Token.Name("x"), Token.Name("string-seq"), Token.Value("value0", true), Token.Value("bb"),
82 Token.Name("unknown"), Token.Value("value0", true), Token.Name("switch") })
83 //,Enumerable.Empty<Error>()),
84 , nameLookup);
85
86 // Verify outcome
87 result.Should().BeEquivalentTo(expectedTokens);
88
89 // Teardown
90 }
91
92 [Fact]
94 {
95 // Fixture setup
96 var expectedTokens = new[] {
97 Token.Name("x"), Token.Name("string-seq"), Token.Value("value0", true), Token.Value("bb"),
98 Token.Name("switch") };
99 Func<string, bool> nameLookup =
100 name => name.Equals("x") || name.Equals("string-seq") || name.Equals("switch");
101
102 // Exercize system
103 var result =
105 //Result.Succeed(
106 Enumerable.Empty<Token>()
107 .Concat(
108 new[] {
109 Token.Name("x"), Token.Name("string-seq"), Token.Value("value0", true), Token.Value("bb"),
110 Token.Name("unknown"), Token.Name("switch") })
111 //,Enumerable.Empty<Error>()),
112 , nameLookup);
113
114 // Verify outcome
115 result.Should().BeEquivalentTo(expectedTokens);
116
117 // Teardown
118 }
119
120 [Fact]
122 {
128 var args = new[] { "--connectionString=Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;" };
129
130 var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
131
132 var tokens = result.SucceededWith();
133
134 Assert.NotNull(tokens);
135 Assert.Equal(2, tokens.Count());
136 Assert.Equal("connectionString", tokens.First().Text);
137 Assert.Equal("Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;", tokens.Last().Text);
138 }
139
140 [Fact]
142 {
143 var args = new[] { "--option1 = fail", "--option2= fail" };
144
145 var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
146
147 var tokens = result.SuccessMessages();
148
149 Assert.NotNull(tokens);
150 Assert.Equal(2, tokens.Count());
151 Assert.Equal(ErrorType.BadFormatTokenError, tokens.First().Tag);
152 Assert.Equal(ErrorType.BadFormatTokenError, tokens.Last().Tag);
153 }
154
155 [Theory]
156 [InlineData(new[] { "-a", "-" }, 2,"a","-")]
157 [InlineData(new[] { "--file", "-" }, 2,"file","-")]
158 [InlineData(new[] { "-f-" }, 2,"f", "-")]
159 [InlineData(new[] { "--file=-" }, 2, "file", "-")]
160 [InlineData(new[] { "-a", "--" }, 1, "a", "a")]
161 public void Single_dash_as_a_value(string[] args, int countExcepted,string first,string last)
162 {
163 //Arrange
164 //Act
165 var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
166 var tokens = result.SucceededWith().ToList();
167 //Assert
168 tokens.Should().NotBeNull();
169 tokens.Count.Should().Be(countExcepted);
170 tokens.First().Text.Should().Be(first);
171 tokens.Last().Text.Should().Be(last);
172 }
173 }
174
175}
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 Maybe< char > HavingSeparator(string name, IEnumerable< OptionSpecification > specifications, StringComparer comparer)
Definition NameLookup.cs:37
static Token Value(string text)
Definition Token.cs:30
static Token Name(string text)
Definition Token.cs:25
static Result< IEnumerable< Token >, Error > ExplodeOptionList(Result< IEnumerable< Token >, Error > tokenizerResult, Func< string, Maybe< char > > optionSequenceWithSeparatorLookup)
Definition Tokenizer.cs:74
static Result< IEnumerable< Token >, Error > Tokenize(IEnumerable< string > arguments, Func< string, NameLookupResult > nameLookup)
Definition Tokenizer.cs:18
static IEnumerable< Token > Normalize(IEnumerable< Token > tokens, Func< string, bool > nameLookup)
Normalizes the given tokens .
Definition Tokenizer.cs:127
Base type of all errors.
Definition Error.cs:110
void Single_dash_as_a_value(string[] args, int countExcepted, string first, string last)
void Normalize_should_remove_all_names_and_values_with_explicit_assignment_of_non_existing_names()
Represents the result of a successful computation.
Represents the result of a computation.
ErrorType
Discriminator enumeration of CommandLine.Error derivates.
Definition Error.cs:13