Skip to content

Commit

Permalink
consolidate and clean up a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Mar 29, 2023
1 parent fe66356 commit d18f598
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 603 deletions.
50 changes: 0 additions & 50 deletions src/System.CommandLine.Tests/Binding/SetHandlerTests.cs

This file was deleted.

256 changes: 249 additions & 7 deletions src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.CommandLine.Help;
using System.IO;
using System.Linq;
using FluentAssertions;
using Xunit;
using static System.Environment;
Expand All @@ -29,9 +31,9 @@ public Customization()
private HelpBuilder GetHelpBuilder(int maxWidth) => new (maxWidth);

[Fact]
public void Option_can_customize_default_value()
public void Option_can_customize_displayed_default_value()
{
var option = new Option<string>("--the-option") { DefaultValueFactory = (_) => "not 42" };
var option = new Option<string>("--the-option") { DefaultValueFactory = _ => "not 42" };
var command = new Command("the-command", "command help")
{
option
Expand Down Expand Up @@ -136,9 +138,9 @@ public void Option_can_customize_second_column_text_based_on_parse_result()
ctx.Command.Equals(commandA)
? optionADescription
: optionBDescription);
command.Options.Add(new HelpOption()
command.Options.Add(new HelpOption
{
Action = new HelpAction()
Action = new HelpAction
{
Builder = helpBuilder
}
Expand Down Expand Up @@ -201,7 +203,7 @@ public void Command_arguments_can_customize_second_column_text()
var argument = new Argument<string>("some-arg")
{
Description = "Default description",
DefaultValueFactory = (_) => "not 42"
DefaultValueFactory = _ => "not 42"
};
var command = new Command("the-command", "command help")
{
Expand Down Expand Up @@ -313,9 +315,9 @@ public void Argument_can_fallback_to_default_when_customizing(

CommandLineConfiguration config = new (command);

command.Options.Add(new HelpOption()
command.Options.Add(new HelpOption
{
Action = new HelpAction()
Action = new HelpAction
{
Builder = helpBuilder
}
Expand All @@ -325,6 +327,246 @@ public void Argument_can_fallback_to_default_when_customizing(
command.Parse("test -h", config).Invoke();
config.Output.ToString().Should().MatchRegex(expected);
}


[Fact]
public void Individual_symbols_can_be_customized()
{
var subcommand = new Command("subcommand", "The default command description");
var option = new Option<int>("-x") { Description = "The default option description" };
var argument = new Argument<int>("int-value") { Description = "The default argument description" };

var rootCommand = new RootCommand
{
subcommand,
option,
argument,
};

CommandLineConfiguration config = new(rootCommand)
{
Output = new StringWriter()
};

ParseResult parseResult = rootCommand.Parse("-h", config);

if (parseResult.Action is HelpAction helpAction)
{
helpAction.Builder.CustomizeSymbol(subcommand, secondColumnText: "The custom command description");
helpAction.Builder.CustomizeSymbol(option, secondColumnText: "The custom option description");
helpAction.Builder.CustomizeSymbol(argument, secondColumnText: "The custom argument description");
}

parseResult.Invoke();

config.Output
.ToString()
.Should()
.ContainAll("The custom command description",
"The custom option description",
"The custom argument description");
}

[Fact]
public void Help_sections_can_be_replaced()
{
CommandLineConfiguration config = new(new RootCommand())
{
Output = new StringWriter()
};

ParseResult parseResult = config.Parse("-h");

if (parseResult.Action is HelpAction helpAction)
{
helpAction.Builder.CustomizeLayout(CustomLayout);
}

parseResult.Invoke();

config.Output.ToString().Should().Be($"one{NewLine}{NewLine}two{NewLine}{NewLine}three{NewLine}{NewLine}{NewLine}");

IEnumerable<Action<HelpContext>> CustomLayout(HelpContext _)
{
yield return ctx => ctx.Output.WriteLine("one");
yield return ctx => ctx.Output.WriteLine("two");
yield return ctx => ctx.Output.WriteLine("three");
}
}

[Fact]
public void Help_sections_can_be_supplemented()
{
CommandLineConfiguration config = new(new RootCommand("hello"))
{
Output = new StringWriter(),
};

ParseResult parseResult = config.Parse("-h");

if (parseResult.Action is HelpAction helpAction)
{
helpAction.Builder.CustomizeLayout(CustomLayout);
}

parseResult.Invoke();

var output = config.Output.ToString();
var defaultHelp = GetDefaultHelp(config.RootCommand);

var expected = $"first{NewLine}{NewLine}{defaultHelp}last{NewLine}{NewLine}";

output.Should().Be(expected);

IEnumerable<Action<HelpContext>> CustomLayout(HelpContext _)
{
yield return ctx => ctx.Output.WriteLine("first");

foreach (var section in HelpBuilder.Default.GetLayout())
{
yield return section;
}

yield return ctx => ctx.Output.WriteLine("last");
}
}

[Fact]
public void Layout_can_be_composed_dynamically_based_on_context()
{
HelpBuilder helpBuilder = new();
var commandWithTypicalHelp = new Command("typical");
var commandWithCustomHelp = new Command("custom");
var command = new RootCommand
{
commandWithTypicalHelp,
commandWithCustomHelp
};

command.Options.OfType<HelpOption>().Single().Action = new HelpAction()
{
Builder = helpBuilder
};

var config = new CommandLineConfiguration(command);
helpBuilder.CustomizeLayout(c =>
c.Command == commandWithTypicalHelp
? HelpBuilder.Default.GetLayout()
: new Action<HelpContext>[]
{
c => c.Output.WriteLine("Custom layout!")
}
.Concat(HelpBuilder.Default.GetLayout()));

var typicalOutput = new StringWriter();
config.Output = typicalOutput;
command.Parse("typical -h", config).Invoke();

var customOutput = new StringWriter();
config.Output = customOutput;
command.Parse("custom -h", config).Invoke();

typicalOutput.ToString().Should().Be(GetDefaultHelp(commandWithTypicalHelp, false));
customOutput.ToString().Should().Be($"Custom layout!{NewLine}{NewLine}{GetDefaultHelp(commandWithCustomHelp, false)}");
}

[Fact]
public void Help_default_sections_can_be_wrapped()
{
Command command = new("test")
{
new Option<string>("--option")
{
Description = "option description",
HelpName = "option"
},
new HelpOption
{
Action = new HelpAction
{
Builder = new HelpBuilder(30)
}
}
};

CommandLineConfiguration config = new(command)
{
Output = new StringWriter()
};

config.Invoke("test -h");

string result = config.Output.ToString();
result.Should().Be(
$"Description:{NewLine}{NewLine}" +
$"Usage:{NewLine} test [options]{NewLine}{NewLine}" +
$"Options:{NewLine}" +
$" --option option {NewLine}" +
$" <option> description{NewLine}" +
$" -?, -h, Show help and {NewLine}" +
$" --help usage {NewLine}" +
$" information{NewLine}{NewLine}{NewLine}");
}

[Fact]
public void Help_customized_sections_can_be_wrapped()
{
CommandLineConfiguration config = new(new RootCommand())
{
Output = new StringWriter()
};

ParseResult parseResult = config.Parse("-h");

if (parseResult.Action is HelpAction helpAction)
{
helpAction.Builder = new HelpBuilder(10);
helpAction.Builder.CustomizeLayout(CustomLayout);
}

parseResult.Invoke();

string result = config.Output.ToString();
result.Should().Be($" 123 123{NewLine} 456 456{NewLine} 78 789{NewLine} 0{NewLine}{NewLine}{NewLine}");

IEnumerable<Action<HelpContext>> CustomLayout(HelpContext _)
{
yield return ctx => ctx.HelpBuilder.WriteColumns(new[] { new TwoColumnHelpRow("12345678", "1234567890") }, ctx);
}
}

private string GetDefaultHelp(Command command, bool trimOneNewline = true)
{
// The command might have already defined a HelpOption with custom settings,
// we need to overwrite it to get the actual defaults.
HelpOption defaultHelp = new();
// HelpOption overrides Equals and treats every other instance of same type as equal
int index = command.Options.IndexOf(defaultHelp);
if (index >= 0)
{
command.Options[index] = defaultHelp;
}
else
{
command.Options.Add(defaultHelp);
}

CommandLineConfiguration config = new(command)
{
Output = new StringWriter()
};

config.Invoke("-h");

var output = config.Output.ToString();

if (trimOneNewline)
{
output = output.Substring(0, output.Length - NewLine.Length);
}

return output;
}
}
}
}
Loading

0 comments on commit d18f598

Please sign in to comment.