TextTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Size (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. // Test that TextFormatter is init only
  55. [Fact]
  56. public void TextFormatterIsInitOnly ()
  57. {
  58. var view = new View ();
  59. // Use reflection to ensure the TextFormatter property is `init` only
  60. Assert.True (
  61. typeof (View).GetMethod ("set_TextFormatter")
  62. .ReturnParameter.GetRequiredCustomModifiers ()
  63. .Contains (typeof (IsExternalInit))
  64. );
  65. }
  66. // Test that the Text property is set correctly.
  67. [Fact]
  68. public void TextProperty ()
  69. {
  70. var view = new View { Text = "Hello World" };
  71. Assert.Equal ("Hello World", view.Text);
  72. }
  73. // Setting Text sets the HotKey
  74. [Fact]
  75. public void TextSetsHotKey ()
  76. {
  77. var view = new View { HotKeySpecifier = (Rune)'_', Text = "_Hello World" };
  78. Assert.Equal (Key.H, view.HotKey);
  79. }
  80. // Test view.UpdateTextFormatterText overridden in a subclass updates TextFormatter.Text
  81. [Fact]
  82. public void UpdateTextFormatterText_Overridden ()
  83. {
  84. var view = new TestView { Text = "Hello World" };
  85. Assert.Equal ("Hello World", view.Text);
  86. Assert.Equal (">Hello World<", view.TextFormatter.Text);
  87. }
  88. private class TestView : View
  89. {
  90. protected override void UpdateTextFormatterText () { TextFormatter.Text = $">{Text}<"; }
  91. }
  92. // Test behavior of AutoSize property.
  93. // - Default is false
  94. // - Setting to true invalidates Height/Width
  95. // - Setting to false invalidates Height/Width
  96. }