Skip to content

Commit ca660cc

Browse files
Keboojonsequitur
authored andcommitted
Adding unit test to ensure validators are invoked
1 parent 14175ac commit ca660cc

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/System.CommandLine.Tests/ParsingValidationTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public void When_a_required_argument_is_not_supplied_then_an_error_is_returned()
8383
.Contain(e => e.Message == "Required argument missing for option: -x");
8484
}
8585

86-
8786
[Fact]
8887
public void When_a_required_option_is_not_supplied_then_an_error_is_returned()
8988
{
@@ -147,7 +146,6 @@ public void Required_options_on_parent_commands_do_not_create_parse_errors_when_
147146
result.Errors.Should().BeEmpty();
148147
}
149148

150-
151149
[Fact]
152150
public void When_no_option_accepts_arguments_but_one_is_supplied_then_an_error_is_returned()
153151
{
@@ -246,6 +244,35 @@ public void A_custom_validator_can_be_added_to_an_argument()
246244
.Be("Argument x cannot be set to 123");
247245
}
248246

247+
[Theory]
248+
[InlineData("--file \"Foo\" subcommand")]
249+
[InlineData("subcommand --file \"Foo\"")]
250+
public void Validators_on_global_options_are_executed_when_invoking_a_subcommand(string commandLine)
251+
{
252+
var option = new Option<FileInfo>("--file");
253+
option.Argument.AddValidator(r =>
254+
{
255+
return "Invoked validator";
256+
});
257+
258+
var subCommand = new Command("subcommand");
259+
var rootCommand = new RootCommand
260+
{
261+
subCommand
262+
};
263+
rootCommand.AddGlobalOption(option);
264+
265+
var result = rootCommand.Parse(commandLine);
266+
267+
result.Errors
268+
.Should()
269+
.ContainSingle(e => e.SymbolResult.Symbol == option.Argument)
270+
.Which
271+
.Message
272+
.Should()
273+
.Be("Invoked validator");
274+
}
275+
249276
[Theory]
250277
[InlineData("--value 123")]
251278
[InlineData("--value=123 child")]

0 commit comments

Comments
 (0)