TableViewTests.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. using Xunit.Abstractions;
  10. using System.Reflection;
  11. namespace Terminal.Gui.Views {
  12. public class TableViewTests {
  13. readonly ITestOutputHelper output;
  14. public TableViewTests (ITestOutputHelper output)
  15. {
  16. this.output = output;
  17. }
  18. [Fact]
  19. public void EnsureValidScrollOffsets_WithNoCells ()
  20. {
  21. var tableView = new TableView ();
  22. Assert.Equal (0, tableView.RowOffset);
  23. Assert.Equal (0, tableView.ColumnOffset);
  24. // Set empty table
  25. tableView.Table = new DataTable ();
  26. // Since table has no rows or columns scroll offset should default to 0
  27. tableView.EnsureValidScrollOffsets ();
  28. Assert.Equal (0, tableView.RowOffset);
  29. Assert.Equal (0, tableView.ColumnOffset);
  30. }
  31. [Fact]
  32. public void EnsureValidScrollOffsets_LoadSmallerTable ()
  33. {
  34. var tableView = new TableView ();
  35. tableView.Bounds = new Rect (0, 0, 25, 10);
  36. Assert.Equal (0, tableView.RowOffset);
  37. Assert.Equal (0, tableView.ColumnOffset);
  38. // Set big table
  39. tableView.Table = BuildTable (25, 50);
  40. // Scroll down and along
  41. tableView.RowOffset = 20;
  42. tableView.ColumnOffset = 10;
  43. tableView.EnsureValidScrollOffsets ();
  44. // The scroll should be valid at the moment
  45. Assert.Equal (20, tableView.RowOffset);
  46. Assert.Equal (10, tableView.ColumnOffset);
  47. // Set small table
  48. tableView.Table = BuildTable (2, 2);
  49. // Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
  50. Assert.Equal (0, tableView.RowOffset);
  51. Assert.Equal (0, tableView.ColumnOffset);
  52. // Trying to set invalid indexes should not be possible
  53. tableView.RowOffset = 20;
  54. tableView.ColumnOffset = 10;
  55. Assert.Equal (1, tableView.RowOffset);
  56. Assert.Equal (1, tableView.ColumnOffset);
  57. }
  58. [Fact]
  59. [AutoInitShutdown]
  60. public void Redraw_EmptyTable ()
  61. {
  62. var tableView = new TableView ();
  63. tableView.ColorScheme = new ColorScheme();
  64. tableView.Bounds = new Rect (0, 0, 25, 10);
  65. // Set a table with 1 column
  66. tableView.Table = BuildTable (1, 50);
  67. tableView.Redraw(tableView.Bounds);
  68. tableView.Table.Columns.Remove(tableView.Table.Columns[0]);
  69. tableView.Redraw(tableView.Bounds);
  70. }
  71. [Fact]
  72. public void SelectedCellChanged_NotFiredForSameValue ()
  73. {
  74. var tableView = new TableView () {
  75. Table = BuildTable (25, 50)
  76. };
  77. bool called = false;
  78. tableView.SelectedCellChanged += (e) => { called = true; };
  79. Assert.Equal (0, tableView.SelectedColumn);
  80. Assert.False (called);
  81. // Changing value to same as it already was should not raise an event
  82. tableView.SelectedColumn = 0;
  83. Assert.False (called);
  84. tableView.SelectedColumn = 10;
  85. Assert.True (called);
  86. }
  87. [Fact]
  88. public void SelectedCellChanged_SelectedColumnIndexesCorrect ()
  89. {
  90. var tableView = new TableView () {
  91. Table = BuildTable (25, 50)
  92. };
  93. bool called = false;
  94. tableView.SelectedCellChanged += (e) => {
  95. called = true;
  96. Assert.Equal (0, e.OldCol);
  97. Assert.Equal (10, e.NewCol);
  98. };
  99. tableView.SelectedColumn = 10;
  100. Assert.True (called);
  101. }
  102. [Fact]
  103. public void SelectedCellChanged_SelectedRowIndexesCorrect ()
  104. {
  105. var tableView = new TableView () {
  106. Table = BuildTable (25, 50)
  107. };
  108. bool called = false;
  109. tableView.SelectedCellChanged += (e) => {
  110. called = true;
  111. Assert.Equal (0, e.OldRow);
  112. Assert.Equal (10, e.NewRow);
  113. };
  114. tableView.SelectedRow = 10;
  115. Assert.True (called);
  116. }
  117. [Fact]
  118. public void Test_SumColumnWidth_UnicodeLength ()
  119. {
  120. Assert.Equal (11, "hello there".Sum (c => Rune.ColumnWidth (c)));
  121. // Creates a string with the peculiar (french?) r symbol
  122. String surrogate = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  123. // The unicode width of this string is shorter than the string length!
  124. Assert.Equal (14, surrogate.Sum (c => Rune.ColumnWidth (c)));
  125. Assert.Equal (15, surrogate.Length);
  126. }
  127. [Fact]
  128. public void IsSelected_MultiSelectionOn_Vertical ()
  129. {
  130. var tableView = new TableView () {
  131. Table = BuildTable (25, 50),
  132. MultiSelect = true
  133. };
  134. // 3 cell vertical selection
  135. tableView.SetSelection (1, 1, false);
  136. tableView.SetSelection (1, 3, true);
  137. Assert.False (tableView.IsSelected (0, 0));
  138. Assert.False (tableView.IsSelected (1, 0));
  139. Assert.False (tableView.IsSelected (2, 0));
  140. Assert.False (tableView.IsSelected (0, 1));
  141. Assert.True (tableView.IsSelected (1, 1));
  142. Assert.False (tableView.IsSelected (2, 1));
  143. Assert.False (tableView.IsSelected (0, 2));
  144. Assert.True (tableView.IsSelected (1, 2));
  145. Assert.False (tableView.IsSelected (2, 2));
  146. Assert.False (tableView.IsSelected (0, 3));
  147. Assert.True (tableView.IsSelected (1, 3));
  148. Assert.False (tableView.IsSelected (2, 3));
  149. Assert.False (tableView.IsSelected (0, 4));
  150. Assert.False (tableView.IsSelected (1, 4));
  151. Assert.False (tableView.IsSelected (2, 4));
  152. }
  153. [Fact]
  154. public void IsSelected_MultiSelectionOn_Horizontal ()
  155. {
  156. var tableView = new TableView () {
  157. Table = BuildTable (25, 50),
  158. MultiSelect = true
  159. };
  160. // 2 cell horizontal selection
  161. tableView.SetSelection (1, 0, false);
  162. tableView.SetSelection (2, 0, true);
  163. Assert.False (tableView.IsSelected (0, 0));
  164. Assert.True (tableView.IsSelected (1, 0));
  165. Assert.True (tableView.IsSelected (2, 0));
  166. Assert.False (tableView.IsSelected (3, 0));
  167. Assert.False (tableView.IsSelected (0, 1));
  168. Assert.False (tableView.IsSelected (1, 1));
  169. Assert.False (tableView.IsSelected (2, 1));
  170. Assert.False (tableView.IsSelected (3, 1));
  171. }
  172. [Fact]
  173. public void IsSelected_MultiSelectionOn_BoxSelection ()
  174. {
  175. var tableView = new TableView () {
  176. Table = BuildTable (25, 50),
  177. MultiSelect = true
  178. };
  179. // 4 cell horizontal in box 2x2
  180. tableView.SetSelection (0, 0, false);
  181. tableView.SetSelection (1, 1, true);
  182. Assert.True (tableView.IsSelected (0, 0));
  183. Assert.True (tableView.IsSelected (1, 0));
  184. Assert.False (tableView.IsSelected (2, 0));
  185. Assert.True (tableView.IsSelected (0, 1));
  186. Assert.True (tableView.IsSelected (1, 1));
  187. Assert.False (tableView.IsSelected (2, 1));
  188. Assert.False (tableView.IsSelected (0, 2));
  189. Assert.False (tableView.IsSelected (1, 2));
  190. Assert.False (tableView.IsSelected (2, 2));
  191. }
  192. [AutoInitShutdown]
  193. [Fact]
  194. public void PageDown_ExcludesHeaders ()
  195. {
  196. var tableView = new TableView () {
  197. Table = BuildTable (25, 50),
  198. MultiSelect = true,
  199. Bounds = new Rect (0, 0, 10, 5)
  200. };
  201. // Header should take up 2 lines
  202. tableView.Style.ShowHorizontalHeaderOverline = false;
  203. tableView.Style.ShowHorizontalHeaderUnderline = true;
  204. tableView.Style.AlwaysShowHeaders = false;
  205. // ensure that TableView has the input focus
  206. Application.Top.Add (tableView);
  207. Application.Top.FocusFirst ();
  208. Assert.True (tableView.HasFocus);
  209. Assert.Equal (0, tableView.RowOffset);
  210. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  211. // window height is 5 rows 2 are header so page down should give 3 new rows
  212. Assert.Equal (3, tableView.SelectedRow);
  213. Assert.Equal (1, tableView.RowOffset);
  214. // header is no longer visible so page down should give 5 new rows
  215. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  216. Assert.Equal (8, tableView.SelectedRow);
  217. Assert.Equal (4, tableView.RowOffset);
  218. }
  219. [Fact]
  220. public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun ()
  221. {
  222. // create a 4 by 4 table
  223. var tableView = new TableView () {
  224. Table = BuildTable (4, 4),
  225. MultiSelect = true,
  226. Bounds = new Rect (0, 0, 10, 5)
  227. };
  228. tableView.SelectAll ();
  229. Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
  230. // delete one of the columns
  231. tableView.Table.Columns.RemoveAt (2);
  232. // table should now be 3x4
  233. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  234. // remove a row
  235. tableView.Table.Rows.RemoveAt (1);
  236. // table should now be 3x3
  237. Assert.Equal (9, tableView.GetAllSelectedCells ().Count ());
  238. }
  239. [Fact]
  240. public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun ()
  241. {
  242. // create a 4 by 4 table
  243. var tableView = new TableView () {
  244. Table = BuildTable (4, 4),
  245. MultiSelect = true,
  246. Bounds = new Rect (0, 0, 10, 5)
  247. };
  248. // select the last row
  249. tableView.MultiSelectedRegions.Clear ();
  250. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
  251. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  252. // remove a row
  253. tableView.Table.Rows.RemoveAt (0);
  254. tableView.EnsureValidSelection ();
  255. // since the selection no longer exists it should be removed
  256. Assert.Empty (tableView.MultiSelectedRegions);
  257. }
  258. [Theory]
  259. [InlineData (true)]
  260. [InlineData (false)]
  261. public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
  262. {
  263. var tableView = new TableView () {
  264. Table = BuildTable (3, 3),
  265. MultiSelect = multiSelect,
  266. Bounds = new Rect (0, 0, 10, 5)
  267. };
  268. tableView.SetSelection (1, 1, false);
  269. Assert.Single (tableView.GetAllSelectedCells ());
  270. Assert.Equal (new Point (1, 1), tableView.GetAllSelectedCells ().Single ());
  271. }
  272. [Fact]
  273. public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
  274. {
  275. var tableView = new TableView () {
  276. Table = BuildTable (3, 3),
  277. MultiSelect = true,
  278. Bounds = new Rect (0, 0, 10, 5)
  279. };
  280. // move cursor to 1,1
  281. tableView.SetSelection (1, 1, false);
  282. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  283. tableView.SetSelection (2, 2, true);
  284. var selected = tableView.GetAllSelectedCells ().ToArray ();
  285. Assert.Equal (4, selected.Length);
  286. Assert.Equal (new Point (1, 1), selected [0]);
  287. Assert.Equal (new Point (2, 1), selected [1]);
  288. Assert.Equal (new Point (1, 2), selected [2]);
  289. Assert.Equal (new Point (2, 2), selected [3]);
  290. }
  291. [Fact]
  292. public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
  293. {
  294. var tableView = new TableView () {
  295. Table = BuildTable (3, 3),
  296. MultiSelect = true,
  297. FullRowSelect = true,
  298. Bounds = new Rect (0, 0, 10, 5)
  299. };
  300. // move cursor to 1,1
  301. tableView.SetSelection (1, 1, false);
  302. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  303. tableView.SetSelection (2, 2, true);
  304. var selected = tableView.GetAllSelectedCells ().ToArray ();
  305. Assert.Equal (6, selected.Length);
  306. Assert.Equal (new Point (0, 1), selected [0]);
  307. Assert.Equal (new Point (1, 1), selected [1]);
  308. Assert.Equal (new Point (2, 1), selected [2]);
  309. Assert.Equal (new Point (0, 2), selected [3]);
  310. Assert.Equal (new Point (1, 2), selected [4]);
  311. Assert.Equal (new Point (2, 2), selected [5]);
  312. }
  313. [Fact]
  314. public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
  315. {
  316. var tableView = new TableView () {
  317. Table = BuildTable (20, 20),
  318. MultiSelect = true,
  319. Bounds = new Rect (0, 0, 10, 5)
  320. };
  321. /*
  322. Sets up disconnected selections like:
  323. 00000000000
  324. 01100000000
  325. 01100000000
  326. 00000001100
  327. 00000000000
  328. */
  329. tableView.MultiSelectedRegions.Clear ();
  330. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
  331. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (7, 3), new Rect (7, 3, 2, 1)));
  332. tableView.SelectedColumn = 8;
  333. tableView.SelectedRow = 3;
  334. var selected = tableView.GetAllSelectedCells ().ToArray ();
  335. Assert.Equal (6, selected.Length);
  336. Assert.Equal (new Point (1, 1), selected [0]);
  337. Assert.Equal (new Point (2, 1), selected [1]);
  338. Assert.Equal (new Point (1, 2), selected [2]);
  339. Assert.Equal (new Point (2, 2), selected [3]);
  340. Assert.Equal (new Point (7, 3), selected [4]);
  341. Assert.Equal (new Point (8, 3), selected [5]);
  342. }
  343. [Fact]
  344. public void TableView_ExpandLastColumn_True ()
  345. {
  346. var tv = SetUpMiniTable ();
  347. // the thing we are testing
  348. tv.Style.ExpandLastColumn = true;
  349. tv.Redraw (tv.Bounds);
  350. string expected = @"
  351. ┌─┬──────┐
  352. │A│B │
  353. ├─┼──────┤
  354. │1│2 │
  355. ";
  356. GraphViewTests.AssertDriverContentsAre (expected, output);
  357. // Shutdown must be called to safely clean up Application if Init has been called
  358. Application.Shutdown ();
  359. }
  360. [Fact]
  361. public void TableView_ExpandLastColumn_False ()
  362. {
  363. var tv = SetUpMiniTable ();
  364. // the thing we are testing
  365. tv.Style.ExpandLastColumn = false;
  366. tv.Redraw (tv.Bounds);
  367. string expected = @"
  368. ┌─┬─┬────┐
  369. │A│B│ │
  370. ├─┼─┼────┤
  371. │1│2│ │
  372. ";
  373. GraphViewTests.AssertDriverContentsAre (expected, output);
  374. // Shutdown must be called to safely clean up Application if Init has been called
  375. Application.Shutdown ();
  376. }
  377. [Fact]
  378. public void TableView_ExpandLastColumn_False_ExactBounds ()
  379. {
  380. var tv = SetUpMiniTable ();
  381. // the thing we are testing
  382. tv.Style.ExpandLastColumn = false;
  383. // width exactly matches the max col widths
  384. tv.Bounds = new Rect (0, 0, 5, 4);
  385. tv.Redraw (tv.Bounds);
  386. string expected = @"
  387. ┌─┬─┐
  388. │A│B│
  389. ├─┼─┤
  390. │1│2│
  391. ";
  392. GraphViewTests.AssertDriverContentsAre (expected, output);
  393. // Shutdown must be called to safely clean up Application if Init has been called
  394. Application.Shutdown ();
  395. }
  396. [Fact]
  397. [AutoInitShutdown]
  398. public void TableView_Activate()
  399. {
  400. string activatedValue = null;
  401. var tv = new TableView (BuildTable(1,1));
  402. tv.CellActivated += (c) => activatedValue = c.Table.Rows[c.Row][c.Col].ToString();
  403. Application.Top.Add (tv);
  404. Application.Begin (Application.Top);
  405. // pressing enter should activate the first cell (selected cell)
  406. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  407. Assert.Equal ("R0C0",activatedValue);
  408. // reset the test
  409. activatedValue = null;
  410. // clear keybindings and ensure that Enter does not trigger the event anymore
  411. tv.ClearKeybindings ();
  412. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  413. Assert.Null(activatedValue);
  414. // New method for changing the activation key
  415. tv.AddKeyBinding (Key.z, Command.Accept);
  416. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  417. Assert.Equal ("R0C0", activatedValue);
  418. // reset the test
  419. activatedValue = null;
  420. tv.ClearKeybindings ();
  421. // Old method for changing the activation key
  422. tv.CellActivationKey = Key.z;
  423. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  424. Assert.Equal ("R0C0", activatedValue);
  425. }
  426. [Fact]
  427. public void TableViewMultiSelect_CannotFallOffLeft()
  428. {
  429. var tv = SetUpMiniTable ();
  430. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  431. tv.MultiSelect = true;
  432. tv.SelectedColumn = 1;
  433. tv.SelectedRow = 1;
  434. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  435. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single().Rect);
  436. // this next shift left should be ignored because we are already at the bounds
  437. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  438. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  439. Assert.Equal (0, tv.SelectedColumn);
  440. Assert.Equal (1, tv.SelectedRow);
  441. Application.Shutdown ();
  442. }
  443. [Fact]
  444. public void TableViewMultiSelect_CannotFallOffRight()
  445. {
  446. var tv = SetUpMiniTable ();
  447. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  448. tv.MultiSelect = true;
  449. tv.SelectedColumn = 0;
  450. tv.SelectedRow = 1;
  451. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  452. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  453. // this next shift right should be ignored because we are already at the right bounds
  454. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  455. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  456. Assert.Equal (1, tv.SelectedColumn);
  457. Assert.Equal (1, tv.SelectedRow);
  458. Application.Shutdown ();
  459. }
  460. [Fact]
  461. public void TableViewMultiSelect_CannotFallOffBottom ()
  462. {
  463. var tv = SetUpMiniTable ();
  464. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  465. tv.MultiSelect = true;
  466. tv.SelectedColumn = 0;
  467. tv.SelectedRow = 0;
  468. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  469. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  470. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  471. // this next moves should be ignored because we already selected the whole table
  472. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  473. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  474. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  475. Assert.Equal (1, tv.SelectedColumn);
  476. Assert.Equal (1, tv.SelectedRow);
  477. Application.Shutdown ();
  478. }
  479. [Fact]
  480. public void TableViewMultiSelect_CannotFallOffTop()
  481. {
  482. var tv = SetUpMiniTable ();
  483. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  484. tv.MultiSelect = true;
  485. tv.SelectedColumn = 1;
  486. tv.SelectedRow = 1;
  487. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  488. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  489. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  490. // this next moves should be ignored because we already selected the whole table
  491. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  492. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  493. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  494. Assert.Equal (0, tv.SelectedColumn);
  495. Assert.Equal (0, tv.SelectedRow);
  496. Application.Shutdown ();
  497. }
  498. [Theory]
  499. [InlineData (false)]
  500. [InlineData (true)]
  501. public void TableView_ColorTests_FocusedOrNot (bool focused)
  502. {
  503. var tv = SetUpMiniTable ();
  504. // width exactly matches the max col widths
  505. tv.Bounds = new Rect (0, 0, 5, 4);
  506. // private method for forcing the view to be focused/not focused
  507. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  508. // when the view is/isn't focused
  509. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  510. tv.Redraw (tv.Bounds);
  511. string expected = @"
  512. ┌─┬─┐
  513. │A│B│
  514. ├─┼─┤
  515. │1│2│
  516. ";
  517. GraphViewTests.AssertDriverContentsAre (expected, output);
  518. string expectedColors = @"
  519. 00000
  520. 00000
  521. 00000
  522. 01000
  523. ";
  524. GraphViewTests.AssertDriverColorsAre (expectedColors, new Attribute [] {
  525. // 0
  526. tv.ColorScheme.Normal,
  527. // 1
  528. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
  529. Application.Shutdown();
  530. }
  531. [Theory]
  532. [InlineData (false)]
  533. [InlineData (true)]
  534. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  535. {
  536. var tv = SetUpMiniTable ();
  537. tv.Style.InvertSelectedCellFirstCharacter = true;
  538. // width exactly matches the max col widths
  539. tv.Bounds = new Rect (0, 0, 5, 4);
  540. // private method for forcing the view to be focused/not focused
  541. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  542. // when the view is/isn't focused
  543. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  544. tv.Redraw (tv.Bounds);
  545. string expected = @"
  546. ┌─┬─┐
  547. │A│B│
  548. ├─┼─┤
  549. │1│2│
  550. ";
  551. GraphViewTests.AssertDriverContentsAre (expected, output);
  552. string expectedColors = @"
  553. 00000
  554. 00000
  555. 00000
  556. 01000
  557. ";
  558. var invertHotFocus = new Attribute(tv.ColorScheme.HotFocus.Background,tv.ColorScheme.HotFocus.Foreground);
  559. var invertHotNormal = new Attribute(tv.ColorScheme.HotNormal.Background,tv.ColorScheme.HotNormal.Foreground);
  560. GraphViewTests.AssertDriverColorsAre (expectedColors, new Attribute [] {
  561. // 0
  562. tv.ColorScheme.Normal,
  563. // 1
  564. focused ? invertHotFocus : invertHotNormal});
  565. Application.Shutdown();
  566. }
  567. [Theory]
  568. [InlineData (false)]
  569. [InlineData (true)]
  570. public void TableView_ColorsTest_RowColorGetter (bool focused)
  571. {
  572. var tv = SetUpMiniTable ();
  573. // width exactly matches the max col widths
  574. tv.Bounds = new Rect (0, 0, 5, 4);
  575. var rowHighlight = new ColorScheme () {
  576. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  577. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  578. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  579. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  580. };
  581. // when B is 2 use the custom highlight colour for the row
  582. tv.Style.RowColorGetter += (e)=>Convert.ToInt32(e.Table.Rows[e.RowIndex][1]) == 2 ? rowHighlight : null;
  583. // private method for forcing the view to be focused/not focused
  584. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  585. // when the view is/isn't focused
  586. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  587. tv.Redraw (tv.Bounds);
  588. string expected = @"
  589. ┌─┬─┐
  590. │A│B│
  591. ├─┼─┤
  592. │1│2│
  593. ";
  594. GraphViewTests.AssertDriverContentsAre (expected, output);
  595. string expectedColors = @"
  596. 00000
  597. 00000
  598. 00000
  599. 21222
  600. ";
  601. GraphViewTests.AssertDriverColorsAre (expectedColors, new Attribute [] {
  602. // 0
  603. tv.ColorScheme.Normal,
  604. // 1
  605. focused ? rowHighlight.HotFocus : rowHighlight.HotNormal,
  606. // 2
  607. rowHighlight.Normal});
  608. // change the value in the table so that
  609. // it no longer matches the RowColorGetter
  610. // delegate conditional ( which checks for
  611. // the value 2)
  612. tv.Table.Rows[0][1] = 5;
  613. tv.Redraw (tv.Bounds);
  614. expected = @"
  615. ┌─┬─┐
  616. │A│B│
  617. ├─┼─┤
  618. │1│5│
  619. ";
  620. GraphViewTests.AssertDriverContentsAre (expected, output);
  621. expectedColors = @"
  622. 00000
  623. 00000
  624. 00000
  625. 01000
  626. ";
  627. // now we only see 2 colors used (the selected cell color and Normal
  628. // rowHighlight should no longer be used because the delegate returned null
  629. // (now that the cell value is 5 - which does not match the conditional)
  630. GraphViewTests.AssertDriverColorsAre (expectedColors, new Attribute [] {
  631. // 0
  632. tv.ColorScheme.Normal,
  633. // 1
  634. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  635. // Shutdown must be called to safely clean up Application if Init has been called
  636. Application.Shutdown ();
  637. }
  638. [Theory]
  639. [InlineData (false)]
  640. [InlineData (true)]
  641. public void TableView_ColorsTest_ColorGetter (bool focused)
  642. {
  643. var tv = SetUpMiniTable ();
  644. // width exactly matches the max col widths
  645. tv.Bounds = new Rect (0, 0, 5, 4);
  646. // Create a style for column B
  647. var bStyle = tv.Style.GetOrCreateColumnStyle (tv.Table.Columns ["B"]);
  648. // when B is 2 use the custom highlight colour
  649. var cellHighlight = new ColorScheme () {
  650. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  651. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  652. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  653. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  654. };
  655. bStyle.ColorGetter = (a) => Convert.ToInt32(a.CellValue) == 2 ? cellHighlight : null;
  656. // private method for forcing the view to be focused/not focused
  657. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  658. // when the view is/isn't focused
  659. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  660. tv.Redraw (tv.Bounds);
  661. string expected = @"
  662. ┌─┬─┐
  663. │A│B│
  664. ├─┼─┤
  665. │1│2│
  666. ";
  667. GraphViewTests.AssertDriverContentsAre (expected, output);
  668. string expectedColors = @"
  669. 00000
  670. 00000
  671. 00000
  672. 01020
  673. ";
  674. GraphViewTests.AssertDriverColorsAre (expectedColors, new Attribute [] {
  675. // 0
  676. tv.ColorScheme.Normal,
  677. // 1
  678. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal,
  679. // 2
  680. cellHighlight.Normal});
  681. // change the value in the table so that
  682. // it no longer matches the ColorGetter
  683. // delegate conditional ( which checks for
  684. // the value 2)
  685. tv.Table.Rows[0][1] = 5;
  686. tv.Redraw (tv.Bounds);
  687. expected = @"
  688. ┌─┬─┐
  689. │A│B│
  690. ├─┼─┤
  691. │1│5│
  692. ";
  693. GraphViewTests.AssertDriverContentsAre (expected, output);
  694. expectedColors = @"
  695. 00000
  696. 00000
  697. 00000
  698. 01000
  699. ";
  700. // now we only see 2 colors used (the selected cell color and Normal
  701. // cellHighlight should no longer be used because the delegate returned null
  702. // (now that the cell value is 5 - which does not match the conditional)
  703. GraphViewTests.AssertDriverColorsAre (expectedColors, new Attribute [] {
  704. // 0
  705. tv.ColorScheme.Normal,
  706. // 1
  707. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  708. // Shutdown must be called to safely clean up Application if Init has been called
  709. Application.Shutdown ();
  710. }
  711. private TableView SetUpMiniTable ()
  712. {
  713. var tv = new TableView ();
  714. tv.Bounds = new Rect (0, 0, 10, 4);
  715. var dt = new DataTable ();
  716. var colA = dt.Columns.Add ("A");
  717. var colB = dt.Columns.Add ("B");
  718. dt.Rows.Add (1, 2);
  719. tv.Table = dt;
  720. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  721. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  722. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  723. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  724. GraphViewTests.InitFakeDriver ();
  725. tv.ColorScheme = Colors.Base;
  726. return tv;
  727. }
  728. [Fact]
  729. [AutoInitShutdown]
  730. public void ScrollDown_OneLineAtATime ()
  731. {
  732. var tableView = new TableView ();
  733. // Set big table
  734. tableView.Table = BuildTable (25, 50);
  735. // 1 header + 4 rows visible
  736. tableView.Bounds = new Rect (0, 0, 25, 5);
  737. tableView.Style.ShowHorizontalHeaderUnderline = false;
  738. tableView.Style.ShowHorizontalHeaderOverline = false;
  739. tableView.Style.AlwaysShowHeaders = true;
  740. // select last row
  741. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  742. // Scroll down
  743. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
  744. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  745. Assert.Equal(4,tableView.SelectedRow);
  746. Assert.Equal (1, tableView.RowOffset);
  747. }
  748. [Fact]
  749. public void ScrollRight_SmoothScrolling ()
  750. {
  751. GraphViewTests.InitFakeDriver ();
  752. var tableView = new TableView ();
  753. tableView.ColorScheme = Colors.TopLevel;
  754. // 3 columns are visibile
  755. tableView.Bounds = new Rect (0, 0, 7, 5);
  756. tableView.Style.ShowHorizontalHeaderUnderline = false;
  757. tableView.Style.ShowHorizontalHeaderOverline = false;
  758. tableView.Style.AlwaysShowHeaders = true;
  759. tableView.Style.SmoothHorizontalScrolling = true;
  760. var dt = new DataTable ();
  761. dt.Columns.Add ("A");
  762. dt.Columns.Add ("B");
  763. dt.Columns.Add ("C");
  764. dt.Columns.Add ("D");
  765. dt.Columns.Add ("E");
  766. dt.Columns.Add ("F");
  767. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  768. tableView.Table = dt;
  769. // select last visible column
  770. tableView.SelectedColumn = 2; // column C
  771. tableView.Redraw (tableView.Bounds);
  772. string expected =
  773. @"
  774. │A│B│C│
  775. │1│2│3│";
  776. GraphViewTests.AssertDriverContentsAre (expected, output);
  777. // Scroll right
  778. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  779. tableView.Redraw (tableView.Bounds);
  780. // Note that with SmoothHorizontalScrolling only a single new column
  781. // is exposed when scrolling right. This is not always the case though
  782. // sometimes if the leftmost column is long (i.e. A is a long column)
  783. // then when A is pushed off the screen multiple new columns could be exposed
  784. // (not just D but also E and F). This is because TableView never shows
  785. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  786. expected =
  787. @"
  788. │B│C│D│
  789. │2│3│4│";
  790. GraphViewTests.AssertDriverContentsAre (expected, output);
  791. // Shutdown must be called to safely clean up Application if Init has been called
  792. Application.Shutdown ();
  793. }
  794. [Fact]
  795. public void ScrollRight_WithoutSmoothScrolling ()
  796. {
  797. GraphViewTests.InitFakeDriver ();
  798. var tableView = new TableView ();
  799. tableView.ColorScheme = Colors.TopLevel;
  800. // 3 columns are visibile
  801. tableView.Bounds = new Rect (0, 0, 7, 5);
  802. tableView.Style.ShowHorizontalHeaderUnderline = false;
  803. tableView.Style.ShowHorizontalHeaderOverline = false;
  804. tableView.Style.AlwaysShowHeaders = true;
  805. tableView.Style.SmoothHorizontalScrolling = false;
  806. var dt = new DataTable ();
  807. dt.Columns.Add ("A");
  808. dt.Columns.Add ("B");
  809. dt.Columns.Add ("C");
  810. dt.Columns.Add ("D");
  811. dt.Columns.Add ("E");
  812. dt.Columns.Add ("F");
  813. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  814. tableView.Table = dt;
  815. // select last visible column
  816. tableView.SelectedColumn = 2; // column C
  817. tableView.Redraw (tableView.Bounds);
  818. string expected =
  819. @"
  820. │A│B│C│
  821. │1│2│3│";
  822. GraphViewTests.AssertDriverContentsAre (expected, output);
  823. // Scroll right
  824. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  825. tableView.Redraw (tableView.Bounds);
  826. // notice that without smooth scrolling we just update the first column
  827. // rendered in the table to the newly exposed column (D). This is fast
  828. // since we don't have to worry about repeatedly measuring the content
  829. // area as we scroll until the new column (D) is exposed. But it makes
  830. // the view 'jump' to expose all new columns
  831. expected =
  832. @"
  833. │D│E│F│
  834. │4│5│6│";
  835. GraphViewTests.AssertDriverContentsAre (expected, output);
  836. // Shutdown must be called to safely clean up Application if Init has been called
  837. Application.Shutdown ();
  838. }
  839. [Fact]
  840. public void LongColumnTest ()
  841. {
  842. GraphViewTests.InitFakeDriver ();
  843. var tableView = new TableView ();
  844. tableView.ColorScheme = Colors.TopLevel;
  845. // 25 characters can be printed into table
  846. tableView.Bounds = new Rect (0, 0, 25, 5);
  847. tableView.Style.ShowHorizontalHeaderUnderline = true;
  848. tableView.Style.ShowHorizontalHeaderOverline = false;
  849. tableView.Style.AlwaysShowHeaders = true;
  850. tableView.Style.SmoothHorizontalScrolling = true;
  851. var dt = new DataTable ();
  852. dt.Columns.Add ("A");
  853. dt.Columns.Add ("B");
  854. dt.Columns.Add ("Very Long Column");
  855. dt.Rows.Add (1, 2, new string('a',500));
  856. dt.Rows.Add (1, 2, "aaa");
  857. tableView.Table = dt;
  858. tableView.Redraw (tableView.Bounds);
  859. // default behaviour of TableView is not to render
  860. // columns unless there is sufficient space
  861. string expected =
  862. @"
  863. │A│B │
  864. ├─┼─────────────────────►
  865. │1│2 │
  866. │1│2 │
  867. ";
  868. GraphViewTests.AssertDriverContentsAre (expected, output);
  869. // get a style for the long column
  870. var style = tableView.Style.GetOrCreateColumnStyle(dt.Columns[2]);
  871. // one way the API user can fix this for long columns
  872. // is to specify a max width for the column
  873. style.MaxWidth = 10;
  874. tableView.Redraw (tableView.Bounds);
  875. expected =
  876. @"
  877. │A│B│Very Long │
  878. ├─┼─┼───────────────────┤
  879. │1│2│aaaaaaaaaa │
  880. │1│2│aaa │
  881. ";
  882. GraphViewTests.AssertDriverContentsAre (expected, output);
  883. // revert the style change
  884. style.MaxWidth = TableView.DefaultMaxCellWidth;
  885. // another way API user can fix problem is to implement
  886. // RepresentationGetter and apply max length there
  887. style.RepresentationGetter = (s)=>{
  888. return s.ToString().Length < 15 ? s.ToString() : s.ToString().Substring(0,13)+"...";
  889. };
  890. tableView.Redraw (tableView.Bounds);
  891. expected =
  892. @"
  893. │A│B│Very Long Column │
  894. ├─┼─┼───────────────────┤
  895. │1│2│aaaaaaaaaaaaa... │
  896. │1│2│aaa │
  897. ";
  898. GraphViewTests.AssertDriverContentsAre (expected, output);
  899. // revert style change
  900. style.RepresentationGetter = null;
  901. // Both of the above methods rely on having a fixed
  902. // size limit for the column. These are awkward if a
  903. // table is resizeable e.g. Dim.Fill(). Ideally we want
  904. // to render in any space available and truncate the content
  905. // of the column dynamically so it fills the free space at
  906. // the end of the table.
  907. // We can now specify that the column can be any length
  908. // (Up to MaxWidth) but the renderer can accept using
  909. // less space down to this limit
  910. style.MinAcceptableWidth = 5;
  911. tableView.Redraw (tableView.Bounds);
  912. expected =
  913. @"
  914. │A│B│Very Long Column │
  915. ├─┼─┼───────────────────┤
  916. │1│2│aaaaaaaaaaaaaaaaaaa│
  917. │1│2│aaa │
  918. ";
  919. GraphViewTests.AssertDriverContentsAre (expected, output);
  920. // Now test making the width too small for the MinAcceptableWidth
  921. // the Column won't fit so should not be rendered
  922. Application.Shutdown ();
  923. GraphViewTests.InitFakeDriver ();
  924. tableView.Bounds = new Rect(0,0,9,5);
  925. tableView.Redraw (tableView.Bounds);
  926. expected =
  927. @"
  928. │A│B │
  929. ├─┼─────►
  930. │1│2 │
  931. │1│2 │
  932. ";
  933. GraphViewTests.AssertDriverContentsAre (expected, output);
  934. // setting width to 10 leaves just enough space for the column to
  935. // meet MinAcceptableWidth of 5. Column width includes terminator line
  936. // symbol (e.g. ┤ or │)
  937. tableView.Bounds = new Rect (0, 0, 10, 5);
  938. tableView.Redraw (tableView.Bounds);
  939. expected =
  940. @"
  941. │A│B│Very│
  942. ├─┼─┼────┤
  943. │1│2│aaaa│
  944. │1│2│aaa │
  945. ";
  946. GraphViewTests.AssertDriverContentsAre (expected, output);
  947. Application.Shutdown ();
  948. }
  949. [Fact]
  950. public void ScrollIndicators ()
  951. {
  952. GraphViewTests.InitFakeDriver ();
  953. var tableView = new TableView ();
  954. tableView.ColorScheme = Colors.TopLevel;
  955. // 3 columns are visibile
  956. tableView.Bounds = new Rect (0, 0, 7, 5);
  957. tableView.Style.ShowHorizontalHeaderUnderline = true;
  958. tableView.Style.ShowHorizontalHeaderOverline = false;
  959. tableView.Style.AlwaysShowHeaders = true;
  960. tableView.Style.SmoothHorizontalScrolling = true;
  961. var dt = new DataTable ();
  962. dt.Columns.Add ("A");
  963. dt.Columns.Add ("B");
  964. dt.Columns.Add ("C");
  965. dt.Columns.Add ("D");
  966. dt.Columns.Add ("E");
  967. dt.Columns.Add ("F");
  968. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  969. tableView.Table = dt;
  970. // select last visible column
  971. tableView.SelectedColumn = 2; // column C
  972. tableView.Redraw (tableView.Bounds);
  973. // user can only scroll right so sees right indicator
  974. // Because first column in table is A
  975. string expected =
  976. @"
  977. │A│B│C│
  978. ├─┼─┼─►
  979. │1│2│3│";
  980. GraphViewTests.AssertDriverContentsAre (expected, output);
  981. // Scroll right
  982. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  983. // since A is now pushed off screen we get indicator showing
  984. // that user can scroll left to see first column
  985. tableView.Redraw (tableView.Bounds);
  986. expected =
  987. @"
  988. │B│C│D│
  989. ◄─┼─┼─►
  990. │2│3│4│";
  991. GraphViewTests.AssertDriverContentsAre (expected, output);
  992. // Scroll right twice more (to end of columns)
  993. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  994. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  995. tableView.Redraw (tableView.Bounds);
  996. expected =
  997. @"
  998. │D│E│F│
  999. ◄─┼─┼─┤
  1000. │4│5│6│";
  1001. GraphViewTests.AssertDriverContentsAre (expected, output);
  1002. // Shutdown must be called to safely clean up Application if Init has been called
  1003. Application.Shutdown ();
  1004. }
  1005. /// <summary>
  1006. /// Builds a simple table of string columns with the requested number of columns and rows
  1007. /// </summary>
  1008. /// <param name="cols"></param>
  1009. /// <param name="rows"></param>
  1010. /// <returns></returns>
  1011. public static DataTable BuildTable (int cols, int rows)
  1012. {
  1013. var dt = new DataTable ();
  1014. for (int c = 0; c < cols; c++) {
  1015. dt.Columns.Add ("Col" + c);
  1016. }
  1017. for (int r = 0; r < rows; r++) {
  1018. var newRow = dt.NewRow ();
  1019. for (int c = 0; c < cols; c++) {
  1020. newRow [c] = $"R{r}C{c}";
  1021. }
  1022. dt.Rows.Add (newRow);
  1023. }
  1024. return dt;
  1025. }
  1026. }
  1027. }