AttributeTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Alias Console to MockConsole so we don't accidentally use Console
  2. namespace UnitTests_Parallelizable.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 (new (Color.White), attr.Foreground);
  96. Assert.Equal (new (Color.Black), attr.Background);
  97. // Test foreground, background
  98. var fg = new Color ();
  99. fg = new (Color.Red);
  100. var bg = new Color ();
  101. bg = new (Color.Blue);
  102. attr = new (fg, bg);
  103. //Assert.True (attr.Initialized);
  104. //Assert.True (attr.HasValidColors);
  105. Assert.Equal (fg, attr.Foreground);
  106. Assert.Equal (bg, attr.Background);
  107. attr = new (fg);
  108. //Assert.True (attr.Initialized);
  109. //Assert.True (attr.HasValidColors);
  110. Assert.Equal (fg, attr.Foreground);
  111. Assert.Equal (fg, attr.Background);
  112. attr = new (bg);
  113. //Assert.True (attr.Initialized);
  114. //Assert.True (attr.HasValidColors);
  115. Assert.Equal (bg, attr.Foreground);
  116. Assert.Equal (bg, attr.Background);
  117. driver.End ();
  118. }
  119. [Fact]
  120. public void DefaultConstructor ()
  121. {
  122. // Arrange & Act
  123. var attribute = new Attribute ();
  124. // Assert
  125. Assert.Equal (new (Color.White), attribute.Foreground);
  126. Assert.Equal (new (Color.Black), attribute.Background);
  127. }
  128. [Fact]
  129. public void Equality_IncludesStyle ()
  130. {
  131. var attr1 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  132. var attr2 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  133. var attr3 = new Attribute (Color.Red, Color.Black, TextStyle.Underline);
  134. Assert.Equal (attr1, attr2);
  135. Assert.NotEqual (attr1, attr3);
  136. }
  137. [Fact]
  138. public void EqualityOperator_ShouldReturnFalseForDifferentAttributes ()
  139. {
  140. // Arrange
  141. var attribute1 = new Attribute (Color.Red, Color.Black);
  142. var attribute2 = new Attribute (Color.Blue, Color.Black);
  143. // Act & Assert
  144. Assert.False (attribute1 == attribute2);
  145. }
  146. [Fact]
  147. public void EqualityOperator_ShouldReturnTrueForEqualAttributes ()
  148. {
  149. // Arrange
  150. var attribute1 = new Attribute (Color.Red, Color.Black);
  151. var attribute2 = new Attribute (Color.Red, Color.Black);
  152. // Act & Assert
  153. Assert.True (attribute1 == attribute2);
  154. }
  155. [Fact]
  156. public void Equals_Initialized ()
  157. {
  158. var attr1 = new Attribute (Color.Red, Color.Green);
  159. var attr2 = new Attribute (Color.Red, Color.Green);
  160. Assert.True (attr1.Equals (attr2));
  161. Assert.True (attr2.Equals (attr1));
  162. }
  163. [Fact]
  164. public void Equals_NotInitialized ()
  165. {
  166. var attr1 = new Attribute (Color.Red, Color.Green);
  167. var attr2 = new Attribute (Color.Red, Color.Green);
  168. Assert.True (attr1.Equals (attr2));
  169. Assert.True (attr2.Equals (attr1));
  170. }
  171. [Fact]
  172. public void GetHashCode_ConsistentWithEquals ()
  173. {
  174. var attr1 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  175. var attr2 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
  176. Assert.Equal (attr1, attr2);
  177. Assert.Equal (attr1.GetHashCode (), attr2.GetHashCode ());
  178. }
  179. [Fact]
  180. public void Implicit_Assign ()
  181. {
  182. var driver = new FakeDriver ();
  183. driver.Init ();
  184. var attr = new Attribute ();
  185. var value = 42;
  186. var fg = new Color ();
  187. fg = new (Color.Red);
  188. var bg = new Color ();
  189. bg = new (Color.Blue);
  190. driver.End ();
  191. }
  192. [Fact]
  193. public void InequalityOperator_ShouldReturnFalseForEqualAttributes ()
  194. {
  195. // Arrange
  196. var attribute1 = new Attribute (Color.Red, Color.Black);
  197. var attribute2 = new Attribute (Color.Red, Color.Black);
  198. // Act & Assert
  199. Assert.False (attribute1 != attribute2);
  200. }
  201. [Fact]
  202. public void InequalityOperator_ShouldReturnTrueForDifferentAttributes ()
  203. {
  204. // Arrange
  205. var attribute1 = new Attribute (Color.Red, Color.Black);
  206. var attribute2 = new Attribute (Color.Blue, Color.Black);
  207. // Act & Assert
  208. Assert.True (attribute1 != attribute2);
  209. }
  210. [Fact]
  211. public void Is_Value_Type ()
  212. {
  213. // prove that Color is a value type
  214. Assert.True (typeof (Attribute).IsValueType);
  215. }
  216. [Fact]
  217. public void List_RoundTrip_EqualityHolds ()
  218. {
  219. List<Attribute> list1 = [new (Color.Red, Color.Black, TextStyle.Bold)];
  220. List<Attribute> list2 = new (list1);
  221. Assert.Equal (list1, list2);
  222. }
  223. [Fact]
  224. public void Make_Creates ()
  225. {
  226. var driver = new FakeDriver ();
  227. driver.Init ();
  228. var fg = new Color ();
  229. fg = new (Color.Red);
  230. var bg = new Color ();
  231. bg = new (Color.Blue);
  232. var attr = new Attribute (fg, bg);
  233. //Assert.True (attr.Initialized);
  234. Assert.Equal (fg, attr.Foreground);
  235. Assert.Equal (bg, attr.Background);
  236. driver.End ();
  237. }
  238. [Fact]
  239. public void Make_Creates_NoDriver ()
  240. {
  241. var fg = new Color ();
  242. fg = new (Color.Red);
  243. var bg = new Color ();
  244. bg = new (Color.Blue);
  245. var attr = new Attribute (fg, bg);
  246. //Assert.False (attr.Initialized);
  247. Assert.Equal (fg, attr.Foreground);
  248. Assert.Equal (bg, attr.Background);
  249. }
  250. [Fact]
  251. public void Make_SetsNotInitialized_NoDriver ()
  252. {
  253. var fg = new Color ();
  254. fg = new (Color.Red);
  255. var bg = new Color ();
  256. bg = new (Color.Blue);
  257. var a = new Attribute (fg, bg);
  258. //Assert.False (a.Initialized);
  259. }
  260. [Fact]
  261. public void MakeColorAndColor_ForegroundAndBackgroundShouldMatchInput ()
  262. {
  263. // Arrange
  264. var foregroundColor = new Color (0, 0, 255);
  265. var backgroundColor = new Color (255, 255, 255);
  266. // Act
  267. var attribute = new Attribute (foregroundColor, backgroundColor);
  268. // Assert
  269. Assert.Equal (foregroundColor, attribute.Foreground);
  270. Assert.Equal (backgroundColor, attribute.Background);
  271. }
  272. [Fact]
  273. public void MakeColorAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  274. {
  275. // Arrange
  276. var foregroundColor = new Color (255, 0);
  277. var backgroundColorName = ColorName16.White;
  278. // Act
  279. var attribute = new Attribute (foregroundColor, backgroundColorName);
  280. // Assert
  281. Assert.Equal (foregroundColor, attribute.Foreground);
  282. Assert.Equal (new (backgroundColorName), attribute.Background);
  283. }
  284. [Fact]
  285. public void MakeColorNamesAndColor_ForegroundAndBackgroundShouldMatchInput ()
  286. {
  287. // Arrange
  288. var foregroundColorName = ColorName16.Green;
  289. var backgroundColor = new Color (128, 128, 128);
  290. // Act
  291. var attribute = new Attribute (foregroundColorName, backgroundColor);
  292. // Assert
  293. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  294. Assert.Equal (backgroundColor, attribute.Background);
  295. }
  296. [Fact]
  297. public void MakeColorNamesAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  298. {
  299. // Arrange
  300. var foregroundColorName = ColorName16.BrightYellow;
  301. var backgroundColorName = ColorName16.Black;
  302. // Act
  303. var attribute = new Attribute (foregroundColorName, backgroundColorName);
  304. // Assert
  305. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  306. Assert.Equal (new (backgroundColorName), attribute.Background);
  307. }
  308. [Fact]
  309. public void NotEquals_Initialized ()
  310. {
  311. var attr1 = new Attribute (Color.Red, Color.Green);
  312. var attr2 = new Attribute (Color.Green, Color.Red);
  313. Assert.False (attr1.Equals (attr2));
  314. Assert.False (attr2.Equals (attr1));
  315. }
  316. [Fact]
  317. public void NotEquals_NotInitialized ()
  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 ToString_Formats_Correctly ()
  326. {
  327. // Arrange
  328. var foregroundColor = new Color (0, 0, 255);
  329. var backgroundColor = new Color (255, 255, 255);
  330. var expectedString = $"[{foregroundColor},{backgroundColor},None]";
  331. // Act
  332. var attribute = new Attribute (foregroundColor, backgroundColor);
  333. var attributeString = attribute.ToString ();
  334. // Assert
  335. Assert.Equal (expectedString, attributeString);
  336. }
  337. [Theory]
  338. [InlineData (TextStyle.Bold, "Bold")]
  339. [InlineData (TextStyle.Bold | TextStyle.Underline, "Bold, Underline")]
  340. [InlineData (TextStyle.None, "None")]
  341. public void ToString_IncludesStyle (TextStyle style, string expectedStyleString)
  342. {
  343. var attr = new Attribute (Color.Red, Color.Black, style);
  344. var result = attr.ToString ();
  345. Assert.Contains (expectedStyleString, result);
  346. }
  347. [Fact]
  348. public void ToString_ShouldFailComparison_IfDifferentInstancesSameContent ()
  349. {
  350. var original = new Attribute (Color.White, Color.White);
  351. var clone = new Attribute (Color.White, Color.White);
  352. // These print the same
  353. Assert.Equal (original.ToString (), clone.ToString ());
  354. // But this will fail if anything differs under the hood
  355. Assert.Equal (original, clone); // Should pass — record struct
  356. }
  357. }