GradientTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. namespace Terminal.Gui.DrawingTests;
  2. public class GradientTests
  3. {
  4. // Static method to provide all enum values
  5. public static IEnumerable<object []> GradientDirectionValues ()
  6. {
  7. return typeof (GradientDirection).GetEnumValues ()
  8. .Cast<GradientDirection> ()
  9. .Select (direction => new object [] { direction });
  10. }
  11. [Theory]
  12. [MemberData (nameof (GradientDirectionValues))]
  13. public void GradientIsInclusive_2_by_2 (GradientDirection direction)
  14. {
  15. // Define the colors of the gradient stops
  16. List<Color> stops = new()
  17. {
  18. new (255, 0), // Red
  19. new (0, 0, 255) // Blue
  20. };
  21. // Define the number of steps between each color
  22. List<int> steps = new() { 10 }; // 10 steps between Red -> Blue
  23. var g = new Gradient (stops, steps);
  24. Assert.Equal (4, g.BuildCoordinateColorMapping (1, 1, direction).Count);
  25. }
  26. [Theory]
  27. [MemberData (nameof (GradientDirectionValues))]
  28. public void GradientIsInclusive_1_by_1 (GradientDirection direction)
  29. {
  30. // Define the colors of the gradient stops
  31. List<Color> stops = new()
  32. {
  33. new (255, 0), // Red
  34. new (0, 0, 255) // Blue
  35. };
  36. // Define the number of steps between each color
  37. List<int> steps = new() { 10 }; // 10 steps between Red -> Blue
  38. var g = new Gradient (stops, steps);
  39. // Note that maxRow and maxCol are inclusive so this results in 1x1 area i.e. a single cell.
  40. KeyValuePair<Point, Color> c = Assert.Single (g.BuildCoordinateColorMapping (0, 0, direction));
  41. Assert.Equal (c.Key, new (0, 0));
  42. Assert.Equal (c.Value, new (0, 0, 255));
  43. }
  44. [Fact]
  45. public void SingleColorStop ()
  46. {
  47. List<Color> stops = new() { new (255, 0) }; // Red
  48. List<int> steps = new ();
  49. var g = new Gradient (stops, steps);
  50. Assert.All (g.Spectrum, color => Assert.Equal (new (255, 0), color));
  51. }
  52. [Fact]
  53. public void LoopingGradient_CorrectColors ()
  54. {
  55. List<Color> stops = new()
  56. {
  57. new (255, 0), // Red
  58. new (0, 0, 255) // Blue
  59. };
  60. List<int> steps = new() { 10 };
  61. var g = new Gradient (stops, steps, true);
  62. Assert.Equal (new (255, 0), g.Spectrum.First ());
  63. Assert.Equal (new (255, 0), g.Spectrum.Last ());
  64. }
  65. [Fact]
  66. public void DifferentStepSizes ()
  67. {
  68. List<Color> stops = new List<Color>
  69. {
  70. new (255, 0), // Red
  71. new (0, 255), // Green
  72. new (0, 0, 255) // Blue
  73. };
  74. List<int> steps = new() { 5, 15 }; // Different steps
  75. var g = new Gradient (stops, steps);
  76. Assert.Equal (22, g.Spectrum.Count);
  77. }
  78. [Fact]
  79. public void FractionOutOfRange_ThrowsException ()
  80. {
  81. List<Color> stops = new()
  82. {
  83. new (255, 0), // Red
  84. new (0, 0, 255) // Blue
  85. };
  86. List<int> steps = new() { 10 };
  87. var g = new Gradient (stops, steps);
  88. Assert.Throws<ArgumentOutOfRangeException> (() => g.GetColorAtFraction (-0.1));
  89. Assert.Throws<ArgumentOutOfRangeException> (() => g.GetColorAtFraction (1.1));
  90. }
  91. [Fact]
  92. public void NaNFraction_ReturnsLastColor ()
  93. {
  94. List<Color> stops = new()
  95. {
  96. new (255, 0), // Red
  97. new (0, 0, 255) // Blue
  98. };
  99. List<int> steps = new() { 10 };
  100. var g = new Gradient (stops, steps);
  101. Assert.Equal (new (0, 0, 255), g.GetColorAtFraction (double.NaN));
  102. }
  103. [Fact]
  104. public void Constructor_SingleStepProvided_ReplicatesForAllPairs ()
  105. {
  106. List<Color> stops = new List<Color>
  107. {
  108. new (255, 0), // Red
  109. new (0, 255), // Green
  110. new (0, 0, 255) // Blue
  111. };
  112. List<int> singleStep = new() { 5 }; // Single step provided
  113. var gradient = new Gradient (stops, singleStep);
  114. Assert.NotNull (gradient.Spectrum);
  115. Assert.Equal (12, gradient.Spectrum.Count); // 5 steps Red -> Green + 5 steps Green -> Blue + 2 end colors
  116. }
  117. [Fact]
  118. public void Constructor_InvalidStepsLength_ThrowsArgumentException ()
  119. {
  120. List<Color> stops = new()
  121. {
  122. new (255, 0), // Red
  123. new (0, 0, 255) // Blue
  124. };
  125. List<int> invalidSteps = new() { 5, 5 }; // Invalid length (N-1 expected)
  126. Assert.Throws<ArgumentException> (() => new Gradient (stops, invalidSteps));
  127. }
  128. [Fact]
  129. public void Constructor_ValidStepsLength_DoesNotThrow ()
  130. {
  131. List<Color> stops = new List<Color>
  132. {
  133. new (255, 0), // Red
  134. new (0, 255), // Green
  135. new (0, 0, 255) // Blue
  136. };
  137. List<int> validSteps = new() { 5, 5 }; // Valid length (N-1)
  138. var gradient = new Gradient (stops, validSteps);
  139. Assert.NotNull (gradient.Spectrum);
  140. Assert.Equal (12, gradient.Spectrum.Count); // 5 steps Red -> Green + 5 steps Green -> Blue + 2 end colors
  141. }
  142. }