AttributeTests.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. Application.Init (driver);
  79. driver.Init ();
  80. // Test parameterless constructor
  81. var attr = new Attribute ();
  82. Assert.Equal (-1, attr.PlatformColor);
  83. Assert.Equal (new Color (Color.White), attr.Foreground);
  84. Assert.Equal (new Color (Color.Black), attr.Background);
  85. // Test foreground, background
  86. var fg = new Color ();
  87. fg = new Color (Color.Red);
  88. var bg = new Color ();
  89. bg = new Color (Color.Blue);
  90. attr = new Attribute (fg, bg);
  91. //Assert.True (attr.Initialized);
  92. //Assert.True (attr.HasValidColors);
  93. Assert.Equal (fg, attr.Foreground);
  94. Assert.Equal (bg, attr.Background);
  95. attr = new Attribute (fg);
  96. //Assert.True (attr.Initialized);
  97. //Assert.True (attr.HasValidColors);
  98. Assert.Equal (fg, attr.Foreground);
  99. Assert.Equal (fg, attr.Background);
  100. attr = new Attribute (bg);
  101. //Assert.True (attr.Initialized);
  102. //Assert.True (attr.HasValidColors);
  103. Assert.Equal (bg, attr.Foreground);
  104. Assert.Equal (bg, attr.Background);
  105. driver.End ();
  106. Application.Shutdown ();
  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. Application.Init (driver);
  161. driver.Init ();
  162. var attr = new Attribute ();
  163. var value = 42;
  164. var fg = new Color ();
  165. fg = new Color (Color.Red);
  166. var bg = new Color ();
  167. bg = new Color (Color.Blue);
  168. // Test conversion to int
  169. attr = new Attribute (value, fg, bg);
  170. int value_implicit = attr.PlatformColor;
  171. Assert.Equal (value, value_implicit);
  172. Assert.Equal (value, attr.PlatformColor);
  173. driver.End ();
  174. Application.Shutdown ();
  175. }
  176. [Fact]
  177. public void Make_SetsNotInitialized_NoDriver ()
  178. {
  179. var fg = new Color ();
  180. fg = new Color (Color.Red);
  181. var bg = new Color ();
  182. bg = new Color (Color.Blue);
  183. var a = new Attribute (fg, bg);
  184. //Assert.False (a.Initialized);
  185. }
  186. [Fact]
  187. public void Make_Creates ()
  188. {
  189. var driver = new FakeDriver ();
  190. Application.Init (driver);
  191. driver.Init ();
  192. var fg = new Color ();
  193. fg = new Color (Color.Red);
  194. var bg = new Color ();
  195. bg = new Color (Color.Blue);
  196. var attr = new Attribute (fg, bg);
  197. //Assert.True (attr.Initialized);
  198. Assert.Equal (fg, attr.Foreground);
  199. Assert.Equal (bg, attr.Background);
  200. driver.End ();
  201. Application.Shutdown ();
  202. }
  203. [Fact]
  204. public void Make_Creates_NoDriver ()
  205. {
  206. var fg = new Color ();
  207. fg = new Color (Color.Red);
  208. var bg = new Color ();
  209. bg = new Color (Color.Blue);
  210. var attr = new Attribute (fg, bg);
  211. //Assert.False (attr.Initialized);
  212. Assert.Equal (fg, attr.Foreground);
  213. Assert.Equal (bg, attr.Background);
  214. }
  215. [Fact]
  216. public void Equals_NotInitialized ()
  217. {
  218. var attr1 = new Attribute (Color.Red, Color.Green);
  219. var attr2 = new Attribute (Color.Red, Color.Green);
  220. Assert.True (attr1.Equals (attr2));
  221. Assert.True (attr2.Equals (attr1));
  222. }
  223. [Fact]
  224. public void NotEquals_NotInitialized ()
  225. {
  226. var attr1 = new Attribute (Color.Red, Color.Green);
  227. var attr2 = new Attribute (Color.Green, Color.Red);
  228. Assert.False (attr1.Equals (attr2));
  229. Assert.False (attr2.Equals (attr1));
  230. }
  231. [Fact, AutoInitShutdown]
  232. public void Equals_Initialized ()
  233. {
  234. Assert.NotNull (Application.Driver);
  235. var attr1 = new Attribute (Color.Red, Color.Green);
  236. var attr2 = new Attribute (Color.Red, Color.Green);
  237. Assert.True (attr1.Equals (attr2));
  238. Assert.True (attr2.Equals (attr1));
  239. }
  240. [Fact, AutoInitShutdown]
  241. public void NotEquals_Initialized ()
  242. {
  243. var attr1 = new Attribute (Color.Red, Color.Green);
  244. var attr2 = new Attribute (Color.Green, Color.Red);
  245. Assert.False (attr1.Equals (attr2));
  246. Assert.False (attr2.Equals (attr1));
  247. }
  248. [Fact]
  249. public void EqualityOperator_ShouldReturnTrueForEqualAttributes ()
  250. {
  251. // Arrange
  252. var attribute1 = new Attribute (Color.Red, Color.Black);
  253. var attribute2 = new Attribute (Color.Red, Color.Black);
  254. // Act & Assert
  255. Assert.True (attribute1 == attribute2);
  256. }
  257. [Fact]
  258. public void EqualityOperator_ShouldReturnFalseForDifferentAttributes ()
  259. {
  260. // Arrange
  261. var attribute1 = new Attribute (Color.Red, Color.Black);
  262. var attribute2 = new Attribute (Color.Blue, Color.Black);
  263. // Act & Assert
  264. Assert.False (attribute1 == attribute2);
  265. }
  266. [Fact]
  267. public void InequalityOperator_ShouldReturnTrueForDifferentAttributes ()
  268. {
  269. // Arrange
  270. var attribute1 = new Attribute (Color.Red, Color.Black);
  271. var attribute2 = new Attribute (Color.Blue, Color.Black);
  272. // Act & Assert
  273. Assert.True (attribute1 != attribute2);
  274. }
  275. [Fact]
  276. public void InequalityOperator_ShouldReturnFalseForEqualAttributes ()
  277. {
  278. // Arrange
  279. var attribute1 = new Attribute (Color.Red, Color.Black);
  280. var attribute2 = new Attribute (Color.Red, Color.Black);
  281. // Act & Assert
  282. Assert.False (attribute1 != attribute2);
  283. }
  284. [Fact]
  285. public void ToString_ShouldReturnFormattedStringWithForegroundAndBackground ()
  286. {
  287. // Arrange
  288. var foregroundColor = new Color (0, 0, 255);
  289. var backgroundColor = new Color (255, 255, 255);
  290. var expectedString = $"{foregroundColor},{backgroundColor}";
  291. // Act
  292. var attribute = new Attribute (foregroundColor, backgroundColor);
  293. var attributeString = attribute.ToString ();
  294. // Assert
  295. Assert.Equal (expectedString, attributeString);
  296. }
  297. }