Skip to content

Commit

Permalink
Add tests for several double-dash scenarios
Browse files Browse the repository at this point in the history
Double dash (--) should end option sequences, but should have no effect
on value sequences. These unit tests reflect that design.
  • Loading branch information
rmunn committed Jun 1, 2020
1 parent b6d3d47 commit 418f6e2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System.Collections.Generic;

namespace CommandLine.Tests.Fakes
{
class Options_With_Value_Sequence_And_Subsequent_Value
{
[Value(0)]
public IEnumerable<string> StringSequence { get; set; }

[Value(1)]
public string NeverReachedValue { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System.Collections.Generic;

namespace CommandLine.Tests.Fakes
{
class Options_With_Value_Sequence_With_Max_And_Subsequent_Value
{
[Value(0, Max=2)]
public IEnumerable<string> StringSequence { get; set; }

[Value(1)]
public string NeverReachedValue { get; set; }
}
}
71 changes: 71 additions & 0 deletions tests/CommandLine.Tests/Unit/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,77 @@ public void Parse_options_with_double_dash_and_option_sequence()
((Parsed<Options_With_Option_Sequence_And_Value_Sequence>)result).Value.Should().BeEquivalentTo(expectedOptions);
}

[Theory]
[InlineData("value1", "value2", "value3")]
[InlineData("--", "value1", "value2", "value3")]
[InlineData("value1", "--", "value2", "value3")]
[InlineData("value1", "value2", "--", "value3")]
[InlineData("value1", "value2", "value3", "--")]
public void Parse_options_with_double_dash_in_various_positions(params string[] args)
{
var expectedOptions = new Options_With_Sequence_And_Only_Max_Constraint_For_Value
{
StringSequence = new[] { "value1", "value2", "value3" }
};

var sut = new Parser(with => with.EnableDashDash = true);

// Exercize system
var result =
sut.ParseArguments<Options_With_Sequence_And_Only_Max_Constraint_For_Value>(args);

// Verify outcome
((Parsed<Options_With_Sequence_And_Only_Max_Constraint_For_Value>)result).Value.Should().BeEquivalentTo(expectedOptions);
}

[Theory]
[InlineData("value1", "value2", "value3")]
[InlineData("--", "value1", "value2", "value3")]
[InlineData("value1", "--", "value2", "value3")]
[InlineData("value1", "value2", "--", "value3")]
[InlineData("value1", "value2", "value3", "--")]
public void Parse_options_with_double_dash_and_all_consuming_sequence_leaves_nothing_for_later_values(params string[] args)
{
var expectedOptions = new Options_With_Value_Sequence_And_Subsequent_Value
{
StringSequence = new[] { "value1", "value2", "value3" },
NeverReachedValue = null
};

var sut = new Parser(with => with.EnableDashDash = true);

// Exercize system
var result =
sut.ParseArguments<Options_With_Value_Sequence_And_Subsequent_Value>(args);

// Verify outcome
((Parsed<Options_With_Value_Sequence_And_Subsequent_Value>)result).Value.Should().BeEquivalentTo(expectedOptions);
}

[Theory]
[InlineData("value1", "value2", "value3")]
[InlineData("--", "value1", "value2", "value3")]
[InlineData("value1", "--", "value2", "value3")]
[InlineData("value1", "value2", "--", "value3")]
[InlineData("value1", "value2", "value3", "--")]
public void Parse_options_with_double_dash_and_limited_sequence_leaves_something_for_later_values(params string[] args)
{
var expectedOptions = new Options_With_Value_Sequence_With_Max_And_Subsequent_Value
{
StringSequence = new[] { "value1", "value2" },
NeverReachedValue = "value3"
};

var sut = new Parser(with => with.EnableDashDash = true);

// Exercize system
var result =
sut.ParseArguments<Options_With_Value_Sequence_With_Max_And_Subsequent_Value>(args);

// Verify outcome
((Parsed<Options_With_Value_Sequence_With_Max_And_Subsequent_Value>)result).Value.Should().BeEquivalentTo(expectedOptions);
}

[Fact]
public void Parse_options_with_double_dash_in_verbs_scenario()
{
Expand Down

0 comments on commit 418f6e2

Please sign in to comment.