AddRuneTests.cs 5.9 KB

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