Add Layout widget (#1041)

* Add width to panels
* Add height to panels
* Replace RenderContext with RenderOptions
* Remove exclusivity from alternative buffer
* Add Layout widget
* Add Align widget
This commit is contained in:
Patrik Svensson
2022-11-15 10:12:17 +01:00
committed by GitHub
parent 9ce3b99cd6
commit c3ec6a7363
137 changed files with 2651 additions and 387 deletions

View File

@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('Windows'))">net7.0;net48</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOSPlatform('Windows'))">net7.0</TargetFrameworks>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
@ -30,15 +29,4 @@
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Expectations\Help\Description_No_Trailing_Period.Output.received.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
<None Update="Expectations\Help\Description_No_Trailing_Period.Output.verified.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@

┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@

┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@
 ┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@

┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@

┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@
┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@

┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@

┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@
 ┌──────────────┐
│ Hello World! │
└──────────────┘

View File

@ -0,0 +1,15 @@
╭─40 x 15──────────────────────────────╮
│ │
│ │
│ │
│ │
│ │
│ │
│ Placeholder │
│ │
│ │
│ │
│ │
│ │
│ │
╰──────────────────────────────────────╯

View File

@ -0,0 +1,15 @@
╭─T1 (40 x 7)──────────────────────────╮
│ │
│ │
│ Placeholder │
│ │
│ │
╰──────────────────────────────────────╯
╭─T2 (40 x 8)──────────────────────────╮
│ │
│ │
│ Placeholder │
│ │
│ │
│ │
╰──────────────────────────────────────╯

View File

@ -0,0 +1,15 @@
╔══════════════════════════════════════╗
║ Hello ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
╚══════════════════════════════════════╝

View File

@ -0,0 +1,15 @@
╭─Left (20 x 15)───╮╭─Right (20 x 15)──╮
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ Placeholder ││ Placeholder │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰──────────────────╯╰──────────────────╯

View File

@ -0,0 +1,15 @@
╭─L1…────╮╭─L2…────╮╭─R1…────╮╭─R2…────╮
│ ││ ││ ││ │
│ ││ ││ ││ │
│ ││ ││ ││ │
│ ││ ││ ││ │
│ ││ ││ ││ │
│ Placeh ││ Placeh ││ Placeh ││ Placeh │
│ older ││ older ││ older ││ older │
│ ││ ││ ││ │
│ ││ ││ ││ │
│ ││ ││ ││ │
│ ││ ││ ││ │
│ ││ ││ ││ │
│ ││ ││ ││ │
╰────────╯╰────────╯╰────────╯╰────────╯

View File

@ -0,0 +1,15 @@
╭─T1 (40 x 3)──────────────────────────╮
│ Placeholder │
╰──────────────────────────────────────╯
╭─T2 (40 x 4)──────────────────────────╮
│ Placeholder │
│ │
╰──────────────────────────────────────╯
╭─B1 (40 x 4)──────────────────────────╮
│ Placeholder │
│ │
╰──────────────────────────────────────╯
╭─B2 (40 x 4)──────────────────────────╮
│ Placeholder │
│ │
╰──────────────────────────────────────╯

View File

@ -0,0 +1,15 @@
╭─A (20 x 3)───────╮╭─B (20 x 3)───────╮
│ Placeholder ││ Placeholder │
╰──────────────────╯╰──────────────────╯
╭─C (20 x 4)───────╮╭─D (20 x 4)───────╮
│ Placeholder ││ Placeholder │
│ ││ │
╰──────────────────╯╰──────────────────╯
╭─E (20 x 4)───────╮╭─F (20 x 4)───────╮
│ Placeholder ││ Placeholder │
│ ││ │
╰──────────────────╯╰──────────────────╯
╭─G (20 x 4)───────╮╭─H (20 x 4)───────╮
│ Placeholder ││ Placeholder │
│ ││ │
╰──────────────────╯╰──────────────────╯

View File

@ -0,0 +1,15 @@
╭─Left (30 x 15)─────────────╮╭─Right…─╮
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ Placeh │
│ Placeholder ││ older │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰────────────────────────────╯╰────────╯

View File

