TextTests.cs 6.3 KB

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