GradientTests.cs 5.4 KB

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