From 4cb2aa412e27377050eb5463541e6ea3ca2e4c0f Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 10 May 2018 01:06:43 +0200 Subject: [PATCH 1/2] invariant formatting for TestImageProvider.SourceFileOrDescription --- .../TestUtilities/ImageProviders/BlankProvider.cs | 3 ++- .../TestUtilities/ImageProviders/SolidProvider.cs | 2 +- .../TestUtilities/ImageProviders/TestPatternProvider.cs | 2 +- .../TestUtilities/ImagingTestCaseUtility.cs | 6 ++---- tests/ImageSharp.Tests/TestUtilities/TestUtils.cs | 4 +++- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs index 78821aac1..7821d0b51 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs @@ -19,13 +19,14 @@ namespace SixLabors.ImageSharp.Tests this.Width = width; this.Height = height; } + public BlankProvider() { this.Width = 100; this.Height = 100; } - public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; + public override string SourceFileOrDescription => TestUtils.AsInvariantString($"Blank{this.Width}x{this.Height}"); protected int Height { get; private set; } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs index df5b424a2..70e762585 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs @@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Tests } public override string SourceFileOrDescription - => $"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})"; + => TestUtils.AsInvariantString($"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})"); public override Image GetImage() { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs index 0b25991ff..f04f891f8 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs @@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp.Tests { } - public override string SourceFileOrDescription => $"TestPattern{this.Width}x{this.Height}"; + public override string SourceFileOrDescription => TestUtils.AsInvariantString($"TestPattern{this.Width}x{this.Height}"); public override Image GetImage() { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 340fc600a..02f8ddd7e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -95,8 +95,6 @@ namespace SixLabors.ImageSharp.Tests return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{details}{extension}"; } - private static string Inv(FormattableString formattable) => System.FormattableString.Invariant(formattable); - /// /// Gets the recommended file name for the output of the test /// @@ -123,7 +121,7 @@ namespace SixLabors.ImageSharp.Tests TypeInfo info = type.GetTypeInfo(); if (info.IsPrimitive || info.IsEnum || type == typeof(decimal)) { - detailsString = Inv($"{testOutputDetails}"); + detailsString = TestUtils.AsInvariantString($"{testOutputDetails}"); } else { @@ -132,7 +130,7 @@ namespace SixLabors.ImageSharp.Tests detailsString = string.Join( "_", properties.ToDictionary(x => x.Name, x => x.GetValue(testOutputDetails)) - .Select(x => Inv($"{x.Key}-{x.Value}")) + .Select(x => TestUtils.AsInvariantString($"{x.Key}-{x.Value}")) ); } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs index 85729acd3..f71793ff2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs @@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp.Tests public static string ToCsv(this IEnumerable items, string separator = ",") { - return string.Join(separator, items.Select(o => string.Format(CultureInfo.InvariantCulture, "{0}", o))); + return String.Join(separator, items.Select(o => String.Format(CultureInfo.InvariantCulture, "{0}", o))); } public static Type GetClrType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType]; @@ -227,5 +227,7 @@ namespace SixLabors.ImageSharp.Tests image.DebugSave(provider, testOutputDetails); } } + + public static string AsInvariantString(this FormattableString formattable) => System.FormattableString.Invariant(formattable); } } \ No newline at end of file From d2e74e11453f966a36a8c4101d5ab069e044999c Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 10 May 2018 01:44:23 +0200 Subject: [PATCH 2/2] use FormattableString for .DebugSave(...) and .CompareToReferenceOutput(...) whenever possible --- .../Transforms/AffineTransformTests.cs | 4 +- .../TestUtilities/ImagingTestCaseUtility.cs | 6 +- .../TestUtilities/TestImageExtensions.cs | 65 ++++++++++++++++++- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs index 9380d4e18..3232c848e 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs @@ -118,7 +118,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); - string testOutputDetails = $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"; + FormattableString testOutputDetails = $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"; image.DebugSave(provider, testOutputDetails); image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails); } @@ -135,7 +135,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms image.Mutate(i => i.Transform(m, KnownResamplers.Bicubic)); - string testOutputDetails = $"R({angleDeg})_S({s})"; + FormattableString testOutputDetails = $"R({angleDeg})_S({s})"; image.DebugSave(provider, testOutputDetails); image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails); } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 02f8ddd7e..2177551af 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -111,7 +111,11 @@ namespace SixLabors.ImageSharp.Tests { string detailsString = null; - if (testOutputDetails is string s) + if (testOutputDetails is FormattableString fs) + { + detailsString = fs.AsInvariantString(); + } + else if (testOutputDetails is string s) { detailsString = s; } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 8955eeb63..99c696f29 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -52,6 +52,23 @@ namespace SixLabors.ImageSharp.Tests }); } + public static Image DebugSave( + this Image image, + ITestImageProvider provider, + FormattableString testOutputDetails, + string extension = "png", + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) + where TPixel : struct, IPixel + { + return image.DebugSave( + provider, + (object)testOutputDetails, + extension, + appendPixelTypeToFileName, + appendSourceFileOrDescription); + } + /// /// Saves the image only when not running in the CI server. /// @@ -86,6 +103,17 @@ namespace SixLabors.ImageSharp.Tests return image; } + public static Image DebugSave( + this Image image, + ITestImageProvider provider, + IImageEncoder encoder, + FormattableString testOutputDetails, + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + return image.DebugSave(provider, encoder, (object)testOutputDetails, appendPixelTypeToFileName); + } + /// /// Saves the image only when not running in the CI server. /// @@ -139,6 +167,23 @@ namespace SixLabors.ImageSharp.Tests return image; } + public static Image CompareToReferenceOutput( + this Image image, + ITestImageProvider provider, + FormattableString testOutputDetails, + string extension = "png", + bool grayscale = false, + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + return image.CompareToReferenceOutput( + provider, + (object)testOutputDetails, + extension, + grayscale, + appendPixelTypeToFileName); + } + /// /// Compares the image against the expected Reference output, throws an exception if the images are not similar enough. /// The output file should be named identically to the output produced by . @@ -150,7 +195,6 @@ namespace SixLabors.ImageSharp.Tests /// The extension /// A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size. /// A boolean indicating whether to append the pixel type to the output file name. - /// A custom for the verification /// public static Image CompareToReferenceOutput( this Image image, @@ -171,6 +215,25 @@ namespace SixLabors.ImageSharp.Tests appendPixelTypeToFileName); } + public static Image CompareToReferenceOutput( + this Image image, + ImageComparer comparer, + ITestImageProvider provider, + FormattableString testOutputDetails, + string extension = "png", + bool grayscale = false, + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + return image.CompareToReferenceOutput( + comparer, + provider, + (object)testOutputDetails, + extension, + grayscale, + appendPixelTypeToFileName); + } + /// /// Compares the image against the expected Reference output, throws an exception if the images are not similar enough. /// The output file should be named identically to the output produced by .