Skip to content

Commit dc9cc82

Browse files
committed
Option group help text tests without errors section
1 parent 33dfcce commit dc9cc82

File tree

4 files changed

+119
-6
lines changed

4 files changed

+119
-6
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
3+
namespace CommandLine.Tests.Fakes
4+
{
5+
public class Simple_Options_With_Multiple_OptionGroups
6+
{
7+
[Option(HelpText = "Define a string value here.", Group = "string-group")]
8+
public string StringValue { get; set; }
9+
10+
[Option('s', "shortandlong", HelpText = "Example with both short and long name.", Group = "string-group")]
11+
public string ShortAndLong { get; set; }
12+
13+
[Option('x', HelpText = "Define a boolean or switch value here.", Group = "second-group")]
14+
public bool BoolValue { get; set; }
15+
16+
[Option('i', Min = 3, Max = 4, HelpText = "Define a int sequence here.", Group = "second-group")]
17+
public IEnumerable<int> IntSequence { get; set; }
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace CommandLine.Tests.Fakes
2+
{
3+
public class Simple_Options_With_OptionGroup
4+
{
5+
[Option(HelpText = "Define a string value here.", Group = "string-group")]
6+
public string StringValue { get; set; }
7+
8+
[Option('s', "shortandlong", HelpText = "Example with both short and long name.", Group = "string-group")]
9+
public string ShortAndLong { get; set; }
10+
11+
[Option('x', HelpText = "Define a boolean or switch value here.")]
12+
public bool BoolValue { get; set; }
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace CommandLine.Tests.Fakes
2+
{
3+
public class Simple_Options_With_Required_OptionGroup
4+
{
5+
[Option(HelpText = "Define a string value here.", Required = true, Group = "string-group")]
6+
public string StringValue { get; set; }
7+
8+
[Option('s', "shortandlong", HelpText = "Example with both short and long name.", Group = "string-group")]
9+
public string ShortAndLong { get; set; }
10+
11+
[Option('x', HelpText = "Define a boolean or switch value here.")]
12+
public bool BoolValue { get; set; }
13+
}
14+
}

tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Globalization;
6-
using CommandLine.Core;
76
using System.Linq;
87
using System.Reflection;
8+
using System.Text;
9+
10+
using CommandLine.Core;
911
using CommandLine.Infrastructure;
1012
using CommandLine.Tests.Fakes;
1113
using CommandLine.Text;
14+
1215
using FluentAssertions;
16+
1317
using Xunit;
14-
using System.Text;
1518

1619
namespace CommandLine.Tests.Unit.Text
1720
{
1821
public class HelpTextTests : IDisposable
1922
{
23+
private readonly HeadingInfo headingInfo = new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131");
24+
2025
public void Dispose()
2126
{
2227
ReflectionHelper.SetAttributeOverride(null);
@@ -143,7 +148,7 @@ public void When_help_text_is_longer_than_width_it_will_wrap_around_as_if_in_a_c
143148
{
144149
// Fixture setup
145150
// Exercize system
146-
var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131"));
151+
var sut = new HelpText(headingInfo);
147152
sut.MaximumDisplayWidth = 40;
148153
sut.AddOptions(
149154
new NotParsed<Simple_Options_With_HelpText_Set_To_Long_Description>(
@@ -166,7 +171,7 @@ public void When_help_text_is_longer_than_width_it_will_wrap_around_as_if_in_a_c
166171
{
167172
// Fixture setup
168173
// Exercize system
169-
var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131")) { MaximumDisplayWidth = 100} ;
174+
var sut = new HelpText(headingInfo) { MaximumDisplayWidth = 100} ;
170175
sut.AddOptions(
171176
new NotParsed<Simple_Options_With_HelpText_Set_To_Long_Description>(
172177
TypeInfo.Create(typeof(Simple_Options_With_HelpText_Set_To_Long_Description)),
@@ -185,7 +190,7 @@ public void When_help_text_has_hidden_option_it_should_not_be_added_to_help_text
185190
{
186191
// Fixture setup
187192
// Exercize system
188-
var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131"));
193+
var sut = new HelpText(headingInfo);
189194
sut.MaximumDisplayWidth = 80;
190195
sut.AddOptions(
191196
new NotParsed<Simple_Options_With_HelpText_Set_To_Long_Description>(
@@ -205,7 +210,7 @@ public void Long_help_text_without_spaces()
205210
{
206211
// Fixture setup
207212
// Exercize system
208-
var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131"));
213+
var sut = new HelpText(headingInfo);
209214
sut.MaximumDisplayWidth = 40;
210215
sut.AddOptions(
211216
new NotParsed<Simple_Options_With_HelpText_Set_To_Long_Description_Without_Spaces>(
@@ -739,5 +744,66 @@ public void Options_should_be_separated_by_spaces()
739744

740745
// Teardown
741746
}
747+
748+
[Fact]
749+
public void Options_Should_Render_OptionGroup_In_Parenthesis_When_Available()
750+
{
751+
var sut = new HelpText(headingInfo) { AddDashesToOption = true, MaximumDisplayWidth = 100 }
752+
.AddOptions(
753+
new NotParsed<Simple_Options_With_OptionGroup>(TypeInfo.Create(typeof(Simple_Options_With_OptionGroup)), Enumerable.Empty<Error>()));
754+
755+
var text = sut.ToString();
756+
var lines = text.ToLines().TrimStringArray();
757+
758+
759+
lines[0].Should().BeEquivalentTo(headingInfo.ToString());
760+
lines[1].Should().BeEmpty();
761+
lines[2].Should().BeEquivalentTo("--stringvalue (Group: string-group) Define a string value here.");
762+
lines[3].Should().BeEquivalentTo("-s, --shortandlong (Group: string-group) Example with both short and long name.");
763+
lines[4].Should().BeEquivalentTo("-x Define a boolean or switch value here.");
764+
lines[5].Should().BeEquivalentTo("--help Display this help screen.");
765+
lines[6].Should().BeEquivalentTo("--version Display version information.");
766+
}
767+
768+
[Fact]
769+
public void Options_Should_Render_OptionGroup_When_Available_And_Should_Not_Render_Required()
770+
{
771+
var sut = new HelpText(headingInfo) { AddDashesToOption = true, MaximumDisplayWidth = 100 }
772+
.AddOptions(
773+
new NotParsed<Simple_Options_With_Required_OptionGroup>(TypeInfo.Create(typeof(Simple_Options_With_Required_OptionGroup)), Enumerable.Empty<Error>()));
774+
775+
var text = sut.ToString();
776+
var lines = text.ToLines().TrimStringArray();
777+
778+
779+
lines[0].Should().BeEquivalentTo(headingInfo.ToString());
780+
lines[1].Should().BeEmpty();
781+
lines[2].Should().BeEquivalentTo("--stringvalue (Group: string-group) Define a string value here.");
782+
lines[3].Should().BeEquivalentTo("-s, --shortandlong (Group: string-group) Example with both short and long name.");
783+
lines[4].Should().BeEquivalentTo("-x Define a boolean or switch value here.");
784+
lines[5].Should().BeEquivalentTo("--help Display this help screen.");
785+
lines[6].Should().BeEquivalentTo("--version Display version information.");
786+
}
787+
788+
[Fact]
789+
public void Options_Should_Render_Multiple_OptionGroups_When_Available()
790+
{
791+
var sut = new HelpText(headingInfo) { AddDashesToOption = true, MaximumDisplayWidth = 100 }
792+
.AddOptions(
793+
new NotParsed<Simple_Options_With_Multiple_OptionGroups>(TypeInfo.Create(typeof(Simple_Options_With_Multiple_OptionGroups)), Enumerable.Empty<Error>()));
794+
795+
var text = sut.ToString();
796+
var lines = text.ToLines().TrimStringArray();
797+
798+
799+
lines[0].Should().BeEquivalentTo(headingInfo.ToString());
800+
lines[1].Should().BeEmpty();
801+
lines[2].Should().BeEquivalentTo("--stringvalue (Group: string-group) Define a string value here.");
802+
lines[3].Should().BeEquivalentTo("-s, --shortandlong (Group: string-group) Example with both short and long name.");
803+
lines[4].Should().BeEquivalentTo("-x (Group: second-group) Define a boolean or switch value here.");
804+
lines[5].Should().BeEquivalentTo("-i (Group: second-group) Define a int sequence here.");
805+
lines[6].Should().BeEquivalentTo("--help Display this help screen.");
806+
lines[7].Should().BeEquivalentTo("--version Display version information.");
807+
}
742808
}
743809
}

0 commit comments

Comments
 (0)