AttributeTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 Constructor_ParsesNamedColorsAndStyle ()
  7. {
  8. var attr = new Attribute ("Red", "Black", "Bold,Underline");
  9. Assert.Equal (Color.Parse ("Red"), attr.Foreground);
  10. Assert.Equal (Color.Parse ("Black"), attr.Background);
  11. Assert.True (attr.Style.HasFlag (TextStyle.Bold));
  12. Assert.True (attr.Style.HasFlag (TextStyle.Underline));
  13. }
  14. [Fact]
  15. public void Constructor_ParsesHexColors ()
  16. {
  17. var attr = new Attribute ("#FF0000", "#000000", "Italic");
  18. Assert.Equal (Color.Parse ("#FF0000"), attr.Foreground);
  19. Assert.Equal (Color.Parse ("#000000"), attr.Background);
  20. Assert.Equal (TextStyle.Italic, attr.Style);
  21. }
  22. [Fact]
  23. public void Constructor_ParsesRgbColors ()
  24. {
  25. var attr = new Attribute ("rgb(0,255,0)", "rgb(0,0,255)", "Faint");
  26. Assert.Equal (Color.Parse ("rgb(0,255,0)"), attr.Foreground);
  27. Assert.Equal (Color.Parse ("rgb(0,0,255)"), attr.Background);
  28. Assert.Equal (TextStyle.Faint, attr.Style);
  29. }
  30. [Fact]
  31. public void Constructor_DefaultsToNoneStyle_WhenStyleIsNullOrEmpty ()
  32. {
  33. var attr1 = new Attribute ("White", "Black");
  34. var attr2 = new Attribute ("White", "Black", null);
  35. var attr3 = new Attribute ("White", "Black", "");
  36. Assert.Equal (TextStyle.None, attr1.Style);
  37. Assert.Equal (TextStyle.None, attr2.Style);
  38. Assert.Equal (TextStyle.None, attr3.Style);
  39. }
  40. [Fact]
  41. public void Constructor_DefaultsToNoneStyle_WhenStyleIsInvalid ()
  42. {
  43. var attr = new Attribute ("White", "Black", "NotAStyle");
  44. Assert.Equal (TextStyle.None, attr.Style);
  45. }
  46. [Fact]
  47. public void ColorAndColorNamesConstructor ()
  48. {
  49. // Arrange & Act
  50. var foregroundColor = new Color (0, 0, 255);
  51. var backgroundColorName = ColorName16.Black;
  52. var attribute = new Attribute (foregroundColor, backgroundColorName);
  53. // Assert
  54. Assert.Equal (foregroundColor, attribute.Foreground);
  55. Assert.Equal (new (backgroundColorName), attribute.Background);
  56. }
  57. [Fact]
  58. public void ColorConstructor ()
  59. {
  60. // Arrange & Act
  61. var foregroundColor = new Color (0, 0, 255);
  62. var backgroundColor = new Color (255, 255, 255);
  63. var attribute = new Attribute (foregroundColor, backgroundColor);
  64. // Assert
  65. Assert.Equal (foregroundColor, attribute.Foreground);
  66. Assert.Equal (backgroundColor, attribute.Background);
  67. }
  68. [Fact]
  69. public void ColorNamesAndColorConstructor ()
  70. {
  71. // Arrange & Act
  72. var foregroundColorName = ColorName16.BrightYellow;
  73. var backgroundColor = new Color (128, 128, 128);
  74. var attribute = new Attribute (foregroundColorName, backgroundColor);
  75. // Assert
  76. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  77. Assert.Equal (backgroundColor, attribute.Background);
  78. }
  79. [Fact]
  80. public void ColorNamesConstructor ()
  81. {
  82. // Arrange & Act
  83. var attribute = new Attribute (ColorName16.Blue);
  84. // Assert
  85. Assert.Equal (new (Color.Blue), attribute.Foreground);
  86. Assert.Equal (new (Color.Blue), attribute.Background);
  87. }
  88. [Fact]
  89. public void Constructors_Construct ()
  90. {
  91. var driver = new FakeDriver ();
  92. driver.Init ();
  93. // Test parameterless constructor
  94. var attr = new Attribute ();
  95. Assert.Equal (-1, attr.PlatformColor);
  96. Assert.Equal (new (Color.White), attr.Foreground);
  97. Assert.Equal (new (Color.Black), attr.Background);
  98. // Test foreground, background
  99. var fg = new Color ();
  100. fg = new (Color.Red);
  101. var bg = new Color ();
  102. bg = new (Color.Blue);
  103. attr = new (fg, bg);
  104. //Assert.True (attr.Initialized);
  105. //Assert.True (attr.HasValidColors);
  106. Assert.Equal (fg, attr.Foreground);
  107. Assert.Equal (bg, attr.Background);
  108. attr = new (fg);
  109. //Assert.True (attr.Initialized);
  110. //Assert.True (attr.HasValidColors);
  111. Assert.Equal (fg, attr.Foreground);
  112. Assert.Equal (fg, attr.Background);
  113. attr = new (bg);
  114. //Assert.True (attr.Initialized);
  115. //Assert.True (attr.HasValidColors);
  116. Assert.Equal (bg, attr.Foreground);
  117. Assert.Equal (bg, attr.Background);
  118. driver.End ();
  119. }
  120. [Fact]
  121. public void DefaultConstructor ()
  122. {
  123. // Arrange & Act
  124. var attribute = new Attribute ();
  125. // Assert
  126. //Assert.False (attribute.Initialized);
  127. Assert.Equal (-1, attribute.PlatformColor);
  128. Assert.Equal (new (Color.White), attribute.Foreground);
  129. Assert.Equal (new (Color.Black), attribute.Background);
  130. }
  131. [Fact]
  132. public void Equality_IncludesStyle ()
  133. {
  134. var attr1 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  135. var attr2 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  136. var attr3 = new Attribute (Color.Red, Color.Black, TextStyle.Underline);
  137. Assert.Equal (attr1, attr2);
  138. Assert.NotEqual (attr1, attr3);
  139. }
  140. [Fact]
  141. public void EqualityOperator_ShouldReturnFalseForDifferentAttributes ()
  142. {
  143. // Arrange
  144. var attribute1 = new Attribute (Color.Red, Color.Black);
  145. var attribute2 = new Attribute (Color.Blue, Color.Black);
  146. // Act & Assert
  147. Assert.False (attribute1 == attribute2);
  148. }
  149. [Fact]
  150. public void EqualityOperator_ShouldReturnTrueForEqualAttributes ()
  151. {
  152. // Arrange
  153. var attribute1 = new Attribute (Color.Red, Color.Black);
  154. var attribute2 = new Attribute (Color.Red, Color.Black);
  155. // Act & Assert
  156. Assert.True (attribute1 == attribute2);
  157. }
  158. [Fact]
  159. public void Equals_Initialized ()
  160. {
  161. var attr1 = new Attribute (Color.Red, Color.Green);
  162. var attr2 = new Attribute (Color.Red, Color.Green);
  163. Assert.True (attr1.Equals (attr2));
  164. Assert.True (attr2.Equals (attr1));
  165. }
  166. [Fact]
  167. public void Equals_NotInitialized ()
  168. {
  169. var attr1 = new Attribute (Color.Red, Color.Green);
  170. var attr2 = new Attribute (Color.Red, Color.Green);
  171. Assert.True (attr1.Equals (attr2));
  172. Assert.True (attr2.Equals (attr1));
  173. }
  174. [Fact]
  175. public void GetHashCode_ConsistentWithEquals ()
  176. {
  177. var attr1 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  178. var attr2 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  179. Assert.Equal (attr1, attr2);
  180. Assert.Equal (attr1.GetHashCode (), attr2.GetHashCode ());
  181. }
  182. [Fact]
  183. public void Implicit_Assign ()
  184. {
  185. var driver = new FakeDriver ();
  186. driver.Init ();
  187. var attr = new Attribute ();
  188. var value = 42;
  189. var fg = new Color ();
  190. fg = new (Color.Red);
  191. var bg = new Color ();
  192. bg = new (Color.Blue);
  193. // Test conversion to int
  194. attr = new (value, fg, bg);
  195. int value_implicit = attr.PlatformColor;
  196. Assert.Equal (value, value_implicit);
  197. Assert.Equal (value, attr.PlatformColor);
  198. driver.End ();
  199. }
  200. [Fact]
  201. public void InequalityOperator_ShouldReturnFalseForEqualAttributes ()
  202. {
  203. // Arrange
  204. var attribute1 = new Attribute (Color.Red, Color.Black);
  205. var attribute2 = new Attribute (Color.Red, Color.Black);
  206. // Act & Assert
  207. Assert.False (attribute1 != attribute2);
  208. }
  209. [Fact]
  210. public void InequalityOperator_ShouldReturnTrueForDifferentAttributes ()
  211. {
  212. // Arrange
  213. var attribute1 = new Attribute (Color.Red, Color.Black);
  214. var attribute2 = new Attribute (Color.Blue, Color.Black);
  215. // Act & Assert
  216. Assert.True (attribute1 != attribute2);
  217. }
  218. [Fact]
  219. public void Is_Value_Type ()
  220. {
  221. // prove that Color is a value type
  222. Assert.True (typeof (Attribute).IsValueType);
  223. }
  224. [Fact]
  225. public void List_RoundTrip_EqualityHolds ()
  226. {
  227. List<Attribute> list1 = [new (Color.Red, Color.Black, TextStyle.Bold)];
  228. List<Attribute> list2 = new (list1);
  229. Assert.Equal (list1, list2);
  230. }
  231. [Fact]
  232. public void Make_Creates ()
  233. {
  234. var driver = new FakeDriver ();
  235. driver.Init ();
  236. var fg = new Color ();
  237. fg = new (Color.Red);
  238. var bg = new Color ();
  239. bg = new (Color.Blue);
  240. var attr = new Attribute (fg, bg);
  241. //Assert.True (attr.Initialized);
  242. Assert.Equal (fg, attr.Foreground);
  243. Assert.Equal (bg, attr.Background);
  244. driver.End ();
  245. }
  246. [Fact]
  247. public void Make_Creates_NoDriver ()
  248. {
  249. var fg = new Color ();
  250. fg = new (Color.Red);
  251. var bg = new Color ();
  252. bg = new (Color.Blue);
  253. var attr = new Attribute (fg, bg);
  254. //Assert.False (attr.Initialized);
  255. Assert.Equal (fg, attr.Foreground);
  256. Assert.Equal (bg, attr.Background);
  257. }
  258. [Fact]
  259. public void Make_SetsNotInitialized_NoDriver ()
  260. {
  261. var fg = new Color ();
  262. fg = new (Color.Red);
  263. var bg = new Color ();
  264. bg = new (Color.Blue);
  265. var a = new Attribute (fg, bg);
  266. //Assert.False (a.Initialized);
  267. }
  268. [Fact]
  269. public void MakeColorAndColor_ForegroundAndBackgroundShouldMatchInput ()
  270. {
  271. // Arrange
  272. var foregroundColor = new Color (0, 0, 255);
  273. var backgroundColor = new Color (255, 255, 255);
  274. // Act
  275. var attribute = new Attribute (foregroundColor, backgroundColor);
  276. // Assert
  277. Assert.Equal (foregroundColor, attribute.Foreground);
  278. Assert.Equal (backgroundColor, attribute.Background);
  279. }
  280. [Fact]
  281. public void MakeColorAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  282. {
  283. // Arrange
  284. var foregroundColor = new Color (255, 0);
  285. var backgroundColorName = ColorName16.White;
  286. // Act
  287. var attribute = new Attribute (foregroundColor, backgroundColorName);
  288. // Assert
  289. Assert.Equal (foregroundColor, attribute.Foreground);
  290. Assert.Equal (new (backgroundColorName), attribute.Background);
  291. }
  292. [Fact]
  293. public void MakeColorNamesAndColor_ForegroundAndBackgroundShouldMatchInput ()
  294. {
  295. // Arrange
  296. var foregroundColorName = ColorName16.Green;
  297. var backgroundColor = new Color (128, 128, 128);
  298. // Act
  299. var attribute = new Attribute (foregroundColorName, backgroundColor);
  300. // Assert
  301. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  302. Assert.Equal (backgroundColor, attribute.Background);
  303. }
  304. [Fact]
  305. public void MakeColorNamesAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  306. {
  307. // Arrange
  308. var foregroundColorName = ColorName16.BrightYellow;
  309. var backgroundColorName = ColorName16.Black;
  310. // Act
  311. var attribute = new Attribute (foregroundColorName, backgroundColorName);
  312. // Assert
  313. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  314. Assert.Equal (new (backgroundColorName), attribute.Background);
  315. }
  316. [Fact]
  317. public void NotEquals_Initialized ()
  318. {
  319. var attr1 = new Attribute (Color.Red, Color.Green);
  320. var attr2 = new Attribute (Color.Green, Color.Red);
  321. Assert.False (attr1.Equals (attr2));
  322. Assert.False (attr2.Equals (attr1));
  323. }
  324. [Fact]
  325. public void NotEquals_NotInitialized ()
  326. {
  327. var attr1 = new Attribute (Color.Red, Color.Green);
  328. var attr2 = new Attribute (Color.Green, Color.Red);
  329. Assert.False (attr1.Equals (attr2));
  330. Assert.False (attr2.Equals (attr1));
  331. }
  332. [Fact]
  333. public void ToString_Formats_Correctly ()
  334. {
  335. // Arrange
  336. var foregroundColor = new Color (0, 0, 255);
  337. var backgroundColor = new Color (255, 255, 255);
  338. var expectedString = $"[{foregroundColor},{backgroundColor},None]";
  339. // Act
  340. var attribute = new Attribute (foregroundColor, backgroundColor);
  341. var attributeString = attribute.ToString ();
  342. // Assert
  343. Assert.Equal (expectedString, attributeString);
  344. }
  345. [Theory]
  346. [InlineData (TextStyle.Bold, "Bold")]
  347. [InlineData (TextStyle.Bold | TextStyle.Underline, "Bold, Underline")]
  348. [InlineData (TextStyle.None, "None")]
  349. public void ToString_IncludesStyle (TextStyle style, string expectedStyleString)
  350. {
  351. var attr = new Attribute (Color.Red, Color.Black, style);
  352. var result = attr.ToString ();
  353. Assert.Contains (expectedStyleString, result);
  354. }
  355. [Fact]
  356. public void ToString_ShouldFailComparison_IfDifferentInstancesSameContent ()
  357. {
  358. var original = new Attribute (Color.White, Color.White);
  359. var clone = new Attribute (Color.White, Color.White);
  360. // These print the same
  361. Assert.Equal (original.ToString (), clone.ToString ());
  362. // But this will fail if anything differs under the hood
  363. Assert.Equal (original, clone); // Should pass — record struct
  364. }
  365. }