AddRuneTests.cs 5.1 KB

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