AttributeTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 InequalityOperator_ShouldReturnFalseForEqualAttributes ()
  181. {
  182. // Arrange
  183. var attribute1 = new Attribute (Color.Red, Color.Black);
  184. var attribute2 = new Attribute (Color.Red, Color.Black);
  185. // Act & Assert
  186. Assert.False (attribute1 != attribute2);
  187. }
  188. [Fact]
  189. public void InequalityOperator_ShouldReturnTrueForDifferentAttributes ()
  190. {
  191. // Arrange
  192. var attribute1 = new Attribute (Color.Red, Color.Black);
  193. var attribute2 = new Attribute (Color.Blue, Color.Black);
  194. // Act & Assert
  195. Assert.True (attribute1 != attribute2);
  196. }
  197. [Fact]
  198. public void Is_Value_Type ()
  199. {
  200. // prove that Color is a value type
  201. Assert.True (typeof (Attribute).IsValueType);
  202. }
  203. [Fact]
  204. public void List_RoundTrip_EqualityHolds ()
  205. {
  206. List<Attribute> list1 = [new (Color.Red, Color.Black, TextStyle.Bold)];
  207. List<Attribute> list2 = new (list1);
  208. Assert.Equal (list1, list2);
  209. }
  210. [Fact]
  211. public void Make_Creates ()
  212. {
  213. var driver = new FakeDriver ();
  214. driver.Init ();
  215. var fg = new Color ();
  216. fg = new (Color.Red);
  217. var bg = new Color ();
  218. bg = new (Color.Blue);
  219. var attr = new Attribute (fg, bg);
  220. //Assert.True (attr.Initialized);
  221. Assert.Equal (fg, attr.Foreground);
  222. Assert.Equal (bg, attr.Background);
  223. driver.End ();
  224. }
  225. [Fact]
  226. public void Make_Creates_NoDriver ()
  227. {
  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.False (attr.Initialized);
  234. Assert.Equal (fg, attr.Foreground);
  235. Assert.Equal (bg, attr.Background);
  236. }
  237. [Fact]
  238. public void Make_SetsNotInitialized_NoDriver ()
  239. {
  240. var fg = new Color ();
  241. fg = new (Color.Red);
  242. var bg = new Color ();
  243. bg = new (Color.Blue);
  244. var a = new Attribute (fg, bg);
  245. //Assert.False (a.Initialized);
  246. }
  247. [Fact]
  248. public void MakeColorAndColor_ForegroundAndBackgroundShouldMatchInput ()
  249. {
  250. // Arrange
  251. var foregroundColor = new Color (0, 0, 255);
  252. var backgroundColor = new Color (255, 255, 255);
  253. // Act
  254. var attribute = new Attribute (foregroundColor, backgroundColor);
  255. // Assert
  256. Assert.Equal (foregroundColor, attribute.Foreground);
  257. Assert.Equal (backgroundColor, attribute.Background);
  258. }
  259. [Fact]
  260. public void MakeColorAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  261. {
  262. // Arrange
  263. var foregroundColor = new Color (255, 0);
  264. var backgroundColorName = ColorName16.White;
  265. // Act
  266. var attribute = new Attribute (foregroundColor, backgroundColorName);
  267. // Assert
  268. Assert.Equal (foregroundColor, attribute.Foreground);
  269. Assert.Equal (new (backgroundColorName), attribute.Background);
  270. }
  271. [Fact]
  272. public void MakeColorNamesAndColor_ForegroundAndBackgroundShouldMatchInput ()
  273. {
  274. // Arrange
  275. var foregroundColorName = ColorName16.Green;
  276. var backgroundColor = new Color (128, 128, 128);
  277. // Act
  278. var attribute = new Attribute (foregroundColorName, backgroundColor);
  279. // Assert
  280. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  281. Assert.Equal (backgroundColor, attribute.Background);
  282. }
  283. [Fact]
  284. public void MakeColorNamesAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  285. {
  286. // Arrange
  287. var foregroundColorName = ColorName16.BrightYellow;
  288. var backgroundColorName = ColorName16.Black;
  289. // Act
  290. var attribute = new Attribute (foregroundColorName, backgroundColorName);
  291. // Assert
  292. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  293. Assert.Equal (new (backgroundColorName), attribute.Background);
  294. }
  295. [Fact]
  296. public void NotEquals_Initialized ()
  297. {
  298. var attr1 = new Attribute (Color.Red, Color.Green);
  299. var attr2 = new Attribute (Color.Green, Color.Red);
  300. Assert.False (attr1.Equals (attr2));
  301. Assert.False (attr2.Equals (attr1));
  302. }
  303. [Fact]
  304. public void NotEquals_NotInitialized ()
  305. {
  306. var attr1 = new Attribute (Color.Red, Color.Green);
  307. var attr2 = new Attribute (Color.Green, Color.Red);
  308. Assert.False (attr1.Equals (attr2));
  309. Assert.False (attr2.Equals (attr1));
  310. }
  311. [Fact]
  312. public void ToString_Formats_Correctly ()
  313. {
  314. // Arrange
  315. var foregroundColor = new Color (0, 0, 255);
  316. var backgroundColor = new Color (255, 255, 255);
  317. var expectedString = $"[{foregroundColor},{backgroundColor},None]";
  318. // Act
  319. var attribute = new Attribute (foregroundColor, backgroundColor);
  320. var attributeString = attribute.ToString ();
  321. // Assert
  322. Assert.Equal (expectedString, attributeString);
  323. }
  324. [Theory]
  325. [InlineData (TextStyle.Bold, "Bold")]
  326. [InlineData (TextStyle.Bold | TextStyle.Underline, "Bold, Underline")]
  327. [InlineData (TextStyle.None, "None")]
  328. public void ToString_IncludesStyle (TextStyle style, string expectedStyleString)
  329. {
  330. var attr = new Attribute (Color.Red, Color.Black, style);
  331. var result = attr.ToString ();
  332. Assert.Contains (expectedStyleString, result);
  333. }
  334. [Fact]
  335. public void ToString_ShouldFailComparison_IfDifferentInstancesSameContent ()
  336. {
  337. var original = new Attribute (Color.White, Color.White);
  338. var clone = new Attribute (Color.White, Color.White);
  339. // These print the same
  340. Assert.Equal (original.ToString (), clone.ToString ());
  341. // But this will fail if anything differs under the hood
  342. Assert.Equal (original, clone); // Should pass — record struct
  343. }
  344. }