ColorTests.ParsingAndFormatting.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 (ColorName16.Blue);// 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. [MemberData (
  51. nameof (ColorTestsTheoryDataGenerators.TryParse_string_Returns_False_For_Invalid_Inputs),
  52. MemberType = typeof (ColorTestsTheoryDataGenerators)
  53. )]
  54. public void TryParse_string_Returns_False_For_Invalid_Inputs (string? input)
  55. {
  56. bool tryParseStatus = Color.TryParse (input ?? string.Empty, out Color? color);
  57. Assert.False (tryParseStatus);
  58. Assert.Null (color);
  59. }
  60. [Theory]
  61. [MemberData (
  62. nameof (ColorTestsTheoryDataGenerators.TryParse_string_Returns_True_For_Valid_Inputs),
  63. MemberType = typeof (ColorTestsTheoryDataGenerators)
  64. )]
  65. public void TryParse_string_Returns_True_For_Valid_Inputs (string? input, int expectedColorArgb)
  66. {
  67. bool tryParseStatus = Color.TryParse (input ?? string.Empty, out Color? color);
  68. Assert.True (tryParseStatus);
  69. Assert.NotNull (color);
  70. Assert.IsType<Color> (color);
  71. Assert.Equal (expectedColorArgb, color.Value.Rgba);
  72. }
  73. }
  74. public static partial class ColorTestsTheoryDataGenerators
  75. {
  76. public static TheoryData<string?> TryParse_string_Returns_False_For_Invalid_Inputs ()
  77. {
  78. TheoryData<string?> values = [];
  79. for (var i = char.MinValue; i < 255; i++)
  80. {
  81. if (!char.IsAsciiDigit (i))
  82. {
  83. values.Add ($"rgb({i},{i},{i})");
  84. values.Add ($"rgba({i},{i},{i})");
  85. }
  86. if (!char.IsAsciiHexDigit (i))
  87. {
  88. values.Add ($"#{i}{i}{i}{i}{i}{i}");
  89. values.Add ($"#{i}{i}{i}{i}{i}{i}{i}{i}");
  90. }
  91. }
  92. //Also throw in a couple of just badly formatted strings
  93. values.Add ("rgbaa(1,2,3,4))");
  94. values.Add ("#rgb(1,FF,3,4)");
  95. values.Add ("rgb(1,FF,3,4");
  96. values.Add ("rgb(1,2,3,4.5))");
  97. return values;
  98. }
  99. public static TheoryData<string?, int> TryParse_string_Returns_True_For_Valid_Inputs ()
  100. {
  101. TheoryData<string?, int> values = []
  102. ;
  103. for (byte i = 16; i < 224; i += 32)
  104. {
  105. // Using this so the span only has to be written one way.
  106. int expectedRgb = BinaryPrimitives.ReadInt32LittleEndian ([(byte)(i + 16), i, (byte)(i - 16), 255]);
  107. int expectedRgba = BinaryPrimitives.ReadInt32LittleEndian ([(byte)(i + 16), i, (byte)(i - 16), i]);
  108. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D})", expectedRgb);
  109. values.Add ($"rgb({i - 16:D},{i:D},{i + 16:D},{i:D})", expectedRgba);
  110. values.Add ($"rgba({i - 16:D},{i:D},{i + 16:D},{i:D})", expectedRgba);
  111. values.Add ($"#{i - 16:X2}{i:X2}{i + 16:X2}", expectedRgb);
  112. values.Add ($"#{i:X2}{i - 16:X2}{i:X2}{i + 16:X2}", expectedRgba);
  113. }
  114. for (byte i = 1; i < 0xE; i++)
  115. {
  116. values.Add (
  117. $"#{i - 1:X0}{i:X0}{i + 1:X0}",
  118. BinaryPrimitives.ReadInt32LittleEndian (
  119. [
  120. // Have to stick the least significant 4 bits in the most significant 4 bits to duplicate the hex values
  121. // Breaking this out just so it's easier to see.
  122. (byte)((i + 1) | ((i + 1) << 4)),
  123. (byte)(i | (i << 4)),
  124. (byte)((i - 1) | ((i - 1) << 4)),
  125. 255
  126. ]
  127. )
  128. );
  129. values.Add (
  130. $"#{i:X0}{i - 1:X0}{i:X0}{i + 1:X0}",
  131. BinaryPrimitives.ReadInt32LittleEndian (
  132. [
  133. (byte)((i + 1) | ((i + 1) << 4)),
  134. (byte)(i | (i << 4)),
  135. (byte)((i - 1) | ((i - 1) << 4)),
  136. (byte)(i | (i << 4))
  137. ]
  138. )
  139. );
  140. }
  141. return values;
  142. }
  143. }