TextTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Runtime.CompilerServices;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. /// <summary>
  6. /// Tests of the <see cref="View.Text"/> and <see cref="View.TextFormatter"/> properties (independent of
  7. /// AutoSize).
  8. /// </summary>
  9. public class TextTests (ITestOutputHelper output)
  10. {
  11. private readonly ITestOutputHelper _output = output;
  12. // Test that View.PreserveTrailingSpaces removes trailing spaces
  13. [Fact]
  14. public void PreserveTrailingSpaces_Removes_Trailing_Spaces ()
  15. {
  16. var view = new View { Text = "Hello World " };
  17. Assert.Equal ("Hello World ", view.TextFormatter.Text);
  18. view.TextFormatter.WordWrap = true;
  19. view.TextFormatter.Size = new (5, 3);
  20. view.PreserveTrailingSpaces = false;
  21. Assert.Equal ($"Hello{Environment.NewLine}World", view.TextFormatter.Format ());
  22. view.PreserveTrailingSpaces = true;
  23. Assert.Equal ($"Hello{Environment.NewLine} {Environment.NewLine}World", view.TextFormatter.Format ());
  24. }
  25. // View.PreserveTrailingSpaces Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  26. // or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  27. // If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  28. // <see cref = "Text" / > is formatted for display.The default is <see langword = "false" / >.
  29. [Fact]
  30. public void PreserveTrailingSpaces_Set_Get ()
  31. {
  32. var view = new View { Text = "Hello World" };
  33. Assert.False (view.PreserveTrailingSpaces);
  34. view.PreserveTrailingSpaces = true;
  35. Assert.True (view.PreserveTrailingSpaces);
  36. }
  37. // Setting TextFormatter DOES NOT update Text
  38. [Fact]
  39. public void SettingTextFormatterDoesNotUpdateText ()
  40. {
  41. var view = new View ();
  42. view.TextFormatter.Text = "Hello World";
  43. Assert.True (string.IsNullOrEmpty (view.Text));
  44. }
  45. // Setting Text updates TextFormatter
  46. [Fact]
  47. public void SettingTextUpdatesTextFormatter ()
  48. {
  49. var view = new View { Text = "Hello World" };
  50. Assert.Equal ("Hello World", view.Text);
  51. Assert.Equal ("Hello World", view.TextFormatter.Text);
  52. }
  53. // Setting Text does NOT set the HotKey
  54. [Fact]
  55. public void Text_Does_Not_Set_HotKey ()
  56. {
  57. var view = new View { HotKeySpecifier = (Rune)'_', Text = "_Hello World" };
  58. Assert.NotEqual (Key.H, view.HotKey);
  59. }
  60. // Test that TextFormatter is init only
  61. [Fact]
  62. public void TextFormatterIsInitOnly ()
  63. {
  64. var view = new View ();
  65. // Use reflection to ensure the TextFormatter property is `init` only
  66. Assert.Contains (
  67. typeof (IsExternalInit),
  68. typeof (View).GetMethod ("set_TextFormatter")
  69. .ReturnParameter.GetRequiredCustomModifiers ());
  70. }
  71. // Test that the Text property is set correctly.
  72. [Fact]
  73. public void TextProperty ()
  74. {
  75. var view = new View { Text = "Hello World" };
  76. Assert.Equal ("Hello World", view.Text);
  77. }
  78. // Test view.UpdateTextFormatterText overridden in a subclass updates TextFormatter.Text
  79. [Fact]
  80. public void UpdateTextFormatterText_Overridden ()
  81. {
  82. var view = new TestView { Text = "Hello World" };
  83. Assert.Equal ("Hello World", view.Text);
  84. Assert.Equal (">Hello World<", view.TextFormatter.Text);
  85. }
  86. private class TestView : View
  87. {
  88. protected override void UpdateTextFormatterText () { TextFormatter.Text = $">{Text}<"; }
  89. }
  90. [Fact]
  91. public void TextDirection_Horizontal_Dims_Correct ()
  92. {
  93. // Initializes a view with a vertical direction
  94. var view = new View
  95. {
  96. Text = "01234",
  97. TextDirection = TextDirection.LeftRight_TopBottom,
  98. Width = Dim.Auto (Dim.DimAutoStyle.Text),
  99. Height = Dim.Auto (Dim.DimAutoStyle.Text)
  100. };
  101. Assert.Equal (new Rectangle (0, 0, 5, 1), view.Frame);
  102. Assert.Equal (new Rectangle (0, 0, 5, 1), view.Viewport);
  103. view.BeginInit ();
  104. view.EndInit ();
  105. Assert.Equal (new Rectangle (0, 0, 5, 1), view.Frame);
  106. Assert.Equal (new Rectangle (0, 0, 5, 1), view.Viewport);
  107. }
  108. // BUGBUG: this is a temporary test that helped identify #3469 - It needs to be expanded upon (and renamed)
  109. [Fact]
  110. public void TextDirection_Horizontal_Dims_Correct_WidthAbsolute ()
  111. {
  112. var view = new View
  113. {
  114. Text = "01234",
  115. TextDirection = TextDirection.LeftRight_TopBottom,
  116. TextAlignment = TextAlignment.Centered,
  117. Width = 10,
  118. Height = Dim.Auto (Dim.DimAutoStyle.Text)
  119. };
  120. view.BeginInit ();
  121. view.EndInit ();
  122. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Frame);
  123. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  124. Assert.Equal (new (10, 1), view.TextFormatter.Size);
  125. }
  126. [Fact]
  127. public void TextDirection_Vertical_Dims_Correct ()
  128. {
  129. // Initializes a view with a vertical direction
  130. var view = new View
  131. {
  132. TextDirection = TextDirection.TopBottom_LeftRight,
  133. Text = "01234",
  134. Width = Dim.Auto (Dim.DimAutoStyle.Text),
  135. Height = Dim.Auto (Dim.DimAutoStyle.Text),
  136. };
  137. Assert.Equal (new Rectangle (0, 0, 1, 5), view.Frame);
  138. Assert.Equal (new Rectangle (0, 0, 1, 5), view.Viewport);
  139. view.BeginInit ();
  140. Assert.Equal (new Rectangle (0, 0, 1, 5), view.Frame);
  141. view.EndInit ();
  142. Assert.Equal (new Rectangle (0, 0, 1, 5), view.Frame);
  143. Assert.Equal (new Rectangle (0, 0, 1, 5), view.Viewport);
  144. }
  145. // Test behavior of AutoSize property.
  146. // - Default is false
  147. // - Setting to true invalidates Height/Width
  148. // - Setting to false invalidates Height/Width
  149. }