SchemeTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #nullable enable
  2. using System.Reflection;
  3. namespace Terminal.Gui.DrawingTests;
  4. public class SchemeTests
  5. {
  6. [Fact]
  7. public void Colors_Schemes_Property_Has_Private_Setter ()
  8. {
  9. // Resharper Code Cleanup likes to remove the `private set; `
  10. // from the Schemes property. This test will fail if
  11. // that happens.
  12. PropertyInfo? property = typeof (SchemeManager).GetProperty ("Schemes");
  13. Assert.NotNull (property);
  14. Assert.NotNull (property.SetMethod);
  15. Assert.True (property.GetSetMethod (true)!.IsPrivate);
  16. }
  17. [Fact]
  18. public void New ()
  19. {
  20. var scheme = new Scheme ();
  21. var lbl = new Label ();
  22. lbl.SetScheme (scheme);
  23. lbl.Draw ();
  24. }
  25. [Fact]
  26. public void Built_Ins ()
  27. {
  28. Dictionary<string, Scheme?> schemes = SchemeManager.GetSchemes ();
  29. Assert.NotNull (schemes);
  30. Assert.Equal (5, schemes.Count);
  31. Assert.True (schemes.ContainsKey ("Base"));
  32. Assert.True (schemes.ContainsKey ("Dialog"));
  33. Assert.True (schemes.ContainsKey ("Error"));
  34. Assert.True (schemes.ContainsKey ("Menu"));
  35. Assert.True (schemes.ContainsKey ("TopLevel"));
  36. }
  37. [Fact]
  38. public void Built_Ins_Are_Implicit ()
  39. {
  40. Dictionary<string, Scheme?> schemes = SchemeManager.GetSchemes ();
  41. Assert.True (schemes ["Base"]!.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
  42. Assert.False (schemes ["Base"]!.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
  43. }
  44. [Fact]
  45. public void With_Same_Attributes_AreEqual ()
  46. {
  47. Attribute attr = new (Color.Red, Color.Blue, TextStyle.Bold);
  48. Scheme s1 = new (attr);
  49. Scheme s2 = new (attr);
  50. Assert.Equal (s1, s2);
  51. Assert.Equal (s1.GetHashCode (), s2.GetHashCode ());
  52. }
  53. [Fact]
  54. public void Scheme_Properties_Are_Immutable ()
  55. {
  56. Scheme scheme = new (new Attribute ("Red", "Blue"));
  57. // The following line should not compile if uncommented:
  58. // scheme.Normal = new Attribute("Green", "Yellow");
  59. // Immutability is enforced by the C# compiler for init-only properties.
  60. Assert.True (true); // This test is a placeholder for documentation purposes.
  61. }
  62. [Fact]
  63. public void ObjectInitializer_Sets_Properties ()
  64. {
  65. Scheme scheme = new ()
  66. {
  67. Normal = new Attribute ("Red", "Blue"),
  68. Focus = new Attribute ("Green", "Yellow"),
  69. HotNormal = new Attribute ("White", "Black"),
  70. HotFocus = new Attribute ("Black", "White"),
  71. Active = new Attribute ("Cyan", "Magenta"),
  72. HotActive = new Attribute ("Magenta", "Cyan"),
  73. Highlight = new Attribute ("Yellow", "Red"),
  74. Editable = new Attribute ("Blue", "Yellow"),
  75. ReadOnly = new Attribute ("Gray", "Black"),
  76. Disabled = new Attribute ("DarkGray", "White")
  77. };
  78. Assert.Equal (new Attribute ("Red", "Blue"), scheme.Normal);
  79. Assert.Equal (new Attribute ("Green", "Yellow"), scheme.Focus);
  80. Assert.Equal (new Attribute ("White", "Black"), scheme.HotNormal);
  81. Assert.Equal (new Attribute ("Black", "White"), scheme.HotFocus);
  82. Assert.Equal (new Attribute ("Cyan", "Magenta"), scheme.Active);
  83. Assert.Equal (new Attribute ("Magenta", "Cyan"), scheme.HotActive);
  84. Assert.Equal (new Attribute ("Yellow", "Red"), scheme.Highlight);
  85. Assert.Equal (new Attribute ("Blue", "Yellow"), scheme.Editable);
  86. Assert.Equal (new Attribute ("Gray", "Black"), scheme.ReadOnly);
  87. Assert.Equal (new Attribute ("DarkGray", "White"), scheme.Disabled);
  88. }
  89. [Fact]
  90. public void With_Different_Attributes_AreNotEqual ()
  91. {
  92. Scheme s1 = new (new Attribute ("Red", "Blue"));
  93. Scheme s2 = new (new Attribute ("Green", "Yellow"));
  94. Assert.NotEqual (s1, s2);
  95. Assert.NotEqual (s1.GetHashCode (), s2.GetHashCode ());
  96. }
  97. [Fact]
  98. public void Default_Constructor_Has_Default_Values ()
  99. {
  100. Scheme scheme = new ();
  101. Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
  102. // All other roles should be implicit and derived from Normal
  103. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
  104. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
  105. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Focus, out _));
  106. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotFocus, out _));
  107. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
  108. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotActive, out _));
  109. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Highlight, out _));
  110. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Editable, out _));
  111. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.ReadOnly, out _));
  112. Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Disabled, out _));
  113. }
  114. [Fact]
  115. public void ToString_Outputs_All_Properties ()
  116. {
  117. Scheme scheme = new (new Attribute ("Red", "Blue"));
  118. string str = scheme.ToString ();
  119. Assert.Contains ("Normal", str, StringComparison.OrdinalIgnoreCase);
  120. Assert.Contains ("HotNormal", str, StringComparison.OrdinalIgnoreCase);
  121. Assert.Contains ("Focus", str, StringComparison.OrdinalIgnoreCase);
  122. Assert.Contains ("HotFocus", str, StringComparison.OrdinalIgnoreCase);
  123. Assert.Contains ("Active", str, StringComparison.OrdinalIgnoreCase);
  124. Assert.Contains ("HotActive", str, StringComparison.OrdinalIgnoreCase);
  125. Assert.Contains ("Highlight", str, StringComparison.OrdinalIgnoreCase);
  126. Assert.Contains ("Editable", str, StringComparison.OrdinalIgnoreCase);
  127. Assert.Contains ("ReadOnly", str, StringComparison.OrdinalIgnoreCase);
  128. Assert.Contains ("Disabled", str, StringComparison.OrdinalIgnoreCase);
  129. }
  130. [Fact]
  131. public void CopyConstructor_Null_Throws ()
  132. {
  133. Assert.Throws<ArgumentNullException> (() => new Scheme (null));
  134. }
  135. [Fact]
  136. public void Is_Thread_Safe_For_Concurrent_Reads ()
  137. {
  138. Scheme scheme = new (new Attribute ("Red", "Blue"));
  139. Parallel.For (0, 1000, i =>
  140. {
  141. // All threads can safely read properties
  142. _ = scheme.Normal;
  143. _ = scheme.GetAttributeForRole (VisualRole.Focus);
  144. _ = scheme.ToString ();
  145. });
  146. }
  147. }