AttributeTests.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using Xunit;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. using Console = Terminal.Gui.FakeConsole;
  4. namespace Terminal.Gui.DrawingTests;
  5. public class AttributeTests {
  6. [Fact]
  7. public void Attribute_Is_Value_Type () =>
  8. // prove that Color is a value type
  9. Assert.True (typeof (Attribute).IsValueType);
  10. [Fact]
  11. public void DefaultConstructor ()
  12. {
  13. // Arrange & Act
  14. var attribute = new Attribute ();
  15. // Assert
  16. //Assert.False (attribute.Initialized);
  17. Assert.Equal (-1, attribute.PlatformColor);
  18. Assert.Equal (new Color (Color.White), attribute.Foreground);
  19. Assert.Equal (new Color (Color.Black), attribute.Background);
  20. }
  21. [Fact]
  22. public void PlatformColorConstructor ()
  23. {
  24. // Arrange & Act
  25. var attribute = new Attribute (42);
  26. // Assert
  27. //Assert.True (attribute.Initialized);
  28. Assert.Equal (42, attribute.PlatformColor);
  29. Assert.Equal (new Color (Color.White), attribute.Foreground);
  30. Assert.Equal (new Color (Color.Black), attribute.Background);
  31. }
  32. [Fact]
  33. public void ColorNamesConstructor ()
  34. {
  35. // Arrange & Act
  36. var attribute = new Attribute (ColorName.Blue);
  37. // Assert
  38. Assert.Equal (new Color (Color.Blue), attribute.Foreground);
  39. Assert.Equal (new Color (Color.Blue), attribute.Background);
  40. }
  41. [Fact]
  42. [AutoInitShutdown]
  43. public void ColorConstructor ()
  44. {
  45. // Arrange & Act
  46. var foregroundColor = new Color (0, 0, 255);
  47. var backgroundColor = new Color (255, 255, 255);
  48. var attribute = new Attribute (foregroundColor, backgroundColor);
  49. // Assert
  50. Assert.Equal (foregroundColor, attribute.Foreground);
  51. Assert.Equal (backgroundColor, attribute.Background);
  52. }
  53. [Fact]
  54. [AutoInitShutdown]
  55. public void ColorAndColorNamesConstructor ()
  56. {
  57. // Arrange & Act
  58. var foregroundColor = new Color (0, 0, 255);
  59. var backgroundColorName = ColorName.Black;
  60. var attribute = new Attribute (foregroundColor, backgroundColorName);
  61. // Assert
  62. Assert.Equal (foregroundColor, attribute.Foreground);
  63. Assert.Equal (new Color (backgroundColorName), attribute.Background);
  64. }
  65. [Fact]
  66. public void ColorNamesAndColorConstructor ()
  67. {
  68. // Arrange & Act
  69. var foregroundColorName = ColorName.BrightYellow;
  70. var backgroundColor = new Color (128, 128, 128);
  71. var attribute = new Attribute (foregroundColorName, backgroundColor);
  72. // Assert
  73. Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
  74. Assert.Equal (backgroundColor, attribute.Background);
  75. }
  76. [Fact]
  77. public void Constructors_Construct ()
  78. {
  79. var driver = new FakeDriver ();
  80. driver.Init ();
  81. // Test parameterless constructor
  82. var attr = new Attribute ();
  83. Assert.Equal (-1, attr.PlatformColor);
  84. Assert.Equal (new Color (Color.White), attr.Foreground);
  85. Assert.Equal (new Color (Color.Black), attr.Background);
  86. // Test foreground, background
  87. var fg = new Color ();
  88. fg = new Color (Color.Red);
  89. var bg = new Color ();
  90. bg = new Color (Color.Blue);
  91. attr = new Attribute (fg, bg);
  92. //Assert.True (attr.Initialized);
  93. //Assert.True (attr.HasValidColors);
  94. Assert.Equal (fg, attr.Foreground);
  95. Assert.Equal (bg, attr.Background);
  96. attr = new Attribute (fg);
  97. //Assert.True (attr.Initialized);
  98. //Assert.True (attr.HasValidColors);
  99. Assert.Equal (fg, attr.Foreground);
  100. Assert.Equal (fg, attr.Background);
  101. attr = new Attribute (bg);
  102. //Assert.True (attr.Initialized);
  103. //Assert.True (attr.HasValidColors);
  104. Assert.Equal (bg, attr.Foreground);
  105. Assert.Equal (bg, attr.Background);
  106. driver.End ();
  107. }
  108. [Fact]
  109. public void MakeColorAndColor_ForegroundAndBackgroundShouldMatchInput ()
  110. {
  111. // Arrange
  112. var foregroundColor = new Color (0, 0, 255);
  113. var backgroundColor = new Color (255, 255, 255);
  114. // Act
  115. var attribute = new Attribute (foregroundColor, backgroundColor);
  116. // Assert
  117. Assert.Equal (foregroundColor, attribute.Foreground);
  118. Assert.Equal (backgroundColor, attribute.Background);
  119. }
  120. [Fact]
  121. public void MakeColorNamesAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  122. {
  123. // Arrange
  124. var foregroundColorName = ColorName.BrightYellow;
  125. var backgroundColorName = ColorName.Black;
  126. // Act
  127. var attribute = new Attribute (foregroundColorName, backgroundColorName);
  128. // Assert
  129. Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
  130. Assert.Equal (new Color (backgroundColorName), attribute.Background);
  131. }
  132. [Fact]
  133. public void MakeColorNamesAndColor_ForegroundAndBackgroundShouldMatchInput ()
  134. {
  135. // Arrange
  136. var foregroundColorName = ColorName.Green;
  137. var backgroundColor = new Color (128, 128, 128);
  138. // Act
  139. var attribute = new Attribute (foregroundColorName, backgroundColor);
  140. // Assert
  141. Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
  142. Assert.Equal (backgroundColor, attribute.Background);
  143. }
  144. [Fact]
  145. public void MakeColorAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  146. {
  147. // Arrange
  148. var foregroundColor = new Color (255, 0, 0);
  149. var backgroundColorName = ColorName.White;
  150. // Act
  151. var attribute = new Attribute (foregroundColor, backgroundColorName);
  152. // Assert
  153. Assert.Equal (foregroundColor, attribute.Foreground);
  154. Assert.Equal (new Color (backgroundColorName), attribute.Background);
  155. }
  156. [Fact]
  157. public void Implicit_Assign ()
  158. {
  159. var driver = new FakeDriver ();
  160. driver.Init ();
  161. var attr = new Attribute ();
  162. var value = 42;
  163. var fg = new Color ();
  164. fg = new Color (Color.Red);
  165. var bg = new Color ();
  166. bg = new Color (Color.Blue);
  167. // Test conversion to int
  168. attr = new Attribute (value, fg, bg);
  169. var value_implicit = attr.PlatformColor;
  170. Assert.Equal (value, value_implicit);
  171. Assert.Equal (value, attr.PlatformColor);
  172. driver.End ();
  173. }
  174. [Fact]
  175. public void Make_SetsNotInitialized_NoDriver ()
  176. {
  177. var fg = new Color ();
  178. fg = new Color (Color.Red);
  179. var bg = new Color ();
  180. bg = new Color (Color.Blue);
  181. var a = new Attribute (fg, bg);
  182. //Assert.False (a.Initialized);
  183. }
  184. [Fact]
  185. public void Make_Creates ()
  186. {
  187. var driver = new FakeDriver ();
  188. driver.Init ();
  189. var fg = new Color ();
  190. fg = new Color (Color.Red);
  191. var bg = new Color ();
  192. bg = new Color (Color.Blue);
  193. var attr = new Attribute (fg, bg);
  194. //Assert.True (attr.Initialized);
  195. Assert.Equal (fg, attr.Foreground);
  196. Assert.Equal (bg, attr.Background);
  197. driver.End ();
  198. }
  199. [Fact]
  200. public void Make_Creates_NoDriver ()
  201. {
  202. var fg = new Color ();
  203. fg = new Color (Color.Red);
  204. var bg = new Color ();
  205. bg = new Color (Color.Blue);
  206. var attr = new Attribute (fg, bg);
  207. //Assert.False (attr.Initialized);
  208. Assert.Equal (fg, attr.Foreground);
  209. Assert.Equal (bg, attr.Background);
  210. }
  211. [Fact]
  212. public void Equals_NotInitialized ()
  213. {
  214. var attr1 = new Attribute (Color.Red, Color.Green);
  215. var attr2 = new Attribute (Color.Red, Color.Green);
  216. Assert.True (attr1.Equals (attr2));
  217. Assert.True (attr2.Equals (attr1));
  218. }
  219. [Fact]
  220. public void NotEquals_NotInitialized ()
  221. {
  222. var attr1 = new Attribute (Color.Red, Color.Green);
  223. var attr2 = new Attribute (Color.Green, Color.Red);
  224. Assert.False (attr1.Equals (attr2));
  225. Assert.False (attr2.Equals (attr1));
  226. }
  227. [Fact, AutoInitShutdown]
  228. public void Equals_Initialized ()
  229. {
  230. Assert.NotNull (Application.Driver);
  231. var attr1 = new Attribute (Color.Red, Color.Green);
  232. var attr2 = new Attribute (Color.Red, Color.Green);
  233. Assert.True (attr1.Equals (attr2));
  234. Assert.True (attr2.Equals (attr1));
  235. }
  236. [Fact, AutoInitShutdown]
  237. public void NotEquals_Initialized ()
  238. {
  239. var attr1 = new Attribute (Color.Red, Color.Green);
  240. var attr2 = new Attribute (Color.Green, Color.Red);
  241. Assert.False (attr1.Equals (attr2));
  242. Assert.False (attr2.Equals (attr1));
  243. }
  244. [Fact]
  245. public void EqualityOperator_ShouldReturnTrueForEqualAttributes ()
  246. {
  247. // Arrange
  248. var attribute1 = new Attribute (Color.Red, Color.Black);
  249. var attribute2 = new Attribute (Color.Red, Color.Black);
  250. // Act & Assert
  251. Assert.True (attribute1 == attribute2);
  252. }
  253. [Fact]
  254. public void EqualityOperator_ShouldReturnFalseForDifferentAttributes ()
  255. {
  256. // Arrange
  257. var attribute1 = new Attribute (Color.Red, Color.Black);
  258. var attribute2 = new Attribute (Color.Blue, Color.Black);
  259. // Act & Assert
  260. Assert.False (attribute1 == attribute2);
  261. }
  262. [Fact]
  263. public void InequalityOperator_ShouldReturnTrueForDifferentAttributes ()
  264. {
  265. // Arrange
  266. var attribute1 = new Attribute (Color.Red, Color.Black);
  267. var attribute2 = new Attribute (Color.Blue, Color.Black);
  268. // Act & Assert
  269. Assert.True (attribute1 != attribute2);
  270. }
  271. [Fact]
  272. public void InequalityOperator_ShouldReturnFalseForEqualAttributes ()
  273. {
  274. // Arrange
  275. var attribute1 = new Attribute (Color.Red, Color.Black);
  276. var attribute2 = new Attribute (Color.Red, Color.Black);
  277. // Act & Assert
  278. Assert.False (attribute1 != attribute2);
  279. }
  280. [Fact]
  281. public void ToString_ShouldReturnFormattedStringWithForegroundAndBackground ()
  282. {
  283. // Arrange
  284. var foregroundColor = new Color (0, 0, 255);
  285. var backgroundColor = new Color (255, 255, 255);
  286. var expectedString = $"[{foregroundColor},{backgroundColor}]";
  287. // Act
  288. var attribute = new Attribute (foregroundColor, backgroundColor);
  289. var attributeString = attribute.ToString ();
  290. // Assert
  291. Assert.Equal (expectedString, attributeString);
  292. }
  293. }