AddRuneTests.cs 5.9 KB

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