-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathInstanceChooserTests.cs
170 lines (140 loc) · 6.63 KB
/
InstanceChooserTests.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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.Globalization;
using System.Linq;
using CommandLine.Core;
using CommandLine.Tests.Fakes;
using FluentAssertions;
using Xunit;
namespace CommandLine.Tests.Unit.Core
{
public class InstanceChooserTests
{
private static ParserResult<object> InvokeChoose(
IEnumerable<Type> types,
IEnumerable<string> arguments)
{
return InstanceChooser.Choose(
(args, optionSpecs) => Tokenizer.ConfigureTokenizer(StringComparer.Ordinal, false, false)(args, optionSpecs),
types,
arguments,
StringComparer.Ordinal,
false,
CultureInfo.InvariantCulture,
Enumerable.Empty<ErrorType>());
}
[Fact]
public void Parse_empty_array_returns_NullInstance()
{
// Fixture setup
var expectedErrors = new[] { new NoVerbSelectedError() };
// Exercize system
var result = InvokeChoose(
new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) },
new string[] { });
// Verify outcome
((NotParsed<object>)result).Errors.ShouldBeEquivalentTo(expectedErrors);
// Teardown
}
[Fact]
public void Explicit_help_request_generates_HelpVerbRequestedError()
{
// Fixture setup
var expectedErrors = new[] { new HelpVerbRequestedError(null, null, false) };
// Exercize system
var result = InvokeChoose(
new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) },
new[] { "help" });
// Verify outcome
((NotParsed<object>)result).Errors.ShouldBeEquivalentTo(expectedErrors);
// Teardown
}
[Fact]
public void Explicit_help_request_for_a_valid_verb_generates_HelpVerbRequestedError_with_appropriate_data()
{
// Fixture setup
var expectedErrors = new[] { new HelpVerbRequestedError("commit", typeof(Commit_Verb), true) };
// Exercize system
var result = InvokeChoose(
new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) },
new[] { "help", "commit" });
// Verify outcome
((NotParsed<object>)result).Errors.ShouldBeEquivalentTo(expectedErrors);
// Teardown
}
[Fact]
public void Explicit_help_request_for_an_invalid_verb_generates_HelpVerbRequestedError_with_Matched_set_to_false()
{
// Fixture setup
var expectedErrors = new[] { new HelpVerbRequestedError(null, null, false) };
// Exercize system
var result = InvokeChoose(
new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) },
new[] { "help", "earthunderalienattack" });
// Verify outcome
((NotParsed<object>)result).Errors.ShouldBeEquivalentTo(expectedErrors);
// Teardown
}
[Fact]
public void Parse_existing_verb_returns_verb_instance()
{
// Fixture setup
var expected = new Add_Verb { Patch = true, FileName = "dummy.bin"};
// Exercize system
var result = InvokeChoose(
new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) },
new[] { "add", "--patch", "dummy.bin" });
// Verify outcome
Assert.IsType<Add_Verb>(((Parsed<object>)result).Value);
expected.ShouldBeEquivalentTo(((Parsed<object>)result).Value);
// Teardown
}
[Fact]
public void Parse_existing_verb_returns_verb_immutable_instance()
{
// Fixture setup
var expected = new Immutable_Add_Verb(true, default(bool), "dummy.bin");
// Exercize system
var result = InvokeChoose(
new[] { typeof(Immutable_Add_Verb), typeof(Immutable_Commit_Verb), typeof(Immutable_Clone_Verb) },
new[] { "add", "--patch", "dummy.bin" });
// Verify outcome
Assert.IsType<Immutable_Add_Verb>(((Parsed<object>)result).Value);
expected.ShouldBeEquivalentTo(((Parsed<object>)result).Value);
// Teardown
}
[Fact]
public void Parse_sequence_verb_returns_verb_instance()
{
// Fixture setup
var expected = new SequenceOptions { LongSequence = new long[] { }, StringSequence = new[] { "aa", "b" } };
// Exercize system
var result = InvokeChoose(
new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb), typeof(SequenceOptions) },
new[] { "sequence", "-s", "aa", "b" });
// Verify outcome
Assert.IsType<SequenceOptions>(((Parsed<object>)result).Value);
expected.ShouldBeEquivalentTo(((Parsed<object>)result).Value);
// Teardown
}
[Theory]
[InlineData(new[] { "sequence", "-s", "here-one-elem-but-no-sep" }, new[] { "here-one-elem-but-no-sep" })]
[InlineData(new[] { "sequence", "-shere-one-elem-but-no-sep" }, new[] { "here-one-elem-but-no-sep" })]
[InlineData(new[] { "sequence", "-s", "[email protected],[email protected],[email protected]" }, new[] { "[email protected]", "[email protected]", "[email protected]" })]
[InlineData(new[] { "sequence", "[email protected],[email protected],[email protected],another,the-last-one" }, new[] { "[email protected]", "[email protected]", "[email protected]", "another", "the-last-one" })]
public void Parse_sequence_verb_with_separator_returns_verb_instance(string[] arguments, string[] expectedString)
{
// Fixture setup
var expected = new SequenceOptions { LongSequence = new long[] { }, StringSequence = expectedString };
// Exercize system
var result = InvokeChoose(
new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb), typeof(SequenceOptions) },
arguments);
// Verify outcome
Assert.IsType<SequenceOptions>(((Parsed<object>)result).Value);
expected.ShouldBeEquivalentTo(((Parsed<object>)result).Value);
// Teardown
}
}
}