AttributeTests.cs 9.0 KB

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