Add new test framework for consoles

This commit is contained in:
Patrik Svensson 2021-04-18 22:40:18 +02:00 committed by Phil Scott
parent f5a9c0ca26
commit 04efd1719c
78 changed files with 1276 additions and 1132 deletions

View File

@ -1,17 +1,3 @@
root = false root = false
[*.cs] [*.cs]
# CS1591: Missing XML comment for publicly visible type or member
dotnet_diagnostic.CS1591.severity = none
# SA1600: Elements should be documented
dotnet_diagnostic.SA1600.severity = none
# SA1200: Using directives should be placed correctly
dotnet_diagnostic.SA1200.severity = none
# Default severity for analyzer diagnostics with category 'StyleCop.CSharp.OrderingRules'
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.OrderingRules.severity = none
# CA1819: Properties should not return arrays
dotnet_diagnostic.CA1819.severity = none

View File

@ -0,0 +1,28 @@
using System;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
/// <summary>
/// A <see cref="ICommandInterceptor"/> that triggers a callback when invoked.
/// </summary>
public sealed class CallbackCommandInterceptor : ICommandInterceptor
{
private readonly Action<CommandContext, CommandSettings> _callback;
/// <summary>
/// Initializes a new instance of the <see cref="CallbackCommandInterceptor"/> class.
/// </summary>
/// <param name="callback">The callback to call when the interceptor is invoked.</param>
public CallbackCommandInterceptor(Action<CommandContext, CommandSettings> callback)
{
_callback = callback ?? throw new ArgumentNullException(nameof(callback));
}
/// <inheritdoc/>
public void Intercept(CommandContext context, CommandSettings settings)
{
_callback(context, settings);
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
/// <summary>
/// Represents a <see cref="CommandApp"/> runtime failure.
/// </summary>
public sealed class CommandAppFailure
{
/// <summary>
/// Gets the exception that was thrown.
/// </summary>
public Exception Exception { get; }
/// <summary>
/// Gets the console output.
/// </summary>
public string Output { get; }
internal CommandAppFailure(Exception exception, string output)
{
Exception = exception ?? throw new ArgumentNullException(nameof(exception));
Output = output.NormalizeLineEndings()
.TrimLines()
.Trim();
}
}
}

View File

@ -0,0 +1,43 @@
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
/// <summary>
/// Represents the result of a completed <see cref="CommandApp"/> run.
/// </summary>
public sealed class CommandAppResult
{
/// <summary>
/// Gets the exit code.
/// </summary>
public int ExitCode { get; }
/// <summary>
/// Gets the console output.
/// </summary>
public string Output { get; }
/// <summary>
/// Gets the command context.
/// </summary>
public CommandContext? Context { get; }
/// <summary>
/// Gets the command settings.
/// </summary>
public CommandSettings? Settings { get; }
internal CommandAppResult(int exitCode, string output, CommandContext? context, CommandSettings? settings)
{
ExitCode = exitCode;
Output = output ?? string.Empty;
Context = context;
Settings = settings;
Output = Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
}
}
}

View File

@ -0,0 +1,112 @@
using System;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
/// <summary>
/// A <see cref="CommandApp"/> test harness.
/// </summary>
public sealed class CommandAppTester
{
private Action<CommandApp>? _appConfiguration;
private Action<IConfigurator>? _configuration;
/// <summary>
/// Sets the default command.
/// </summary>
/// <typeparam name="T">The default command type.</typeparam>
public void SetDefaultCommand<T>()
where T : class, ICommand
{
_appConfiguration = (app) => app.SetDefaultCommand<T>();
}
/// <summary>
/// Configures the command application.
/// </summary>
/// <param name="action">The configuration action.</param>
public void Configure(Action<IConfigurator> action)
{
if (_configuration != null)
{
throw new InvalidOperationException("The command app harnest have already been configured.");
}
_configuration = action;
}
/// <summary>
/// Runs the command application and expects an exception of a specific type to be thrown.
/// </summary>
/// <typeparam name="T">The expected exception type.</typeparam>
/// <param name="args">The arguments.</param>
/// <returns>The information about the failure.</returns>
public CommandAppFailure RunAndCatch<T>(params string[] args)
where T : Exception
{
var console = new TestConsole().Width(int.MaxValue);
try
{
Run(args, console, c => c.PropagateExceptions());
throw new InvalidOperationException("Expected an exception to be thrown, but there was none.");
}
catch (T ex)
{
return new CommandAppFailure(ex, console.Output);
}
catch (Exception ex)
{
throw new InvalidOperationException(
$"Expected an exception of type '{typeof(T).FullName}' to be thrown, "
+ $"but received {ex.GetType().FullName}.");
}
}
/// <summary>
/// Runs the command application.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns>The result.</returns>
public CommandAppResult Run(params string[] args)
{
var console = new TestConsole().Width(int.MaxValue);
return Run(args, console);
}
private CommandAppResult Run(string[] args, TestConsole console, Action<IConfigurator>? config = null)
{
CommandContext? context = null;
CommandSettings? settings = null;
var app = new CommandApp();
_appConfiguration?.Invoke(app);
if (_configuration != null)
{
app.Configure(_configuration);
}
if (config != null)
{
app.Configure(config);
}
app.Configure(c => c.ConfigureConsole(console));
app.Configure(c => c.SetInterceptor(new CallbackCommandInterceptor((ctx, s) =>
{
context = ctx;
settings = s;
})));
var result = app.Run(args);
var output = console.Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
return new CommandAppResult(result, output, context, settings);
}
}
}

View File

@ -1,92 +0,0 @@
using System;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
public sealed class CommandAppFixture
{
private Action<CommandApp> _appConfiguration = _ => { };
private Action<IConfigurator> _configuration;
public CommandAppFixture()
{
_configuration = (_) => { };
}
public CommandAppFixture WithDefaultCommand<T>()
where T : class, ICommand
{
_appConfiguration = (app) => app.SetDefaultCommand<T>();
return this;
}
public void Configure(Action<IConfigurator> action)
{
_configuration = action;
}
public (string Message, string Output) RunAndCatch<T>(params string[] args)
where T : Exception
{
CommandContext context = null;
CommandSettings settings = null;
using var console = new FakeConsole();
var app = new CommandApp();
_appConfiguration?.Invoke(app);
app.Configure(_configuration);
app.Configure(c => c.ConfigureConsole(console));
app.Configure(c => c.SetInterceptor(new FakeCommandInterceptor((ctx, s) =>
{
context = ctx;
settings = s;
})));
try
{
app.Run(args);
}
catch (T ex)
{
var output = console.Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
return (ex.Message, output);
}
throw new InvalidOperationException("No exception was thrown");
}
public (int ExitCode, string Output, CommandContext Context, CommandSettings Settings) Run(params string[] args)
{
CommandContext context = null;
CommandSettings settings = null;
using var console = new FakeConsole(width: int.MaxValue);
var app = new CommandApp();
_appConfiguration?.Invoke(app);
app.Configure(_configuration);
app.Configure(c => c.ConfigureConsole(console));
app.Configure(c => c.SetInterceptor(new FakeCommandInterceptor((ctx, s) =>
{
context = ctx;
settings = s;
})));
var result = app.Run(args);
var output = console.Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
return (result, output, context, settings);
}
}
}

View File

@ -1,49 +0,0 @@
using System;
using System.Diagnostics;
using Shouldly;
namespace Spectre.Console
{
public static class ShouldlyExtensions
{
[DebuggerStepThrough]
public static T And<T>(this T item, Action<T> action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
action(item);
return item;
}
[DebuggerStepThrough]
public static void As<T>(this T item, Action<T> action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
action(item);
}
[DebuggerStepThrough]
public static void As<T>(this object item, Action<T> action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
action((T)item);
}
[DebuggerStepThrough]
public static void ShouldBe<T>(this Type item)
{
item.ShouldBe(typeof(T));
}
}
}

View File

@ -1,13 +1,18 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace Spectre.Console namespace Spectre.Console.Testing
{ {
/// <summary>
/// Contains extensions for <see cref="string"/>.
/// </summary>
public static class StringExtensions public static class StringExtensions
{ {
private static readonly Regex _lineNumberRegex = new Regex(":\\d+", RegexOptions.Singleline); /// <summary>
private static readonly Regex _filenameRegex = new Regex("\\sin\\s.*cs:nn", RegexOptions.Multiline); /// Returns a new string with all lines trimmed of trailing whitespace.
/// </summary>
/// <param name="value">The string to trim.</param>
/// <returns>A new string with all lines trimmed of trailing whitespace.</returns>
public static string TrimLines(this string value) public static string TrimLines(this string value)
{ {
if (value is null) if (value is null)
@ -16,24 +21,19 @@ namespace Spectre.Console
} }
var result = new List<string>(); var result = new List<string>();
var lines = value.Split(new[] { '\n' }); foreach (var line in value.Split(new[] { '\n' }))
foreach (var line in lines)
{ {
var current = line.TrimEnd(); result.Add(line.TrimEnd());
if (string.IsNullOrWhiteSpace(current))
{
result.Add(string.Empty);
}
else
{
result.Add(current);
}
} }
return string.Join("\n", result); return string.Join("\n", result);
} }
/// <summary>
/// Returns a new string with normalized line endings.
/// </summary>
/// <param name="value">The string to normalize line endings for.</param>
/// <returns>A new string with normalized line endings.</returns>
public static string NormalizeLineEndings(this string value) public static string NormalizeLineEndings(this string value)
{ {
if (value != null) if (value != null)
@ -44,36 +44,5 @@ namespace Spectre.Console
return string.Empty; return string.Empty;
} }
public static string NormalizeStackTrace(this string text)
{
text = _lineNumberRegex.Replace(text, match =>
{
return ":nn";
});
return _filenameRegex.Replace(text, match =>
{
var value = match.Value;
var index = value.LastIndexOfAny(new[] { '\\', '/' });
var filename = value.Substring(index + 1, value.Length - index - 1);
return $" in /xyz/{filename}";
});
}
internal static string ReplaceExact(this string text, string oldValue, string newValue)
{
if (string.IsNullOrWhiteSpace(newValue))
{
return text;
}
#if NET5_0
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
#else
return text.Replace(oldValue, newValue);
#endif
}
} }
} }

View File

@ -1,7 +1,17 @@
namespace Spectre.Console.Tests namespace Spectre.Console.Testing
{ {
/// <summary>
/// Contains extensions for <see cref="Style"/>.
/// </summary>
public static class StyleExtensions public static class StyleExtensions
{ {
/// <summary>
/// Sets the foreground or background color of the specified style.
/// </summary>
/// <param name="style">The style.</param>
/// <param name="color">The color.</param>
/// <param name="foreground">Whether or not to set the foreground color.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Style SetColor(this Style style, Color color, bool foreground) public static Style SetColor(this Style style, Color color, bool foreground)
{ {
if (foreground) if (foreground)

View File

@ -1,69 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Xml;
namespace Spectre.Console
{
public static class XmlElementExtensions
{
public static void SetNullableAttribute(this XmlElement element, string name, string value)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
element.SetAttribute(name, value ?? "NULL");
}
public static void SetNullableAttribute(this XmlElement element, string name, IEnumerable<string> values)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
if (values?.Any() != true)
{
element.SetAttribute(name, "NULL");
}
element.SetAttribute(name, string.Join(",", values));
}
public static void SetBooleanAttribute(this XmlElement element, string name, bool value)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
element.SetAttribute(name, value ? "true" : "false");
}
public static void SetEnumAttribute(this XmlElement element, string name, Enum value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
var field = value.GetType().GetField(value.ToString());
var attribute = field.GetCustomAttribute<DescriptionAttribute>(false);
if (attribute == null)
{
throw new InvalidOperationException("Enum is missing description.");
}
element.SetAttribute(name, attribute.Description);
}
}
}

View File

@ -1,64 +0,0 @@
using System;
using System.IO;
using Spectre.Console.Rendering;
namespace Spectre.Console.Testing
{
public sealed class FakeAnsiConsole : IDisposable, IAnsiConsole
{
private readonly StringWriter _writer;
private readonly IAnsiConsole _console;
private readonly FakeExclusivityMode _exclusivityLock;
public string Output => _writer.ToString();
public Profile Profile => _console.Profile;
public IAnsiConsoleCursor Cursor => _console.Cursor;
public FakeConsoleInput Input { get; }
public IExclusivityMode ExclusivityMode => _exclusivityLock;
public RenderPipeline Pipeline => _console.Pipeline;
IAnsiConsoleInput IAnsiConsole.Input => Input;
public FakeAnsiConsole(
ColorSystem colors,
AnsiSupport ansi = AnsiSupport.Yes,
int width = 80)
{
_exclusivityLock = new FakeExclusivityMode();
_writer = new StringWriter();
var factory = new AnsiConsoleFactory();
_console = factory.Create(new AnsiConsoleSettings
{
Ansi = ansi,
ColorSystem = (ColorSystemSupport)colors,
Out = new AnsiConsoleOutput(_writer),
Enrichment = new ProfileEnrichment
{
UseDefaultEnrichers = false,
},
});
_console.Profile.Width = width;
_console.Profile.Capabilities.Unicode = true;
Input = new FakeConsoleInput();
}
public void Dispose()
{
_writer?.Dispose();
}
public void Clear(bool home)
{
_console.Clear(home);
}
public void Write(IRenderable renderable)
{
_console.Write(renderable);
}
}
}

View File

@ -1,19 +0,0 @@
namespace Spectre.Console.Testing
{
public sealed class FakeCapabilities : IReadOnlyCapabilities
{
public ColorSystem ColorSystem { get; set; } = ColorSystem.TrueColor;
public bool Ansi { get; set; }
public bool Links { get; set; }
public bool Legacy { get; set; }
public bool IsTerminal { get; set; }
public bool Interactive { get; set; }
public bool Unicode { get; set; }
}
}

View File

@ -1,20 +0,0 @@
using System;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
public sealed class FakeCommandInterceptor : ICommandInterceptor
{
private readonly Action<CommandContext, CommandSettings> _action;
public FakeCommandInterceptor(Action<CommandContext, CommandSettings> action)
{
_action = action ?? throw new ArgumentNullException(nameof(action));
}
public void Intercept(CommandContext context, CommandSettings settings)
{
_action(context, settings);
}
}
}

View File

@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Spectre.Console.Rendering;
namespace Spectre.Console.Testing
{
public sealed class FakeConsole : IAnsiConsole, IDisposable
{
public Profile Profile { get; }
public IAnsiConsoleCursor Cursor => new FakeAnsiConsoleCursor();
IAnsiConsoleInput IAnsiConsole.Input => Input;
public IExclusivityMode ExclusivityMode { get; }
public RenderPipeline Pipeline { get; }
public FakeConsoleInput Input { get; }
public string Output => Profile.Out.Writer.ToString();
public IReadOnlyList<string> Lines => Output.TrimEnd('\n').Split(new char[] { '\n' });
public FakeConsole(
int width = 80, int height = 9000, Encoding encoding = null,
bool supportsAnsi = true, ColorSystem colorSystem = ColorSystem.Standard,
bool legacyConsole = false, bool interactive = true)
{
Input = new FakeConsoleInput();
ExclusivityMode = new FakeExclusivityMode();
Pipeline = new RenderPipeline();
Profile = new Profile(new AnsiConsoleOutput(new StringWriter()), encoding ?? Encoding.UTF8);
Profile.Width = width;
Profile.Height = height;
Profile.Capabilities.ColorSystem = colorSystem;
Profile.Capabilities.Ansi = supportsAnsi;
Profile.Capabilities.Legacy = legacyConsole;
Profile.Capabilities.Interactive = interactive;
Profile.Capabilities.Links = true;
Profile.Capabilities.Unicode = true;
}
public void Dispose()
{
Profile.Out.Writer.Dispose();
}
public void Clear(bool home)
{
}
public void Write(IRenderable renderable)
{
foreach (var segment in renderable.GetSegments(this))
{
Profile.Out.Writer.Write(segment.Text);
}
}
public string WriteNormalizedException(Exception ex, ExceptionFormats formats = ExceptionFormats.Default)
{
this.WriteException(ex, formats);
return string.Join("\n", Output.NormalizeStackTrace()
.NormalizeLineEndings()
.Split(new char[] { '\n' })
.Select(line => line.TrimEnd()));
}
}
}

View File

@ -1,55 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Testing
{
public sealed class FakeConsoleInput : IAnsiConsoleInput
{
private readonly Queue<ConsoleKeyInfo> _input;
public FakeConsoleInput()
{
_input = new Queue<ConsoleKeyInfo>();
}
public void PushText(string input)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
foreach (var character in input)
{
PushCharacter(character);
}
}
public void PushTextWithEnter(string input)
{
PushText(input);
PushKey(ConsoleKey.Enter);
}
public void PushCharacter(char character)
{
var control = char.IsUpper(character);
_input.Enqueue(new ConsoleKeyInfo(character, (ConsoleKey)character, false, false, control));
}
public void PushKey(ConsoleKey key)
{
_input.Enqueue(new ConsoleKeyInfo((char)key, key, false, false, false));
}
public ConsoleKeyInfo ReadKey(bool intercept)
{
if (_input.Count == 0)
{
throw new InvalidOperationException("No input available.");
}
return _input.Dequeue();
}
}
}

View File

@ -1,31 +0,0 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
public sealed class FakeTypeResolver : ITypeResolver
{
private readonly IDictionary<Type, object> _lookup;
public FakeTypeResolver()
{
_lookup = new Dictionary<Type, object>();
}
public void Register<T>(T instance)
{
_lookup[typeof(T)] = instance;
}
public object Resolve(Type type)
{
if (_lookup.TryGetValue(type, out var value))
{
return value;
}
return Activator.CreateInstance(type);
}
}
}

