TextTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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
  10. {
  11. private readonly ITestOutputHelper _output;
  12. public TextTests (ITestOutputHelper output) { _output = output; }
  13. // Test that View.PreserveTrailingSpaces removes trailing spaces
  14. [Fact]
  15. public void PreserveTrailingSpaces_Removes_Trailing_Spaces ()
  16. {
  17. var view = new View { Text = "Hello World " };
  18. Assert.Equal ("Hello World ", view.TextFormatter.Text);
  19. view.TextFormatter.WordWrap = true;
  20. view.TextFormatter.Size = new (5, 3);
  21. view.PreserveTrailingSpaces = false;
  22. Assert.Equal ($"Hello{Environment.NewLine}World", view.TextFormatter.Format ());
  23. view.PreserveTrailingSpaces = true;
  24. Assert.Equal ($"Hello{Environment.NewLine} {Environment.NewLine}World", view.TextFormatter.Format ());
  25. }
  26. // View.PreserveTrailingSpaces Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  27. // or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  28. // If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  29. // <see cref = "Text" / > is formatted for display.The default is <see langword = "false" / >.
  30. [Fact]
  31. public void PreserveTrailingSpaces_Set_Get ()
  32. {
  33. var view = new View { Text = "Hello World" };
  34. Assert.False (view.PreserveTrailingSpaces);
  35. view.PreserveTrailingSpaces = true;
  36. Assert.True (view.PreserveTrailingSpaces);
  37. }
  38. // Setting TextFormatter DOES NOT update Text
  39. [Fact]
  40. public void SettingTextFormatterDoesNotUpdateText ()
  41. {
  42. var view = new View ();
  43. view.TextFormatter.Text = "Hello World";
  44. Assert.True (string.IsNullOrEmpty (view.Text));
  45. }
  46. // Setting Text updates TextFormatter
  47. [Fact]
  48. public void SettingTextUpdatesTextFormatter ()
  49. {
  50. var view = new View { Text = "Hello World" };
  51. Assert.Equal ("Hello World", view.Text);
  52. Assert.Equal ("Hello World", view.TextFormatter.Text);
  53. }
  54. // Setting Text does NOT set the HotKey
  55. [Fact]
  56. public void Text_Does_Not_Set_HotKey ()
  57. {
  58. var view = new View { HotKeySpecifier = (Rune)'_', Text = "_Hello World" };
  59. Assert.NotEqual (Key.H, view.HotKey);
  60. }
  61. // Test that TextFormatter is init only
  62. [Fact]
  63. public void TextFormatterIsInitOnly ()
  64. {
  65. var view = new View ();
  66. // Use reflection to ensure the TextFormatter property is `init` only
  67. Assert.Contains (
  68. typeof (IsExternalInit),
  69. typeof (View).GetMethod ("set_TextFormatter")
  70. .ReturnParameter.GetRequiredCustomModifiers ());
  71. }
  72. // Test that the Text property is set correctly.
  73. [Fact]
  74. public void TextProperty ()
  75. {
  76. var view = new View { Text = "Hello World" };
  77. Assert.Equal ("Hello World", view.Text);
  78. }
  79. // Test view.UpdateTextFormatterText overridden in a subclass updates TextFormatter.Text
  80. [Fact]
  81. public void UpdateTextFormatterText_Overridden ()
  82. {
  83. var view = new TestView { Text = "Hello World" };
  84. Assert.Equal ("Hello World", view.Text);
  85. Assert.Equal (">Hello World<", view.TextFormatter.Text);
  86. }
  87. private class TestView : View
  88. {
  89. protected override void UpdateTextFormatterText () { TextFormatter.Text = $">{Text}<"; }
  90. }
  91. // Test behavior of AutoSize property.
  92. // - Default is false
  93. // - Setting to true invalidates Height/Width
  94. // - Setting to false invalidates Height/Width
  95. }