AddRuneTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Microsoft.VisualStudio.TestPlatform.Utilities;
  2. using System.Buffers;
  3. using System.Text;
  4. using Xunit;
  5. using Xunit.Abstractions;
  6. using static Terminal.Gui.SpinnerStyle;
  7. // Alias Console to MockConsole so we don't accidentally use Console
  8. namespace Terminal.Gui.DriverTests;
  9. public class AddRuneTests {
  10. readonly ITestOutputHelper _output;
  11. public AddRuneTests (ITestOutputHelper output)
  12. {
  13. this._output = output;
  14. }
  15. [Fact]
  16. public void AddRune ()
  17. {
  18. var driver = new FakeDriver ();
  19. Application.Init (driver);
  20. driver.Init (() => { });
  21. driver.AddRune (new Rune ('a'));
  22. Assert.Equal ((Rune)'a', driver.Contents [0, 0].Runes [0]);
  23. driver.End ();
  24. Application.Shutdown ();
  25. }
  26. [Fact]
  27. public void AddRune_InvalidLocation_DoesNothing ()
  28. {
  29. var driver = new FakeDriver ();
  30. Application.Init (driver);
  31. driver.Init (() => { });
  32. driver.Move (driver.Cols, driver.Rows);
  33. driver.AddRune ('a');
  34. for (var col = 0; col < driver.Cols; col++) {
  35. for (var row = 0; row < driver.Rows; row++) {
  36. Assert.Equal ((Rune)' ', driver.Contents [row, col].Runes [0]);
  37. }
  38. }
  39. driver.End ();
  40. Application.Shutdown ();
  41. }
  42. [Fact]
  43. public void AddRune_MovesToNextColumn ()
  44. {
  45. var driver = new FakeDriver ();
  46. Application.Init (driver);
  47. driver.Init (() => { });
  48. driver.AddRune ('a');
  49. Assert.Equal ((Rune)'a', driver.Contents [0, 0].Runes [0]);
  50. Assert.Equal (0, driver.Row);
  51. Assert.Equal (1, driver.Col);
  52. driver.AddRune ('b');
  53. Assert.Equal ((Rune)'b', driver.Contents [0, 1].Runes [0]);
  54. Assert.Equal (0, driver.Row);
  55. Assert.Equal (2, driver.Col);
  56. // Move to the last column of the first row
  57. var lastCol = driver.Cols - 1;
  58. driver.Move (lastCol, 0);
  59. Assert.Equal (0, driver.Row);
  60. Assert.Equal (lastCol, driver.Col);
  61. // Add a rune to the last column of the first row; should increment the row or col even though it's now invalid
  62. driver.AddRune ('c');
  63. Assert.Equal ((Rune)'c', driver.Contents [0, lastCol].Runes [0]);
  64. Assert.Equal (lastCol + 1, driver.Col);
  65. // Add a rune; should succeed but do nothing as it's outside of Contents
  66. driver.AddRune ('d');
  67. Assert.Equal (lastCol + 2, driver.Col);
  68. for (var col = 0; col < driver.Cols; col++) {
  69. for (var row = 0; row < driver.Rows; row++) {
  70. Assert.NotEqual ((Rune)'d', driver.Contents [row, col].Runes [0]);
  71. }
  72. }
  73. driver.End ();
  74. Application.Shutdown ();
  75. }
  76. [Fact]
  77. public void AddRune_MovesToNextColumn_Wide ()
  78. {
  79. var driver = new FakeDriver ();
  80. Application.Init (driver);
  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].Runes [0]);
  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].Runes [0]);
  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].Runes [0]);
  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].Runes [0]);
  110. // }
  111. //}
  112. driver.End ();
  113. Application.Shutdown ();
  114. }
  115. [Fact]
  116. public void AddRune_Accented_Letter_With_Three_Combining_Unicode_Chars ()
  117. {
  118. var driver = new FakeDriver ();
  119. Application.Init (driver);
  120. driver.Init (() => { });
  121. var expected = new Rune ('ắ');
  122. var text = "\u1eaf";
  123. driver.AddStr (text);
  124. Assert.Equal (expected, driver.Contents [0, 0].Runes [0]);
  125. Assert.Equal ((Rune)' ', driver.Contents [0, 1].Runes [0]);
  126. driver.ClearContents ();
  127. driver.Move (0, 0);
  128. text = "\u0103\u0301";
  129. driver.AddStr (text);
  130. Assert.Equal (expected, driver.Contents [0, 0].Runes [0]);
  131. Assert.Equal ((Rune)' ', driver.Contents [0, 1].Runes [0]);
  132. driver.ClearContents ();
  133. driver.Move (0, 0);
  134. text = "\u0061\u0306\u0301";
  135. driver.AddStr (text);
  136. Assert.Equal (expected, driver.Contents [0, 0].Runes [0]);
  137. Assert.Equal ((Rune)' ', driver.Contents [0, 1].Runes [0]);
  138. // var s = "a\u0301\u0300\u0306";
  139. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  140. //ắ", output);
  141. // tf.Text = "\u1eaf";
  142. // Application.Refresh ();
  143. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  144. //ắ", output);
  145. // tf.Text = "\u0103\u0301";
  146. // Application.Refresh ();
  147. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  148. //ắ", output);
  149. // tf.Text = "\u0061\u0306\u0301";
  150. // Application.Refresh ();
  151. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  152. //ắ", output);
  153. driver.End ();
  154. Application.Shutdown ();
  155. }
  156. }