Skip to content

Commit f8fb094

Browse files
committed
Add tests for --help usage when AutoHelp=off
Ensure that UnknownOptionError is still generated for --help option when AutoHelp is off.
1 parent 229347b commit f8fb094

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

tests/CommandLine.Tests/Unit/AutoHelpTests.cs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,78 @@ public void Dash_h_consumed_by_valid_args_with_invalid_args_produces_no_HelpRequ
356356
result.As<NotParsed<Simple_Options>>().Errors.Should().NotBeOfType<HelpRequestedError>();
357357
}
358358

359-
// Then with AutoHelp = false, we'd have UnknownArgumentError("help"), which would NOT suppress other errors. So invalid inputs would have more than 1 error.
359+
[Fact]
360+
public void Explicit_help_request_generates_help_requested_error()
361+
{
362+
// Fixture setup
363+
var expectedError = new HelpRequestedError();
364+
var sut = new Parser();
365+
366+
// Exercize system
367+
var result = sut.ParseArguments<Simple_Options>(new[] { "--help" });
368+
369+
// Verify outcome
370+
((NotParsed<Simple_Options>)result).Errors.Should().HaveCount(x => x == 1);
371+
((NotParsed<Simple_Options>)result).Errors.Should().ContainSingle(e => e.Equals(expectedError));
372+
// Teardown
373+
}
374+
375+
[Fact]
376+
public void Explicit_help_request_with_AutoHelp_off_generates_unknown_option_error()
377+
{
378+
// Fixture setup
379+
var expectedError = new UnknownOptionError("help");
380+
var sut = new Parser(config => { config.AutoHelp = false; });
360381

361-
// TODO: Write tests for https://github.com/commandlineparser/commandline/issues/596 (ensure that UnknownArgumentError("help") is produced when AutoHelp=false)
362-
// Also check help text output to make sure it contains "ERROR(S): (...) Option 'help' is unknown."
382+
// Exercise system
383+
var result = sut.ParseArguments<Simple_Options>(new[] { "--help" });
384+
385+
// Verify outcome
386+
((NotParsed<Simple_Options>)result).Errors.Should().HaveCount(x => x == 1);
387+
((NotParsed<Simple_Options>)result).Errors.Single().Tag.Should().Be(expectedError.Tag);
388+
((NotParsed<Simple_Options>)result).Errors.First().As<UnknownOptionError>().Token.Should().BeEquivalentTo(expectedError.Token);
389+
390+
// Teardown
391+
}
392+
393+
[Fact]
394+
public void Explicit_help_request_with_AutoHelp_off_displays_unknown_option_error()
395+
{
396+
// Fixture setup
397+
var help = new StringWriter();
398+
var sut = new Parser(config => { config.AutoHelp = false; config.HelpWriter = help; });
399+
400+
// Exercise system
401+
sut.ParseArguments<Simple_Options>(new[] { "--help" });
402+
var result = help.ToString();
403+
404+
// Verify outcome
405+
406+
// Verify outcome
407+
result.Length.Should().BeGreaterThan(0);
408+
var lines = result.ToNotEmptyLines().TrimStringArray();
409+
lines[0].Should().Be(CommandLine.Text.HeadingInfo.Default.ToString());
410+
lines[1].Should().Be(CommandLine.Text.CopyrightInfo.Default.ToString());
411+
lines[2].Should().BeEquivalentTo("ERROR(S):");
412+
lines[3].Should().BeEquivalentTo("Option 'help' is unknown.");
413+
414+
// Teardown
415+
}
416+
417+
[Fact]
418+
public void Explicit_help_request_with_AutoHelp_off_and_IgnoreUnknownArguments_on_does_not_generate_help_screen()
419+
{
420+
// Fixture setup
421+
var help = new StringWriter();
422+
var sut = new Parser(config => { config.HelpWriter = help; config.AutoHelp = false; config.IgnoreUnknownArguments = true; });
423+
424+
// Exercize system
425+
sut.ParseArguments<Simple_Options>(new[] { "--help" });
426+
var result = help.ToString();
427+
428+
// Verify outcome
429+
result.Should().BeEquivalentTo("");
430+
// Teardown
431+
}
363432
}
364433
}

0 commit comments

Comments
 (0)