Add support for manipulating individual table rows

Closes #500
This commit is contained in:
Patrik Svensson
2021-08-05 22:14:19 +02:00
committed by Patrik Svensson
parent 57731c0d55
commit e169df6303
25 changed files with 833 additions and 59 deletions

View File

@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using Shouldly;
using Spectre.Console.Testing;
using Spectre.Console.Tests.Data;

View File

@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spectre.Console.Testing;
using Spectre.Verify.Extensions;
using VerifyXunit;
using Xunit;
namespace Spectre.Console.Tests.Unit
{
[UsesVerify]
[ExpectationPath("Widgets/Table/Rows/Extensions")]
public sealed class TableRowCollectionExtensionsTests
{
[UsesVerify]
public sealed class TheAddRowMethod
{
[Fact]
[Expectation("Add", "Renderables")]
public Task Should_Add_Renderables()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.AddColumn("Column #2");
table.AddRow(new[] { new Text("1"), new Text("1-2") });
table.AddRow(new[] { new Text("2"), new Text("2-2") });
table.AddRow(new[] { new Text("3"), new Text("3-2") });
// When
console.Write(table);
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Add", "Strings")]
public Task Should_Add_Strings()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.AddColumn("Column #2");
table.AddRow("1", "1-2");
table.AddRow("2", "2-2");
table.AddRow("3", "3-2");
// When
console.Write(table);
// Then
return Verifier.Verify(console.Output);
}
}
[UsesVerify]
public sealed class TheInsertRowMethod
{
[Fact]
[Expectation("Insert", "Renderables")]
public Task Should_Insert_Renderables()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.AddColumn("Column #2");
table.AddRow(new[] { new Text("1"), new Text("1-2") });
table.AddRow(new[] { new Text("2"), new Text("2-2") });
// When
table.InsertRow(1, new[] { new Text("3"), new Text("3-2") });
// Then
console.Write(table);
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("Insert", "Strings")]
public Task Should_Insert_Strings()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.AddColumn("Column #2");
table.AddRow("1", "1-2");
table.AddRow("2", "2-2");
// When
table.InsertRow(1, "3", "3-2");
// Then
console.Write(table);
return Verifier.Verify(console.Output);
}
}
[UsesVerify]
public sealed class TheRemoveRowMethod
{
[Fact]
[Expectation("Remove")]
public Task Should_Remove_Row()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.AddColumn("Column #2");
table.AddRow(new[] { new Text("1"), new Text("1-2") });
table.AddRow(new[] { new Text("2"), new Text("2-2") });
table.AddRow(new[] { new Text("3"), new Text("3-2") });
// When
table.RemoveRow(1);
// Then
console.Write(table);
return Verifier.Verify(console.Output);
}
}
}
}

View File

