Fixed namespace, comments and added tests

This commit is contained in:
tznind
2020-12-29 06:47:51 +00:00
parent d3ec8b2f03
commit 52af2a609e
3 changed files with 104 additions and 8 deletions

View File

@@ -4,7 +4,7 @@ using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace Terminal.Gui.Views {
namespace Terminal.Gui {
/// <summary>
/// Describes how to render a given column in a <see cref="TableView"/> including <see cref="Alignment"/> and textual representation of cells (e.g. date formats)
@@ -148,23 +148,22 @@ namespace Terminal.Gui.Views {
public TableStyle Style { get => style; set {style = value; Update(); } }
/// <summary>
/// Zero indexed offset for the upper left <see cref="DataColumn"/> to display in <see cref="Table"/>.
/// Horizontal scroll offset. The index of the first column in <see cref="Table"/> to display when when rendering the view.
/// </summary>
/// <remarks>This property allows very wide tables to be rendered with horizontal scrolling</remarks>
public int ColumnOffset {
get => columnOffset;
//try to prevent this being set to an out of bounds column
set => columnOffset = Table == null ? 0 : Math.Min (Table.Columns.Count - 1, Math.Max (0, value));
set => columnOffset = Table == null ? 0 :Math.Max (0,Math.Min (Table.Columns.Count - 1, value));
}
/// <summary>
/// Zero indexed offset for the <see cref="DataRow"/> to display in <see cref="Table"/> on line 2 of the control (first line being headers)
/// Vertical scroll offset. The index of the first row in <see cref="Table"/> to display in the first non header line of the control when rendering the view.
/// </summary>
/// <remarks>This property allows very wide tables to be rendered with horizontal scrolling</remarks>
public int RowOffset {
get => rowOffset;
set => rowOffset = Table == null ? 0 : Math.Min (Table.Rows.Count - 1, Math.Max (0, value));
set => rowOffset = Table == null ? 0 : Math.Max (0,Math.Min (Table.Rows.Count - 1, value));
}
/// <summary>
@@ -666,7 +665,7 @@ namespace Terminal.Gui.Views {
/// <remarks>Changes will not be immediately visible in the display until you call <see cref="View.SetNeedsDisplay()"/></remarks>
public void EnsureSelectedCellIsVisible ()
{
if(Table == null){
if(Table == null || Table.Columns.Count <= 0){
return;
}

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Data;
using Terminal.Gui;
using Terminal.Gui.Views;
namespace UICatalog.Scenarios {

View File

@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terminal.Gui;
using Xunit;
namespace UnitTests {
public class TableViewTests
{
[Fact]
public void EnsureValidScrollOffsets_WithNoCells()
{
var tableView = new TableView();
Assert.Equal(0,tableView.RowOffset);
Assert.Equal(0,tableView.ColumnOffset);
// Set empty table
tableView.Table = new DataTable();
// Since table has no rows or columns scroll offset should default to 0
tableView.EnsureValidScrollOffsets();
Assert.Equal(0,tableView.RowOffset);
Assert.Equal(0,tableView.ColumnOffset);
}
[Fact]
public void EnsureValidScrollOffsets_LoadSmallerTable()
{
var tableView = new TableView();
tableView.Bounds = new Rect(0,0,25,10);
Assert.Equal(0,tableView.RowOffset);
Assert.Equal(0,tableView.ColumnOffset);
// Set big table
tableView.Table = BuildTable(25,50);
// Scroll down and along
tableView.RowOffset = 20;
tableView.ColumnOffset = 10;
tableView.EnsureValidScrollOffsets();
// The scroll should be valid at the moment
Assert.Equal(20,tableView.RowOffset);
Assert.Equal(10,tableView.ColumnOffset);
// Set small table
tableView.Table = BuildTable(2,2);
// Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
Assert.Equal(0,tableView.RowOffset);
Assert.Equal(0,tableView.ColumnOffset);
// Trying to set invalid indexes should not be possible
tableView.RowOffset = 20;
tableView.ColumnOffset = 10;
Assert.Equal(1,tableView.RowOffset);
Assert.Equal(1,tableView.ColumnOffset);
}
/// <summary>
/// Builds a simple table of string columns with the requested number of columns and rows
/// </summary>
/// <param name="cols"></param>
/// <param name="rows"></param>
/// <returns></returns>
public static DataTable BuildTable(int cols, int rows)
{
var dt = new DataTable();
for(int c = 0; c < cols; c++) {
dt.Columns.Add("Col"+c);
}
for(int r = 0; r < rows; r++) {
var newRow = dt.NewRow();
for(int c = 0; c < cols; c++) {
newRow[c] = $"R{r}C{c}";
}
dt.Rows.Add(newRow);
}
return dt;
}
}
}