AttributeTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. using Xunit;
  6. // Alias Console to MockConsole so we don't accidentally use Console
  7. using Console = Terminal.Gui.FakeConsole;
  8. namespace Terminal.Gui.DriverTests {
  9. public class AttributeTests {
  10. [Fact]
  11. public void Constuctors_Constuct ()
  12. {
  13. var driver = new FakeDriver ();
  14. Application.Init (driver);
  15. driver.Init (() => { });
  16. // Test parameterless constructor
  17. var attr = new Attribute ();
  18. Assert.Equal (default (int), attr.Value);
  19. Assert.Equal (default (Color), attr.Foreground);
  20. Assert.Equal (default (Color), attr.Background);
  21. // Test value, foreground, background
  22. var value = 42;
  23. var fg = new Color ();
  24. fg = Color.Red;
  25. var bg = new Color ();
  26. bg = Color.Blue;
  27. attr = new Attribute (value, fg, bg);
  28. Assert.Equal (value, attr.Value);
  29. Assert.Equal (fg, attr.Foreground);
  30. Assert.Equal (bg, attr.Background);
  31. // value, foreground, background
  32. attr = new Attribute (fg, bg);
  33. Assert.Equal (fg, attr.Foreground);
  34. Assert.Equal (bg, attr.Background);
  35. attr = new Attribute (fg);
  36. Assert.Equal (fg, attr.Foreground);
  37. Assert.Equal (fg, attr.Background);
  38. attr = new Attribute (bg);
  39. Assert.Equal (bg, attr.Foreground);
  40. Assert.Equal (bg, attr.Background);
  41. driver.End ();
  42. Application.Shutdown ();
  43. }
  44. [Fact]
  45. public void Implicit_Assign ()
  46. {
  47. var driver = new FakeDriver ();
  48. Application.Init (driver);
  49. driver.Init (() => { });
  50. var attr = new Attribute ();
  51. var value = 42;
  52. var fg = new Color ();
  53. fg = Color.Red;
  54. var bg = new Color ();
  55. bg = Color.Blue;
  56. // Test conversion to int
  57. attr = new Attribute (value, fg, bg);
  58. int value_implicit = (int)attr.Value;
  59. Assert.Equal (value, value_implicit);
  60. // Test conversion from int
  61. attr = value;
  62. Assert.Equal (value, attr.Value);
  63. driver.End ();
  64. Application.Shutdown ();
  65. }
  66. [Fact]
  67. public void Implicit_Assign_NoDriver ()
  68. {
  69. var attr = new Attribute ();
  70. var fg = new Color ();
  71. fg = Color.Red;
  72. var bg = new Color ();
  73. bg = Color.Blue;
  74. // Test conversion to int
  75. attr = new Attribute (fg, bg);
  76. int value_implicit = (int)attr.Value;
  77. Assert.False (attr.Initialized);
  78. Assert.Equal (-1, value_implicit);
  79. Assert.False (attr.Initialized);
  80. // Test conversion from int
  81. attr = -1;
  82. Assert.Equal (-1, attr.Value);
  83. Assert.False (attr.Initialized);
  84. }
  85. [Fact]
  86. public void Make_SetsNotInitialized_NoDriver ()
  87. {
  88. var fg = new Color ();
  89. fg = Color.Red;
  90. var bg = new Color ();
  91. bg = Color.Blue;
  92. var a = Attribute.Make (fg, bg);
  93. Assert.False (a.Initialized);
  94. }
  95. [Fact]
  96. public void Make_Creates ()
  97. {
  98. var driver = new FakeDriver ();
  99. Application.Init (driver);
  100. driver.Init (() => { });
  101. var fg = new Color ();
  102. fg = Color.Red;
  103. var bg = new Color ();
  104. bg = Color.Blue;
  105. var attr = Attribute.Make (fg, bg);
  106. Assert.True (attr.Initialized);
  107. Assert.Equal (fg, attr.Foreground);
  108. Assert.Equal (bg, attr.Background);
  109. driver.End ();
  110. Application.Shutdown ();
  111. }
  112. [Fact]
  113. public void Make_Creates_NoDriver ()
  114. {
  115. var fg = new Color ();
  116. fg = Color.Red;
  117. var bg = new Color ();
  118. bg = Color.Blue;
  119. var attr = Attribute.Make (fg, bg);
  120. Assert.False (attr.Initialized);
  121. Assert.Equal (fg, attr.Foreground);
  122. Assert.Equal (bg, attr.Background);
  123. }
  124. [Fact]
  125. public void Get_Asserts_NoDriver ()
  126. {
  127. Assert.Throws<InvalidOperationException> (() => Attribute.Get ());
  128. }
  129. [Fact]
  130. public void Get_Gets ()
  131. {
  132. var driver = new FakeDriver ();
  133. Application.Init (driver);
  134. driver.Init (() => { });
  135. var value = 42;
  136. var fg = new Color ();
  137. fg = Color.Red;
  138. var bg = new Color ();
  139. bg = Color.Blue;
  140. var attr = new Attribute (value, fg, bg);
  141. driver.SetAttribute (attr);
  142. var ret_attr = Attribute.Get ();
  143. Assert.Equal (value, ret_attr.Value);
  144. Assert.Equal (fg, ret_attr.Foreground);
  145. Assert.Equal (bg, ret_attr.Background);
  146. driver.End ();
  147. Application.Shutdown ();
  148. }
  149. [Fact]
  150. [AutoInitShutdown]
  151. public void GetColors_Based_On_Value ()
  152. {
  153. var driver = Application.Driver;
  154. var attrValue = new Attribute (Color.Red, Color.Green).Value;
  155. driver.GetColors (attrValue, out Color fg, out Color bg);
  156. Assert.Equal (Color.Red, fg);
  157. Assert.Equal (Color.Green, bg);
  158. }
  159. [Fact]
  160. public void IsValid_Tests ()
  161. {
  162. var attr = new Attribute ();
  163. Assert.True (attr.HasValidColors);
  164. attr = new Attribute (Color.Red, Color.Green);
  165. Assert.True (attr.HasValidColors);
  166. attr = new Attribute (Color.Red, (Color)(-1));
  167. Assert.False (attr.HasValidColors);
  168. attr = new Attribute ((Color)(-1), Color.Green);
  169. Assert.False (attr.HasValidColors);
  170. attr = new Attribute ((Color)(-1), (Color)(-1));
  171. Assert.False (attr.HasValidColors);
  172. }
  173. }
  174. }