diff --git a/src/Spectre.Console.Testing/Cli/CommandAppResult.cs b/src/Spectre.Console.Testing/Cli/CommandAppResult.cs index 3b5a914..ebc30f0 100644 --- a/src/Spectre.Console.Testing/Cli/CommandAppResult.cs +++ b/src/Spectre.Console.Testing/Cli/CommandAppResult.cs @@ -31,10 +31,5 @@ public sealed class CommandAppResult Output = output ?? string.Empty; Context = context; Settings = settings; - - Output = Output - .NormalizeLineEndings() - .TrimLines() - .Trim(); } } \ No newline at end of file diff --git a/src/Spectre.Console.Testing/Cli/CommandAppTester.cs b/src/Spectre.Console.Testing/Cli/CommandAppTester.cs index 314054a..597dba1 100644 --- a/src/Spectre.Console.Testing/Cli/CommandAppTester.cs +++ b/src/Spectre.Console.Testing/Cli/CommandAppTester.cs @@ -8,20 +8,36 @@ public sealed class CommandAppTester private Action? _appConfiguration; private Action? _configuration; - /// - /// Initializes a new instance of the class. - /// - /// The registrar. - public CommandAppTester(ITypeRegistrar? registrar = null) - { - Registrar = registrar; - } - /// /// Gets or sets the Registrar to use in the CommandApp. /// public ITypeRegistrar? Registrar { get; set; } + /// + /// Gets or sets the settings for the . + /// + public CommandAppTesterSettings TestSettings { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The registrar. + /// The settings. + public CommandAppTester(ITypeRegistrar? registrar = null, CommandAppTesterSettings? settings = null) + { + Registrar = registrar; + TestSettings = settings ?? new CommandAppTesterSettings(); + } + + /// + /// Initializes a new instance of the class. + /// + /// The settings. + public CommandAppTester(CommandAppTesterSettings settings) + { + TestSettings = settings; + } + /// /// Sets the default command. /// @@ -135,10 +151,8 @@ public sealed class CommandAppTester var result = app.Run(args); - var output = console.Output - .NormalizeLineEndings() - .TrimLines() - .Trim(); + var output = console.Output.NormalizeLineEndings(); + output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output; return new CommandAppResult(result, output, context, settings); } @@ -181,10 +195,8 @@ public sealed class CommandAppTester var result = await app.RunAsync(args); - var output = console.Output - .NormalizeLineEndings() - .TrimLines() - .Trim(); + var output = console.Output.NormalizeLineEndings(); + output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output; return new CommandAppResult(result, output, context, settings); } diff --git a/src/Spectre.Console.Testing/Cli/CommandAppTesterSettings.cs b/src/Spectre.Console.Testing/Cli/CommandAppTesterSettings.cs new file mode 100644 index 0000000..c4d149d --- /dev/null +++ b/src/Spectre.Console.Testing/Cli/CommandAppTesterSettings.cs @@ -0,0 +1,15 @@ +namespace Spectre.Console.Testing; + +/// +/// Represents the configuration settings for the class. +/// +public sealed class CommandAppTesterSettings +{ + /// + /// Gets or sets a value indicating whether whitespace should be trimmed from the console output. + /// + /// + /// When enabled, leading and trailing whitespace from the console output and trailing whitespace from each line will be trimmed. + /// + public bool TrimConsoleOutput { get; set; } = true; +} \ No newline at end of file diff --git a/src/Tests/Spectre.Console.Cli.Tests/Data/Commands/AsynchronousCommand.cs b/src/Tests/Spectre.Console.Cli.Tests/Data/Commands/AsynchronousCommand.cs index 550f2cc..0da13ac 100644 --- a/src/Tests/Spectre.Console.Cli.Tests/Data/Commands/AsynchronousCommand.cs +++ b/src/Tests/Spectre.Console.Cli.Tests/Data/Commands/AsynchronousCommand.cs @@ -20,7 +20,7 @@ public sealed class AsynchronousCommand : AsyncCommand + { + private readonly IAnsiConsole _console; + + public CommandAppTesterCommand(IAnsiConsole console) + { + _console = console; + } + + public override int Execute(CommandContext context, OptionalArgumentWithDefaultValueSettings settings) + { + _console.Write(settings.Greeting); + return 0; + } + } + + [Theory] + [InlineData(false, " Hello ", " Hello ")] + [InlineData(true, " Hello ", "Hello")] + [InlineData(false, " Hello \n World ", " Hello \n World ")] + [InlineData(true, " Hello \n World ", "Hello\n World")] + public void Should_Respect_Trim_Setting(bool trim, string actual, string expected) + { + // Given + var settings = new CommandAppTesterSettings { TrimConsoleOutput = trim }; + + var app = new CommandAppTester(settings); + app.SetDefaultCommand(); + app.Configure(config => + { + config.PropagateExceptions(); + }); + + // When + var result = app.Run(actual); + + // Then + result.Output.ShouldBe(expected); + } +}