AttributeTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.Value);
  18. Assert.Equal ((Color)Color.White, attribute.Foreground);
  19. Assert.Equal ((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.Value);
  29. Assert.Equal ((Color)Color.White, attribute.Foreground);
  30. Assert.Equal ((Color)Color.Black, attribute.Background);
  31. }
  32. [Fact]
  33. public void ColorNamesConstructor ()
  34. {
  35. // Arrange & Act
  36. var attribute = new Attribute (ColorNames.Blue);
  37. // Assert
  38. Assert.Equal ((Color)Color.Blue, attribute.Foreground);
  39. Assert.Equal ((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 = ColorNames.Black;
  58. var attribute = new Attribute (foregroundColor, backgroundColorName);
  59. // Assert
  60. Assert.Equal (foregroundColor, attribute.Foreground);
  61. Assert.Equal ((Color)backgroundColorName, attribute.Background);
  62. }
  63. [Fact]
  64. public void ColorNamesAndColorConstructor ()
  65. {
  66. // Arrange & Act
  67. var foregroundColorName = ColorNames.BrightYellow;
  68. var backgroundColor = new Color (128, 128, 128);
  69. var attribute = new Attribute (foregroundColorName, backgroundColor);
  70. // Assert
  71. Assert.Equal ((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.Value);
  83. Assert.Equal ((Color)Color.White, attr.Foreground);
  84. Assert.Equal ((Color)Color.Black, attr.Background);
  85. // Test foreground, background
  86. var fg = new Color ();
  87. fg = (Color)Color.Red;
  88. var bg = new Color ();
  89. bg = (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 = ColorNames.BrightYellow;
  125. var backgroundColorName = ColorNames.Black;
  126. // Act
  127. var attribute = new Attribute (foregroundColorName, backgroundColorName);
  128. // Assert
  129. Assert.Equal ((Color)foregroundColorName, attribute.Foreground);
  130. Assert.Equal ((Color)backgroundColorName, attribute.Background);
  131. }
  132. [Fact]
  133. public void MakeColorNamesAndColor_ForegroundAndBackgroundShouldMatchInput ()
  134. {
  135. // Arrange
  136. var foregroundColorName = ColorNames.Green;
  137. var backgroundColor = new Color (128, 128, 128);
  138. // Act
  139. var attribute = new Attribute (foregroundColorName, backgroundColor);
  140. // Assert
  141. Assert.Equal ((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 = ColorNames.White;
  150. // Act
  151. var attribute = new Attribute (foregroundColor, backgroundColorName);
  152. // Assert
  153. Assert.Equal (foregroundColor, attribute.Foreground);
  154. Assert.Equal ((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 = (Color)Color.Red;
  166. var bg = new Color ();
  167. bg = (Color)Color.Blue;
  168. // Test conversion to int
  169. attr = new Attribute (value, fg, bg);
  170. int value_implicit = attr.Value;
  171. Assert.Equal (value, value_implicit);
  172. Assert.Equal (value, attr.Value);
  173. driver.End ();
  174. Application.Shutdown ();
  175. }
  176. [Fact]
  177. public void Make_SetsNotInitialized_NoDriver ()
  178. {
  179. var fg = new Color ();
  180. fg = (Color)Color.Red;
  181. var bg = new Color ();
  182. bg = (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 = (Color)Color.Red;
  194. var bg = new Color ();
  195. bg = (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 = (Color)Color.Red;
  208. var bg = new Color ();
  209. bg = (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 Get_Asserts_NoDriver ()
  217. {
  218. Assert.Throws<InvalidOperationException> (() => Attribute.Get ());
  219. }
  220. [Fact]
  221. public void Get_Gets ()
  222. {
  223. var driver = new FakeDriver ();
  224. Application.Init (driver);
  225. driver.Init (() => { });
  226. var value = 42;
  227. var fg = new Color ();
  228. fg = (Color)Color.Red;
  229. var bg = new Color ();
  230. bg = (Color)Color.Blue;
  231. var attr = new Attribute (value, fg, bg);
  232. driver.SetAttribute (attr);
  233. var ret_attr = Attribute.Get ();
  234. Assert.Equal (value, ret_attr.Value);
  235. Assert.Equal (fg, ret_attr.Foreground);
  236. Assert.Equal (bg, ret_attr.Background);
  237. driver.End ();
  238. Application.Shutdown ();
  239. }
  240. [Fact]
  241. [AutoInitShutdown]
  242. public void GetColors_Based_On_Value ()
  243. {
  244. var driver = Application.Driver;
  245. var attrValue = new Attribute (Color.Red, Color.Green).Value;
  246. driver.GetColors (attrValue, out ColorNames fg, out ColorNames bg);
  247. Assert.Equal ((Color)Color.Red, (Color)fg);
  248. Assert.Equal ((Color)Color.Green, (Color)bg);
  249. }
  250. [Fact]
  251. public void IsValid_Tests ()
  252. {
  253. var attr = new Attribute ();
  254. Assert.True (attr.HasValidColors);
  255. attr = new Attribute (Color.Red, Color.Green);
  256. Assert.True (attr.HasValidColors);
  257. //attr = new Attribute (Color.Red, (Color)(-1));
  258. //Assert.False (attr.HasValidColors);
  259. //attr = new Attribute ((Color)(-1), Color.Green);
  260. //Assert.False (attr.HasValidColors);
  261. //attr = new Attribute ((Color)(-1), (Color)(-1));
  262. //Assert.False (attr.HasValidColors);
  263. }
  264. [Fact]
  265. public void Equals_NotInitialized ()
  266. {
  267. var attr1 = new Attribute (Color.Red, Color.Green);
  268. var attr2 = new Attribute (Color.Red, Color.Green);
  269. Assert.True (attr1.Equals (attr2));
  270. Assert.True (attr2.Equals (attr1));
  271. }
  272. [Fact]
  273. public void NotEquals_NotInitialized ()
  274. {
  275. var attr1 = new Attribute (Color.Red, Color.Green);
  276. var attr2 = new Attribute (Color.Green, Color.Red);
  277. Assert.False (attr1.Equals (attr2));
  278. Assert.False (attr2.Equals (attr1));
  279. }
  280. [Fact, AutoInitShutdown]
  281. public void Equals_Initialized ()
  282. {
  283. Assert.NotNull (Application.Driver);
  284. var attr1 = new Attribute (Color.Red, Color.Green);
  285. var attr2 = new Attribute (Color.Red, Color.Green);
  286. Assert.True (attr1.Equals (attr2));
  287. Assert.True (attr2.Equals (attr1));
  288. }
  289. [Fact, AutoInitShutdown]
  290. public void NotEquals_Initialized ()
  291. {
  292. var attr1 = new Attribute (Color.Red, Color.Green);
  293. var attr2 = new Attribute (Color.Green, Color.Red);
  294. Assert.False (attr1.Equals (attr2));
  295. Assert.False (attr2.Equals (attr1));
  296. }
  297. [Fact]
  298. public void EqualityOperator_ShouldReturnTrueForEqualAttributes ()
  299. {
  300. // Arrange
  301. var attribute1 = new Attribute (Color.Red, Color.Black);
  302. var attribute2 = new Attribute (Color.Red, Color.Black);
  303. // Act & Assert
  304. Assert.True (attribute1 == attribute2);
  305. }
  306. [Fact]
  307. public void EqualityOperator_ShouldReturnFalseForDifferentAttributes ()
  308. {
  309. // Arrange
  310. var attribute1 = new Attribute (Color.Red, Color.Black);
  311. var attribute2 = new Attribute (Color.Blue, Color.Black);
  312. // Act & Assert
  313. Assert.False (attribute1 == attribute2);
  314. }
  315. [Fact]
  316. public void InequalityOperator_ShouldReturnTrueForDifferentAttributes ()
  317. {
  318. // Arrange
  319. var attribute1 = new Attribute (Color.Red, Color.Black);
  320. var attribute2 = new Attribute (Color.Blue, Color.Black);
  321. // Act & Assert
  322. Assert.True (attribute1 != attribute2);
  323. }
  324. [Fact]
  325. public void InequalityOperator_ShouldReturnFalseForEqualAttributes ()
  326. {
  327. // Arrange
  328. var attribute1 = new Attribute (Color.Red, Color.Black);
  329. var attribute2 = new Attribute (Color.Red, Color.Black);
  330. // Act & Assert
  331. Assert.False (attribute1 != attribute2);
  332. }
  333. [Fact]
  334. public void ToString_ShouldReturnFormattedStringWithForegroundAndBackground ()
  335. {
  336. // Arrange
  337. var foregroundColor = new Color (0, 0, 255);
  338. var backgroundColor = new Color (255, 255, 255);
  339. var expectedString = $"{foregroundColor},{backgroundColor}";
  340. // Act
  341. var attribute = new Attribute (foregroundColor, backgroundColor);
  342. var attributeString = attribute.ToString ();
  343. // Assert
  344. Assert.Equal (expectedString, attributeString);
  345. }
  346. }