AttributeTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // Alias Console to MockConsole so we don't accidentally use Console
  2. using UnitTests;
  3. namespace DrawingTests;
  4. public class AttributeTests : FakeDriverBase
  5. {
  6. [Fact]
  7. public void Constructor_ParsesNamedColorsAndStyle ()
  8. {
  9. var attr = new Attribute ("Red", "Black", "Bold,Underline");
  10. Assert.Equal (Color.Parse ("Red"), attr.Foreground);
  11. Assert.Equal (Color.Parse ("Black"), attr.Background);
  12. Assert.True (attr.Style.HasFlag (TextStyle.Bold));
  13. Assert.True (attr.Style.HasFlag (TextStyle.Underline));
  14. }
  15. [Fact]
  16. public void Constructor_ParsesHexColors ()
  17. {
  18. var attr = new Attribute ("#FF0000", "#000000", "Italic");
  19. Assert.Equal (Color.Parse ("#FF0000"), attr.Foreground);
  20. Assert.Equal (Color.Parse ("#000000"), attr.Background);
  21. Assert.Equal (TextStyle.Italic, attr.Style);
  22. }
  23. [Fact]
  24. public void Constructor_ParsesRgbColors ()
  25. {
  26. var attr = new Attribute ("rgb(0,255,0)", "rgb(0,0,255)", "Faint");
  27. Assert.Equal (Color.Parse ("rgb(0,255,0)"), attr.Foreground);
  28. Assert.Equal (Color.Parse ("rgb(0,0,255)"), attr.Background);
  29. Assert.Equal (TextStyle.Faint, attr.Style);
  30. }
  31. [Fact]
  32. public void Constructor_DefaultsToNoneStyle_WhenStyleIsNullOrEmpty ()
  33. {
  34. var attr1 = new Attribute ("White", "Black");
  35. var attr2 = new Attribute ("White", "Black");
  36. var attr3 = new Attribute ("White", "Black", "");
  37. Assert.Equal (TextStyle.None, attr1.Style);
  38. Assert.Equal (TextStyle.None, attr2.Style);
  39. Assert.Equal (TextStyle.None, attr3.Style);
  40. }
  41. [Fact]
  42. public void Constructor_DefaultsToNoneStyle_WhenStyleIsInvalid ()
  43. {
  44. var attr = new Attribute ("White", "Black", "NotAStyle");
  45. Assert.Equal (TextStyle.None, attr.Style);
  46. }
  47. [Fact]
  48. public void ColorAndColorNamesConstructor ()
  49. {
  50. // Arrange & Act
  51. var foregroundColor = new Color (0, 0, 255);
  52. var backgroundColorName = ColorName16.Black;
  53. var attribute = new Attribute (foregroundColor, backgroundColorName);
  54. // Assert
  55. Assert.Equal (foregroundColor, attribute.Foreground);
  56. Assert.Equal (new (backgroundColorName), attribute.Background);
  57. }
  58. [Fact]
  59. public void ColorConstructor ()
  60. {
  61. // Arrange & Act
  62. var foregroundColor = new Color (0, 0, 255);
  63. var backgroundColor = new Color (255, 255, 255);
  64. var attribute = new Attribute (foregroundColor, backgroundColor);
  65. // Assert
  66. Assert.Equal (foregroundColor, attribute.Foreground);
  67. Assert.Equal (backgroundColor, attribute.Background);
  68. }
  69. [Fact]
  70. public void ColorNamesAndColorConstructor ()
  71. {
  72. // Arrange & Act
  73. var foregroundColorName = ColorName16.BrightYellow;
  74. var backgroundColor = new Color (128, 128, 128);
  75. var attribute = new Attribute (foregroundColorName, backgroundColor);
  76. // Assert
  77. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  78. Assert.Equal (backgroundColor, attribute.Background);
  79. }
  80. [Fact]
  81. public void ColorNamesConstructor ()
  82. {
  83. // Arrange & Act
  84. var attribute = new Attribute (ColorName16.Blue);
  85. // Assert
  86. Assert.Equal (new (Color.Blue), attribute.Foreground);
  87. Assert.Equal (new (Color.Blue), attribute.Background);
  88. }
  89. [Fact]
  90. public void Constructors_Construct ()
  91. {
  92. IDriver driver = CreateFakeDriver ();
  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.Dispose ();
  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. IDriver driver = CreateFakeDriver ();
  214. var fg = new Color ();
  215. fg = new (Color.Red);
  216. var bg = new Color ();
  217. bg = new (Color.Blue);
  218. var attr = new Attribute (fg, bg);
  219. //Assert.True (attr.Initialized);
  220. Assert.Equal (fg, attr.Foreground);
  221. Assert.Equal (bg, attr.Background);
  222. driver.Dispose ();
  223. }
  224. [Fact]
  225. public void Make_Creates_NoDriver ()
  226. {
  227. var fg = new Color ();
  228. fg = new (Color.Red);
  229. var bg = new Color ();
  230. bg = new (Color.Blue);
  231. var attr = new Attribute (fg, bg);
  232. //Assert.False (attr.Initialized);
  233. Assert.Equal (fg, attr.Foreground);
  234. Assert.Equal (bg, attr.Background);
  235. }
  236. [Fact]
  237. public void Make_SetsNotInitialized_NoDriver ()
  238. {
  239. var fg = new Color ();
  240. fg = new (Color.Red);
  241. var bg = new Color ();
  242. bg = new (Color.Blue);
  243. var a = new Attribute (fg, bg);
  244. //Assert.False (a.Initialized);
  245. }
  246. [Fact]
  247. public void MakeColorAndColor_ForegroundAndBackgroundShouldMatchInput ()
  248. {
  249. // Arrange
  250. var foregroundColor = new Color (0, 0, 255);
  251. var backgroundColor = new Color (255, 255, 255);
  252. // Act
  253. var attribute = new Attribute (foregroundColor, backgroundColor);
  254. // Assert
  255. Assert.Equal (foregroundColor, attribute.Foreground);
  256. Assert.Equal (backgroundColor, attribute.Background);
  257. }
  258. [Fact]
  259. public void MakeColorAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  260. {
  261. // Arrange
  262. var foregroundColor = new Color (255, 0);
  263. var backgroundColorName = ColorName16.White;
  264. // Act
  265. var attribute = new Attribute (foregroundColor, backgroundColorName);
  266. // Assert
  267. Assert.Equal (foregroundColor, attribute.Foreground);
  268. Assert.Equal (new (backgroundColorName), attribute.Background);
  269. }
  270. [Fact]
  271. public void MakeColorNamesAndColor_ForegroundAndBackgroundShouldMatchInput ()
  272. {
  273. // Arrange
  274. var foregroundColorName = ColorName16.Green;
  275. var backgroundColor = new Color (128, 128, 128);
  276. // Act
  277. var attribute = new Attribute (foregroundColorName, backgroundColor);
  278. // Assert
  279. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  280. Assert.Equal (backgroundColor, attribute.Background);
  281. }
  282. [Fact]
  283. public void MakeColorNamesAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
  284. {
  285. // Arrange
  286. var foregroundColorName = ColorName16.BrightYellow;
  287. var backgroundColorName = ColorName16.Black;
  288. // Act
  289. var attribute = new Attribute (foregroundColorName, backgroundColorName);
  290. // Assert
  291. Assert.Equal (new (foregroundColorName), attribute.Foreground);
  292. Assert.Equal (new (backgroundColorName), attribute.Background);
  293. }
  294. [Fact]
  295. public void NotEquals_Initialized ()
  296. {
  297. var attr1 = new Attribute (Color.Red, Color.Green);
  298. var attr2 = new Attribute (Color.Green, Color.Red);
  299. Assert.False (attr1.Equals (attr2));
  300. Assert.False (attr2.Equals (attr1));
  301. }
  302. [Fact]
  303. public void NotEquals_NotInitialized ()
  304. {
  305. var attr1 = new Attribute (Color.Red, Color.Green);
  306. var attr2 = new Attribute (Color.Green, Color.Red);
  307. Assert.False (attr1.Equals (attr2));
  308. Assert.False (attr2.Equals (attr1));
  309. }
  310. [Fact]
  311. public void ToString_Formats_Correctly ()
  312. {
  313. // Arrange
  314. var foregroundColor = new Color (0, 0, 255);
  315. var backgroundColor = new Color (255, 255, 255);
  316. var expectedString = $"[{foregroundColor},{backgroundColor},None]";
  317. // Act
  318. var attribute = new Attribute (foregroundColor, backgroundColor);
  319. var attributeString = attribute.ToString ();
  320. // Assert
  321. Assert.Equal (expectedString, attributeString);
  322. }
  323. [Theory]
  324. [InlineData (TextStyle.Bold, "Bold")]
  325. [InlineData (TextStyle.Bold | TextStyle.Underline, "Bold, Underline")]
  326. [InlineData (TextStyle.None, "None")]
  327. public void ToString_IncludesStyle (TextStyle style, string expectedStyleString)
  328. {
  329. var attr = new Attribute (Color.Red, Color.Black, style);
  330. var result = attr.ToString ();
  331. Assert.Contains (expectedStyleString, result);
  332. }
  333. [Fact]
  334. public void ToString_ShouldFailComparison_IfDifferentInstancesSameContent ()
  335. {
  336. var original = new Attribute (Color.White, Color.White);
  337. var clone = new Attribute (Color.White, Color.White);
  338. // These print the same
  339. Assert.Equal (original.ToString (), clone.ToString ());
  340. // But this will fail if anything differs under the hood
  341. Assert.Equal (original, clone); // Should pass — record struct
  342. }
  343. }