ColorTests.ParsingAndFormatting.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 ?? string.Empty, 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 ?? string.Empty, 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. for (var i = char.MinValue; i < 255; i++)
  95. {
  96. if (!char.IsAsciiDigit (i))
  97. {
  98. values.Add ($"rgb({i},{i},{i})");
  99. values.Add ($"rgba({i},{i},{i})");
  100. }
  101. if (!char.IsAsciiHexDigit (i))
  102. {
  103. values.Add ($"#{i}{i}{i}{i}{i}{i}");
  104. values.Add ($"#{i}{i}{i}{i}{i}{i}{i}{i}");
  105. }
  106. }
  107. //Also throw in a couple of just badly formatted strings
  108. values.Add ("rgbaa(1,2,3,4))");
  109. values.Add ("#rgb(1,FF,3,4)");
  110. values.Add ("rgb(1,FF,3,4");
  111. values.Add ("rgb(1,2,3,4.5))");
  112. return values;
  113. }
  114. public static TheoryData<string?, int> TryParse_string_Returns_True_For_Valid_Inputs ()
  115. {
  116. TheoryData<string?, int> values = []
  117. ;
  118. for (byte i = 16; i < 224; i += 32)
  119. {
  120. // Using this so the span only has to be written one way.
  121. int expectedRgb = BinaryPrimitives.ReadInt32LittleEndian ([(byte)(i + 16), i, (byte)(i - 16), 255]);
  122. int expectedRgba = BinaryPrimitives.ReadInt32LittleEndian ([(byte)(i + 16), i, (byte)(i - 16), i]);
  123. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D})", expectedRgb);
  124. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D},{i:D})", expectedRgba);
  125. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D})", expectedRgb);
  126. values.Add ($"rgba({i - 16:D},{i:D},{i + 16:D},{i:D})", expectedRgba);
  127. values.Add ($"#{i - 16:X2}{i:X2}{i + 16:X2}", expectedRgb);
  128. values.Add ($"#{i:X2}{i - 16:X2}{i:X2}{i + 16:X2}", expectedRgba);
  129. }
  130. for (byte i = 1; i < 0xE; i++)
  131. {
  132. values.Add (
  133. $"#{i - 1:X0}{i:X0}{i + 1:X0}",
  134. BinaryPrimitives.ReadInt32LittleEndian (
  135. [
  136. // Have to stick the least significant 4 bits in the most significant 4 bits to duplicate the hex values
  137. // Breaking this out just so it's easier to see.
  138. (byte)((i + 1) | ((i + 1) << 4)),
  139. (byte)(i | (i << 4)),
  140. (byte)((i - 1) | ((i - 1) << 4)),
  141. 255
  142. ]
  143. )
  144. );
  145. values.Add (
  146. $"#{i:X0}{i - 1:X0}{i:X0}{i + 1:X0}",
  147. BinaryPrimitives.ReadInt32LittleEndian (
  148. [
  149. (byte)((i + 1) | ((i + 1) << 4)),
  150. (byte)(i | (i << 4)),
  151. (byte)((i - 1) | ((i - 1) << 4)),
  152. (byte)(i | (i << 4))
  153. ]
  154. )
  155. );
  156. }
  157. return values;
  158. }
  159. }