ColorTests.ParsingAndFormatting.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #nullable enable
  2. using System.Buffers.Binary;
  3. using System.Globalization;
  4. namespace Terminal.Gui.DrawingTests;
  5. public partial class ColorTests
  6. {
  7. [Fact]
  8. public void Color_ToString_WithNamedColor ()
  9. {
  10. // Arrange
  11. var color = new Color (0, 55, 218); // Blue
  12. // Act
  13. var colorString = color.ToString ();
  14. // Assert
  15. Assert.Equal ("Blue", colorString);
  16. }
  17. [Fact]
  18. public void Color_ToString_WithRGBColor ()
  19. {
  20. // Arrange
  21. var color = new Color (1, 64, 32); // Custom RGB color
  22. // Act
  23. var colorString = color.ToString ();
  24. // Assert
  25. Assert.Equal ("#014020", colorString);
  26. }
  27. [Theory]
  28. [CombinatorialData]
  29. public void Parse_And_ToString_RoundTrip_For_Known_FormatStrings (
  30. [CombinatorialValues (null, "", "g", "G", "d", "D")] string formatString,
  31. [CombinatorialValues (0, 64, 255)] byte r,
  32. [CombinatorialValues (0, 64, 255)] byte g,
  33. [CombinatorialValues (0, 64, 255)] byte b
  34. )
  35. {
  36. Color constructedColor = new (r, g, b, 255);
  37. // Pre-conditions for the rest of the test to be valid
  38. Assert.Equal (r, constructedColor.R);
  39. Assert.Equal (g, constructedColor.G);
  40. Assert.Equal (b, constructedColor.B);
  41. Assert.Equal (255, constructedColor.A);
  42. //Get the ToString result with the specified format string
  43. var formattedColorString = constructedColor.ToString (formatString);
  44. // Now parse that string
  45. Color parsedColor = Color.Parse (formattedColorString);
  46. // They should have identical underlying values
  47. Assert.Equal (constructedColor.Argb, parsedColor.Argb);
  48. }
  49. [Theory]
  50. [CombinatorialData]
  51. public void ToString_WithInvariantCultureAndNullString_IsSameAsParameterless (
  52. [CombinatorialValues (0, 64, 128, 255)] byte r,
  53. [CombinatorialValues (0, 64, 128, 255)] byte g,
  54. [CombinatorialValues (0, 64, 128, 255)] byte b
  55. )
  56. {
  57. var expected = $"#{r:X2}{g:X2}{b:X2}";
  58. Color testColor = new (r, g, b);
  59. var testStringWithExplicitInvariantCulture = testColor.ToString (null, CultureInfo.InvariantCulture);
  60. Assert.Equal (expected, testStringWithExplicitInvariantCulture);
  61. var parameterlessToStringValue = testColor.ToString ();
  62. Assert.Equal (parameterlessToStringValue, testStringWithExplicitInvariantCulture);
  63. }
  64. [Theory]
  65. [MemberData (
  66. nameof (ColorTestsTheoryDataGenerators.TryParse_string_Returns_False_For_Invalid_Inputs),
  67. MemberType = typeof (ColorTestsTheoryDataGenerators)
  68. )]
  69. public void TryParse_string_Returns_False_For_Invalid_Inputs (string? input)
  70. {
  71. bool tryParseStatus = Color.TryParse (input, out Color? color);
  72. Assert.False (tryParseStatus);
  73. Assert.Null (color);
  74. }
  75. [Theory]
  76. [MemberData (
  77. nameof (ColorTestsTheoryDataGenerators.TryParse_string_Returns_True_For_Valid_Inputs),
  78. MemberType = typeof (ColorTestsTheoryDataGenerators)
  79. )]
  80. public void TryParse_string_Returns_True_For_Valid_Inputs (string input, int expectedColorArgb)
  81. {
  82. bool tryParseStatus = Color.TryParse (input, out Color? color);
  83. Assert.True (tryParseStatus);
  84. Assert.NotNull (color);
  85. Assert.IsType<Color> (color);
  86. Assert.Equal (expectedColorArgb, color.Value.Rgba);
  87. }
  88. }
  89. public static partial class ColorTestsTheoryDataGenerators
  90. {
  91. public static TheoryData<string?> TryParse_string_Returns_False_For_Invalid_Inputs ()
  92. {
  93. TheoryData<string?> values =
  94. [
  95. null
  96. ]
  97. ;
  98. for (var i = char.MinValue; i < 255; i++)
  99. {
  100. if (!char.IsAsciiDigit (i))
  101. {
  102. values.Add ($"rgb({i},{i},{i})");
  103. values.Add ($"rgba({i},{i},{i})");
  104. }
  105. if (!char.IsAsciiHexDigit (i))
  106. {
  107. values.Add ($"#{i}{i}{i}{i}{i}{i}");
  108. values.Add ($"#{i}{i}{i}{i}{i}{i}{i}{i}");
  109. }
  110. }
  111. //Also throw in a couple of just badly formatted strings
  112. values.Add ("rgbaa(1,2,3,4))");
  113. values.Add ("#rgb(1,FF,3,4)");
  114. values.Add ("rgb(1,FF,3,4");
  115. values.Add ("rgb(1,2,3,4.5))");
  116. return values;
  117. }
  118. public static TheoryData<string?, int> TryParse_string_Returns_True_For_Valid_Inputs ()
  119. {
  120. TheoryData<string?, int> values = []
  121. ;
  122. for (byte i = 16; i < 224; i += 32)
  123. {
  124. // Using this so the span only has to be written one way.
  125. int expectedRgb = BinaryPrimitives.ReadInt32LittleEndian ([(byte)(i + 16), i, (byte)(i - 16), 255]);
  126. int expectedRgba = BinaryPrimitives.ReadInt32LittleEndian ([(byte)(i + 16), i, (byte)(i - 16), i]);
  127. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D})", expectedRgb);
  128. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D},{i:D})", expectedRgba);
  129. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D})", expectedRgb);
  130. values.Add ($"rgba({i - 16:D},{i:D},{i + 16:D},{i:D})", expectedRgba);
  131. values.Add ($"#{i - 16:X2}{i:X2}{i + 16:X2}", expectedRgb);
  132. values.Add ($"#{i:X2}{i - 16:X2}{i:X2}{i + 16:X2}", expectedRgba);
  133. }
  134. for (byte i = 1; i < 0xE; i++)
  135. {
  136. values.Add (
  137. $"#{i - 1:X0}{i:X0}{i + 1:X0}",
  138. BinaryPrimitives.ReadInt32LittleEndian (
  139. [
  140. // Have to stick the least significant 4 bits in the most significant 4 bits to duplicate the hex values
  141. // Breaking this out just so it's easier to see.
  142. (byte)((i + 1) | ((i + 1) << 4)),
  143. (byte)(i | (i << 4)),
  144. (byte)((i - 1) | ((i - 1) << 4)),
  145. 255
  146. ]
  147. )
  148. );
  149. values.Add (
  150. $"#{i:X0}{i - 1:X0}{i:X0}{i + 1:X0}",
  151. BinaryPrimitives.ReadInt32LittleEndian (
  152. [
  153. (byte)((i + 1) | ((i + 1) << 4)),
  154. (byte)(i | (i << 4)),
  155. (byte)((i - 1) | ((i - 1) << 4)),
  156. (byte)(i | (i << 4))
  157. ]
  158. )
  159. );
  160. }
  161. return values;
  162. }
  163. }