From 4601ecc7f908a8aad3fb8ae70912c8365e82bece Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 13 May 2018 11:42:37 +0200 Subject: [PATCH] add tolerance to comparison in tests --- .../Drawing/FillEllipticGradientBrushTest.cs | 6 ++ .../Drawing/FillLinearGradientBrushTests.cs | 8 ++ .../Drawing/FillRadialGradientBrushTests.cs | 5 ++ .../TestUtilities/TestImageExtensions.cs | 81 +++++++++++++++---- 4 files changed, 85 insertions(+), 15 deletions(-) diff --git a/tests/ImageSharp.Tests/Drawing/FillEllipticGradientBrushTest.cs b/tests/ImageSharp.Tests/Drawing/FillEllipticGradientBrushTest.cs index 5801e7276..7c9fa2088 100644 --- a/tests/ImageSharp.Tests/Drawing/FillEllipticGradientBrushTest.cs +++ b/tests/ImageSharp.Tests/Drawing/FillEllipticGradientBrushTest.cs @@ -12,9 +12,13 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + [GroupOutput("Drawing/GradientBrushes")] public class FillEllipticGradientBrushTests { + public static ImageComparer TolerantComparer = ImageComparer.TolerantPercentage(0.01f); + [Theory] [WithBlankImages(10, 10, PixelTypes.Rgba32)] public void WithEqualColorsReturnsUnicolorImage( @@ -61,6 +65,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing TPixel black = NamedColors.Black; provider.VerifyOperation( + TolerantComparer, image => { var unicolorLinearGradientBrush = new EllipticGradientBrush( @@ -108,6 +113,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing FormattableString variant = $"{ratio:F2}_AT_{rotationInDegree:00}deg"; provider.VerifyOperation( + TolerantComparer, image => { TPixel yellow = NamedColors.Yellow; diff --git a/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs b/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs index a9f08eb44..78b7d11e0 100644 --- a/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs @@ -17,10 +17,13 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { using SixLabors.ImageSharp.Advanced; + using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; [GroupOutput("Drawing/GradientBrushes")] public class FillLinearGradientBrushTests { + public static ImageComparer TolerantComparer = ImageComparer.TolerantPercentage(0.01f); + [Theory] [WithBlankImages(10, 10, PixelTypes.Rgba32)] public void WithEqualColorsReturnsUnicolorImage(TestImageProvider provider) @@ -54,6 +57,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing where TPixel : struct, IPixel { provider.VerifyOperation( + TolerantComparer, image => { var unicolorLinearGradientBrush = new LinearGradientBrush( @@ -74,6 +78,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing where TPixel : struct, IPixel { provider.VerifyOperation( + TolerantComparer, image => { TPixel red = NamedColors.Red; @@ -103,6 +108,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing where TPixel : struct, IPixel { provider.VerifyOperation( + TolerantComparer, image => { TPixel red = NamedColors.Red; @@ -175,6 +181,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing color => Assert.True(color.Equals(black) || color.Equals(white))); image.CompareToReferenceOutput( + TolerantComparer, provider, variant, appendPixelTypeToFileName: false, @@ -286,6 +293,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing } image.CompareToReferenceOutput( + TolerantComparer, provider, startCorner, appendPixelTypeToFileName: false, diff --git a/tests/ImageSharp.Tests/Drawing/FillRadialGradientBrushTests.cs b/tests/ImageSharp.Tests/Drawing/FillRadialGradientBrushTests.cs index 98004326e..eafbf3df1 100644 --- a/tests/ImageSharp.Tests/Drawing/FillRadialGradientBrushTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillRadialGradientBrushTests.cs @@ -9,9 +9,13 @@ namespace SixLabors.ImageSharp.Tests.Drawing { using System; + using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + [GroupOutput("Drawing/GradientBrushes")] public class FillRadialGradientBrushTests { + public static ImageComparer TolerantComparer = ImageComparer.TolerantPercentage(0.01f); + [Theory] [WithBlankImages(200, 200, PixelTypes.Rgba32)] public void WithEqualColorsReturnsUnicolorImage( @@ -52,6 +56,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing where TPixel : struct, IPixel { provider.VerifyOperation( + TolerantComparer, image => { var brush = new RadialGradientBrush( diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index f4c7869e2..b8c0489c8 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -544,6 +544,7 @@ namespace SixLabors.ImageSharp.Tests /// internal static void VerifyOperation( this TestImageProvider provider, + ImageComparer comparer, Action> operation, FormattableString testOutputDetails, bool appendPixelTypeToFileName = true, @@ -560,13 +561,58 @@ namespace SixLabors.ImageSharp.Tests appendPixelTypeToFileName: appendPixelTypeToFileName, appendSourceFileOrDescription: appendSourceFileOrDescription); - image.CompareToReferenceOutput(provider, + image.CompareToReferenceOutput(comparer, + provider, testOutputDetails, appendPixelTypeToFileName: appendPixelTypeToFileName, appendSourceFileOrDescription: appendSourceFileOrDescription); } } + /// + /// Utility method for doing the following in one step: + /// 1. Executing an operation (taken as a delegate) + /// 2. Executing DebugSave() + /// 3. Executing CopareToReferenceOutput() + /// + internal static void VerifyOperation( + this TestImageProvider provider, + Action> operation, + FormattableString testOutputDetails, + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) + where TPixel : struct, IPixel + { + provider.VerifyOperation( + ImageComparer.Tolerant(), + operation, + testOutputDetails, + appendPixelTypeToFileName, + appendSourceFileOrDescription); + } + + /// + /// Utility method for doing the following in one step: + /// 1. Executing an operation (taken as a delegate) + /// 2. Executing DebugSave() + /// 3. Executing CopareToReferenceOutput() + /// + internal static void VerifyOperation( + this TestImageProvider provider, + ImageComparer comparer, + Action> operation, + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) + where TPixel : struct, IPixel + { + provider.VerifyOperation( + comparer, + operation, + $"", + appendPixelTypeToFileName, + appendSourceFileOrDescription); + } + /// /// Utility method for doing the following in one step: /// 1. Executing an operation (taken as a delegate) @@ -582,23 +628,28 @@ namespace SixLabors.ImageSharp.Tests { provider.VerifyOperation(operation, $"", appendPixelTypeToFileName, appendSourceFileOrDescription); } - + /// - /// Loads the expected image with a reference decoder + compares it to . - /// Also performs a debug save using . - /// - internal static void VerifyEncoder(this Image image, - ITestImageProvider provider, - string extension, - object testOutputDetails, - IImageEncoder encoder, - ImageComparer customComparer = null, - bool appendPixelTypeToFileName = true, - string referenceImageExtension = null - ) + /// Loads the expected image with a reference decoder + compares it to . + /// Also performs a debug save using . + /// + internal static void VerifyEncoder( + this Image image, + ITestImageProvider provider, + string extension, + object testOutputDetails, + IImageEncoder encoder, + ImageComparer customComparer = null, + bool appendPixelTypeToFileName = true, + string referenceImageExtension = null) where TPixel : struct, IPixel { - string actualOutputFile = provider.Utility.SaveTestOutputFile(image, extension, encoder, testOutputDetails, appendPixelTypeToFileName); + string actualOutputFile = provider.Utility.SaveTestOutputFile( + image, + extension, + encoder, + testOutputDetails, + appendPixelTypeToFileName); IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile); using (var actualImage = Image.Load(actualOutputFile, referenceDecoder))