View File

@ -1,6 +1,6 @@
namespace Spectre.Console.Testing namespace Spectre.Console.Testing
{ {
public sealed class FakeAnsiConsoleCursor : IAnsiConsoleCursor internal sealed class NoopCursor : IAnsiConsoleCursor
{ {
public void Move(CursorDirection direction, int steps) public void Move(CursorDirection direction, int steps)
{ {

View File

@ -3,7 +3,7 @@ using System.Threading.Tasks;
namespace Spectre.Console.Testing namespace Spectre.Console.Testing
{ {
public sealed class FakeExclusivityMode : IExclusivityMode internal sealed class NoopExclusivityMode : IExclusivityMode
{ {
public T Run<T>(Func<T> func) public T Run<T>(Func<T> func)
{ {

View File

@ -1,18 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks> <TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
<IsTestProject>false</IsTestProject> <IsTestProject>false</IsTestProject>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<Description>Contains testing utilities for Spectre.Console.</Description>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="..\stylecop.json" Link="Properties/stylecop.json" />
<None Include="../../resources/gfx/small-logo.png" Pack="true" PackagePath="\" Link="Properties/small-logo.png" />
</ItemGroup>
<ItemGroup Label="Project References"> <ItemGroup Label="Project References">
<ProjectReference Include="..\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup Label="Package References"> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <Folder Include="Properties\" />
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,5 +0,0 @@
<ProjectConfiguration>
<Settings>
<XUnit2Enabled>False</XUnit2Enabled>
</Settings>
</ProjectConfiguration>

View File

@ -0,0 +1,40 @@
using Spectre.Console.Rendering;
namespace Spectre.Console.Testing
{
/// <summary>
/// Represents fake capabilities useful in tests.
/// </summary>
public sealed class TestCapabilities : IReadOnlyCapabilities
{
/// <inheritdoc/>
public ColorSystem ColorSystem { get; set; } = ColorSystem.TrueColor;
/// <inheritdoc/>
public bool Ansi { get; set; }
/// <inheritdoc/>
public bool Links { get; set; }
/// <inheritdoc/>
public bool Legacy { get; set; }
/// <inheritdoc/>
public bool IsTerminal { get; set; }
/// <inheritdoc/>
public bool Interactive { get; set; }
/// <inheritdoc/>
public bool Unicode { get; set; }
/// <summary>
/// Creates a <see cref="RenderContext"/> with the same capabilities as this instace.
/// </summary>
/// <returns>A <see cref="RenderContext"/> with the same capabilities as this instace.</returns>
public RenderContext CreateRenderContext()
{
return new RenderContext(this);
}
}
}

View File

@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.IO;
using Spectre.Console.Rendering;
namespace Spectre.Console.Testing
{
/// <summary>
/// A testable console.
/// </summary>
public sealed class TestConsole : IAnsiConsole, IDisposable
{
private readonly IAnsiConsole _console;
private readonly StringWriter _writer;
private IAnsiConsoleCursor? _cursor;
/// <inheritdoc/>
public Profile Profile => _console.Profile;
/// <inheritdoc/>
public IExclusivityMode ExclusivityMode => _console.ExclusivityMode;
/// <summary>
/// Gets the console input.
/// </summary>
public TestConsoleInput Input { get; }
/// <inheritdoc/>
public RenderPipeline Pipeline => _console.Pipeline;
/// <inheritdoc/>
public IAnsiConsoleCursor Cursor => _cursor ?? _console.Cursor;
/// <inheritdoc/>
IAnsiConsoleInput IAnsiConsole.Input => Input;
/// <summary>
/// Gets the console output.
/// </summary>
public string Output => _writer.ToString();
/// <summary>
/// Gets the console output lines.
/// </summary>
public IReadOnlyList<string> Lines => Output.NormalizeLineEndings().TrimEnd('\n').Split(new char[] { '\n' });
/// <summary>
/// Gets or sets a value indicating whether or not VT/ANSI sequences
/// should be emitted to the console.
/// </summary>
public bool EmitAnsiSequences { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="TestConsole"/> class.
/// </summary>
public TestConsole()
{
_writer = new StringWriter();
_cursor = new NoopCursor();
Input = new TestConsoleInput();
EmitAnsiSequences = false;
var factory = new AnsiConsoleFactory();
_console = factory.Create(new AnsiConsoleSettings
{
Ansi = AnsiSupport.Yes,
ColorSystem = (ColorSystemSupport)ColorSystem.TrueColor,
Out = new AnsiConsoleOutput(_writer),
Interactive = InteractionSupport.No,
ExclusivityMode = new NoopExclusivityMode(),
Enrichment = new ProfileEnrichment
{
UseDefaultEnrichers = false,
},
});
_console.Profile.Width = 80;
_console.Profile.Height = 24;
_console.Profile.Capabilities.Ansi = true;
_console.Profile.Capabilities.Unicode = true;
}
/// <inheritdoc/>
public void Dispose()
{
_writer.Dispose();
}
/// <inheritdoc/>
public void Clear(bool home)
{
_console.Clear(home);
}
/// <inheritdoc/>
public void Write(IRenderable renderable)
{
if (EmitAnsiSequences)
{
_console.Write(renderable);
}
else
{
foreach (var segment in renderable.GetSegments(this))
{
if (segment.IsControlCode)
{
continue;
}
Profile.Out.Writer.Write(segment.Text);
}
}
}
internal void SetCursor(IAnsiConsoleCursor? cursor)
{
_cursor = cursor;
}
}
}

View File

@ -0,0 +1,55 @@
namespace Spectre.Console.Testing
{
/// <summary>
/// Contains extensions for <see cref="TestConsole"/>.
/// </summary>
public static class TestConsoleExtensions
{
/// <summary>
/// Sets the console's color system.
/// </summary>
/// <param name="console">The console.</param>
/// <param name="colors">The color system to use.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TestConsole Colors(this TestConsole console, ColorSystem colors)
{
console.Profile.Capabilities.ColorSystem = colors;
return console;
}
/// <summary>
/// Makes the console interactive.
/// </summary>
/// <param name="console">The console.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TestConsole Interactive(this TestConsole console)
{
console.Profile.Capabilities.Interactive = true;
return console;
}
/// <summary>
/// Sets the console width.
/// </summary>
/// <param name="console">The console.</param>
/// <param name="width">The console width.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TestConsole Width(this TestConsole console, int width)
{
console.Profile.Width = width;
return console;
}
/// <summary>
/// Turns on emitting of VT/ANSI sequences.
/// </summary>
/// <param name="console">The console.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TestConsole EmitAnsiSequences(this TestConsole console)
{
console.SetCursor(null);
console.EmitAnsiSequences = true;
return console;
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Testing
{
/// <summary>
/// Represents a testable console input mechanism.
/// </summary>
public sealed class TestConsoleInput : IAnsiConsoleInput
{
private readonly Queue<ConsoleKeyInfo> _input;
/// <summary>
/// Initializes a new instance of the <see cref="TestConsoleInput"/> class.
/// </summary>
public TestConsoleInput()
{
_input = new Queue<ConsoleKeyInfo>();
}
/// <summary>
/// Pushes the specified text to the input queue.
/// </summary>
/// <param name="input">The input string.</param>
public void PushText(string input)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
foreach (var character in input)
{
PushCharacter(character);
}
}
/// <summary>
/// Pushes the specified text followed by 'Enter' to the input queue.
/// </summary>
/// <param name="input">The input.</param>
public void PushTextWithEnter(string input)
{
PushText(input);
PushKey(ConsoleKey.Enter);
}
/// <summary>
/// Pushes the specified character to the input queue.
/// </summary>
/// <param name="input">The input.</param>
public void PushCharacter(char input)
{
var control = char.IsUpper(input);
_input.Enqueue(new ConsoleKeyInfo(input, (ConsoleKey)input, false, false, control));
}
/// <summary>
/// Pushes the specified key to the input queue.
/// </summary>
/// <param name="input">The input.</param>
public void PushKey(ConsoleKey input)
{
_input.Enqueue(new ConsoleKeyInfo((char)input, input, false, false, false));
}
/// <inheritdoc/>
public ConsoleKeyInfo ReadKey(bool intercept)
{
if (_input.Count == 0)
{
throw new InvalidOperationException("No input available.");
}
return _input.Dequeue();
}
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Testing
{
public sealed class DummySpinner1 : Spinner
{
public override TimeSpan Interval => TimeSpan.FromMilliseconds(100);
public override bool IsUnicode => true;
public override IReadOnlyList<string> Frames => new List<string> { "*", };
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Testing
{
public sealed class DummySpinner2 : Spinner
{
public override TimeSpan Interval => TimeSpan.FromMilliseconds(100);
public override bool IsUnicode => true;
public override IReadOnlyList<string> Frames => new List<string> { "-", };
}
}

View File

@ -34,7 +34,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Return_Correct_Code(bool foreground, string expected) public void Should_Return_Correct_Code(bool foreground, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.TrueColor); var console = new TestConsole()
.Colors(ColorSystem.TrueColor)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(new Color(128, 0, 128), foreground)); console.Write("Hello", new Style().SetColor(new Color(128, 0, 128), foreground));
@ -49,7 +51,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Return_Eight_Bit_Ansi_Code_For_Known_Colors(bool foreground, string expected) public void Should_Return_Eight_Bit_Ansi_Code_For_Known_Colors(bool foreground, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.TrueColor); var console = new TestConsole()
.Colors(ColorSystem.TrueColor)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(Color.Purple, foreground)); console.Write("Hello", new Style().SetColor(Color.Purple, foreground));
@ -67,7 +71,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Return_Correct_Code_For_Known_Color(bool foreground, string expected) public void Should_Return_Correct_Code_For_Known_Color(bool foreground, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.EightBit); var console = new TestConsole()
.Colors(ColorSystem.EightBit)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(Color.Olive, foreground)); console.Write("Hello", new Style().SetColor(Color.Olive, foreground));
@ -82,7 +88,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Map_TrueColor_To_Nearest_Eight_Bit_Color_If_Possible(bool foreground, string expected) public void Should_Map_TrueColor_To_Nearest_Eight_Bit_Color_If_Possible(bool foreground, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.EightBit); var console = new TestConsole()
.Colors(ColorSystem.EightBit)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(new Color(128, 128, 0), foreground)); console.Write("Hello", new Style().SetColor(new Color(128, 128, 0), foreground));
@ -97,7 +105,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Estimate_TrueColor_To_Nearest_Eight_Bit_Color(bool foreground, string expected) public void Should_Estimate_TrueColor_To_Nearest_Eight_Bit_Color(bool foreground, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.EightBit); var console = new TestConsole()
.Colors(ColorSystem.EightBit)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(new Color(126, 127, 0), foreground)); console.Write("Hello", new Style().SetColor(new Color(126, 127, 0), foreground));
@ -115,7 +125,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Return_Correct_Code_For_Known_Color(bool foreground, string expected) public void Should_Return_Correct_Code_For_Known_Color(bool foreground, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(Color.Olive, foreground)); console.Write("Hello", new Style().SetColor(Color.Olive, foreground));
@ -135,7 +147,9 @@ namespace Spectre.Console.Tests.Unit
string expected) string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground)); console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground));
@ -155,7 +169,9 @@ namespace Spectre.Console.Tests.Unit
string expected) string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground)); console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground));
@ -173,7 +189,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Return_Correct_Code_For_Known_Color(bool foreground, string expected) public void Should_Return_Correct_Code_For_Known_Color(bool foreground, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Legacy); var console = new TestConsole()
.Colors(ColorSystem.Legacy)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(Color.Olive, foreground)); console.Write("Hello", new Style().SetColor(Color.Olive, foreground));
@ -193,7 +211,9 @@ namespace Spectre.Console.Tests.Unit
string expected) string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Legacy); var console = new TestConsole()
.Colors(ColorSystem.Legacy)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground)); console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground));
@ -213,7 +233,9 @@ namespace Spectre.Console.Tests.Unit
string expected) string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Legacy); var console = new TestConsole()
.Colors(ColorSystem.Legacy)
.EmitAnsiSequences();
// When // When
console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground)); console.Write("Hello", new Style().SetColor(new Color(r, g, b), foreground));

View File

