mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-12-27 19:25:47 +08:00
Add column support
Adds support for rendering arbitrary data into columns. Closes #67
This commit is contained in:
committed by
Patrik Svensson
parent
e946289bd9
commit
ae6d2c63a3
46
src/Spectre.Console.Tests/Unit/ColumnsTests.cs
Normal file
46
src/Spectre.Console.Tests/Unit/ColumnsTests.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit
|
||||
{
|
||||
public sealed class ColumnsTests
|
||||
{
|
||||
private sealed class User
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Country { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Render_Columns_Correctly()
|
||||
{
|
||||
// Given
|
||||
var console = new PlainConsole(width: 61);
|
||||
var users = new[]
|
||||
{
|
||||
new User { Name = "Savannah Thompson", Country = "Australia" },
|
||||
new User { Name = "Sophie Ramos", Country = "United States" },
|
||||
new User { Name = "Katrin Goldberg", Country = "Germany" },
|
||||
};
|
||||
|
||||
var cards = new List<Panel>();
|
||||
foreach (var user in users)
|
||||
{
|
||||
cards.Add(
|
||||
new Panel($"[b]{user.Name}[/]\n[yellow]{user.Country}[/]")
|
||||
.RoundedBorder().Expand());
|
||||
}
|
||||
|
||||
// When
|
||||
console.Render(new Columns(cards));
|
||||
|
||||
// Then
|
||||
console.Lines.Count.ShouldBe(4);
|
||||
console.Lines[0].ShouldBe("╭────────────────────╮ ╭────────────────╮ ╭─────────────────╮");
|
||||
console.Lines[1].ShouldBe("│ Savannah Thompson │ │ Sophie Ramos │ │ Katrin Goldberg │");
|
||||
console.Lines[2].ShouldBe("│ Australia │ │ United States │ │ Germany │");
|
||||
console.Lines[3].ShouldBe("╰────────────────────╯ ╰────────────────╯ ╰─────────────────╯");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ namespace Spectre.Console.Tests.Unit
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Row_Columns_Is_Less_Than_Number_Of_Columns()
|
||||
public void Should_Add_Empty_Items_If_User_Provides_Less_Row_Items_Than_Columns()
|
||||
{
|
||||
// Given
|
||||
var grid = new Grid();
|
||||
@@ -50,11 +50,10 @@ namespace Spectre.Console.Tests.Unit
|
||||
grid.AddColumn();
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => grid.AddRow("Foo"));
|
||||
grid.AddRow("Foo");
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<InvalidOperationException>();
|
||||
result.Message.ShouldBe("The number of row columns are less than the number of grid columns.");
|
||||
grid.RowCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -87,7 +87,7 @@ namespace Spectre.Console.Tests.Unit
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Row_Columns_Is_Less_Than_Number_Of_Columns()
|
||||
public void Should_Add_Empty_Items_If_User_Provides_Less_Row_Items_Than_Columns()
|
||||
{
|
||||
// Given
|
||||
var table = new Table();
|
||||
@@ -95,11 +95,10 @@ namespace Spectre.Console.Tests.Unit
|
||||
table.AddColumn("World");
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => table.AddRow("Foo"));
|
||||
table.AddRow("Foo");
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<InvalidOperationException>();
|
||||
result.Message.ShouldBe("The number of row columns are less than the number of table columns.");
|
||||
table.RowCount.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -65,6 +65,21 @@ namespace Spectre.Console.Tests.Unit
|
||||
fixture.RawOutput.ShouldBe("Hello\n\nWorld\n\n");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Render_Panel_2()
|
||||
{
|
||||
// Given
|
||||
var console = new PlainConsole(width: 80);
|
||||
|
||||
// When
|
||||
console.Render(new Markup("[b]Hello World[/]\n[yellow]Hello World[/]"));
|
||||
|
||||
// Then
|
||||
console.Lines.Count.ShouldBe(2);
|
||||
console.Lines[0].ShouldBe("Hello World");
|
||||
console.Lines[1].ShouldBe("Hello World");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(5, "Hello World", "Hello\nWorld")]
|
||||
[InlineData(10, "Hello Sweet Nice World", "Hello \nSweet Nice\nWorld")]
|
||||
|
||||
Reference in New Issue
Block a user