AddRuneTests.cs 5.1 KB

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