AttributeTests.cs 9.8 KB

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