|
| 1 | +using System.Linq; |
| 2 | +using CommandLine.Tests.Fakes; |
| 3 | +using CommandLine.Text; |
| 4 | +using FluentAssertions; |
| 5 | +using Xunit; |
| 6 | +using Xunit.Abstractions; |
| 7 | + |
| 8 | +//Issue #104 |
| 9 | +//When outputting HelpText, the code will correctly list valid values for enum type options. However, if the option is a nullable enum type, then it will not list the valid values. |
| 10 | + |
| 11 | +namespace CommandLine.Tests.Unit |
| 12 | +{ |
| 13 | + public class Issue104Tests |
| 14 | + { |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Create_instance_with_enum_options_enabled_and_nullable_enum() |
| 18 | + { |
| 19 | + // Fixture setup |
| 20 | + // Exercize system |
| 21 | + var sut = new HelpText { AddDashesToOption = true, AddEnumValuesToHelpText = true, MaximumDisplayWidth = 80 } |
| 22 | + .AddPreOptionsLine("pre-options") |
| 23 | + .AddOptions(new NotParsed<Options_With_Nullable_Enum_Having_HelpText>(TypeInfo.Create(typeof(Options_With_Enum_Having_HelpText)), Enumerable.Empty<Error>())) |
| 24 | + .AddPostOptionsLine("post-options"); |
| 25 | + |
| 26 | + // Verify outcome |
| 27 | + |
| 28 | + var lines = sut.ToString().ToNotEmptyLines().TrimStringArray(); |
| 29 | + lines[0].Should().BeEquivalentTo("pre-options"); |
| 30 | + lines[1].Should().BeEquivalentTo("--stringvalue Define a string value here."); |
| 31 | + lines[2].Should().BeEquivalentTo("--shape Define a enum value here. Valid values: Circle, Square,"); |
| 32 | + lines[3].Should().BeEquivalentTo("Triangle"); |
| 33 | + lines[4].Should().BeEquivalentTo("--help Display this help screen."); |
| 34 | + lines[5].Should().BeEquivalentTo("--version Display version information."); |
| 35 | + lines[6].Should().BeEquivalentTo("post-options"); |
| 36 | + // Teardown |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void Help_with_enum_options_enabled_and_nullable_enum() |
| 41 | + { |
| 42 | + // Fixture setup |
| 43 | + // Exercize system |
| 44 | + var args = "--help".Split(); |
| 45 | + var sut = new Parser(config => config.HelpWriter = null); |
| 46 | + var parserResult = sut.ParseArguments<Options_With_Nullable_Enum_Having_HelpText>(args); |
| 47 | + HelpText helpText = null; |
| 48 | + parserResult.WithNotParsed(errors => |
| 49 | + { |
| 50 | + // Use custom help text to ensure valid enum values are displayed |
| 51 | + helpText = HelpText.AutoBuild(parserResult); |
| 52 | + helpText.AddEnumValuesToHelpText = true; |
| 53 | + helpText.AddOptions(parserResult); |
| 54 | + }); |
| 55 | + |
| 56 | + // Verify outcome |
| 57 | + |
| 58 | + var lines = helpText.ToString().ToNotEmptyLines().TrimStringArray(); |
| 59 | + lines[2].Should().BeEquivalentTo("--stringvalue Define a string value here."); |
| 60 | + lines[3].Should().BeEquivalentTo("--shape Define a enum value here. Valid values: Circle, Square,"); |
| 61 | + lines[4].Should().BeEquivalentTo("Triangle"); |
| 62 | + lines[5].Should().BeEquivalentTo("--help Display this help screen."); |
| 63 | + lines[6].Should().BeEquivalentTo("--version Display version information."); |
| 64 | + // Teardown |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments