Skip to content

Commit 418f6e2

Browse files
committed
Add tests for several double-dash scenarios
Double dash (--) should end option sequences, but should have no effect on value sequences. These unit tests reflect that design.
1 parent b6d3d47 commit 418f6e2

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
3+
using System.Collections.Generic;
4+
5+
namespace CommandLine.Tests.Fakes
6+
{
7+
class Options_With_Value_Sequence_And_Subsequent_Value
8+
{
9+
[Value(0)]
10+
public IEnumerable<string> StringSequence { get; set; }
11+
12+
[Value(1)]
13+
public string NeverReachedValue { get; set; }
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
3+
using System.Collections.Generic;
4+
5+
namespace CommandLine.Tests.Fakes
6+
{
7+
class Options_With_Value_Sequence_With_Max_And_Subsequent_Value
8+
{
9+
[Value(0, Max=2)]
10+
public IEnumerable<string> StringSequence { get; set; }
11+
12+
[Value(1)]
13+
public string NeverReachedValue { get; set; }
14+
}
15+
}

tests/CommandLine.Tests/Unit/ParserTests.cs

+71
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,77 @@ public void Parse_options_with_double_dash_and_option_sequence()
152152
((Parsed<Options_With_Option_Sequence_And_Value_Sequence>)result).Value.Should().BeEquivalentTo(expectedOptions);
153153
}
154154

155+
[Theory]
156+
[InlineData("value1", "value2", "value3")]
157+
[InlineData("--", "value1", "value2", "value3")]
158+
[InlineData("value1", "--", "value2", "value3")]
159+
[InlineData("value1", "value2", "--", "value3")]
160+
[InlineData("value1", "value2", "value3", "--")]
161+
public void Parse_options_with_double_dash_in_various_positions(params string[] args)
162+
{
163+
var expectedOptions = new Options_With_Sequence_And_Only_Max_Constraint_For_Value
164+
{
165+
StringSequence = new[] { "value1", "value2", "value3" }
166+
};
167+
168+
var sut = new Parser(with => with.EnableDashDash = true);
169+
170+
// Exercize system
171+
var result =
172+
sut.ParseArguments<Options_With_Sequence_And_Only_Max_Constraint_For_Value>(args);
173+
174+
// Verify outcome
175+
((Parsed<Options_With_Sequence_And_Only_Max_Constraint_For_Value>)result).Value.Should().BeEquivalentTo(expectedOptions);
176+
}
177+
178+
[Theory]
179+
[InlineData("value1", "value2", "value3")]
180+
[InlineData("--", "value1", "value2", "value3")]
181+
[InlineData("value1", "--", "value2", "value3")]
182+
[InlineData("value1", "value2", "--", "value3")]
183+
[InlineData("value1", "value2", "value3", "--")]
184+
public void Parse_options_with_double_dash_and_all_consuming_sequence_leaves_nothing_for_later_values(params string[] args)
185+
{
186+
var expectedOptions = new Options_With_Value_Sequence_And_Subsequent_Value
187+
{
188+
StringSequence = new[] { "value1", "value2", "value3" },
189+
NeverReachedValue = null
190+
};
191+
192+
var sut = new Parser(with => with.EnableDashDash = true);
193+
194+
// Exercize system
195+
var result =
196+
sut.ParseArguments<Options_With_Value_Sequence_And_Subsequent_Value>(args);
197+
198+
// Verify outcome
199+
((Parsed<Options_With_Value_Sequence_And_Subsequent_Value>)result).Value.Should().BeEquivalentTo(expectedOptions);
200+
}
201+
202+
[Theory]
203+
[InlineData("value1", "value2", "value3")]
204+
[InlineData("--", "value1", "value2", "value3")]
205+
[InlineData("value1", "--", "value2", "value3")]
206+
[InlineData("value1", "value2", "--", "value3")]
207+
[InlineData("value1", "value2", "value3", "--")]
208+
public void Parse_options_with_double_dash_and_limited_sequence_leaves_something_for_later_values(params string[] args)
209+
{
210+
var expectedOptions = new Options_With_Value_Sequence_With_Max_And_Subsequent_Value
211+
{
212+
StringSequence = new[] { "value1", "value2" },
213+
NeverReachedValue = "value3"
214+
};
215+
216+
var sut = new Parser(with => with.EnableDashDash = true);
217+
218+
// Exercize system
219+
var result =
220+
sut.ParseArguments<Options_With_Value_Sequence_With_Max_And_Subsequent_Value>(args);
221+
222+
// Verify outcome
223+
((Parsed<Options_With_Value_Sequence_With_Max_And_Subsequent_Value>)result).Value.Should().BeEquivalentTo(expectedOptions);
224+
}
225+
155226
[Fact]
156227
public void Parse_options_with_double_dash_in_verbs_scenario()
157228
{

0 commit comments

Comments
 (0)