AddRuneTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Buffers;
  3. using System.Text;
  4. using Xunit;
  5. using Xunit.Abstractions;
  6. // Alias Console to MockConsole so we don't accidentally use Console
  7. namespace Terminal.Gui.DriverTests;
  8. public class AddRuneTests {
  9. readonly ITestOutputHelper _output;
  10. public AddRuneTests (ITestOutputHelper output)
  11. {
  12. ConsoleDriver.RunningUnitTests = true;
  13. this._output = output;
  14. }
  15. [Theory]
  16. [InlineData (typeof (FakeDriver))]
  17. [InlineData (typeof (NetDriver))]
  18. [InlineData (typeof (CursesDriver))]
  19. [InlineData (typeof (WindowsDriver))]
  20. public void AddRune (Type driverType)
  21. {
  22. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  23. driver.Init ();
  24. driver.Rows = 25;
  25. driver.Cols = 80;
  26. driver.Init ();
  27. driver.AddRune (new Rune ('a'));
  28. Assert.Equal ((Rune)'a', driver.Contents [0, 0].Rune);
  29. driver.End ();
  30. }
  31. [Fact]
  32. public void AddRune_InvalidLocation_DoesNothing ()
  33. {
  34. var driver = new FakeDriver ();
  35. driver.Init ();
  36. driver.Move (driver.Cols, driver.Rows);
  37. driver.AddRune ('a');
  38. for (var col = 0; col < driver.Cols; col++) {
  39. for (var row = 0; row < driver.Rows; row++) {
  40. Assert.Equal ((Rune)' ', driver.Contents [row, col].Rune);
  41. }
  42. }
  43. driver.End ();
  44. }
  45. [Fact]
  46. public void AddRune_MovesToNextColumn ()
  47. {
  48. var driver = new FakeDriver ();
  49. driver.Init ();
  50. driver.AddRune ('a');
  51. Assert.Equal ((Rune)'a', driver.Contents [0, 0].Rune);
  52. Assert.Equal (0, driver.Row);
  53. Assert.Equal (1, driver.Col);
  54. driver.AddRune ('b');
  55. Assert.Equal ((Rune)'b', driver.Contents [0, 1].Rune);
  56. Assert.Equal (0, driver.Row);
  57. Assert.Equal (2, driver.Col);
  58. // Move to the last column of the first row
  59. var lastCol = driver.Cols - 1;
  60. driver.Move (lastCol, 0);
  61. Assert.Equal (0, driver.Row);
  62. Assert.Equal (lastCol, driver.Col);
  63. // Add a rune to the last column of the first row; should increment the row or col even though it's now invalid
  64. driver.AddRune ('c');
  65. Assert.Equal ((Rune)'c', driver.Contents [0, lastCol].Rune);
  66. Assert.Equal (lastCol + 1, driver.Col);
  67. // Add a rune; should succeed but do nothing as it's outside of Contents
  68. driver.AddRune ('d');
  69. Assert.Equal (lastCol + 2, driver.Col);
  70. for (var col = 0; col < driver.Cols; col++) {
  71. for (var row = 0; row < driver.Rows; row++) {
  72. Assert.NotEqual ((Rune)'d', driver.Contents [row, col].Rune);
  73. }
  74. }
  75. driver.End ();
  76. }
  77. [Fact]
  78. public void AddRune_MovesToNextColumn_Wide ()
  79. {
  80. var driver = new FakeDriver ();
  81. driver.Init ();
  82. // 🍕 Slice of Pizza "\U0001F355"
  83. var operationStatus = Rune.DecodeFromUtf16 ("\U0001F355", out Rune rune, out int charsConsumed);
  84. Assert.Equal (OperationStatus.Done, operationStatus);
  85. Assert.Equal (charsConsumed, rune.Utf16SequenceLength);
  86. Assert.Equal (2, rune.GetColumns ());
  87. driver.AddRune (rune);
  88. Assert.Equal (rune, driver.Contents [0, 0].Rune);
  89. Assert.Equal (0, driver.Row);
  90. Assert.Equal (2, driver.Col);
  91. //driver.AddRune ('b');
  92. //Assert.Equal ((Rune)'b', driver.Contents [0, 1].Rune);
  93. //Assert.Equal (0, driver.Row);
  94. //Assert.Equal (2, driver.Col);
  95. //// Move to the last column of the first row
  96. //var lastCol = driver.Cols - 1;
  97. //driver.Move (lastCol, 0);
  98. //Assert.Equal (0, driver.Row);
  99. //Assert.Equal (lastCol, driver.Col);
  100. //// Add a rune to the last column of the first row; should increment the row or col even though it's now invalid
  101. //driver.AddRune ('c');
  102. //Assert.Equal ((Rune)'c', driver.Contents [0, lastCol].Rune);
  103. //Assert.Equal (lastCol + 1, driver.Col);
  104. //// Add a rune; should succeed but do nothing as it's outside of Contents
  105. //driver.AddRune ('d');
  106. //Assert.Equal (lastCol + 2, driver.Col);
  107. //for (var col = 0; col < driver.Cols; col++) {
  108. // for (var row = 0; row < driver.Rows; row++) {
  109. // Assert.NotEqual ((Rune)'d', driver.Contents [row, col].Rune);
  110. // }
  111. //}
  112. driver.End ();
  113. }
  114. [Fact]
  115. public void AddRune_Accented_Letter_With_Three_Combining_Unicode_Chars ()
  116. {
  117. var driver = new FakeDriver ();
  118. driver.Init ();
  119. var expected = new Rune ('ắ');
  120. var text = "\u1eaf";
  121. driver.AddStr (text);
  122. Assert.Equal (expected, driver.Contents [0, 0].Rune);
  123. Assert.Equal ((Rune)' ', driver.Contents [0, 1].Rune);
  124. driver.ClearContents ();
  125. driver.Move (0, 0);
  126. text = "\u0103\u0301";
  127. driver.AddStr (text);
  128. Assert.Equal (expected, driver.Contents [0, 0].Rune);
  129. Assert.Equal ((Rune)' ', driver.Contents [0, 1].Rune);
  130. driver.ClearContents ();
  131. driver.Move (0, 0);
  132. text = "\u0061\u0306\u0301";
  133. driver.AddStr (text);
  134. Assert.Equal (expected, driver.Contents [0, 0].Rune);
  135. Assert.Equal ((Rune)' ', driver.Contents [0, 1].Rune);
  136. // var s = "a\u0301\u0300\u0306";
  137. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  138. //ắ", output);
  139. // tf.Text = "\u1eaf";
  140. // Application.Refresh ();
  141. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  142. //ắ", output);
  143. // tf.Text = "\u0103\u0301";
  144. // Application.Refresh ();
  145. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  146. //ắ", output);
  147. // tf.Text = "\u0061\u0306\u0301";
  148. // Application.Refresh ();
  149. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  150. //ắ", output);
  151. driver.End ();
  152. }
  153. }