@ -18,7 +18,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Return_Correct_Ansi_Code(CursorDirection direction, string expected) public void Should_Return_Correct_Ansi_Code(CursorDirection direction, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole().EmitAnsiSequences();
// When // When
console.Write("Hello"); console.Write("Hello");
@ -36,7 +36,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Return_Correct_Ansi_Code() public void Should_Return_Correct_Ansi_Code()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole().EmitAnsiSequences();
// When // When
console.Write("Hello"); console.Write("Hello");

View File

@ -15,7 +15,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Output_Expected_Ansi_For_Markup(string markup, string expected) public void Should_Output_Expected_Ansi_For_Markup(string markup, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Markup(markup); console.Markup(markup);
@ -28,7 +30,8 @@ namespace Spectre.Console.Tests.Unit
public void Should_Output_Expected_Ansi_For_Link_With_Url_And_Text() public void Should_Output_Expected_Ansi_For_Link_With_Url_And_Text()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole()
.EmitAnsiSequences();
// When // When
console.Markup("[link=https://patriksvensson.se]Click to visit my blog[/]"); console.Markup("[link=https://patriksvensson.se]Click to visit my blog[/]");
@ -41,7 +44,8 @@ namespace Spectre.Console.Tests.Unit
public void Should_Output_Expected_Ansi_For_Link_With_Only_Url() public void Should_Output_Expected_Ansi_For_Link_With_Only_Url()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole()
.EmitAnsiSequences();
// When // When
console.Markup("[link]https://patriksvensson.se[/]"); console.Markup("[link]https://patriksvensson.se[/]");
@ -55,7 +59,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Be_Able_To_Escape_Tags(string markup, string expected) public void Should_Be_Able_To_Escape_Tags(string markup, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Markup(markup); console.Markup(markup);
@ -72,7 +78,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Throw_If_Encounters_Malformed_Tag(string markup, string expected) public void Should_Throw_If_Encounters_Malformed_Tag(string markup, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
var result = Record.Exception(() => console.Markup(markup)); var result = Record.Exception(() => console.Markup(markup));
@ -86,7 +92,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Throw_If_Tags_Are_Unbalanced() public void Should_Throw_If_Tags_Are_Unbalanced()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
var result = Record.Exception(() => console.Markup("[yellow][blue]Hello[/]")); var result = Record.Exception(() => console.Markup("[yellow][blue]Hello[/]"));
@ -100,7 +106,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Throw_If_Encounters_Closing_Tag() public void Should_Throw_If_Encounters_Closing_Tag()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
var result = Record.Exception(() => console.Markup("Hello[/]World")); var result = Record.Exception(() => console.Markup("Hello[/]World"));

View File

@ -19,7 +19,8 @@ namespace Spectre.Console.Tests.Unit
public void Should_Write_Decorated_Text_Correctly(Decoration decoration, string expected) public void Should_Write_Decorated_Text_Correctly(Decoration decoration, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.TrueColor); var console = new TestConsole()
.EmitAnsiSequences();
// When // When
console.Write("Hello World", new Style().Decoration(decoration)); console.Write("Hello World", new Style().Decoration(decoration));
@ -34,7 +35,8 @@ namespace Spectre.Console.Tests.Unit
public void Should_Write_Text_With_Multiple_Decorations_Correctly(Decoration decoration, string expected) public void Should_Write_Text_With_Multiple_Decorations_Correctly(Decoration decoration, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.TrueColor); var console = new TestConsole()
.EmitAnsiSequences();
// When // When
console.Write("Hello World", new Style().Decoration(decoration)); console.Write("Hello World", new Style().Decoration(decoration));

View File

@ -13,7 +13,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Clear_Screen(bool home, string expected) public void Should_Clear_Screen(bool home, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write("Hello"); console.Write("Hello");
@ -28,7 +30,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Combine_Decoration_And_Colors() public void Should_Combine_Decoration_And_Colors()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write( console.Write(
@ -46,7 +50,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Not_Include_Foreground_If_Set_To_Default_Color() public void Should_Not_Include_Foreground_If_Set_To_Default_Color()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write( console.Write(
@ -64,7 +70,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Not_Include_Background_If_Set_To_Default_Color() public void Should_Not_Include_Background_If_Set_To_Default_Color()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write( console.Write(
@ -82,7 +90,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Not_Include_Decoration_If_Set_To_None() public void Should_Not_Include_Decoration_If_Set_To_None()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.Write( console.Write(
@ -102,7 +112,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Reset_Colors_Correctly_After_Line_Break() public void Should_Reset_Colors_Correctly_After_Line_Break()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.WriteLine("Hello", new Style().Background(ConsoleColor.Red)); console.WriteLine("Hello", new Style().Background(ConsoleColor.Red));
@ -117,7 +129,9 @@ namespace Spectre.Console.Tests.Unit
public void Should_Reset_Colors_Correctly_After_Line_Break_In_Text() public void Should_Reset_Colors_Correctly_After_Line_Break_In_Text()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
// When // When
console.WriteLine("Hello\nWorld", new Style().Background(ConsoleColor.Red)); console.WriteLine("Hello\nWorld", new Style().Background(ConsoleColor.Red));

View File

@ -15,7 +15,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correctly() public async Task Should_Render_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new BarChart() console.Write(new BarChart()
@ -34,7 +34,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correctly_2() public async Task Should_Render_Correctly_2()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new BarChart() console.Write(new BarChart()

View File

@ -33,7 +33,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var panel = Fixture.GetPanel().NoBorder(); var panel = Fixture.GetPanel().NoBorder();
// When // When
@ -65,7 +65,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var panel = Fixture.GetPanel().AsciiBorder(); var panel = Fixture.GetPanel().AsciiBorder();
// When // When
@ -97,7 +97,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var panel = Fixture.GetPanel().DoubleBorder(); var panel = Fixture.GetPanel().DoubleBorder();
// When // When
@ -129,7 +129,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var panel = Fixture.GetPanel().HeavyBorder(); var panel = Fixture.GetPanel().HeavyBorder();
// When // When
@ -158,7 +158,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var panel = Fixture.GetPanel().RoundedBorder(); var panel = Fixture.GetPanel().RoundedBorder();
// When // When
@ -187,7 +187,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var panel = Fixture.GetPanel().SquareBorder(); var panel = Fixture.GetPanel().SquareBorder();
// When // When

View File

@ -15,7 +15,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correctly() public async Task Should_Render_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var chart = Fixture.GetChart(); var chart = Fixture.GetChart();
// When // When
@ -30,7 +30,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_With_Specific_Width() public async Task Should_Render_With_Specific_Width()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var chart = Fixture.GetChart().Width(60); var chart = Fixture.GetChart().Width(60);
// When // When
@ -45,7 +45,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correctly_With_Specific_Value_Formatter() public async Task Should_Render_Correctly_With_Specific_Value_Formatter()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var chart = Fixture.GetChart() var chart = Fixture.GetChart()
.Width(60) .Width(60)
.Culture("sv-SE") .Culture("sv-SE")
@ -63,7 +63,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correctly_Without_Tags() public async Task Should_Render_Correctly_Without_Tags()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var chart = Fixture.GetChart().Width(60).HideTags(); var chart = Fixture.GetChart().Width(60).HideTags();
// When // When
@ -78,7 +78,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correctly_Without_Tag_Values() public async Task Should_Render_Correctly_Without_Tag_Values()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var chart = Fixture.GetChart().Width(60).HideTagValues(); var chart = Fixture.GetChart().Width(60).HideTagValues();
// When // When
@ -93,7 +93,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correctly_With_Specific_Culture() public async Task Should_Render_Correctly_With_Specific_Culture()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var chart = Fixture.GetChart().Width(60).Culture("sv-SE"); var chart = Fixture.GetChart().Width(60).Culture("sv-SE");
// When // When
@ -108,7 +108,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_FullSize_Mode_Correctly() public async Task Should_Render_FullSize_Mode_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var chart = Fixture.GetChart().Width(60).FullSize(); var chart = Fixture.GetChart().Width(60).FullSize();
// When // When
@ -123,7 +123,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Correct_Ansi() public async Task Should_Render_Correct_Ansi()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.EightBit, width: 80); var console = new TestConsole().EmitAnsiSequences();
var chart = Fixture.GetChart().Width(60).FullSize(); var chart = Fixture.GetChart().Width(60).FullSize();
// When // When

View File

@ -16,7 +16,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Calendar_Correctly() public Task Should_Render_Calendar_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var calendar = new Calendar(2020, 10) var calendar = new Calendar(2020, 10)
.AddCalendarEvent(new DateTime(2020, 9, 1)) .AddCalendarEvent(new DateTime(2020, 9, 1))
.AddCalendarEvent(new DateTime(2020, 10, 3)) .AddCalendarEvent(new DateTime(2020, 10, 3))
@ -34,7 +34,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Center_Calendar_Correctly() public Task Should_Center_Calendar_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var calendar = new Calendar(2020, 10) var calendar = new Calendar(2020, 10)
.Centered() .Centered()
.AddCalendarEvent(new DateTime(2020, 9, 1)) .AddCalendarEvent(new DateTime(2020, 9, 1))
@ -53,7 +53,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Left_Align_Calendar_Correctly() public Task Should_Left_Align_Calendar_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var calendar = new Calendar(2020, 10) var calendar = new Calendar(2020, 10)
.LeftAligned() .LeftAligned()
.AddCalendarEvent(new DateTime(2020, 9, 1)) .AddCalendarEvent(new DateTime(2020, 9, 1))
@ -72,7 +72,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Right_Align_Calendar_Correctly() public Task Should_Right_Align_Calendar_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var calendar = new Calendar(2020, 10) var calendar = new Calendar(2020, 10)
.RightAligned() .RightAligned()
.AddCalendarEvent(new DateTime(2020, 9, 1)) .AddCalendarEvent(new DateTime(2020, 9, 1))
@ -91,7 +91,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Calendar_Correctly_For_Specific_Culture() public Task Should_Render_Calendar_Correctly_For_Specific_Culture()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var calendar = new Calendar(2020, 10, 15) var calendar = new Calendar(2020, 10, 15)
.Culture("de-DE") .Culture("de-DE")
.AddCalendarEvent(new DateTime(2020, 9, 1)) .AddCalendarEvent(new DateTime(2020, 9, 1))

View File

@ -42,7 +42,10 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Canvas_Correctly() public async Task Should_Render_Canvas_Correctly()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
var canvas = new Canvas(width: 5, height: 5); var canvas = new Canvas(width: 5, height: 5);
canvas.SetPixel(0, 0, Color.Red); canvas.SetPixel(0, 0, Color.Red);
canvas.SetPixel(4, 0, Color.Green); canvas.SetPixel(4, 0, Color.Green);
@ -61,7 +64,10 @@ namespace Spectre.Console.Tests.Unit
public async Task Simple_Measure() public async Task Simple_Measure()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
var panel = new Panel(new Canvas(width: 2, height: 2) var panel = new Panel(new Canvas(width: 2, height: 2)
.SetPixel(0, 0, Color.Aqua) .SetPixel(0, 0, Color.Aqua)
.SetPixel(1, 1, Color.Grey)); .SetPixel(1, 1, Color.Grey));
@ -78,7 +84,11 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Scale_Down_Canvas_Is_Bigger_Than_Terminal() public async Task Should_Scale_Down_Canvas_Is_Bigger_Than_Terminal()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, width: 10); var console = new TestConsole()
.Width(10)
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
var canvas = new Canvas(width: 20, height: 10); var canvas = new Canvas(width: 20, height: 10);
canvas.SetPixel(0, 0, Color.Aqua); canvas.SetPixel(0, 0, Color.Aqua);
canvas.SetPixel(19, 9, Color.Grey); canvas.SetPixel(19, 9, Color.Grey);
@ -95,7 +105,10 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Scale_Down_Canvas_If_MaxWidth_Is_Set() public async Task Should_Scale_Down_Canvas_If_MaxWidth_Is_Set()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, width: 80); var console = new TestConsole()
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
var canvas = new Canvas(width: 20, height: 10) { MaxWidth = 10 }; var canvas = new Canvas(width: 20, height: 10) { MaxWidth = 10 };
canvas.SetPixel(0, 0, Color.Aqua); canvas.SetPixel(0, 0, Color.Aqua);
canvas.SetPixel(19, 9, Color.Aqua); canvas.SetPixel(19, 9, Color.Aqua);
@ -111,7 +124,11 @@ namespace Spectre.Console.Tests.Unit
public void Should_Not_Render_Canvas_If_Canvas_Cannot_Be_Scaled_Down() public void Should_Not_Render_Canvas_If_Canvas_Cannot_Be_Scaled_Down()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, width: 10); var console = new TestConsole()
.Width(10)
.Colors(ColorSystem.Standard)
.EmitAnsiSequences();
var canvas = new Canvas(width: 20, height: 2); var canvas = new Canvas(width: 20, height: 2);
canvas.SetPixel(0, 0, Color.Aqua); canvas.SetPixel(0, 0, Color.Aqua);
canvas.SetPixel(19, 1, Color.Grey); canvas.SetPixel(19, 1, Color.Grey);

View File

@ -79,7 +79,7 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public static string Run<TSettings>(params string[] args) public static string Run<TSettings>(params string[] args)
where TSettings : CommandSettings where TSettings : CommandSettings
{ {
using (var writer = new FakeConsole()) using (var writer = new TestConsole())
{ {
var app = new CommandApp(); var app = new CommandApp();
app.Configure(c => c.ConfigureConsole(writer)); app.Configure(c => c.ConfigureConsole(writer));

View File

@ -26,11 +26,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Encountered unexpected character '$'."); result.Exception.Message.ShouldBe("Encountered unexpected character '$'.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -48,11 +48,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Encountered unterminated value name 'BAR'."); result.Exception.Message.ShouldBe("Encountered unterminated value name 'BAR'.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -70,11 +70,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Options without name are not allowed."); result.Exception.Message.ShouldBe("Options without name are not allowed.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -92,11 +92,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Option names cannot start with a digit."); result.Exception.Message.ShouldBe("Option names cannot start with a digit.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -114,11 +114,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Encountered invalid character '$' in option name."); result.Exception.Message.ShouldBe("Encountered invalid character '$' in option name.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -136,11 +136,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Long option names must consist of more than one character."); result.Exception.Message.ShouldBe("Long option names must consist of more than one character.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -158,11 +158,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Short option names can not be longer than one character."); result.Exception.Message.ShouldBe("Short option names can not be longer than one character.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -180,11 +180,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Multiple option values are not supported."); result.Exception.Message.ShouldBe("Multiple option values are not supported.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -202,11 +202,11 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("Encountered invalid character '$' in value name."); result.Exception.Message.ShouldBe("Encountered invalid character '$' in value name.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
@ -224,23 +224,22 @@ namespace Spectre.Console.Tests.Unit.Cli.Annotations
public Task Should_Return_Correct_Text() public Task Should_Return_Correct_Text()
{ {
// Given, When // Given, When
var (message, result) = Fixture.Run<Settings>(); var result = Fixture.Run<Settings>();
// Then // Then
message.ShouldBe("No long or short name for option has been specified."); result.Exception.Message.ShouldBe("No long or short name for option has been specified.");
return Verifier.Verify(result); return Verifier.Verify(result.Output);
} }
} }
private static class Fixture private static class Fixture
{ {
public static (string Message, string Output) Run<TSettings>(params string[] args) public static CommandAppFailure Run<TSettings>(params string[] args)
where TSettings : CommandSettings where TSettings : CommandSettings
{ {
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(c => app.Configure(c =>
{ {
c.PropagateExceptions();
c.AddCommand<GenericCommand<TSettings>>("foo"); c.AddCommand<GenericCommand<TSettings>>("foo");
}); });

View File

@ -1,4 +1,4 @@
using Shouldly; using Shouldly;
using Spectre.Console.Cli; using Spectre.Console.Cli;
using Spectre.Console.Testing; using Spectre.Console.Testing;
using Xunit; using Xunit;
@ -42,10 +42,10 @@ namespace Spectre.Console.Tests.Unit.Cli
} }
[Fact] [Fact]
public void Nullable_objects_in_settings_are_populated_properly() public void Should_Populate_Nullable_Objects_In_Settings()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -53,19 +53,21 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, _, _, settings) = fixture.Run("null"); var result = fixture.Run("null");
var nullableSettings = (NullableSettings)settings;
// Then // Then
nullableSettings.Detailed.ShouldBeNull(); result.Settings.ShouldBeOfType<NullableSettings>().And(settings =>
nullableSettings.Extra.ShouldBeNull(); {
settings.Detailed.ShouldBeNull();
settings.Extra.ShouldBeNull();
});
} }
[Fact] [Fact]
public void Nullable_objects_with_init_in_settings_are_populated_properly() public void Should_Populate_Nullable_Objects_With_Init_In_Settings()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -73,19 +75,21 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, _, _, settings) = fixture.Run("null"); var result = fixture.Run("null");
var nullableSettings = (NullableWithInitSettings)settings;
// Then // Then
nullableSettings.Detailed.ShouldBeNull(); result.Settings.ShouldBeOfType<NullableWithInitSettings>().And(settings =>
nullableSettings.Extra.ShouldBeNull(); {
settings.Detailed.ShouldBeNull();
settings.Extra.ShouldBeNull();
});
} }
[Fact] [Fact]
public void Regular_settings_are_populated_properly() public void Should_Populate_Regular_Settings()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -93,12 +97,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, _, _, settings) = fixture.Run("null", "-d", "true", "first-item"); var result = fixture.Run("null", "-d", "true", "first-item");
var nullableSettings = (NullableSettings)settings;
// Then // Then
nullableSettings.Detailed.ShouldBe(true); result.Settings.ShouldBeOfType<NullableSettings>().And(settings =>
nullableSettings.Extra.ShouldBe(new[] { "first-item" }); {
settings.Detailed.ShouldBe(true);
settings.Extra.ShouldBe(new[] { "first-item" });
});
} }
} }
} }

View File

@ -66,7 +66,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Set_Flag_And_Value_If_Both_Were_Provided() public void Should_Set_Flag_And_Value_If_Both_Were_Provided()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -74,14 +74,11 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[] { "foo", "--serve", "123", });
{
"foo", "--serve", "123",
});
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<FlagSettings>().And(flag => result.Settings.ShouldBeOfType<FlagSettings>().And(flag =>
{ {
flag.Serve.IsSet.ShouldBeTrue(); flag.Serve.IsSet.ShouldBeTrue();
flag.Serve.Value.ShouldBe(123); flag.Serve.Value.ShouldBe(123);
@ -92,7 +89,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Only_Set_Flag_If_No_Value_Was_Provided() public void Should_Only_Set_Flag_If_No_Value_Was_Provided()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -100,14 +97,11 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[] { "foo", "--serve" });
{
"foo", "--serve",
});
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<FlagSettings>().And(flag => result.Settings.ShouldBeOfType<FlagSettings>().And(flag =>
{ {
flag.Serve.IsSet.ShouldBeTrue(); flag.Serve.IsSet.ShouldBeTrue();
flag.Serve.Value.ShouldBe(0); flag.Serve.Value.ShouldBe(0);
@ -118,7 +112,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Set_Value_To_Default_Value_If_None_Was_Explicitly_Set() public void Should_Set_Value_To_Default_Value_If_None_Was_Explicitly_Set()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -126,14 +120,11 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[] { "foo", "--serve" });
{
"foo", "--serve",
});
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<FlagSettingsWithDefaultValue>().And(flag => result.Settings.ShouldBeOfType<FlagSettingsWithDefaultValue>().And(flag =>
{ {
flag.Serve.IsSet.ShouldBeTrue(); flag.Serve.IsSet.ShouldBeTrue();
flag.Serve.Value.ShouldBe(987); flag.Serve.Value.ShouldBe(987);
@ -144,7 +135,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Create_Unset_Instance_If_Flag_Was_Not_Set() public void Should_Create_Unset_Instance_If_Flag_Was_Not_Set()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -152,14 +143,11 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[] { "foo" });
{
"foo",
});
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<FlagSettings>().And(flag => result.Settings.ShouldBeOfType<FlagSettings>().And(flag =>
{ {
flag.Serve.IsSet.ShouldBeFalse(); flag.Serve.IsSet.ShouldBeFalse();
flag.Serve.Value.ShouldBe(0); flag.Serve.Value.ShouldBe(0);
@ -170,7 +158,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Create_Unset_Instance_With_Null_For_Nullable_Value_Type_If_Flag_Was_Not_Set() public void Should_Create_Unset_Instance_With_Null_For_Nullable_Value_Type_If_Flag_Was_Not_Set()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -178,14 +166,11 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[] { "foo" });
{
"foo",
});
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<FlagSettingsWithNullableValueType>().And(flag => result.Settings.ShouldBeOfType<FlagSettingsWithNullableValueType>().And(flag =>
{ {
flag.Serve.IsSet.ShouldBeFalse(); flag.Serve.IsSet.ShouldBeFalse();
flag.Serve.Value.ShouldBeNull(); flag.Serve.Value.ShouldBeNull();
@ -201,9 +186,11 @@ namespace Spectre.Console.Tests.Unit.Cli
string expected) string expected)
{ {
// Given // Given
var flag = new FlagValue<string>(); var flag = new FlagValue<string>
flag.Value = value; {
flag.IsSet = isSet; Value = value,
IsSet = isSet,
};
// When // When
var result = flag.ToString(); var result = flag.ToString();
@ -220,8 +207,10 @@ namespace Spectre.Console.Tests.Unit.Cli
string expected) string expected)
{ {
// Given // Given
var flag = new FlagValue<string>(); var flag = new FlagValue<string>
flag.IsSet = isSet; {
IsSet = isSet,
};
// When // When
var result = flag.ToString(); var result = flag.ToString();

View File

@ -19,7 +19,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Root_Correctly() public Task Should_Output_Root_Correctly()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -29,10 +29,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -40,7 +40,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Skip_Hidden_Commands() public Task Should_Skip_Hidden_Commands()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -52,10 +52,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -63,7 +63,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Command_Correctly() public Task Should_Output_Command_Correctly()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -75,10 +75,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("cat", "--help"); var result = fixture.Run("cat", "--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -86,7 +86,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Leaf_Correctly() public Task Should_Output_Leaf_Correctly()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -98,10 +98,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("cat", "lion", "--help"); var result = fixture.Run("cat", "lion", "--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -109,18 +109,18 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Default_Command_Correctly() public Task Should_Output_Default_Command_Correctly()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.WithDefaultCommand<LionCommand>(); fixture.SetDefaultCommand<LionCommand>();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -128,7 +128,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Root_Examples_Defined_On_Root() public Task Should_Output_Root_Examples_Defined_On_Root()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -139,10 +139,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -150,7 +150,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Root_Examples_Defined_On_Direct_Children_If_Root_Have_No_Examples() public Task Should_Output_Root_Examples_Defined_On_Direct_Children_If_Root_Have_No_Examples()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -161,10 +161,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -172,7 +172,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Root_Examples_Defined_On_Leaves_If_No_Other_Examples_Are_Found() public Task Should_Output_Root_Examples_Defined_On_Leaves_If_No_Other_Examples_Are_Found()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -187,10 +187,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -198,7 +198,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Only_Output_Command_Examples_Defined_On_Command() public Task Should_Only_Output_Command_Examples_Defined_On_Command()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -215,10 +215,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("animal", "--help"); var result = fixture.Run("animal", "--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -226,8 +226,8 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Output_Root_Examples_If_Default_Command_Is_Specified() public Task Should_Output_Root_Examples_If_Default_Command_Is_Specified()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.WithDefaultCommand<LionCommand>(); fixture.SetDefaultCommand<LionCommand>();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -235,10 +235,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -246,7 +246,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Not_Show_Truncated_Command_Table_If_Commands_Are_Missing_Description() public Task Should_Not_Show_Truncated_Command_Table_If_Commands_Are_Missing_Description()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
@ -254,10 +254,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -265,18 +265,18 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_List_Arguments_In_Correct_Order() public Task Should_List_Arguments_In_Correct_Order()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.WithDefaultCommand<GenericCommand<ArgumentOrderSettings>>(); fixture.SetDefaultCommand<GenericCommand<ArgumentOrderSettings>>();
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
}); });
// When // When
var (_, output, _, _) = fixture.Run("--help"); var result = fixture.Run("--help");
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
} }
} }

View File

@ -35,10 +35,10 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Inject_Parameters() public void Should_Inject_Parameters()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
var dependency = new FakeDependency(); var dependency = new FakeDependency();
app.WithDefaultCommand<GenericCommand<InjectSettings>>(); app.SetDefaultCommand<GenericCommand<InjectSettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.Settings.Registrar.RegisterInstance(dependency); config.Settings.Registrar.RegisterInstance(dependency);
@ -46,15 +46,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"--name", "foo", "--name", "foo",
"--age", "35", "--age", "35",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<InjectSettings>().And(injected => result.Settings.ShouldBeOfType<InjectSettings>().And(injected =>
{ {
injected.ShouldNotBeNull(); injected.ShouldNotBeNull();
injected.Fake.ShouldBeSameAs(dependency); injected.Fake.ShouldBeSameAs(dependency);

View File

@ -127,15 +127,15 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Map_Pairs_To_Pair_Deconstructable_Collection_Using_Default_Deconstructort() public void Should_Map_Pairs_To_Pair_Deconstructable_Collection_Using_Default_Deconstructort()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<DefaultPairDeconstructorSettings>>(); app.SetDefaultCommand<GenericCommand<DefaultPairDeconstructorSettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"--var", "foo=1", "--var", "foo=1",
"--var", "foo=3", "--var", "foo=3",
@ -143,8 +143,8 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DefaultPairDeconstructorSettings>().And(pair => result.Settings.ShouldBeOfType<DefaultPairDeconstructorSettings>().And(pair =>
{ {
pair.Values.ShouldNotBeNull(); pair.Values.ShouldNotBeNull();
pair.Values.Count.ShouldBe(2); pair.Values.Count.ShouldBe(2);
@ -160,41 +160,41 @@ namespace Spectre.Console.Tests.Unit.Cli
string input, string expected) string input, string expected)
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<DefaultPairDeconstructorSettings>>(); app.SetDefaultCommand<GenericCommand<DefaultPairDeconstructorSettings>>();
// When // When
var (result, output, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"--var", input, "--var", input,
}); });
// Then // Then
result.ShouldBe(-1); result.ExitCode.ShouldBe(-1);
output.ShouldBe(expected); result.Output.ShouldBe(expected);
} }
[Fact] [Fact]
public void Should_Map_Lookup_Values() public void Should_Map_Lookup_Values()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<LookupSettings>>(); app.SetDefaultCommand<GenericCommand<LookupSettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"--var", "foo=bar", "--var", "foo=bar",
"--var", "foo=qux", "--var", "foo=qux",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<LookupSettings>().And(pair => result.Settings.ShouldBeOfType<LookupSettings>().And(pair =>
{ {
pair.Values.ShouldNotBeNull(); pair.Values.ShouldNotBeNull();
pair.Values.Count.ShouldBe(1); pair.Values.Count.ShouldBe(1);
@ -206,23 +206,23 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Map_Dictionary_Values() public void Should_Map_Dictionary_Values()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<DictionarySettings>>(); app.SetDefaultCommand<GenericCommand<DictionarySettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"--var", "foo=bar", "--var", "foo=bar",
"--var", "baz=qux", "--var", "baz=qux",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DictionarySettings>().And(pair => result.Settings.ShouldBeOfType<DictionarySettings>().And(pair =>
{ {
pair.Values.ShouldNotBeNull(); pair.Values.ShouldNotBeNull();
pair.Values.Count.ShouldBe(2); pair.Values.Count.ShouldBe(2);
@ -235,23 +235,23 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Map_Latest_Value_Of_Same_Key_When_Mapping_To_Dictionary() public void Should_Map_Latest_Value_Of_Same_Key_When_Mapping_To_Dictionary()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<DictionarySettings>>(); app.SetDefaultCommand<GenericCommand<DictionarySettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"--var", "foo=bar", "--var", "foo=bar",
"--var", "foo=qux", "--var", "foo=qux",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DictionarySettings>().And(pair => result.Settings.ShouldBeOfType<DictionarySettings>().And(pair =>
{ {
pair.Values.ShouldNotBeNull(); pair.Values.ShouldNotBeNull();
pair.Values.Count.ShouldBe(1); pair.Values.Count.ShouldBe(1);
@ -263,23 +263,23 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Map_ReadOnly_Dictionary_Values() public void Should_Map_ReadOnly_Dictionary_Values()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<ReadOnlyDictionarySettings>>(); app.SetDefaultCommand<GenericCommand<ReadOnlyDictionarySettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"--var", "foo=bar", "--var", "foo=bar",
"--var", "baz=qux", "--var", "baz=qux",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<ReadOnlyDictionarySettings>().And(pair => result.Settings.ShouldBeOfType<ReadOnlyDictionarySettings>().And(pair =>
{ {
pair.Values.ShouldNotBeNull(); pair.Values.ShouldNotBeNull();
pair.Values.Count.ShouldBe(2); pair.Values.Count.ShouldBe(2);

View File

@ -657,7 +657,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public string Run(params string[] args) public string Run(params string[] args)
{ {
using (var console = new FakeConsole()) using (var console = new TestConsole())
{ {
var app = new CommandApp(); var app = new CommandApp();
_appConfiguration?.Invoke(app); _appConfiguration?.Invoke(app);

View File

@ -14,7 +14,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Register_Remaining_Parsed_Arguments_With_Context() public void Should_Register_Remaining_Parsed_Arguments_With_Context()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -25,7 +25,7 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "--", "animal", "4", "dog", "12", "--",
"--foo", "bar", "--foo", "baz", "--foo", "bar", "--foo", "baz",
@ -34,18 +34,18 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// Then // Then
ctx.Remaining.Parsed.Count.ShouldBe(4); result.Context.Remaining.Parsed.Count.ShouldBe(4);
ctx.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" }); result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
ctx.ShouldHaveRemainingArgument("b", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
ctx.ShouldHaveRemainingArgument("a", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
ctx.ShouldHaveRemainingArgument("r", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
} }
[Fact] [Fact]
public void Should_Register_Remaining_Raw_Arguments_With_Context() public void Should_Register_Remaining_Raw_Arguments_With_Context()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -56,7 +56,7 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "--", "animal", "4", "dog", "12", "--",
"--foo", "bar", "-bar", "\"baz\"", "qux", "--foo", "bar", "-bar", "\"baz\"", "qux",
@ -64,13 +64,13 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// Then // Then
ctx.Remaining.Raw.Count.ShouldBe(6); result.Context.Remaining.Raw.Count.ShouldBe(6);
ctx.Remaining.Raw[0].ShouldBe("--foo"); result.Context.Remaining.Raw[0].ShouldBe("--foo");
ctx.Remaining.Raw[1].ShouldBe("bar"); result.Context.Remaining.Raw[1].ShouldBe("bar");
ctx.Remaining.Raw[2].ShouldBe("-bar"); result.Context.Remaining.Raw[2].ShouldBe("-bar");
ctx.Remaining.Raw[3].ShouldBe("baz"); result.Context.Remaining.Raw[3].ShouldBe("baz");
ctx.Remaining.Raw[4].ShouldBe("qux"); result.Context.Remaining.Raw[4].ShouldBe("qux");
ctx.Remaining.Raw[5].ShouldBe("foo bar baz qux"); result.Context.Remaining.Raw[5].ShouldBe("foo bar baz qux");
} }
} }
} }

View File

@ -12,7 +12,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Treat_Commands_As_Case_Sensitive_If_Specified() public void Should_Treat_Commands_As_Case_Sensitive_If_Specified()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandApp();
app.Configure(config => app.Configure(config =>
{ {
config.UseStrictParsing(); config.UseStrictParsing();
@ -39,7 +39,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Treat_Long_Options_As_Case_Sensitive_If_Specified() public void Should_Treat_Long_Options_As_Case_Sensitive_If_Specified()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandApp();
app.Configure(config => app.Configure(config =>
{ {
config.UseStrictParsing(); config.UseStrictParsing();
@ -66,7 +66,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Treat_Short_Options_As_Case_Sensitive() public void Should_Treat_Short_Options_As_Case_Sensitive()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandApp();
app.Configure(config => app.Configure(config =>
{ {
config.UseStrictParsing(); config.UseStrictParsing();
@ -92,7 +92,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Suppress_Case_Sensitivity_If_Specified() public void Should_Suppress_Case_Sensitivity_If_Specified()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.UseStrictParsing(); config.UseStrictParsing();
@ -102,14 +102,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"Command", "--Foo", "bar", "Command", "--Foo", "bar",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<StringOptionSettings>().And(vec => result.Settings.ShouldBeOfType<StringOptionSettings>().And(vec =>
{ {
vec.Foo.ShouldBe("bar"); vec.Foo.ShouldBe("bar");
}); });

View File

@ -14,7 +14,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Bind_Using_Custom_Type_Converter_If_Specified() public void Should_Bind_Using_Custom_Type_Converter_If_Specified()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -22,15 +22,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"cat", "--name", "Tiger", "cat", "--name", "Tiger",
"--agility", "FOOBAR", "--agility", "FOOBAR",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<CatSettings>().And(cat => result.Settings.ShouldBeOfType<CatSettings>().And(cat =>
{ {
cat.Agility.ShouldBe(6); cat.Agility.ShouldBe(6);
}); });

View File

@ -15,7 +15,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Can_Mix_Safe_And_Unsafe_Configurators() public void Can_Mix_Safe_And_Unsafe_Configurators()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -31,15 +31,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "--alive", "mammal", "--name", "animal", "--alive", "mammal", "--name",
"Rufus", "dog", "12", "--good-boy", "Rufus", "dog", "12", "--good-boy",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
dog.GoodBoy.ShouldBe(true); dog.GoodBoy.ShouldBe(true);
@ -52,7 +52,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Can_Turn_Safety_On_After_Turning_It_Off_For_Branch() public void Can_Turn_Safety_On_After_Turning_It_Off_For_Branch()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -69,15 +69,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "--alive", "mammal", "--name", "animal", "--alive", "mammal", "--name",
"Rufus", "dog", "12", "--good-boy", "Rufus", "dog", "12", "--good-boy",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
dog.GoodBoy.ShouldBe(true); dog.GoodBoy.ShouldBe(true);
@ -112,7 +112,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_1() public void Should_Pass_Case_1()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -128,15 +128,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "--alive", "mammal", "--name", "animal", "--alive", "mammal", "--name",
"Rufus", "dog", "12", "--good-boy", "Rufus", "dog", "12", "--good-boy",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
dog.GoodBoy.ShouldBe(true); dog.GoodBoy.ShouldBe(true);
@ -149,7 +149,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_2() public void Should_Pass_Case_2()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -157,15 +157,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"dog", "12", "4", "--good-boy", "dog", "12", "4", "--good-boy",
"--name", "Rufus", "--alive", "--name", "Rufus", "--alive",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Legs.ShouldBe(12); dog.Legs.ShouldBe(12);
dog.Age.ShouldBe(4); dog.Age.ShouldBe(4);
@ -179,7 +179,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_3() public void Should_Pass_Case_3()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -191,15 +191,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "dog", "12", "--good-boy", "animal", "dog", "12", "--good-boy",
"--name", "Rufus", "--name", "Rufus",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
dog.GoodBoy.ShouldBe(true); dog.GoodBoy.ShouldBe(true);
@ -212,7 +212,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_4() public void Should_Pass_Case_4()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -223,15 +223,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "animal", "4", "dog", "12",
"--good-boy", "--name", "Rufus", "--good-boy", "--name", "Rufus",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Legs.ShouldBe(4); dog.Legs.ShouldBe(4);
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
@ -245,7 +245,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_5() public void Should_Pass_Case_5()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -253,14 +253,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"multi", "--foo", "a", "--foo", "b", "--bar", "1", "--foo", "c", "--bar", "2", "multi", "--foo", "a", "--foo", "b", "--bar", "1", "--foo", "c", "--bar", "2",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<OptionVectorSettings>().And(vec => result.Settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
{ {
vec.Foo.Length.ShouldBe(3); vec.Foo.Length.ShouldBe(3);
vec.Foo.ShouldBe(new[] { "a", "b", "c" }); vec.Foo.ShouldBe(new[] { "a", "b", "c" });
@ -273,7 +273,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_6() public void Should_Pass_Case_6()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -281,14 +281,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"multi", "a", "b", "c", "multi", "a", "b", "c",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec => result.Settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec =>
{ {
vec.Foo.Length.ShouldBe(3); vec.Foo.Length.ShouldBe(3);
vec.Foo.ShouldBe(new[] { "a", "b", "c" }); vec.Foo.ShouldBe(new[] { "a", "b", "c" });

View File

@ -56,7 +56,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Assign_Values_To_Argument_Vector() public void Should_Assign_Values_To_Argument_Vector()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -64,14 +64,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"multi", "a", "b", "c", "multi", "a", "b", "c",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec => result.Settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec =>
{ {
vec.Foo.Length.ShouldBe(3); vec.Foo.Length.ShouldBe(3);
vec.Foo[0].ShouldBe("a"); vec.Foo[0].ShouldBe("a");
@ -84,7 +84,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Assign_Values_To_Option_Vector() public void Should_Assign_Values_To_Option_Vector()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -92,15 +92,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"cmd", "--foo", "red", "cmd", "--foo", "red",
"--bar", "4", "--foo", "blue", "--bar", "4", "--foo", "blue",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<OptionVectorSettings>().And(vec => result.Settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
{ {
vec.Foo.ShouldBe(new string[] { "red", "blue" }); vec.Foo.ShouldBe(new string[] { "red", "blue" });
vec.Bar.ShouldBe(new int[] { 4 }); vec.Bar.ShouldBe(new int[] { 4 });

View File

@ -13,7 +13,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Output_The_Version_To_The_Console() public void Should_Output_The_Version_To_The_Console()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(config => fixture.Configure(config =>
{ {
config.AddBranch<AnimalSettings>("animal", animal => config.AddBranch<AnimalSettings>("animal", animal =>
@ -27,10 +27,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run(Constants.VersionCommand); var result = fixture.Run(Constants.VersionCommand);
// Then // Then
output.ShouldStartWith("Spectre.Cli version "); result.Output.ShouldStartWith("Spectre.Cli version ");
} }
} }
} }

View File

@ -19,7 +19,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Dump_Correct_Model_For_Case_1() public Task Should_Dump_Correct_Model_For_Case_1()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(config => fixture.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -34,10 +34,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand); var result = fixture.Run(Constants.XmlDocCommand);
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -45,17 +45,17 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Dump_Correct_Model_For_Case_2() public Task Should_Dump_Correct_Model_For_Case_2()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(config => fixture.Configure(config =>
{ {
config.AddCommand<DogCommand>("dog"); config.AddCommand<DogCommand>("dog");
}); });
// When // When
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand); var result = fixture.Run(Constants.XmlDocCommand);
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -63,7 +63,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Dump_Correct_Model_For_Case_3() public Task Should_Dump_Correct_Model_For_Case_3()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(config => fixture.Configure(config =>
{ {
config.AddBranch<AnimalSettings>("animal", animal => config.AddBranch<AnimalSettings>("animal", animal =>
@ -74,10 +74,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand); var result = fixture.Run(Constants.XmlDocCommand);
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -85,7 +85,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Dump_Correct_Model_For_Case_4() public Task Should_Dump_Correct_Model_For_Case_4()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(config => fixture.Configure(config =>
{ {
config.AddBranch<AnimalSettings>("animal", animal => config.AddBranch<AnimalSettings>("animal", animal =>
@ -95,10 +95,10 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand); var result = fixture.Run(Constants.XmlDocCommand);
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -106,17 +106,17 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Dump_Correct_Model_For_Case_5() public Task Should_Dump_Correct_Model_For_Case_5()
{ {
// Given // Given
var fixture = new CommandAppFixture(); var fixture = new CommandAppTester();
fixture.Configure(config => fixture.Configure(config =>
{ {
config.AddCommand<OptionVectorCommand>("cmd"); config.AddCommand<OptionVectorCommand>("cmd");
}); });
// When // When
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand); var result = fixture.Run(Constants.XmlDocCommand);
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
[Fact] [Fact]
@ -124,17 +124,18 @@ namespace Spectre.Console.Tests.Unit.Cli
public Task Should_Dump_Correct_Model_For_Model_With_Default_Command() public Task Should_Dump_Correct_Model_For_Model_With_Default_Command()
{ {
// Given // Given
var fixture = new CommandAppFixture().WithDefaultCommand<DogCommand>(); var fixture = new CommandAppTester();
fixture.SetDefaultCommand<DogCommand>();
fixture.Configure(config => fixture.Configure(config =>
{ {
config.AddCommand<HorseCommand>("horse"); config.AddCommand<HorseCommand>("horse");
}); });
// When // When
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand); var result = fixture.Run(Constants.XmlDocCommand);
// Then // Then
return Verifier.Verify(output); return Verifier.Verify(result.Output);
} }
} }
} }

View File

@ -13,7 +13,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_1() public void Should_Pass_Case_1()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -28,15 +28,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "--alive", "mammal", "--name", "animal", "--alive", "mammal", "--name",
"Rufus", "dog", "12", "--good-boy", "Rufus", "dog", "12", "--good-boy",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
dog.GoodBoy.ShouldBe(true); dog.GoodBoy.ShouldBe(true);
@ -49,7 +49,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_2() public void Should_Pass_Case_2()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -57,15 +57,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"dog", "12", "4", "--good-boy", "dog", "12", "4", "--good-boy",
"--name", "Rufus", "--alive", "--name", "Rufus", "--alive",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Legs.ShouldBe(12); dog.Legs.ShouldBe(12);
dog.Age.ShouldBe(4); dog.Age.ShouldBe(4);
@ -79,7 +79,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_3() public void Should_Pass_Case_3()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -91,15 +91,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "dog", "12", "--good-boy", "animal", "dog", "12", "--good-boy",
"--name", "Rufus", "--name", "Rufus",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
dog.GoodBoy.ShouldBe(true); dog.GoodBoy.ShouldBe(true);
@ -112,7 +112,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_4() public void Should_Pass_Case_4()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -123,15 +123,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "--good-boy", "animal", "4", "dog", "12", "--good-boy",
"--name", "Rufus", "--name", "Rufus",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Legs.ShouldBe(4); dog.Legs.ShouldBe(4);
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
@ -145,7 +145,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_5() public void Should_Pass_Case_5()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -153,15 +153,15 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"multi", "--foo", "a", "--foo", "b", "multi", "--foo", "a", "--foo", "b",
"--bar", "1", "--foo", "c", "--bar", "2", "--bar", "1", "--foo", "c", "--bar", "2",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<OptionVectorSettings>().And(vec => result.Settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
{ {
vec.Foo.Length.ShouldBe(3); vec.Foo.Length.ShouldBe(3);
vec.Foo.ShouldBe(new[] { "a", "b", "c" }); vec.Foo.ShouldBe(new[] { "a", "b", "c" });
@ -174,7 +174,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Pass_Case_6() public void Should_Pass_Case_6()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -182,14 +182,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"multi", "a", "b", "c", "multi", "a", "b", "c",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec => result.Settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec =>
{ {
vec.Foo.Length.ShouldBe(3); vec.Foo.Length.ShouldBe(3);
vec.Foo.ShouldBe(new[] { "a", "b", "c" }); vec.Foo.ShouldBe(new[] { "a", "b", "c" });
@ -200,7 +200,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Be_Able_To_Use_Command_Alias() public void Should_Be_Able_To_Use_Command_Alias()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -208,14 +208,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"multiple", "--foo", "a", "multiple", "--foo", "a",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<OptionVectorSettings>().And(vec => result.Settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
{ {
vec.Foo.Length.ShouldBe(1); vec.Foo.Length.ShouldBe(1);
vec.Foo.ShouldBe(new[] { "a" }); vec.Foo.ShouldBe(new[] { "a" });
@ -226,19 +226,19 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Assign_Default_Value_To_Optional_Argument() public void Should_Assign_Default_Value_To_Optional_Argument()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<OptionalArgumentWithDefaultValueSettings>>(); app.SetDefaultCommand<GenericCommand<OptionalArgumentWithDefaultValueSettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
}); });
// When // When
var (result, _, _, settings) = app.Run(Array.Empty<string>()); var result = app.Run(Array.Empty<string>());
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<OptionalArgumentWithDefaultValueSettings>().And(settings => result.Settings.ShouldBeOfType<OptionalArgumentWithDefaultValueSettings>().And(settings =>
{ {
settings.Greeting.ShouldBe("Hello World"); settings.Greeting.ShouldBe("Hello World");
}); });
@ -248,19 +248,19 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Assign_Default_Value_To_Optional_Argument_Using_Converter_If_Necessary() public void Should_Assign_Default_Value_To_Optional_Argument_Using_Converter_If_Necessary()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<OptionalArgumentWithDefaultValueAndTypeConverterSettings>>(); app.SetDefaultCommand<GenericCommand<OptionalArgumentWithDefaultValueAndTypeConverterSettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
}); });
// When // When
var (result, _, _, settings) = app.Run(Array.Empty<string>()); var result = app.Run(Array.Empty<string>());
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<OptionalArgumentWithDefaultValueAndTypeConverterSettings>().And(settings => result.Settings.ShouldBeOfType<OptionalArgumentWithDefaultValueAndTypeConverterSettings>().And(settings =>
{ {
settings.Greeting.ShouldBe(5); settings.Greeting.ShouldBe(5);
}); });
@ -270,8 +270,8 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Throw_If_Required_Argument_Have_Default_Value() public void Should_Throw_If_Required_Argument_Have_Default_Value()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<GenericCommand<RequiredArgumentWithDefaultValueSettings>>(); app.SetDefaultCommand<GenericCommand<RequiredArgumentWithDefaultValueSettings>>();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -385,35 +385,6 @@ namespace Spectre.Console.Tests.Unit.Cli
registrar.Registrations[typeof(DogSettings)].ShouldContain(typeof(DogSettings)); registrar.Registrations[typeof(DogSettings)].ShouldContain(typeof(DogSettings));
} }
[Theory]
[InlineData("true", true)]
[InlineData("True", true)]
[InlineData("false", false)]
[InlineData("False", false)]
public void Should_Accept_Explicit_Boolan_Flag(string value, bool expected)
{
// Given
var app = new CommandAppFixture();
app.Configure(config =>
{
config.PropagateExceptions();
config.AddCommand<DogCommand>("dog");
});
// When
var (result, _, _, settings) = app.Run(new[]
{
"dog", "12", "4", "--alive", value,
});
// Then
result.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog =>
{
dog.IsAlive.ShouldBe(expected);
});
}
[Fact] [Fact]
public void Can_Register_Command_Settings_When_Configuring_Application() public void Can_Register_Command_Settings_When_Configuring_Application()
{ {
@ -447,6 +418,35 @@ namespace Spectre.Console.Tests.Unit.Cli
registrar.Registrations[typeof(MammalSettings)].ShouldContain(typeof(MammalSettings)); registrar.Registrations[typeof(MammalSettings)].ShouldContain(typeof(MammalSettings));
} }
[Theory]
[InlineData("true", true)]
[InlineData("True", true)]
[InlineData("false", false)]
[InlineData("False", false)]
public void Should_Accept_Explicit_Boolan_Flag(string value, bool expected)
{
// Given
var app = new CommandAppTester();
app.Configure(config =>
{
config.PropagateExceptions();
config.AddCommand<DogCommand>("dog");
});
// When
var result = app.Run(new[]
{
"dog", "12", "4", "--alive", value,
});
// Then
result.ExitCode.ShouldBe(0);
result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{
dog.IsAlive.ShouldBe(expected);
});
}
[Fact] [Fact]
public void Should_Throw_When_Encountering_Unknown_Option_In_Strict_Mode() public void Should_Throw_When_Encountering_Unknown_Option_In_Strict_Mode()
{ {
@ -473,7 +473,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Add_Unknown_Option_To_Remaining_Arguments_In_Relaxed_Mode() public void Should_Add_Unknown_Option_To_Remaining_Arguments_In_Relaxed_Mode()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -484,23 +484,23 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "animal", "4", "dog", "12",
"--foo", "bar", "--foo", "bar",
}); });
// Then // Then
ctx.ShouldNotBeNull(); result.Context.ShouldNotBeNull();
ctx.Remaining.Parsed.Count.ShouldBe(1); result.Context.Remaining.Parsed.Count.ShouldBe(1);
ctx.ShouldHaveRemainingArgument("foo", values: new[] { "bar" }); result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar" });
} }
[Fact] [Fact]
public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_Mode() public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_Mode()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -511,33 +511,33 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "--foo", "animal", "4", "dog", "12", "--foo",
}); });
// Then // Then
ctx.ShouldNotBeNull(); result.Context.ShouldNotBeNull();
ctx.Remaining.Parsed.Count.ShouldBe(1); result.Context.Remaining.Parsed.Count.ShouldBe(1);
ctx.ShouldHaveRemainingArgument("foo", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("foo", values: new[] { (string)null });
} }
[Fact] [Fact]
public void Should_Be_Able_To_Set_The_Default_Command() public void Should_Be_Able_To_Set_The_Default_Command()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.WithDefaultCommand<DogCommand>(); app.SetDefaultCommand<DogCommand>();
// When // When
var (result, _, _, settings) = app.Run(new[] var result = app.Run(new[]
{ {
"4", "12", "--good-boy", "--name", "Rufus", "4", "12", "--good-boy", "--name", "Rufus",
}); });
// Then // Then
result.ShouldBe(0); result.ExitCode.ShouldBe(0);
settings.ShouldBeOfType<DogSettings>().And(dog => result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
{ {
dog.Legs.ShouldBe(4); dog.Legs.ShouldBe(4);
dog.Age.ShouldBe(12); dog.Age.ShouldBe(12);
@ -550,7 +550,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Set_Command_Name_In_Context() public void Should_Set_Command_Name_In_Context()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -561,21 +561,21 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "animal", "4", "dog", "12",
}); });
// Then // Then
ctx.ShouldNotBeNull(); result.Context.ShouldNotBeNull();
ctx.Name.ShouldBe("dog"); result.Context.Name.ShouldBe("dog");
} }
[Fact] [Fact]
public void Should_Pass_Command_Data_In_Context() public void Should_Pass_Command_Data_In_Context()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -586,14 +586,14 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "animal", "4", "dog", "12",
}); });
// Then // Then
ctx.ShouldNotBeNull(); result.Context.ShouldNotBeNull();
ctx.Data.ShouldBe(123); result.Context.Data.ShouldBe(123);
} }
public sealed class Delegate_Commands public sealed class Delegate_Commands
@ -670,7 +670,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Register_Remaining_Parsed_Arguments_With_Context() public void Should_Register_Remaining_Parsed_Arguments_With_Context()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -681,7 +681,7 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "--", "animal", "4", "dog", "12", "--",
"--foo", "bar", "--foo", "baz", "--foo", "bar", "--foo", "baz",
@ -689,18 +689,18 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// Then // Then
ctx.Remaining.Parsed.Count.ShouldBe(4); result.Context.Remaining.Parsed.Count.ShouldBe(4);
ctx.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" }); result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
ctx.ShouldHaveRemainingArgument("b", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
ctx.ShouldHaveRemainingArgument("a", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
ctx.ShouldHaveRemainingArgument("r", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
} }
[Fact] [Fact]
public void Should_Register_Remaining_Raw_Arguments_With_Context() public void Should_Register_Remaining_Raw_Arguments_With_Context()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.PropagateExceptions(); config.PropagateExceptions();
@ -711,19 +711,19 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, ctx, _) = app.Run(new[] var result = app.Run(new[]
{ {
"animal", "4", "dog", "12", "--", "animal", "4", "dog", "12", "--",
"--foo", "bar", "-bar", "\"baz\"", "qux", "--foo", "bar", "-bar", "\"baz\"", "qux",
}); });
// Then // Then
ctx.Remaining.Raw.Count.ShouldBe(5); result.Context.Remaining.Raw.Count.ShouldBe(5);
ctx.Remaining.Raw[0].ShouldBe("--foo"); result.Context.Remaining.Raw[0].ShouldBe("--foo");
ctx.Remaining.Raw[1].ShouldBe("bar"); result.Context.Remaining.Raw[1].ShouldBe("bar");
ctx.Remaining.Raw[2].ShouldBe("-bar"); result.Context.Remaining.Raw[2].ShouldBe("-bar");
ctx.Remaining.Raw[3].ShouldBe("baz"); result.Context.Remaining.Raw[3].ShouldBe("baz");
ctx.Remaining.Raw[4].ShouldBe("qux"); result.Context.Remaining.Raw[4].ShouldBe("qux");
} }
} }
@ -733,7 +733,7 @@ namespace Spectre.Console.Tests.Unit.Cli
public void Should_Not_Propagate_Runtime_Exceptions_If_Not_Explicitly_Told_To_Do_So() public void Should_Not_Propagate_Runtime_Exceptions_If_Not_Explicitly_Told_To_Do_So()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.AddBranch<AnimalSettings>("animal", animal => config.AddBranch<AnimalSettings>("animal", animal =>
@ -744,27 +744,27 @@ namespace Spectre.Console.Tests.Unit.Cli
}); });
// When // When
var (result, _, _, _) = app.Run(new[] { "animal", "4", "dog", "101", "--name", "Rufus" }); var result = app.Run(new[] { "animal", "4", "dog", "101", "--name", "Rufus" });
// Then // Then
result.ShouldBe(-1); result.ExitCode.ShouldBe(-1);
} }
[Fact] [Fact]
public void Should_Not_Propagate_Exceptions_If_Not_Explicitly_Told_To_Do_So() public void Should_Not_Propagate_Exceptions_If_Not_Explicitly_Told_To_Do_So()
{ {
// Given // Given
var app = new CommandAppFixture(); var app = new CommandAppTester();
app.Configure(config => app.Configure(config =>
{ {
config.AddCommand<ThrowingCommand>("throw"); config.AddCommand<ThrowingCommand>("throw");
}); });
// When // When
var (result, _, _, _) = app.Run(new[] { "throw" }); var result = app.Run(new[] { "throw" });
// Then // Then
result.ShouldBe(-1); result.ExitCode.ShouldBe(-1);
} }
} }
} }

View File

@ -22,7 +22,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Columns_Correctly() public Task Should_Render_Columns_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 61); var console = new TestConsole().Width(61);
var users = new[] var users = new[]
{ {
new User { Name = "Savannah Thompson", Country = "Australia" }, new User { Name = "Savannah Thompson", Country = "Australia" },

View File

@ -10,7 +10,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Substitute_Emoji_Shortcodes_In_Markdown() public void Should_Substitute_Emoji_Shortcodes_In_Markdown()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
console.Markup("Hello :globe_showing_europe_africa:!"); console.Markup("Hello :globe_showing_europe_africa:!");
@ -57,7 +57,7 @@ namespace Spectre.Console.Tests.Unit
public void Can_Handle_Different_Combinations(string markup, string expected) public void Can_Handle_Different_Combinations(string markup, string expected)
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
console.Markup(markup); console.Markup(markup);
@ -70,7 +70,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Leave_Single_Colons() public void Should_Leave_Single_Colons()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
console.Markup("Hello :globe_showing_europe_africa:! Output: good"); console.Markup("Hello :globe_showing_europe_africa:! Output: good");
@ -80,10 +80,10 @@ namespace Spectre.Console.Tests.Unit
} }
[Fact] [Fact]
public void Unknown_emojis_should_remain() public void Unknown_emojis_should_remain_unchanged()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
console.Markup("Hello :globe_showing_flat_earth:!"); console.Markup("Hello :globe_showing_flat_earth:!");

View File

@ -17,7 +17,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Write_Exception() public Task Should_Write_Exception()
{ {
// Given // Given
var console = new FakeConsole(width: 1024); var console = new TestConsole().Width(1024);
var dex = GetException(() => TestExceptions.MethodThatThrows(null)); var dex = GetException(() => TestExceptions.MethodThatThrows(null));
// When // When
@ -32,7 +32,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Write_Exception_With_Shortened_Types() public Task Should_Write_Exception_With_Shortened_Types()
{ {
// Given // Given
var console = new FakeConsole(width: 1024); var console = new TestConsole().Width(1024);
var dex = GetException(() => TestExceptions.MethodThatThrows(null)); var dex = GetException(() => TestExceptions.MethodThatThrows(null));
// When // When
@ -47,7 +47,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Write_Exception_With_Shortened_Methods() public Task Should_Write_Exception_With_Shortened_Methods()
{ {
// Given // Given
var console = new FakeConsole(width: 1024); var console = new TestConsole().Width(1024);
var dex = GetException(() => TestExceptions.MethodThatThrows(null)); var dex = GetException(() => TestExceptions.MethodThatThrows(null));
// When // When
@ -62,7 +62,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Write_Exception_With_Inner_Exception() public Task Should_Write_Exception_With_Inner_Exception()
{ {
// Given // Given
var console = new FakeConsole(width: 1024); var console = new TestConsole().Width(1024);
var dex = GetException(() => TestExceptions.ThrowWithInnerException()); var dex = GetException(() => TestExceptions.ThrowWithInnerException());
// When // When
@ -77,7 +77,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Write_Exceptions_With_Generic_Type_Parameters_In_Callsite_As_Expected() public Task Should_Write_Exceptions_With_Generic_Type_Parameters_In_Callsite_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(width: 1024); var console = new TestConsole().Width(1024);
var dex = GetException(() => TestExceptions.ThrowWithGenericInnerException()); var dex = GetException(() => TestExceptions.ThrowWithGenericInnerException());
// When // When

View File

@ -15,7 +15,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Load_Font_From_Stream() public async Task Should_Load_Font_From_Stream()
{ {
// Given // Given
var console = new FakeConsole(width: 180); var console = new TestConsole().Width(180);
var font = FigletFont.Load(EmbeddedResourceReader.LoadResourceStream("Spectre.Console.Tests/Data/starwars.flf")); var font = FigletFont.Load(EmbeddedResourceReader.LoadResourceStream("Spectre.Console.Tests/Data/starwars.flf"));
var text = new FigletText(font, "Patrik was here"); var text = new FigletText(font, "Patrik was here");
@ -31,7 +31,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Text_Correctly() public async Task Should_Render_Text_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 70); var console = new TestConsole().Width(70);
var text = new FigletText(FigletFont.Default, "Patrik was here"); var text = new FigletText(FigletFont.Default, "Patrik was here");
// When // When
@ -46,7 +46,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Wrapped_Text_Correctly() public async Task Should_Render_Wrapped_Text_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 70); var console = new TestConsole().Width(70);
var text = new FigletText(FigletFont.Default, "Spectre.Console"); var text = new FigletText(FigletFont.Default, "Spectre.Console");
// When // When
@ -61,7 +61,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Left_Aligned_Text_Correctly() public async Task Should_Render_Left_Aligned_Text_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 120); var console = new TestConsole().Width(120);
var text = new FigletText(FigletFont.Default, "Spectre.Console") var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Left); .Alignment(Justify.Left);
@ -77,7 +77,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Centered_Text_Correctly() public async Task Should_Render_Centered_Text_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 120); var console = new TestConsole().Width(120);
var text = new FigletText(FigletFont.Default, "Spectre.Console") var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Center); .Alignment(Justify.Center);
@ -93,7 +93,7 @@ namespace Spectre.Console.Tests.Unit
public async Task Should_Render_Right_Aligned_Text_Correctly() public async Task Should_Render_Right_Aligned_Text_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 120); var console = new TestConsole().Width(120);
var text = new FigletText(FigletFont.Default, "Spectre.Console") var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Right); .Alignment(Justify.Right);

View File

@ -87,7 +87,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Add_Empty_Row() public Task Should_Add_Empty_Row()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var grid = new Grid(); var grid = new Grid();
grid.AddColumns(2); grid.AddColumns(2);
grid.AddRow("Foo", "Bar"); grid.AddRow("Foo", "Bar");
@ -108,7 +108,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Grid_Correctly() public Task Should_Render_Grid_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var grid = new Grid(); var grid = new Grid();
grid.AddColumn(); grid.AddColumn();
grid.AddColumn(); grid.AddColumn();
@ -127,7 +127,7 @@ namespace Spectre.Console.Tests.Unit
[Expectation("Render_2")] [Expectation("Render_2")]
public Task Should_Render_Grid_Correctly_2() public Task Should_Render_Grid_Correctly_2()
{ {
var console = new FakeConsole(width: 80); var console = new TestConsole();
var grid = new Grid(); var grid = new Grid();
grid.AddColumn(new GridColumn { NoWrap = true }); grid.AddColumn(new GridColumn { NoWrap = true });
grid.AddColumn(new GridColumn { Padding = new Padding(2, 0, 0, 0) }); grid.AddColumn(new GridColumn { Padding = new Padding(2, 0, 0, 0) });
@ -147,7 +147,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Grid_Column_Alignment_Correctly() public Task Should_Render_Grid_Column_Alignment_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var grid = new Grid(); var grid = new Grid();
grid.AddColumn(new GridColumn { Alignment = Justify.Right }); grid.AddColumn(new GridColumn { Alignment = Justify.Right });
grid.AddColumn(new GridColumn { Alignment = Justify.Center }); grid.AddColumn(new GridColumn { Alignment = Justify.Center });
@ -168,7 +168,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Use_Default_Padding() public Task Should_Use_Default_Padding()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var grid = new Grid(); var grid = new Grid();
grid.AddColumns(3); grid.AddColumns(3);
grid.AddRow("Foo", "Bar", "Baz"); grid.AddRow("Foo", "Bar", "Baz");
@ -187,7 +187,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Explicit_Grid_Column_Padding_Correctly() public Task Should_Render_Explicit_Grid_Column_Padding_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var grid = new Grid(); var grid = new Grid();
grid.AddColumn(new GridColumn { Padding = new Padding(3, 0, 0, 0) }); grid.AddColumn(new GridColumn { Padding = new Padding(3, 0, 0, 0) });
grid.AddColumn(new GridColumn { Padding = new Padding(0, 0, 0, 0) }); grid.AddColumn(new GridColumn { Padding = new Padding(0, 0, 0, 0) });

View File

@ -47,7 +47,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Throw_If_Closing_Tag_Is_Not_Properly_Escaped(string input) public void Should_Throw_If_Closing_Tag_Is_Not_Properly_Escaped(string input)
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
// When // When
var result = Record.Exception(() => new Markup(input)); var result = Record.Exception(() => new Markup(input));
@ -62,7 +62,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Escape_Markup_Blocks_As_Expected() public void Should_Escape_Markup_Blocks_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var markup = new Markup("Hello [[ World ]] !"); var markup = new Markup("Hello [[ World ]] !");
// When // When
@ -78,7 +78,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Render_Links_As_Expected(string input, string output) public void Should_Render_Links_As_Expected(string input, string output)
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var markup = new Markup(input); var markup = new Markup(input);
// When // When
@ -89,10 +89,10 @@ namespace Spectre.Console.Tests.Unit
} }
[Fact] [Fact]
public void Should_not_fail_with_brackets_on_calls_without_args() public void Should_Not_Fail_With_Brackets_On_Calls_Without_Args()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes); var console = new TestConsole();
// When // When
console.MarkupLine("{"); console.MarkupLine("{");

View File

@ -15,7 +15,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Padded_Object_Correctly() public Task Should_Render_Padded_Object_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 60); var console = new TestConsole().Width(60);
var table = new Table(); var table = new Table();
table.AddColumn("Foo"); table.AddColumn("Foo");
table.AddColumn("Bar"); table.AddColumn("Bar");
@ -34,7 +34,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Expanded_Padded_Object_Correctly() public Task Should_Render_Expanded_Padded_Object_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 60); var console = new TestConsole().Width(60);
var table = new Table(); var table = new Table();
table.AddColumn("Foo"); table.AddColumn("Foo");
table.AddColumn("Bar"); table.AddColumn("Bar");
@ -55,7 +55,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Padded_Object_Correctly_When_Nested_Within_Other_Object() public Task Should_Render_Padded_Object_Correctly_When_Nested_Within_Other_Object()
{ {
// Given // Given
var console = new FakeConsole(width: 60); var console = new TestConsole().Width(60);
var table = new Table(); var table = new Table();
table.AddColumn("Foo"); table.AddColumn("Foo");
table.AddColumn("Bar", c => c.PadLeft(0).PadRight(0)); table.AddColumn("Bar", c => c.PadLeft(0).PadRight(0));

View File

@ -17,7 +17,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel() public Task Should_Render_Panel()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel(new Text("Hello World"))); console.Write(new Panel(new Text("Hello World")));
@ -31,7 +31,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Padding_Set_To_Zero() public Task Should_Render_Panel_With_Padding_Set_To_Zero()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel(new Text("Hello World")) console.Write(new Panel(new Text("Hello World"))
@ -48,7 +48,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Padding() public Task Should_Render_Panel_With_Padding()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel(new Text("Hello World")) console.Write(new Panel(new Text("Hello World"))
@ -65,7 +65,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Header() public Task Should_Render_Panel_With_Header()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel("Hello World") console.Write(new Panel("Hello World")
@ -84,7 +84,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Left_Aligned_Header() public Task Should_Render_Panel_With_Left_Aligned_Header()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel("Hello World") console.Write(new Panel("Hello World")
@ -102,7 +102,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Centered_Header() public Task Should_Render_Panel_With_Centered_Header()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel("Hello World") console.Write(new Panel("Hello World")
@ -120,7 +120,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Right_Aligned_Header() public Task Should_Render_Panel_With_Right_Aligned_Header()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel("Hello World") console.Write(new Panel("Hello World")
@ -138,7 +138,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Collapse_Header_If_It_Will_Not_Fit() public Task Should_Collapse_Header_If_It_Will_Not_Fit()
{ {
// Given // Given
var console = new FakeConsole(width: 10); var console = new TestConsole().Width(10);
// When // When
console.Write(new Panel("Hello World") console.Write(new Panel("Hello World")
@ -156,7 +156,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Unicode_Correctly() public Task Should_Render_Panel_With_Unicode_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel(new Text(" \n💩\n "))); console.Write(new Panel(new Text(" \n💩\n ")));
@ -170,7 +170,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_With_Multiple_Lines() public Task Should_Render_Panel_With_Multiple_Lines()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel(new Text("Hello World\nFoo Bar"))); console.Write(new Panel(new Text("Hello World\nFoo Bar")));
@ -184,7 +184,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Preserve_Explicit_Line_Ending() public Task Should_Preserve_Explicit_Line_Ending()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var text = new Panel( var text = new Panel(
new Markup("I heard [underline on blue]you[/] like 📦\n\n\n\nSo I put a 📦 in a 📦")); new Markup("I heard [underline on blue]you[/] like 📦\n\n\n\nSo I put a 📦 in a 📦"));
@ -200,7 +200,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Expand_Panel_If_Enabled() public Task Should_Expand_Panel_If_Enabled()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel(new Text("Hello World")) console.Write(new Panel(new Text("Hello World"))
@ -217,7 +217,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Justify_Child_To_Right_Correctly() public Task Should_Justify_Child_To_Right_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 25); var console = new TestConsole().Width(25);
// When // When
console.Write( console.Write(
@ -235,7 +235,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Center_Child_Correctly() public Task Should_Center_Child_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 25); var console = new TestConsole().Width(25);
// When // When
console.Write( console.Write(
@ -253,7 +253,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Panel_Inside_Panel_Correctly() public Task Should_Render_Panel_Inside_Panel_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Panel(new Panel(new Text("Hello World")))); console.Write(new Panel(new Panel(new Text("Hello World"))));
@ -267,7 +267,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Wrap_Content_Correctly() public Task Should_Wrap_Content_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 84); var console = new TestConsole().Width(84);
var rows = new List<IRenderable>(); var rows = new List<IRenderable>();
var grid = new Grid(); var grid = new Grid();
grid.AddColumn(new GridColumn().PadLeft(2).PadRight(0)); grid.AddColumn(new GridColumn().PadLeft(2).PadRight(0));
@ -292,7 +292,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Wrap_Table_With_CJK_Tables_In_Panel_Correctly() public Task Should_Wrap_Table_With_CJK_Tables_In_Panel_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumn("测试"); table.AddColumn("测试");

View File

@ -19,7 +19,7 @@ namespace Spectre.Console.Tests.Unit
public string Render() public string Render()
{ {
var console = new FakeConsole(); var console = new TestConsole();
var context = new RenderContext(console.Profile.Capabilities); var context = new RenderContext(console.Profile.Capabilities);
console.Write(Column.Render(context, Task, TimeSpan.Zero)); console.Write(Column.Render(context, Task, TimeSpan.Zero));
return console.Output; return console.Output;

View File

@ -15,7 +15,10 @@ namespace Spectre.Console.Tests.Unit
public void Should_Render_Task_Correctly() public void Should_Render_Task_Correctly()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.TrueColor, width: 10); var console = new TestConsole()
.Width(10)
.Interactive()
.EmitAnsiSequences();
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() }) .Columns(new[] { new ProgressBarColumn() })
@ -40,7 +43,10 @@ namespace Spectre.Console.Tests.Unit
public void Should_Not_Auto_Clear_If_Specified() public void Should_Not_Auto_Clear_If_Specified()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.TrueColor, width: 10); var console = new TestConsole()
.Width(10)
.Interactive()
.EmitAnsiSequences();
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() }) .Columns(new[] { new ProgressBarColumn() })
@ -66,7 +72,9 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Reduce_Width_If_Needed() public Task Should_Reduce_Width_If_Needed()
{ {
// Given // Given
var console = new FakeConsole(width: 20); var console = new TestConsole()
.Width(20)
.Interactive();
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new ProgressColumn[] .Columns(new ProgressColumn[]
@ -96,8 +104,10 @@ namespace Spectre.Console.Tests.Unit
public void Setting_Max_Value_Should_Set_The_MaxValue_And_Cap_Value() public void Setting_Max_Value_Should_Set_The_MaxValue_And_Cap_Value()
{ {
// Given // Given
var console = new TestConsole()
.Interactive();
var task = default(ProgressTask); var task = default(ProgressTask);
var console = new FakeConsole();
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() }) .Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false) .AutoRefresh(false)
@ -120,8 +130,10 @@ namespace Spectre.Console.Tests.Unit
public void Setting_Value_Should_Override_Incremented_Value() public void Setting_Value_Should_Override_Incremented_Value()
{ {
// Given // Given
var console = new TestConsole()
.Interactive();
var task = default(ProgressTask); var task = default(ProgressTask);
var console = new FakeConsole();
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() }) .Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false) .AutoRefresh(false)
@ -144,8 +156,10 @@ namespace Spectre.Console.Tests.Unit
public void Setting_Value_To_MaxValue_Should_Finish_Task() public void Setting_Value_To_MaxValue_Should_Finish_Task()
{ {
// Given // Given
var console = new TestConsole()
.Interactive();
var task = default(ProgressTask); var task = default(ProgressTask);
var console = new FakeConsole();
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() }) .Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false) .AutoRefresh(false)
@ -166,8 +180,10 @@ namespace Spectre.Console.Tests.Unit
public void Should_Increment_Manually_Set_Value() public void Should_Increment_Manually_Set_Value()
{ {
// Given // Given
var console = new TestConsole()
.Interactive();
var task = default(ProgressTask); var task = default(ProgressTask);
var console = new FakeConsole();
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() }) .Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false) .AutoRefresh(false)
@ -189,10 +205,14 @@ namespace Spectre.Console.Tests.Unit
public void Should_Hide_Completed_Tasks() public void Should_Hide_Completed_Tasks()
{ {
// Given // Given
var console = new TestConsole()
.Width(10)
.Interactive()
.EmitAnsiSequences();
var taskFinished = default(ProgressTask); var taskFinished = default(ProgressTask);
var taskInProgress1 = default(ProgressTask); var taskInProgress1 = default(ProgressTask);
var taskInProgress2 = default(ProgressTask); var taskInProgress2 = default(ProgressTask);
var console = new FakeAnsiConsole(ColorSystem.TrueColor, width: 10);
var progress = new Progress(console) var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() }) .Columns(new[] { new ProgressBarColumn() })

View File

@ -17,7 +17,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Return_Validation_Error_If_Value_Cannot_Be_Converted() public Task Should_Return_Validation_Error_If_Value_Cannot_Be_Converted()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushTextWithEnter("ninety-nine"); console.Input.PushTextWithEnter("ninety-nine");
console.Input.PushTextWithEnter("99"); console.Input.PushTextWithEnter("99");
@ -33,7 +33,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Chose_Default_Value_If_Nothing_Is_Entered() public Task Should_Chose_Default_Value_If_Nothing_Is_Entered()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter); console.Input.PushKey(ConsoleKey.Enter);
// When // When
@ -52,7 +52,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Return_Error_If_An_Invalid_Choice_Is_Made() public Task Should_Return_Error_If_An_Invalid_Choice_Is_Made()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushTextWithEnter("Apple"); console.Input.PushTextWithEnter("Apple");
console.Input.PushTextWithEnter("Banana"); console.Input.PushTextWithEnter("Banana");
@ -72,7 +72,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Accept_Choice_In_List() public Task Should_Accept_Choice_In_List()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushTextWithEnter("Orange"); console.Input.PushTextWithEnter("Orange");
// When // When
@ -91,7 +91,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Auto_Complete_To_First_Choice_If_Pressing_Tab_On_Empty_String() public Task Should_Auto_Complete_To_First_Choice_If_Pressing_Tab_On_Empty_String()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Tab); console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter); console.Input.PushKey(ConsoleKey.Enter);
@ -111,7 +111,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Auto_Complete_To_Best_Match() public Task Should_Auto_Complete_To_Best_Match()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushText("Band"); console.Input.PushText("Band");
console.Input.PushKey(ConsoleKey.Tab); console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter); console.Input.PushKey(ConsoleKey.Enter);
@ -132,7 +132,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Auto_Complete_To_Next_Choice_When_Pressing_Tab_On_A_Match() public Task Should_Auto_Complete_To_Next_Choice_When_Pressing_Tab_On_A_Match()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushText("Apple"); console.Input.PushText("Apple");
console.Input.PushKey(ConsoleKey.Tab); console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter); console.Input.PushKey(ConsoleKey.Enter);
@ -153,7 +153,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Return_Error_If_Custom_Validation_Fails() public Task Should_Return_Error_If_Custom_Validation_Fails()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushTextWithEnter("22"); console.Input.PushTextWithEnter("22");
console.Input.PushTextWithEnter("102"); console.Input.PushTextWithEnter("102");
console.Input.PushTextWithEnter("ABC"); console.Input.PushTextWithEnter("ABC");
@ -186,7 +186,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Use_Custom_Converter() public Task Should_Use_Custom_Converter()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushTextWithEnter("Banana"); console.Input.PushTextWithEnter("Banana");
// When // When
@ -206,7 +206,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Chose_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret() public Task Should_Chose_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter); console.Input.PushKey(ConsoleKey.Enter);
// When // When

View File

@ -15,7 +15,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Export_Text_As_Expected() public Task Should_Export_Text_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var recorder = new Recorder(console); var recorder = new Recorder(console);
recorder.Write(new Table() recorder.Write(new Table()
@ -35,7 +35,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Export_Html_Text_As_Expected() public Task Should_Export_Html_Text_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var recorder = new Recorder(console); var recorder = new Recorder(console);
recorder.Write(new Table() recorder.Write(new Table()

View File

@ -21,7 +21,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Inject_Renderable_Before_Writing_To_Console() public void Should_Inject_Renderable_Before_Writing_To_Console()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
console.Pipeline.Attach(new HelloRenderHook()); console.Pipeline.Attach(new HelloRenderHook());
// When // When

View File

@ -16,7 +16,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Rows() public Task Should_Render_Rows()
{ {
// Given // Given
var console = new FakeConsole(width: 60); var console = new TestConsole().Width(60);
var rows = new Rows( var rows = new Rows(
new IRenderable[] new IRenderable[]
{ {
@ -39,7 +39,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Rows_Correctly_Inside_Other_Widget() public Task Should_Render_Rows_Correctly_Inside_Other_Widget()
{ {
// Given // Given
var console = new FakeConsole(width: 60); var console = new TestConsole().Width(60);
var table = new Table() var table = new Table()
.AddColumns("Foo", "Bar") .AddColumns("Foo", "Bar")
.AddRow("HELLO WORLD") .AddRow("HELLO WORLD")
@ -62,7 +62,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Rows_Correctly_Inside_Other_Widget_When_Expanded() public Task Should_Render_Rows_Correctly_Inside_Other_Widget_When_Expanded()
{ {
// Given // Given
var console = new FakeConsole(width: 60); var console = new TestConsole().Width(60);
var table = new Table() var table = new Table()
.AddColumns("Foo", "Bar") .AddColumns("Foo", "Bar")
.AddRow("HELLO WORLD") .AddRow("HELLO WORLD")

View File

@ -16,7 +16,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Default_Rule_Without_Title() public Task Should_Render_Default_Rule_Without_Title()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule()); console.Write(new Rule());
@ -30,7 +30,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Default_Rule_With_Specified_Border() public Task Should_Render_Default_Rule_With_Specified_Border()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule().DoubleBorder()); console.Write(new Rule().DoubleBorder());
@ -44,7 +44,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_With_Specified_Box() public Task Should_Render_With_Specified_Box()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule("Hello World").DoubleBorder()); console.Write(new Rule("Hello World").DoubleBorder());
@ -58,7 +58,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Default_Rule_With_Title_Centered_By_Default() public Task Should_Render_Default_Rule_With_Title_Centered_By_Default()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule("Hello World")); console.Write(new Rule("Hello World"));
@ -72,7 +72,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Default_Rule_With_Title_Left_Aligned() public Task Should_Render_Default_Rule_With_Title_Left_Aligned()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule("Hello World") console.Write(new Rule("Hello World")
@ -89,7 +89,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Default_Rule_With_Title_Right_Aligned() public Task Should_Render_Default_Rule_With_Title_Right_Aligned()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule("Hello World") console.Write(new Rule("Hello World")
@ -106,7 +106,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Convert_Line_Breaks_In_Title_To_Spaces() public Task Should_Convert_Line_Breaks_In_Title_To_Spaces()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule("Hello\nWorld\r\n!")); console.Write(new Rule("Hello\nWorld\r\n!"));
@ -120,7 +120,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Truncate_Title() public Task Should_Truncate_Title()
{ {
// Given // Given
var console = new FakeConsole(width: 40); var console = new TestConsole().Width(40);
// When // When
console.Write(new Rule(" Hello World ")); console.Write(new Rule(" Hello World "));
@ -145,7 +145,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Truncate_Too_Long_Title(int width, string input, string expected) public void Should_Truncate_Too_Long_Title(int width, string input, string expected)
{ {
// Given // Given
var console = new FakeConsole(width); var console = new TestConsole().Width(width);
// When // When
console.Write(new Rule(input)); console.Write(new Rule(input));

View File

@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Spectre.Console.Testing; using Spectre.Console.Testing;
using Spectre.Verify.Extensions; using Spectre.Verify.Extensions;
@ -10,16 +12,36 @@ namespace Spectre.Console.Tests.Unit
[ExpectationPath("Widgets/Status")] [ExpectationPath("Widgets/Status")]
public sealed class StatusTests public sealed class StatusTests
{ {
public sealed class DummySpinner1 : Spinner
{
public override TimeSpan Interval => TimeSpan.FromMilliseconds(100);
public override bool IsUnicode => true;
public override IReadOnlyList<string> Frames => new List<string> { "*", };
}
public sealed class DummySpinner2 : Spinner
{
public override TimeSpan Interval => TimeSpan.FromMilliseconds(100);
public override bool IsUnicode => true;
public override IReadOnlyList<string> Frames => new List<string> { "-", };
}
[Fact] [Fact]
[Expectation("Render")] [Expectation("Render")]
public Task Should_Render_Status_Correctly() public Task Should_Render_Status_Correctly()
{ {
// Given // Given
var console = new FakeAnsiConsole(ColorSystem.TrueColor, width: 10); var console = new TestConsole()
.Colors(ColorSystem.TrueColor)
.Width(10)
.Interactive()
.EmitAnsiSequences();
var status = new Status(console); var status = new Status(console)
status.AutoRefresh = false; {
status.Spinner = new DummySpinner1(); AutoRefresh = false,
Spinner = new DummySpinner1(),
};
// When // When
status.Start("foo", ctx => status.Start("foo", ctx =>

View File

@ -43,7 +43,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().NoBorder(); var table = Fixture.GetTable().NoBorder();
// When // When
@ -85,7 +85,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().AsciiBorder(); var table = Fixture.GetTable().AsciiBorder();
// When // When
@ -127,7 +127,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().Ascii2Border(); var table = Fixture.GetTable().Ascii2Border();
// When // When
@ -169,7 +169,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().AsciiDoubleHeadBorder(); var table = Fixture.GetTable().AsciiDoubleHeadBorder();
// When // When
@ -211,7 +211,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().SquareBorder(); var table = Fixture.GetTable().SquareBorder();
// When // When
@ -253,7 +253,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().RoundedBorder(); var table = Fixture.GetTable().RoundedBorder();
// When // When
@ -295,7 +295,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().MinimalBorder(); var table = Fixture.GetTable().MinimalBorder();
// When // When
@ -337,7 +337,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().MinimalHeavyHeadBorder(); var table = Fixture.GetTable().MinimalHeavyHeadBorder();
// When // When
@ -379,7 +379,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().MinimalDoubleHeadBorder(); var table = Fixture.GetTable().MinimalDoubleHeadBorder();
// When // When
@ -421,7 +421,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().SimpleBorder(); var table = Fixture.GetTable().SimpleBorder();
// When // When
@ -463,7 +463,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().HorizontalBorder(); var table = Fixture.GetTable().HorizontalBorder();
// When // When
@ -505,7 +505,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().SimpleHeavyBorder(); var table = Fixture.GetTable().SimpleHeavyBorder();
// When // When
@ -547,7 +547,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().HeavyBorder(); var table = Fixture.GetTable().HeavyBorder();
// When // When
@ -589,7 +589,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().HeavyEdgeBorder(); var table = Fixture.GetTable().HeavyEdgeBorder();
// When // When
@ -631,7 +631,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().HeavyHeadBorder(); var table = Fixture.GetTable().HeavyHeadBorder();
// When // When
@ -673,7 +673,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().DoubleBorder(); var table = Fixture.GetTable().DoubleBorder();
// When // When
@ -715,7 +715,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().DoubleEdgeBorder(); var table = Fixture.GetTable().DoubleEdgeBorder();
// When // When
@ -757,7 +757,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_As_Expected() public Task Should_Render_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable().MarkdownBorder(); var table = Fixture.GetTable().MarkdownBorder();
// When // When
@ -772,7 +772,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Left_Aligned_Table_Columns_As_Expected() public Task Should_Render_Left_Aligned_Table_Columns_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable(header2: Justify.Left).MarkdownBorder(); var table = Fixture.GetTable(header2: Justify.Left).MarkdownBorder();
// When // When
@ -787,7 +787,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Center_Aligned_Table_Columns_As_Expected() public Task Should_Render_Center_Aligned_Table_Columns_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable(header2: Justify.Center).MarkdownBorder(); var table = Fixture.GetTable(header2: Justify.Center).MarkdownBorder();
// When // When
@ -802,7 +802,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Right_Aligned_Table_Columns_As_Expected() public Task Should_Render_Right_Aligned_Table_Columns_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(); var console = new TestConsole();
var table = Fixture.GetTable(header2: Justify.Right).MarkdownBorder(); var table = Fixture.GetTable(header2: Justify.Right).MarkdownBorder();
// When // When

View File

@ -131,7 +131,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_Correctly() public Task Should_Render_Table_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumns("Foo", "Bar", "Baz"); table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo"); table.AddRow("Qux", "Corgi", "Waldo");
@ -151,7 +151,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_Correctly() public Task Should_Render_Table_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumns("Foo", "Bar", "Baz"); table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo"); table.AddRow("Qux", "Corgi", "Waldo");
@ -169,7 +169,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_With_Footers_Correctly() public Task Should_Render_Table_With_Footers_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumn(new TableColumn("Foo").Footer("Oof").RightAligned()); table.AddColumn(new TableColumn("Foo").Footer("Oof").RightAligned());
table.AddColumn("Bar"); table.AddColumn("Bar");
@ -189,7 +189,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Left_Align_Table_Correctly() public Task Should_Left_Align_Table_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.Alignment = Justify.Left; table.Alignment = Justify.Left;
table.AddColumns("Foo", "Bar", "Baz"); table.AddColumns("Foo", "Bar", "Baz");
@ -208,7 +208,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Center_Table_Correctly() public Task Should_Center_Table_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.Alignment = Justify.Center; table.Alignment = Justify.Center;
table.AddColumns("Foo", "Bar", "Baz"); table.AddColumns("Foo", "Bar", "Baz");
@ -227,7 +227,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Right_Align_Table_Correctly() public Task Should_Right_Align_Table_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.Alignment = Justify.Right; table.Alignment = Justify.Right;
table.AddColumns("Foo", "Bar", "Baz"); table.AddColumns("Foo", "Bar", "Baz");
@ -246,7 +246,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_Nested_In_Panels_Correctly() public Task Should_Render_Table_Nested_In_Panels_Correctly()
{ {
// A simple table // A simple table
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table() { Border = TableBorder.Rounded }; var table = new Table() { Border = TableBorder.Rounded };
table.AddColumn("Foo"); table.AddColumn("Foo");
table.AddColumn("Bar"); table.AddColumn("Bar");
@ -269,7 +269,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_With_Column_Justification_Correctly() public Task Should_Render_Table_With_Column_Justification_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumn(new TableColumn("Foo") { Alignment = Justify.Left }); table.AddColumn(new TableColumn("Foo") { Alignment = Justify.Left });
table.AddColumn(new TableColumn("Bar") { Alignment = Justify.Right }); table.AddColumn(new TableColumn("Bar") { Alignment = Justify.Right });
@ -289,7 +289,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Expand_Table_To_Available_Space_If_Specified() public Task Should_Expand_Table_To_Available_Space_If_Specified()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table() { Expand = true }; var table = new Table() { Expand = true };
table.AddColumns("Foo", "Bar", "Baz"); table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo"); table.AddRow("Qux", "Corgi", "Waldo");
@ -307,7 +307,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_With_Multiple_Rows_In_Cell_Correctly() public Task Should_Render_Table_With_Multiple_Rows_In_Cell_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumns("Foo", "Bar", "Baz"); table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux\nQuuux", "Corgi", "Waldo"); table.AddRow("Qux\nQuuux", "Corgi", "Waldo");
@ -325,7 +325,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_With_Cell_Padding_Correctly() public Task Should_Render_Table_With_Cell_Padding_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumns("Foo", "Bar"); table.AddColumns("Foo", "Bar");
table.AddColumn(new TableColumn("Baz") { Padding = new Padding(3, 0, 2, 0) }); table.AddColumn(new TableColumn("Baz") { Padding = new Padding(3, 0, 2, 0) });
@ -344,7 +344,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_Without_Rows() public Task Should_Render_Table_Without_Rows()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table(); var table = new Table();
table.AddColumns("Foo", "Bar"); table.AddColumns("Foo", "Bar");
table.AddColumn(new TableColumn("Baz") { Padding = new Padding(3, 0, 2, 0) }); table.AddColumn(new TableColumn("Baz") { Padding = new Padding(3, 0, 2, 0) });
@ -361,7 +361,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Not_Draw_Tables_That_Are_Impossible_To_Draw() public Task Should_Not_Draw_Tables_That_Are_Impossible_To_Draw()
{ {
// Given // Given
var console = new FakeConsole(width: 25); var console = new TestConsole().Width(25);
var first = new Table().Border(TableBorder.Rounded).BorderColor(Color.Red); var first = new Table().Border(TableBorder.Rounded).BorderColor(Color.Red);
first.AddColumn(new TableColumn("[u]PS1[/]").Centered()); first.AddColumn(new TableColumn("[u]PS1[/]").Centered());
@ -399,7 +399,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Table_With_Title_And_Caption_Correctly() public Task Should_Render_Table_With_Title_And_Caption_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table { Border = TableBorder.Rounded }; var table = new Table { Border = TableBorder.Rounded };
table.Title = new TableTitle("Hello World"); table.Title = new TableTitle("Hello World");
table.Caption = new TableTitle("Goodbye World"); table.Caption = new TableTitle("Goodbye World");
@ -419,7 +419,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Left_Align_Table_With_Title_And_Caption_Correctly() public Task Should_Left_Align_Table_With_Title_And_Caption_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table { Border = TableBorder.Rounded }; var table = new Table { Border = TableBorder.Rounded };
table.LeftAligned(); table.LeftAligned();
table.Title = new TableTitle("Hello World"); table.Title = new TableTitle("Hello World");
@ -440,7 +440,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Center_Table_With_Title_And_Caption_Correctly() public Task Should_Center_Table_With_Title_And_Caption_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table { Border = TableBorder.Rounded }; var table = new Table { Border = TableBorder.Rounded };
table.Centered(); table.Centered();
table.Title = new TableTitle("Hello World"); table.Title = new TableTitle("Hello World");
@ -461,7 +461,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Right_Align_Table_With_Title_And_Caption_Correctly() public Task Should_Right_Align_Table_With_Title_And_Caption_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var table = new Table { Border = TableBorder.Rounded }; var table = new Table { Border = TableBorder.Rounded };
table.RightAligned(); table.RightAligned();
table.Title = new TableTitle("Hello World"); table.Title = new TableTitle("Hello World");

View File

@ -11,11 +11,11 @@ namespace Spectre.Console.Tests.Unit
public void Should_Consider_The_Longest_Word_As_Minimum_Width() public void Should_Consider_The_Longest_Word_As_Minimum_Width()
{ {
// Given // Given
var caps = new FakeCapabilities { Unicode = true }; var caps = new TestCapabilities { Unicode = true };
var text = new Text("Foo Bar Baz\nQux\nLol mobile"); var text = new Text("Foo Bar Baz\nQux\nLol mobile");
// When // When
var result = ((IRenderable)text).Measure(new RenderContext(caps), 80); var result = ((IRenderable)text).Measure(caps.CreateRenderContext(), 80);
// Then // Then
result.Min.ShouldBe(6); result.Min.ShouldBe(6);
@ -25,11 +25,11 @@ namespace Spectre.Console.Tests.Unit
public void Should_Consider_The_Longest_Line_As_Maximum_Width() public void Should_Consider_The_Longest_Line_As_Maximum_Width()
{ {
// Given // Given
var caps = new FakeCapabilities { Unicode = true }; var caps = new TestCapabilities { Unicode = true };
var text = new Text("Foo Bar Baz\nQux\nLol mobile"); var text = new Text("Foo Bar Baz\nQux\nLol mobile");
// When // When
var result = ((IRenderable)text).Measure(new RenderContext(caps), 80); var result = ((IRenderable)text).Measure(caps.CreateRenderContext(), 80);
// Then // Then
result.Max.ShouldBe(11); result.Max.ShouldBe(11);
@ -39,7 +39,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Render_Unstyled_Text_As_Expected() public void Should_Render_Unstyled_Text_As_Expected()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var text = new Text("Hello World"); var text = new Text("Hello World");
// When // When
@ -55,7 +55,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Write_Line_Breaks(string input) public void Should_Write_Line_Breaks(string input)
{ {
// Given // Given
var console = new FakeConsole(width: 5); var console = new TestConsole();
var text = new Text(input); var text = new Text(input);
// When // When
@ -69,7 +69,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Render_Panel_2() public void Should_Render_Panel_2()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
// When // When
console.Write(new Markup("[b]Hello World[/]\n[yellow]Hello World[/]")); console.Write(new Markup("[b]Hello World[/]\n[yellow]Hello World[/]"));
@ -87,7 +87,7 @@ namespace Spectre.Console.Tests.Unit
int width, string input, string expected) int width, string input, string expected)
{ {
// Given // Given
var console = new FakeConsole(width); var console = new TestConsole().Width(width);
var text = new Text(input); var text = new Text(input);
// When // When
@ -106,7 +106,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Overflow_Text_Correctly(Overflow overflow, string expected) public void Should_Overflow_Text_Correctly(Overflow overflow, string expected)
{ {
// Given // Given
var console = new FakeConsole(14); var console = new TestConsole().Width(14);
var text = new Text("foo pneumonoultramicroscopicsilicovolcanoconiosis bar qux") var text = new Text("foo pneumonoultramicroscopicsilicovolcanoconiosis bar qux")
.Overflow(overflow); .Overflow(overflow);

View File

@ -17,7 +17,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Tree_Correctly() public Task Should_Render_Tree_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var tree = new Tree(new Text("Root node")).Guide(TreeGuide.DoubleLine); var tree = new Tree(new Text("Root node")).Guide(TreeGuide.DoubleLine);
@ -48,7 +48,7 @@ namespace Spectre.Console.Tests.Unit
public Task Should_Render_Tree_With_No_Child_Nodes_Correctly() public Task Should_Render_Tree_With_No_Child_Nodes_Correctly()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var tree = new Tree(new Text("Root node")); var tree = new Tree(new Text("Root node"));
// When // When
@ -62,7 +62,7 @@ namespace Spectre.Console.Tests.Unit
public void Should_Throw_If_Tree_Contains_Cycles() public void Should_Throw_If_Tree_Contains_Cycles()
{ {
// Given // Given
var console = new FakeConsole(width: 80); var console = new TestConsole();
var child2 = new TreeNode(new Text("child 2")); var child2 = new TreeNode(new Text("child 2"));
var child3 = new TreeNode(new Text("child 3")); var child3 = new TreeNode(new Text("child 3"));

View File

@ -14,7 +14,7 @@ namespace Spectre.Console.Tests
} }
var assembly = Assembly.GetCallingAssembly(); var assembly = Assembly.GetCallingAssembly();
resourceName = resourceName.ReplaceExact("/", "."); resourceName = resourceName.Replace("/", ".");
return assembly.GetManifestResourceStream(resourceName); return assembly.GetManifestResourceStream(resourceName);
} }
@ -31,7 +31,7 @@ namespace Spectre.Console.Tests
throw new ArgumentNullException(nameof(resourceName)); throw new ArgumentNullException(nameof(resourceName));
} }
resourceName = resourceName.ReplaceExact("/", "."); resourceName = resourceName.Replace("/", ".");
return assembly.GetManifestResourceStream(resourceName); return assembly.GetManifestResourceStream(resourceName);
} }
} }

View File

@ -0,0 +1,20 @@
using System;
using System.Diagnostics;
namespace Spectre.Console
{
public static class ShouldlyExtensions
{
[DebuggerStepThrough]
public static T And<T>(this T item, Action<T> action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
action(item);
return item;
}
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Spectre.Console.Testing;
namespace Spectre.Console.Tests
{
public static class TestConsoleExtensions
{
private static readonly Regex _lineNumberRegex = new Regex(":\\d+", RegexOptions.Singleline);
private static readonly Regex _filenameRegex = new Regex("\\sin\\s.*cs:nn", RegexOptions.Multiline);
public static string WriteNormalizedException(this TestConsole console, Exception ex, ExceptionFormats formats = ExceptionFormats.Default)
{
if (!string.IsNullOrWhiteSpace(console.Output))
{
throw new InvalidOperationException("Output buffer is not empty.");
}
console.WriteException(ex, formats);
return string.Join("\n", NormalizeStackTrace(console.Output)
.NormalizeLineEndings()
.Split(new char[] { '\n' })
.Select(line => line.TrimEnd()));
}
public static string NormalizeStackTrace(string text)
{
text = _lineNumberRegex.Replace(text, match =>
{
return ":nn";
});
return _filenameRegex.Replace(text, match =>
{
var value = match.Value;
var index = value.LastIndexOfAny(new[] { '\\', '/' });
var filename = value.Substring(index + 1, value.Length - index - 1);
return $" in /xyz/{filename}";
});
}
}
}

View File

@ -6,13 +6,11 @@ namespace Spectre.Console.Testing
{ {
public sealed class FakeTypeRegistrar : ITypeRegistrar public sealed class FakeTypeRegistrar : ITypeRegistrar
{ {
private readonly ITypeResolver _resolver;
public Dictionary<Type, List<Type>> Registrations { get; } public Dictionary<Type, List<Type>> Registrations { get; }
public Dictionary<Type, List<object>> Instances { get; } public Dictionary<Type, List<object>> Instances { get; }
public FakeTypeRegistrar(ITypeResolver resolver = null) public FakeTypeRegistrar()
{ {
_resolver = resolver;
Registrations = new Dictionary<Type, List<Type>>(); Registrations = new Dictionary<Type, List<Type>>();
Instances = new Dictionary<Type, List<object>>(); Instances = new Dictionary<Type, List<object>>();
} }
@ -52,7 +50,7 @@ namespace Spectre.Console.Testing
public ITypeResolver Build() public ITypeResolver Build()
{ {
return _resolver; return null;
} }
} }
} }

View File

@ -86,6 +86,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{A0C772BA-C
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "..\examples\Shared\Shared.csproj", "{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "..\examples\Shared\Shared.csproj", "{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Internal", "Internal", "{0FC844AD-FCBB-4B2F-9AEC-6CB5505E49E3}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU