AttributeTests.cs 10.0 KB

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