TableViewTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. using Xunit;
  8. using System.Globalization;
  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. [Fact]
  53. public void SelectedCellChanged_NotFiredForSameValue()
  54. {
  55. var tableView = new TableView(){
  56. Table = BuildTable(25,50)
  57. };
  58. bool called = false;
  59. tableView.SelectedCellChanged += (e)=>{called=true;};
  60. Assert.Equal(0,tableView.SelectedColumn);
  61. Assert.False(called);
  62. // Changing value to same as it already was should not raise an event
  63. tableView.SelectedColumn = 0;
  64. Assert.False(called);
  65. tableView.SelectedColumn = 10;
  66. Assert.True(called);
  67. }
  68. [Fact]
  69. public void SelectedCellChanged_SelectedColumnIndexesCorrect()
  70. {
  71. var tableView = new TableView(){
  72. Table = BuildTable(25,50)
  73. };
  74. bool called = false;
  75. tableView.SelectedCellChanged += (e)=>{
  76. called=true;
  77. Assert.Equal(0,e.OldCol);
  78. Assert.Equal(10,e.NewCol);
  79. };
  80. tableView.SelectedColumn = 10;
  81. Assert.True(called);
  82. }
  83. [Fact]
  84. public void SelectedCellChanged_SelectedRowIndexesCorrect()
  85. {
  86. var tableView = new TableView(){
  87. Table = BuildTable(25,50)
  88. };
  89. bool called = false;
  90. tableView.SelectedCellChanged += (e)=>{
  91. called=true;
  92. Assert.Equal(0,e.OldRow);
  93. Assert.Equal(10,e.NewRow);
  94. };
  95. tableView.SelectedRow = 10;
  96. Assert.True(called);
  97. }
  98. [Fact]
  99. public void Test_SumColumnWidth_UnicodeLength()
  100. {
  101. Assert.Equal(11,"hello there".Sum(c=>Rune.ColumnWidth(c)));
  102. // Creates a string with the peculiar (french?) r symbol
  103. String surrogate = "Les Mise" + Char.ConvertFromUtf32(Int32.Parse("0301", NumberStyles.HexNumber)) + "rables";
  104. // The unicode width of this string is shorter than the string length!
  105. Assert.Equal(14,surrogate.Sum(c=>Rune.ColumnWidth(c)));
  106. Assert.Equal(15,surrogate.Length);
  107. }
  108. /// <summary>
  109. /// Builds a simple table of string columns with the requested number of columns and rows
  110. /// </summary>
  111. /// <param name="cols"></param>
  112. /// <param name="rows"></param>
  113. /// <returns></returns>
  114. public static DataTable BuildTable(int cols, int rows)
  115. {
  116. var dt = new DataTable();
  117. for(int c = 0; c < cols; c++) {
  118. dt.Columns.Add("Col"+c);
  119. }
  120. for(int r = 0; r < rows; r++) {
  121. var newRow = dt.NewRow();
  122. for(int c = 0; c < cols; c++) {
  123. newRow[c] = $"R{r}C{c}";
  124. }
  125. dt.Rows.Add(newRow);
  126. }
  127. return dt;
  128. }
  129. }
  130. }