TableViewTests.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Terminal.Gui;
  8. using Xunit;
  9. namespace UnitTests {
  10. public class TableViewTests
  11. {
  12. [Fact]
  13. public void EnsureValidScrollOffsets_WithNoCells()
  14. {
  15. var tableView = new TableView();
  16. Assert.Equal(0,tableView.RowOffset);
  17. Assert.Equal(0,tableView.ColumnOffset);
  18. // Set empty table
  19. tableView.Table = new DataTable();
  20. // Since table has no rows or columns scroll offset should default to 0
  21. tableView.EnsureValidScrollOffsets();
  22. Assert.Equal(0,tableView.RowOffset);
  23. Assert.Equal(0,tableView.ColumnOffset);
  24. }
  25. [Fact]
  26. public void EnsureValidScrollOffsets_LoadSmallerTable()
  27. {
  28. var tableView = new TableView();
  29. tableView.Bounds = new Rect(0,0,25,10);
  30. Assert.Equal(0,tableView.RowOffset);
  31. Assert.Equal(0,tableView.ColumnOffset);
  32. // Set big table
  33. tableView.Table = BuildTable(25,50);
  34. // Scroll down and along
  35. tableView.RowOffset = 20;
  36. tableView.ColumnOffset = 10;
  37. tableView.EnsureValidScrollOffsets();
  38. // The scroll should be valid at the moment
  39. Assert.Equal(20,tableView.RowOffset);
  40. Assert.Equal(10,tableView.ColumnOffset);
  41. // Set small table
  42. tableView.Table = BuildTable(2,2);
  43. // Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
  44. Assert.Equal(0,tableView.RowOffset);
  45. Assert.Equal(0,tableView.ColumnOffset);
  46. // Trying to set invalid indexes should not be possible
  47. tableView.RowOffset = 20;
  48. tableView.ColumnOffset = 10;
  49. Assert.Equal(1,tableView.RowOffset);
  50. Assert.Equal(1,tableView.ColumnOffset);
  51. }
  52. /// <summary>
  53. /// Builds a simple table of string columns with the requested number of columns and rows
  54. /// </summary>
  55. /// <param name="cols"></param>
  56. /// <param name="rows"></param>
  57. /// <returns></returns>
  58. public static DataTable BuildTable(int cols, int rows)
  59. {
  60. var dt = new DataTable();
  61. for(int c = 0; c < cols; c++) {
  62. dt.Columns.Add("Col"+c);
  63. }
  64. for(int r = 0; r < rows; r++) {
  65. var newRow = dt.NewRow();
  66. for(int c = 0; c < cols; c++) {
  67. newRow[c] = $"R{r}C{c}";
  68. }
  69. dt.Rows.Add(newRow);
  70. }
  71. return dt;
  72. }
  73. }
  74. }