ColorTests.ParsingAndFormatting.cs 5.1 KB

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