From a167366b147a46be441684b767a90c14a85fa36b Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 7 Jul 2024 20:13:56 +0100 Subject: [PATCH] Tests, xmldoc and guards --- Terminal.Gui/Drawing/Gradient.cs | 29 ++++++++++ Terminal.Gui/Drawing/GradientFill.cs | 2 +- .../New => Drawing}/GradientFillTests.cs | 11 ++-- UnitTests/Drawing/GradientTests.cs | 53 +++++++++++++++++++ 4 files changed, 88 insertions(+), 7 deletions(-) rename UnitTests/{TextEffects/New => Drawing}/GradientFillTests.cs (94%) create mode 100644 UnitTests/Drawing/GradientTests.cs diff --git a/Terminal.Gui/Drawing/Gradient.cs b/Terminal.Gui/Drawing/Gradient.cs index b0cdf072f..7aea37b90 100644 --- a/Terminal.Gui/Drawing/Gradient.cs +++ b/Terminal.Gui/Drawing/Gradient.cs @@ -58,10 +58,26 @@ public class Gradient Spectrum = GenerateGradient (_steps); } + /// + /// Returns the color to use at the given part of the spectrum + /// + /// Proportion of the way through the spectrum, must be between + /// 0 and 1 (inclusive). Returns the last color if is + /// . + /// + /// public Color GetColorAtFraction (double fraction) { + if (double.IsNaN (fraction)) + { + return Spectrum.Last (); + } + if (fraction < 0 || fraction > 1) + { throw new ArgumentOutOfRangeException (nameof (fraction), "Fraction must be between 0 and 1."); + } + int index = (int)(fraction * (Spectrum.Count - 1)); return Spectrum [index]; } @@ -106,6 +122,19 @@ public class Gradient } } + /// + /// + /// Creates a mapping starting at 0,0 and going to and + /// (inclusively) using the supplied . + /// + /// + /// Note that this method is inclusive i.e. passing 1/1 results in 4 mapped coordinates. + /// + /// + /// + /// + /// + /// public Dictionary BuildCoordinateColorMapping (int maxRow, int maxColumn, GradientDirection direction) { var gradientMapping = new Dictionary (); diff --git a/Terminal.Gui/Drawing/GradientFill.cs b/Terminal.Gui/Drawing/GradientFill.cs index 393a183f4..31339ebe0 100644 --- a/Terminal.Gui/Drawing/GradientFill.cs +++ b/Terminal.Gui/Drawing/GradientFill.cs @@ -18,7 +18,7 @@ public class GradientFill : IFill /// public GradientFill (Rectangle area, Gradient gradient, GradientDirection direction) { - _map = gradient.BuildCoordinateColorMapping (area.Height, area.Width, direction); + _map = gradient.BuildCoordinateColorMapping (area.Height-1, area.Width-1, direction); } /// diff --git a/UnitTests/TextEffects/New/GradientFillTests.cs b/UnitTests/Drawing/GradientFillTests.cs similarity index 94% rename from UnitTests/TextEffects/New/GradientFillTests.cs rename to UnitTests/Drawing/GradientFillTests.cs index 8349edd6d..fd6822539 100644 --- a/UnitTests/TextEffects/New/GradientFillTests.cs +++ b/UnitTests/Drawing/GradientFillTests.cs @@ -1,6 +1,5 @@ -using Terminal.Gui.Drawing; - -namespace Terminal.Gui.TextEffects.Tests; + +namespace Terminal.Gui.DrawingTests; public class GradientFillTests { @@ -29,9 +28,9 @@ public class GradientFillTests // Test the corners var topLeft = new Point (0, 0); - var topRight = new Point (area.Width, 0); - var bottomLeft = new Point (0, area.Height ); - var bottomRight = new Point (area.Width, area.Height); + var topRight = new Point (area.Width - 1, 0); + var bottomLeft = new Point (0, area.Height - 1); + var bottomRight = new Point (area.Width - 1, area.Height - 1); var topLeftColor = gradientFill.GetColor (topLeft); var topRightColor = gradientFill.GetColor (topRight); diff --git a/UnitTests/Drawing/GradientTests.cs b/UnitTests/Drawing/GradientTests.cs new file mode 100644 index 000000000..e9e0194ea --- /dev/null +++ b/UnitTests/Drawing/GradientTests.cs @@ -0,0 +1,53 @@ + +namespace Terminal.Gui.DrawingTests; + +public class GradientTests +{ + // Static method to provide all enum values + public static IEnumerable GradientDirectionValues () + { + return typeof (GradientDirection).GetEnumValues () + .Cast () + .Select (direction => new object [] { direction }); + } + + [Theory] + [MemberData (nameof (GradientDirectionValues))] + public void GradientIsInclusive_2_by_2 (GradientDirection direction) + { + // Define the colors of the gradient stops + var stops = new List + { + new Color(255, 0, 0), // Red + new Color(0, 0, 255) // Blue + }; + + // Define the number of steps between each color + var steps = new List { 10 }; // 10 steps between Red -> Blue + + var g = new Gradient (stops, steps, loop: false); + Assert.Equal (4, g.BuildCoordinateColorMapping (1, 1, direction).Count); + } + + [Theory] + [MemberData (nameof (GradientDirectionValues))] + public void GradientIsInclusive_1_by_1 (GradientDirection direction) + { + // Define the colors of the gradient stops + var stops = new List + { + new Color(255, 0, 0), // Red + new Color(0, 0, 255) // Blue + }; + + // Define the number of steps between each color + var steps = new List { 10 }; // 10 steps between Red -> Blue + + var g = new Gradient (stops, steps, loop: false); + + // Note that + var c = Assert.Single (g.BuildCoordinateColorMapping (0, 0, direction)); + Assert.Equal (c.Key, new Point(0,0)); + Assert.Equal (c.Value, new Color (0, 0, 255)); + } +}