-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathTokenizerTests.cs
148 lines (125 loc) · 6.59 KB
/
TokenizerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using FluentAssertions;
using CSharpx;
using RailwaySharp.ErrorHandling;
using CommandLine.Core;
using CommandLine.Infrastructure;
namespace CommandLine.Tests.Unit.Core
{
public class TokenizerTests
{
[Fact]
public void Explode_scalar_with_separator_in_odd_args_input_returns_sequence()
{
// Fixture setup
var expectedTokens = new[] { Token.Name("i"), Token.Value("10"), Token.Name("string-seq"),
Token.Value("aaa"), Token.Value("bb"), Token.Value("cccc"), Token.Name("switch") };
var specs = new[] { new OptionSpecification(string.Empty, "string-seq",
false, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(), ',', null, string.Empty, string.Empty, new List<string>(), typeof(IEnumerable<string>), TargetType.Sequence, string.Empty)};
// Exercize system
var result =
Tokenizer.ExplodeOptionList(
Result.Succeed(
Enumerable.Empty<Token>().Concat(new[] { Token.Name("i"), Token.Value("10"),
Token.Name("string-seq"), Token.Value("aaa,bb,cccc"), Token.Name("switch") }),
Enumerable.Empty<Error>()),
optionName => NameLookup.HavingSeparator(optionName, specs, StringComparer.Ordinal));
// Verify outcome
((Ok<IEnumerable<Token>, Error>)result).Success.Should().BeEquivalentTo(expectedTokens);
// Teardown
}
[Fact]
public void Explode_scalar_with_separator_in_even_args_input_returns_sequence()
{
// Fixture setup
var expectedTokens = new[] { Token.Name("x"), Token.Name("string-seq"),
Token.Value("aaa"), Token.Value("bb"), Token.Value("cccc"), Token.Name("switch") };
var specs = new[] { new OptionSpecification(string.Empty, "string-seq",
false, string.Empty, Maybe.Nothing<int>(), Maybe.Nothing<int>(), ',', null, string.Empty, string.Empty, new List<string>(), typeof(IEnumerable<string>), TargetType.Sequence, string.Empty)};
// Exercize system
var result =
Tokenizer.ExplodeOptionList(
Result.Succeed(
Enumerable.Empty<Token>().Concat(new[] { Token.Name("x"),
Token.Name("string-seq"), Token.Value("aaa,bb,cccc"), Token.Name("switch") }),
Enumerable.Empty<Error>()),
optionName => NameLookup.HavingSeparator(optionName, specs, StringComparer.Ordinal));
// Verify outcome
((Ok<IEnumerable<Token>, Error>)result).Success.Should().BeEquivalentTo(expectedTokens);
// Teardown
}
[Fact]
public void Normalize_should_remove_all_value_with_explicit_assignment_of_existing_name()
{
// Fixture setup
var expectedTokens = new[] {
Token.Name("x"), Token.Name("string-seq"), Token.Value("aaa"), Token.Value("bb"),
Token.Name("unknown"), Token.Name("switch") };
Func<string, bool> nameLookup =
name => name.Equals("x") || name.Equals("string-seq") || name.Equals("switch");
// Exercize system
var result =
Tokenizer.Normalize(
//Result.Succeed(
Enumerable.Empty<Token>()
.Concat(
new[] {
Token.Name("x"), Token.Name("string-seq"), Token.Value("aaa"), Token.Value("bb"),
Token.Name("unknown"), Token.Value("value0", true), Token.Name("switch") })
//,Enumerable.Empty<Error>()),
, nameLookup);
// Verify outcome
result.Should().BeEquivalentTo(expectedTokens);
// Teardown
}
[Fact]
public void Should_properly_parse_option_with_equals_in_value()
{
/**
* This is how the arg. would look in `static void Main(string[] args)`
* if passed from the command-line and the option-value wrapped in quotes.
* Ex.) ./app --connectionString="Server=localhost;Data Source..."
*/
var args = new[] { "--connectionString=Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;" };
var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
var tokens = result.SucceededWith();
Assert.NotNull(tokens);
Assert.Equal(2, tokens.Count());
Assert.Equal("connectionString", tokens.First().Text);
Assert.Equal("Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;", tokens.Last().Text);
}
[Fact]
public void Should_return_error_if_option_format_with_equals_is_not_correct()
{
var args = new[] { "--option1 = fail", "--option2= fail" };
var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
var tokens = result.SuccessMessages();
Assert.NotNull(tokens);
Assert.Equal(2, tokens.Count());
Assert.Equal(ErrorType.BadFormatTokenError, tokens.First().Tag);
Assert.Equal(ErrorType.BadFormatTokenError, tokens.Last().Tag);
}
[Theory]
[InlineData(new[] { "-a", "-" }, 2,"a","-")]
[InlineData(new[] { "--file", "-" }, 2,"file","-")]
[InlineData(new[] { "-f-" }, 2,"f", "-")]
[InlineData(new[] { "--file=-" }, 2, "file", "-")]
[InlineData(new[] { "-a", "--" }, 1, "a", "a")]
public void single_dash_as_a_value(string[] args, int countExcepted,string first,string last)
{
//Arrange
//Act
var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);
var tokens = result.SucceededWith().ToList();
//Assert
tokens.Should().NotBeNull();
tokens.Count.Should().Be(countExcepted);
tokens.First().Text.Should().Be(first);
tokens.Last().Text.Should().Be(last);
}
}
}