@ -0,0 +1,15 @@
╭─Left (30 x 15)─────────────╮╭─Right…─╮
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ Placeh │
│ Placeholder ││ older │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰────────────────────────────╯╰────────╯

View File

@ -0,0 +1,15 @@
╭─Left (28 x 15)───────────╮╭─Right…───╮
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ Placehol │
│ Placeholder ││ der │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰──────────────────────────╯╰──────────╯

View File

@ -0,0 +1,15 @@
╭─Top (40 x 7)─────────────────────────╮
│ │
│ │
│ Placeholder │
│ │
│ │
╰──────────────────────────────────────╯
╭─Bottom (40 x 8)──────────────────────╮
│ │
│ │
│ Placeholder │
│ │
│ │
│ │
╰──────────────────────────────────────╯

View File

@ -0,0 +1,15 @@
╭─B (40 x 3)───────────────────────────╮
│ Placeholder │
╰──────────────────────────────────────╯
╭─C (20 x 4)───────╮╭─D (20 x 4)───────╮
│ Placeholder ││ Placeholder │
│ ││ │
╰──────────────────╯╰──────────────────╯
╭─E (20 x 4)───────╮╭─F (20 x 4)───────╮
│ Placeholder ││ Placeholder │
│ ││ │
╰──────────────────╯╰──────────────────╯
╭─G (40 x 4)───────────────────────────╮
│ Placeholder │
│ │
╰──────────────────────────────────────╯

View File

@ -0,0 +1,25 @@
┌───────────────────┐
│ Hello World │
│ Hello Hello Hello │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└───────────────────┘

View File

@ -0,0 +1,3 @@
┌───────────────────────┐
│ Hello World │
└───────────────────────┘

View File

@ -0,0 +1,25 @@
┌────────────────────────────────────────────────┐
│ Hello World │
│ Hello Hello Hello │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└────────────────────────────────────────────────┘

View File

@ -0,0 +1,3 @@
┌──────────────────┐
│ Hello World │
└──────────────────┘

View File

@ -0,0 +1,6 @@
 ┌────────┬────────┬───────┐
│ Foo │ Bar │ Baz │
├────────┼────────┼───────┤
│ Qux │ Corgi │ Waldo │
│ Grault │ Garply │ Fred │
└────────┴────────┴───────┘

View File

@ -0,0 +1,6 @@
┌────────┬────────┬───────┐
│ Foo │ Bar │ Baz │
├────────┼────────┼───────┤
│ Qux │ Corgi │ Waldo │
│ Grault │ Garply │ Fred │
└────────┴────────┴───────┘

View File

@ -0,0 +1,6 @@
 ┌────────┬────────┬───────┐
│ Foo │ Bar │ Baz │
├────────┼────────┼───────┤
│ Qux │ Corgi │ Waldo │
│ Grault │ Garply │ Fred │
└────────┴────────┴───────┘

View File

@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('Windows'))">net7.0;net48</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOSPlatform('Windows'))">net7.0</TargetFrameworks>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>

View File

@ -16,7 +16,7 @@ public sealed class ProgressColumnFixture<T>
public string Render()
{
var console = new TestConsole();
var context = new RenderContext(console.Profile.Capabilities);
var context = RenderOptions.Create(console, console.Profile.Capabilities);
console.Write(Column.Render(context, Task, TimeSpan.Zero));
return console.Output;
}

View File