@ -0,0 +1,226 @@
using System;
using System.Threading.Tasks;
using Shouldly;
using Spectre.Console.Testing;
using Spectre.Verify.Extensions;
using VerifyXunit;
using Xunit;
namespace Spectre.Console.Tests.Unit
{
[ExpectationPath("Widgets/Table/Rows")]
public sealed class TableRowCollectionTests
{
[UsesVerify]
public sealed class TheAddMethod
{
[Fact]
public void Should_Throw_If_Columns_Are_Null()
{
// Given
var table = new Table();
// When
var result = Record.Exception(() => table.Rows.Add(null));
// Then
result.ShouldBeOfType<ArgumentNullException>()
.ParamName.ShouldBe("columns");
}
[Fact]
public void Should_Add_Row_To_Collection()
{
// Given
var table = new Table();
table.AddColumn("Column #1");
// When
table.Rows.Add(new[] { Text.Empty });
// Then
table.Rows.Count.ShouldBe(1);
}
[Fact]
public void Should_Return_Index_Of_Added_Row()
{
// Given
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { Text.Empty });
// When
var result = table.Rows.Add(new[] { Text.Empty });
// Then
result.ShouldBe(1);
}
[Fact]
[Expectation("Add")]
public Task Should_Add_Item_At_Correct_Place()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { new Text("1") });
table.Rows.Add(new[] { new Text("2") });
table.Rows.Add(new[] { new Text("3") });
// When
console.Write(table);
// Then
return Verifier.Verify(console.Output);
}
}
[UsesVerify]
public sealed class TheInsertMethod
{
[Fact]
public void Should_Throw_If_Columns_Are_Null()
{
// Given
var table = new Table();
// When
var result = Record.Exception(() => table.Rows.Insert(0, null));
// Then
result.ShouldBeOfType<ArgumentNullException>()
.ParamName.ShouldBe("columns");
}
[Fact]
public void Should_Insert_Row()
{
// Given
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { Text.Empty });
// When
table.Rows.Insert(0, new[] { Text.Empty });
// Then
table.Rows.Count.ShouldBe(2);
}
[Fact]
public void Should_Return_Index_Of_Inserted_Row()
{
// Given
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { new Text("1") });
table.Rows.Add(new[] { new Text("2") });
// When
var result = table.Rows.Insert(1, new[] { new Text("3") });
// Then
result.ShouldBe(1);
}
[Fact]
[Expectation("Insert")]
public Task Should_Insert_Item_At_Correct_Place()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { new Text("1") });
table.Rows.Add(new[] { new Text("2") });
table.Rows.Insert(1, new[] { new Text("3") });
// When
console.Write(table);
// Then
return Verifier.Verify(console.Output);
}
}
[UsesVerify]
public sealed class TheRemoveMethod
{
[Fact]
public void Should_Throw_If_Index_Is_Negative()
{
// Given
var table = new Table();
table.AddColumn("Column #1");
// When
var result = Record.Exception(() => table.Rows.RemoveAt(-1));
// Then
result.ShouldBeOfType<IndexOutOfRangeException>()
.Message.ShouldBe("Table row index cannot be negative.");
}
[Fact]
public void Should_Throw_If_Index_Is_Larger_Than_Number_Of_Rows()
{
// Given
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { new Text("1") });
table.Rows.Add(new[] { new Text("2") });
table.Rows.Add(new[] { new Text("3") });
// When
var result = Record.Exception(() => table.Rows.RemoveAt(3));
// Then
result.ShouldBeOfType<IndexOutOfRangeException>()
.Message.ShouldBe("Table row index cannot exceed the number of rows in the table.");
}
[Fact]
[Expectation("Remove")]
public Task Should_Remove_Row()
{
// Given
var console = new TestConsole();
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { new Text("1") });
table.Rows.Add(new[] { new Text("2") });
table.Rows.Add(new[] { new Text("3") });
table.Rows.RemoveAt(1);
// When
console.Write(table);
// Then
return Verifier.Verify(console.Output);
}
}
public sealed class TheClearMethod
{
[Fact]
public void Should_Remove_All_Rows()
{
// Given
var table = new Table();
table.AddColumn("Column #1");
table.Rows.Add(new[] { new Text("1") });
table.Rows.Add(new[] { new Text("2") });
table.Rows.Add(new[] { new Text("3") });
table.Rows.Clear();
// When
var result = table.Rows.Count;
// Then
result.ShouldBe(0);
}
}
}
}

View File

@ -78,20 +78,6 @@ namespace Spectre.Console.Tests.Unit
.ParamName.ShouldBe("columns");
}
[Fact]
public void Should_Throw_If_Renderable_Rows_Are_Null()
{
// Given
var table = new Table();
// When
var result = Record.Exception(() => table.AddRow(null));
// Then
result.ShouldBeOfType<ArgumentNullException>()
.ParamName.ShouldBe("columns");
}
[Fact]
public void Should_Add_Empty_Items_If_User_Provides_Less_Row_Items_Than_Columns()
{