mirror of
https://github.com/spectreconsole/spectre.console.git
synced 2026-01-02 01:03:32 +01:00
Add support for recording console output
This commit adds support for recording console output as well as exporting it to either text or HTML. A user can also provide their own encoder if they wish.
This commit is contained in:
committed by
Patrik Svensson
parent
b197f278ed
commit
cd0d182f12
66
src/Spectre.Console.Tests/Unit/RecorderTests.cs
Normal file
66
src/Spectre.Console.Tests/Unit/RecorderTests.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit
|
||||
{
|
||||
public sealed class RecorderTests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Export_Text_As_Expected()
|
||||
{
|
||||
// Given
|
||||
var console = new PlainConsole();
|
||||
var recorder = new Recorder(console);
|
||||
|
||||
recorder.Render(new Table()
|
||||
.AddColumns("Foo", "Bar", "Qux")
|
||||
.AddRow("Corgi", "Waldo", "Zap")
|
||||
.AddRow(new Panel("Hello World").RoundedBorder()));
|
||||
|
||||
// When
|
||||
var result = recorder.ExportText().Split(new[] { '\n' });
|
||||
|
||||
// Then
|
||||
result.Length.ShouldBe(8);
|
||||
result[0].ShouldBe("┌─────────────────┬───────┬─────┐");
|
||||
result[1].ShouldBe("│ Foo │ Bar │ Qux │");
|
||||
result[2].ShouldBe("├─────────────────┼───────┼─────┤");
|
||||
result[3].ShouldBe("│ Corgi │ Waldo │ Zap │");
|
||||
result[4].ShouldBe("│ ╭─────────────╮ │ │ │");
|
||||
result[5].ShouldBe("│ │ Hello World │ │ │ │");
|
||||
result[6].ShouldBe("│ ╰─────────────╯ │ │ │");
|
||||
result[7].ShouldBe("└─────────────────┴───────┴─────┘");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Export_Html_As_Expected()
|
||||
{
|
||||
// Given
|
||||
var console = new PlainConsole();
|
||||
var recorder = new Recorder(console);
|
||||
|
||||
recorder.Render(new Table()
|
||||
.AddColumns("[red on black]Foo[/]", "[green bold]Bar[/]", "[blue italic]Qux[/]")
|
||||
.AddRow("[invert underline]Corgi[/]", "[bold strikethrough]Waldo[/]", "[dim]Zap[/]")
|
||||
.AddRow(new Panel("[blue]Hello World[/]")
|
||||
.SetBorderColor(Color.Red).RoundedBorder()));
|
||||
|
||||
// When
|
||||
var html = recorder.ExportHtml();
|
||||
var result = html.Split(new[] { '\n' });
|
||||
|
||||
// Then
|
||||
result.Length.ShouldBe(10);
|
||||
result[0].ShouldBe("<pre style=\"font-size:90%;font-family:consolas,'Courier New',monospace\">");
|
||||
result[1].ShouldBe("<span>┌─────────────────┬───────┬─────┐</span>");
|
||||
result[2].ShouldBe("<span>│ </span><span style=\"color: #FF0000;background-color: #000000\">Foo</span><span> │ </span><span style=\"color: #008000;font-weight: bold;font-style: italic\">Bar</span><span> │ </span><span style=\"color: #0000FF\">Qux</span><span> │</span>");
|
||||
result[3].ShouldBe("<span>├─────────────────┼───────┼─────┤</span>");
|
||||
result[4].ShouldBe("<span>│ </span><span style=\"text-decoration: underline\">Corgi</span><span> │ </span><span style=\"font-weight: bold;font-style: italic;text-decoration: line-through\">Waldo</span><span> │ </span><span style=\"color: #7F7F7F\">Zap</span><span> │</span>");
|
||||
result[5].ShouldBe("<span>│ </span><span style=\"color: #FF0000\">╭─────────────╮</span><span> │ │ │</span>");
|
||||
result[6].ShouldBe("<span>│ </span><span style=\"color: #FF0000\">│</span><span> </span><span style=\"color: #0000FF\">Hello World</span><span> </span><span style=\"color: #FF0000\">│</span><span> │ │ │</span>");
|
||||
result[7].ShouldBe("<span>│ </span><span style=\"color: #FF0000\">╰─────────────╯</span><span> │ │ │</span>");
|
||||
result[8].ShouldBe("<span>└─────────────────┴───────┴─────┘</span>");
|
||||
result[9].ShouldBe("</pre>");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user