@ -4,7 +4,7 @@ public sealed class RenderHookTests
{
private sealed class HelloRenderHook : IRenderHook
{
public IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
public IEnumerable<IRenderable> Process(RenderOptions options, IEnumerable<IRenderable> renderables)
{
return new IRenderable[] { new Text("Hello\n") }.Concat(renderables);
}

View File

@ -0,0 +1,155 @@
using Spectre.Console.Extensions;
namespace Spectre.Console.Tests.Unit;
[UsesVerify]
[ExpectationPath("Widgets/Align")]
public sealed class AlignTests
{
[UsesVerify]
public sealed class Left
{
[Fact]
[Expectation("Left_Top")]
public Task Should_Render_Panel_Left_Aligned_At_Top()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Left(new Panel("Hello World!"), VerticalAlignment.Top).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Left_Middle")]
public Task Should_Render_Panel_Left_Aligned_At_Middle()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Left(new Panel("Hello World!"), VerticalAlignment.Middle).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Left_Bottom")]
public Task Should_Render_Panel_Left_Aligned_At_Bottom()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Left(new Panel("Hello World!"), VerticalAlignment.Bottom).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
}
[UsesVerify]
public sealed class Center
{
[Fact]
[Expectation("Center_Top")]
public Task Should_Render_Panel_Center_Aligned_At_Top()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Center(new Panel("Hello World!"), VerticalAlignment.Top).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Center_Middle")]
public Task Should_Render_Panel_Center_Aligned_At_Middle()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Center(new Panel("Hello World!"), VerticalAlignment.Middle).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Center_Bottom")]
public Task Should_Render_Panel_Center_Aligned_At_Bottom()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Center(new Panel("Hello World!"), VerticalAlignment.Bottom).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
}
[UsesVerify]
public sealed class Right
{
[Fact]
[Expectation("Right_Top")]
public Task Should_Render_Panel_Right_Aligned_At_Top()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Right(new Panel("Hello World!"), VerticalAlignment.Top).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Right_Middle")]
public Task Should_Render_Panel_Right_Aligned_At_Middle()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Right(new Panel("Hello World!"), VerticalAlignment.Middle).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Right_Bottom")]
public Task Should_Render_Panel_Right_Aligned_At_Bottom()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var align = Align.Right(new Panel("Hello World!"), VerticalAlignment.Bottom).Height(15);
// When
console.Write(align);
// Then
return Verifier.Verify(console.Output);
}
}
}

View File

@ -60,7 +60,7 @@ public sealed class FigletTests
// Given
var console = new TestConsole().Width(120);
var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Left);
.Justify(Justify.Left);
// When
console.Write(text);
@ -76,7 +76,7 @@ public sealed class FigletTests
// Given
var console = new TestConsole().Width(120);
var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Center);
.Justify(Justify.Center);
// When
console.Write(text);
@ -92,7 +92,7 @@ public sealed class FigletTests
// Given
var console = new TestConsole().Width(120);
var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Right);
.Justify(Justify.Right);
// When
console.Write(text);

View File

@ -0,0 +1,268 @@
namespace Spectre.Console.Tests.Unit;
[UsesVerify]
[ExpectationPath("Widgets/Layout")]
public sealed class LayoutTests
{
[Fact]
[Expectation("Render_Empty_Layout")]
public Task Should_Render_Empty_Layout()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout();
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout")]
public Task Should_Render_Empty_Layout_With_Renderable()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout().Update(new Panel("Hello").DoubleBorder().Expand());
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Columns")]
public Task Should_Render_Layout_With_Columns()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitColumns(
new Layout("Left"),
new Layout("Right"));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Rows")]
public Task Should_Render_Layout_With_Rows()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitRows(
new Layout("Top"),
new Layout("Bottom"));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Nested_Columns")]
public Task Should_Render_Layout_With_Nested_Columns()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitColumns(
new Layout("Left")
.SplitColumns(
new Layout("L1"),
new Layout("L2")),
new Layout("Right")
.SplitColumns(
new Layout("R1"),
new Layout("R2")));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Nested_Rows")]
public Task Should_Render_Layout_With_Nested_Rows()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitRows(
new Layout("Top")
.SplitRows(
new Layout("T1"),
new Layout("T2")),
new Layout("Bottom")
.SplitRows(
new Layout("B1"),
new Layout("B2")));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Nested_Rows_And_Columns")]
public Task Should_Render_Layout_With_Nested_Rows_And_Columns()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitRows(
new Layout("Top")
.SplitRows(
new Layout("T1")
.SplitColumns(
new Layout("A"),
new Layout("B")),
new Layout("T2")
.SplitColumns(
new Layout("C"),
new Layout("D"))),
new Layout("Bottom")
.SplitRows(
new Layout("B1")
.SplitColumns(
new Layout("E"),
new Layout("F")),
new Layout("B2")
.SplitColumns(
new Layout("G"),
new Layout("H"))));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_Without_Invisible_Children")]
public Task Should_Render_Layout_Without_Invisible_Children()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitRows(
new Layout("Top")
.SplitRows(
new Layout("T1")
.SplitColumns(
new Layout("A").Invisible(),
new Layout("B")),
new Layout("T2")
.SplitColumns(
new Layout("C"),
new Layout("D"))),
new Layout("Bottom")
.SplitRows(
new Layout("B1")
.SplitColumns(
new Layout("E"),
new Layout("F")),
new Layout("B2")
.SplitColumns(
new Layout("G"),
new Layout("H").Invisible())));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Respect_To_Ratio")]
public Task Should_Render_Layout_With_Respect_To_Ratio()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitColumns(
new Layout("Left").Ratio(3),
new Layout("Right"));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Respect_To_Size")]
public Task Should_Render_Layout_With_Respect_To_Size()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitColumns(
new Layout("Left").Size(28),
new Layout("Right"));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Layout_With_Respect_To_Minimum_Size")]
public Task Should_Render_Layout_With_Respect_To_Minimum_Size()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitColumns(
new Layout("Left").Size(28).MinimumSize(30),
new Layout("Right"));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Fallback_Layout")]
public Task Should_Fall_Back_To_Parent_Layout_If_All_Children_Are_Invisible()
{
// Given
var console = new TestConsole().Size(new Size(40, 15));
var layout = new Layout()
.SplitRows(
new Layout("T1").SplitColumns(
new Layout("A").Invisible(),
new Layout("B").Invisible()),
new Layout("T2").SplitColumns(
new Layout("C").Invisible(),
new Layout("D").Invisible()));
// When
console.Write(layout);
// Then
return Verifier.Verify(console.Output);
}
}

