Skip to content

Commit 6a781d1

Browse files
committed
Added tests for AutoHelp and AutoVersion config options.
1 parent 5dad6ed commit 6a781d1

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

tests/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
<Link>Properties\SharedAssemblyInfo.cs</Link>
5959
</Compile>
6060
<Compile Include="CultureInfoExtensions.cs" />
61+
<Compile Include="Fakes\Options_With_Custom_Help_Option.cs" />
62+
<Compile Include="Fakes\Options_With_Custom_Version_Option.cs" />
6163
<Compile Include="Fakes\Options_With_Default_Set_To_Sequence.cs" />
6264
<Compile Include="Fakes\Options_With_Guid.cs" />
6365
<Compile Include="Fakes\Options_With_Option_And_Value_Of_String_Type.cs" />
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CommandLine.Tests.Fakes
2+
{
3+
class Options_With_Custom_Help_Option : Simple_Options
4+
{
5+
[Option('h', "help")]
6+
public bool Help { get; set; }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CommandLine.Tests.Fakes
2+
{
3+
class Options_With_Custom_Version_Option : Simple_Options
4+
{
5+
[Option('v', "version")]
6+
public bool MyVersion { get; set; }
7+
}
8+
}

tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace CommandLine.Tests.Unit.Core
1919
{
2020
public class InstanceBuilderTests
2121
{
22-
private static ParserResult<T> InvokeBuild<T>(string[] arguments)
22+
private static ParserResult<T> InvokeBuild<T>(string[] arguments, bool autoHelp = true, bool autoVersion = true)
2323
where T : new()
2424
{
2525
return InstanceBuilder.Build(
@@ -29,8 +29,8 @@ private static ParserResult<T> InvokeBuild<T>(string[] arguments)
2929
StringComparer.Ordinal,
3030
false,
3131
CultureInfo.InvariantCulture,
32-
true,
33-
true,
32+
autoHelp,
33+
autoVersion,
3434
Enumerable.Empty<ErrorType>());
3535
}
3636

@@ -1015,6 +1015,76 @@ public void Parse_string_with_dashes_except_in_beginning(string[] arguments, str
10151015
// Teardown
10161016
}
10171017

1018+
[Theory]
1019+
[InlineData(new[] { "--help" }, ErrorType.UnknownOptionError)]
1020+
public void Parse_without_auto_help_should_not_recognize_help_option(string[] arguments, ErrorType errorType)
1021+
{
1022+
// Fixture setup in attributes
1023+
1024+
// Exercize system
1025+
var result = InvokeBuild<Simple_Options>(arguments, autoHelp: false);
1026+
1027+
// Verify outcome
1028+
result.Should().BeOfType<NotParsed<Simple_Options>>()
1029+
.Which.Errors.Should().ContainSingle()
1030+
.Which.Tag.Should().Be(errorType);
1031+
1032+
// Teardown
1033+
}
1034+
1035+
[Theory]
1036+
[InlineData(new[] { "--help" }, true)]
1037+
[InlineData(new[] { "-h" }, true)]
1038+
[InlineData(new[] { "-x" }, false)]
1039+
public void Parse_with_custom_help_option(string[] arguments, bool isHelp)
1040+
{
1041+
// Fixture setup in attributes
1042+
1043+
// Exercize system
1044+
var result = InvokeBuild<Options_With_Custom_Help_Option>(arguments, autoHelp: false);
1045+
1046+
// Verify outcome
1047+
result.Should().BeOfType<Parsed<Options_With_Custom_Help_Option>>()
1048+
.Which.Value.Help.Should().Be(isHelp);
1049+
1050+
// Teardown
1051+
}
1052+
1053+
[Theory]
1054+
[InlineData(new[] { "--version" }, ErrorType.UnknownOptionError)]
1055+
public void Parse_without_auto_version_should_not_recognize_version_option(string[] arguments, ErrorType errorType)
1056+
{
1057+
// Fixture setup in attributes
1058+
1059+
// Exercize system
1060+
var result = InvokeBuild<Simple_Options>(arguments, autoVersion: false);
1061+
1062+
// Verify outcome
1063+
result.Should().BeOfType<NotParsed<Simple_Options>>()
1064+
.Which.Errors.Should().ContainSingle()
1065+
.Which.Tag.Should().Be(errorType);
1066+
1067+
// Teardown
1068+
}
1069+
1070+
[Theory]
1071+
[InlineData(new[] { "--version" }, true)]
1072+
[InlineData(new[] { "-v" }, true)]
1073+
[InlineData(new[] { "-s", "s" }, false)]
1074+
public void Parse_with_custom_version_option(string[] arguments, bool isVersion)
1075+
{
1076+
// Fixture setup in attributes
1077+
1078+
// Exercize system
1079+
var result = InvokeBuild<Options_With_Custom_Version_Option>(arguments, autoVersion: false);
1080+
1081+
// Verify outcome
1082+
result.Should().BeOfType<Parsed<Options_With_Custom_Version_Option>>()
1083+
.Which.Value.MyVersion.Should().Be(isVersion);
1084+
1085+
// Teardown
1086+
}
1087+
10181088
[Theory]
10191089
[MemberData("GuidData")]
10201090
public void Parse_Guid(string[] arguments, Options_With_Guid expected)

0 commit comments

Comments
 (0)