View File

@ -81,7 +81,7 @@ public sealed class PanelTests
// When
console.Write(new Panel("Hello World")
{
Header = new PanelHeader("Greeting").LeftAligned(),
Header = new PanelHeader("Greeting").LeftJustified(),
Expand = true,
});
@ -117,7 +117,7 @@ public sealed class PanelTests
// When
console.Write(new Panel("Hello World")
{
Header = new PanelHeader("Greeting").RightAligned(),
Header = new PanelHeader("Greeting").RightJustified(),
Expand = true,
});
@ -204,6 +204,76 @@ public sealed class PanelTests
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Width")]
public Task Should_Render_To_Specified_Width()
{
// Given
var console = new TestConsole();
// When
console.Write(new Panel(new Text("Hello World"))
{
Width = 25,
});
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Width_MaxWidth")]
public Task Should_Use_Max_Width_If_Specified_Width_Is_Too_Large()
{
// Given
var console = new TestConsole();
console.Profile.Width = 20;
// When
console.Write(new Panel(new Text("Hello World"))
{
Width = 25,
});
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Height")]
public Task Should_Render_To_Specified_Height()
{
// Given
var console = new TestConsole();
// When
console.Write(new Panel(new Text("Hello World\nHello Hello Hello"))
{
Height = 25,
});
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Width_Height")]
public Task Should_Render_To_Specified_Width_And_Height()
{
// Given
var console = new TestConsole();
// When
console.Write(new Panel("Hello World\nHello Hello Hello")
{
Width = 50,
Height = 25,
});
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Child_RightAligned")]
public Task Should_Justify_Child_To_Right_Correctly()
@ -213,7 +283,7 @@ public sealed class PanelTests
// When
console.Write(
new Panel(new Text("Hello World").RightAligned())
new Panel(new Text("Hello World").RightJustified())
{
Expand = true,
});

View File

@ -70,7 +70,7 @@ public sealed class RuleTests
// When
console.Write(new Rule("Hello World")
{
Alignment = Justify.Left,
Justification = Justify.Left,
});
// Then
@ -87,7 +87,7 @@ public sealed class RuleTests
// When
console.Write(new Rule("Hello World")
{
Alignment = Justify.Right,
Justification = Justify.Right,
});
// Then

View File

@ -187,7 +187,9 @@ public sealed class TableTests
// Given
var console = new TestConsole();
var table = new Table();
#pragma warning disable CS0618 // Type or member is obsolete
table.Alignment = Justify.Left;
#pragma warning restore CS0618 // Type or member is obsolete
table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo");
table.AddRow("Grault", "Garply", "Fred");
@ -199,6 +201,24 @@ public sealed class TableTests
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_LeftAligned", "Align_Widget")]
public Task Should_Left_Align_Table_Correctly_When_Wrapped_In_Align_Widget()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo");
table.AddRow("Grault", "Garply", "Fred");
// When
console.Write(Align.Left(table));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Centered")]
public Task Should_Center_Table_Correctly()
@ -206,7 +226,9 @@ public sealed class TableTests
// Given
var console = new TestConsole();
var table = new Table();
#pragma warning disable CS0618 // Type or member is obsolete
table.Alignment = Justify.Center;
#pragma warning restore CS0618 // Type or member is obsolete
table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo");
table.AddRow("Grault", "Garply", "Fred");
@ -218,6 +240,24 @@ public sealed class TableTests
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Centered", "Align_Widget")]
public Task Should_Center_Table_Correctly_When_Wrapped_In_Align_Widget()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo");
table.AddRow("Grault", "Garply", "Fred");
// When
console.Write(Align.Center(table));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_RightAligned")]
public Task Should_Right_Align_Table_Correctly()
@ -225,7 +265,9 @@ public sealed class TableTests
// Given
var console = new TestConsole();
var table = new Table();
#pragma warning disable CS0618 // Type or member is obsolete
table.Alignment = Justify.Right;
#pragma warning restore CS0618 // Type or member is obsolete
table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo");
table.AddRow("Grault", "Garply", "Fred");
@ -237,6 +279,24 @@ public sealed class TableTests
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_RightAligned", "Align_Widget")]
public Task Should_Right_Align_Table_Correctly_When_Wrapped_In_Align_Widget()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumns("Foo", "Bar", "Baz");
table.AddRow("Qux", "Corgi", "Waldo");
table.AddRow("Grault", "Garply", "Fred");
// When
console.Write(Align.Right(table));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Render_Nested")]
public Task Should_Render_Table_Nested_In_Panels_Correctly()
@ -380,7 +440,7 @@ public sealed class TableTests
table.AddColumn(new TableColumn(new Panel("[u]DEF[/]").BorderColor(Color.Green)));
table.AddColumn(new TableColumn(new Panel("[u]GHI[/]").BorderColor(Color.Blue)));
table.AddRow(new Text("Hello").Centered(), new Markup("[red]World[/]"), Text.Empty);
table.AddRow(second, new Text("Whaat"), new Text("Lol").RightAligned());
table.AddRow(second, new Text("Whaat"), new Text("Lol").RightJustified());
table.AddRow(new Markup("[blue]Hej[/]"), new Markup("[yellow]Världen[/]"), Text.Empty);
// When

View File

@ -77,7 +77,7 @@ public sealed class TextPathTests
var console = new TestConsole().Width(40);
// When
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").RightAligned());
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").RightJustified());
// Then
console.Output.TrimEnd('\n')

View File

@ -42,11 +42,13 @@ public sealed class TextTests
public void Should_Consider_The_Longest_Word_As_Minimum_Width()
{
// Given
var console = new TestConsole();
var caps = new TestCapabilities { Unicode = true };
var text = new Text("Foo Bar Baz\nQux\nLol mobile");
// When
var result = ((IRenderable)text).Measure(caps.CreateRenderContext(), 80);
var result = ((IRenderable)text).Measure(
caps.CreateRenderContext(console), 80);
// Then
result.Min.ShouldBe(6);
@ -56,11 +58,13 @@ public sealed class TextTests
public void Should_Consider_The_Longest_Line_As_Maximum_Width()
{
// Given
var console = new TestConsole();
var caps = new TestCapabilities { Unicode = true };
var text = new Text("Foo Bar Baz\nQux\nLol mobile");
// When
var result = ((IRenderable)text).Measure(caps.CreateRenderContext(), 80);
var result = ((IRenderable)text).Measure(
caps.CreateRenderContext(console), 80);
// Then
result.Max.ShouldBe(11);