From 59705eeefbb5eaf0047f5007ba15da8e6fb9bf2b Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 16 Aug 2017 17:47:43 +0200 Subject: [PATCH] introducing the "doNotAppendPixelType" feature --- .../Formats/Jpg/JpegDecoderTests.cs | 3 +- .../TestUtilities/ImagingTestCaseUtility.cs | 82 +++++++++++-------- .../TestUtilities/TestImageExtensions.cs | 35 ++++---- .../Tests/TestImageExtensionsTests.cs | 13 +++ tests/Images/External | 2 +- 5 files changed, 80 insertions(+), 55 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index e14375aab..141105c48 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -37,7 +37,8 @@ namespace ImageSharp.Tests { using (Image image = provider.GetImage()) { - image.CompareToReferenceOutput(provider, VeryTolerantJpegComparer); + image.DebugSave(provider); + image.CompareToReferenceOutput(provider, VeryTolerantJpegComparer, appendPixelTypeToFileName: false); } } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 30cb1bac8..edb8bafac 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -23,100 +23,106 @@ namespace ImageSharp.Tests /// /// Name of the TPixel in the owner /// - public string PixelTypeName { get; set; } = String.Empty; + public string PixelTypeName { get; set; } = string.Empty; /// /// The name of the file which is provided by /// Or a short string describing the image in the case of a non-file based image provider. /// - public string SourceFileOrDescription { get; set; } = String.Empty; + public string SourceFileOrDescription { get; set; } = string.Empty; /// /// By default this is the name of the test class, but it's possible to change it /// - public string TestGroupName { get; set; } = String.Empty; + public string TestGroupName { get; set; } = string.Empty; /// /// The name of the test case (by default) /// - public string TestName { get; set; } = String.Empty; - + public string TestName { get; set; } = string.Empty; - private string GetTestOutputFileNameImpl(string extension, string tag) + private string GetTestOutputFileNameImpl(string extension, string details, bool appendPixelTypeToFileName) { - string fn = String.Empty; + string fn = string.Empty; - if (String.IsNullOrWhiteSpace(extension)) + if (string.IsNullOrWhiteSpace(extension)) { extension = null; } fn = Path.GetFileNameWithoutExtension(this.SourceFileOrDescription); - if (String.IsNullOrWhiteSpace(extension)) + if (string.IsNullOrWhiteSpace(extension)) { extension = Path.GetExtension(this.SourceFileOrDescription); } - if (String.IsNullOrWhiteSpace(extension)) + if (string.IsNullOrWhiteSpace(extension)) { extension = ".bmp"; } + extension = extension.ToLower(); if (extension[0] != '.') { extension = '.' + extension; } - if (fn != String.Empty) fn = '_' + fn; + if (fn != string.Empty) fn = '_' + fn; + + string pixName = ""; - string pixName = this.PixelTypeName; - if (pixName != String.Empty) + if (appendPixelTypeToFileName) { - pixName = '_' + pixName; + pixName = this.PixelTypeName; + + if (pixName != string.Empty) + { + pixName = '_' + pixName; + } } - tag = tag ?? String.Empty; - if (tag != String.Empty) + details = details ?? string.Empty; + if (details != string.Empty) { - tag = '_' + tag; + details = '_' + details; } - - return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{tag}{extension}"; + return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{details}{extension}"; } /// /// Gets the recommended file name for the output of the test /// /// The required extension - /// The settings modifying the output path + /// The settings modifying the output path + /// A boolean indicating whether to append the pixel type to output file name. /// The file test name - public string GetTestOutputFileName(string extension = null, object settings = null) + public string GetTestOutputFileName(string extension = null, object testOutputDetails = null, bool appendPixelTypeToFileName = true) { - string tag = null; - string s = settings as string; + string detailsString = null; + string s = testOutputDetails as string; if (s != null) { - tag = s; + detailsString = s; } - else if (settings != null) + else if (testOutputDetails != null) { - Type type = settings.GetType(); + Type type = testOutputDetails.GetType(); TypeInfo info = type.GetTypeInfo(); if (info.IsPrimitive || info.IsEnum || type == typeof(decimal)) { - tag = settings.ToString(); + detailsString = testOutputDetails.ToString(); } else { - IEnumerable properties = settings.GetType().GetRuntimeProperties(); + IEnumerable properties = testOutputDetails.GetType().GetRuntimeProperties(); - tag = String.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}")); + detailsString = String.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(testOutputDetails)).Select(x => $"{x.Key}-{x.Value}")); } } - return this.GetTestOutputFileNameImpl(extension, tag); + return this.GetTestOutputFileNameImpl(extension, detailsString, appendPixelTypeToFileName); } @@ -132,10 +138,11 @@ namespace ImageSharp.Tests string extension = null, IImageEncoder encoder = null, object testOutputDetails = null, - bool grayscale = false) + bool grayscale = false, + bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { - string path = this.GetTestOutputFileName(extension: extension, settings: testOutputDetails); + string path = this.GetTestOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); string extension1 = Path.GetExtension(path); encoder = encoder ?? GetImageFormatByExtension(extension1, grayscale); @@ -145,8 +152,15 @@ namespace ImageSharp.Tests } } - internal string GetReferenceOutputFileName(string extension = null, object settings = null) => - TestEnvironment.GetReferenceOutputFileName(this.GetTestOutputFileName(extension, settings)); + internal string GetReferenceOutputFileName( + string extension, + object settings, + bool appendPixelTypeToFileName) + { + return TestEnvironment.GetReferenceOutputFileName( + this.GetTestOutputFileName(extension, settings, appendPixelTypeToFileName) + ); + } internal void Init(string typeName, string methodName) { diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 74fa16ad5..35a7d17d4 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -22,13 +22,15 @@ namespace ImageSharp.Tests /// The image provider /// Details to be concatenated to the test output file, describing the parameters of the test. /// The extension - /// /// A boolean indicating whether we should save a smaller in size. + /// A boolean indicating whether we should save a smaller in size. + /// A boolean indicating whether to append the pixel type to the output file name. public static Image DebugSave( this Image image, ITestImageProvider provider, object testOutputDetails = null, string extension = "png", - bool grayscale = false) + bool grayscale = false, + bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { if (TestEnvironment.RunsOnCI) @@ -41,7 +43,8 @@ namespace ImageSharp.Tests image, extension, testOutputDetails: testOutputDetails, - grayscale: grayscale); + grayscale: grayscale, + appendPixelTypeToFileName: appendPixelTypeToFileName); return image; } @@ -52,17 +55,18 @@ namespace ImageSharp.Tests /// The pixel format /// The image /// The image provider - /// The to use /// Details to be concatenated to the test output file, describing the parameters of the test. /// 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. /// public static Image CompareToReferenceOutput( this Image image, ITestImageProvider provider, object testOutputDetails = null, string extension = "png", - bool grayscale = false) + bool grayscale = false, + bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { return CompareToReferenceOutput( @@ -71,7 +75,8 @@ namespace ImageSharp.Tests ImageComparer.Tolerant(), testOutputDetails, extension, - grayscale); + grayscale, + appendPixelTypeToFileName); } /// @@ -85,6 +90,7 @@ namespace ImageSharp.Tests /// Details to be concatenated to the test output file, describing the parameters of the test. /// 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. /// public static Image CompareToReferenceOutput( this Image image, @@ -92,21 +98,12 @@ namespace ImageSharp.Tests ImageComparer comparer, object testOutputDetails = null, string extension = "png", - bool grayscale = false) + bool grayscale = false, + bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel { - string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails); - extension = extension.ToLower(); - - if (!TestEnvironment.RunsOnCI) - { - provider.Utility.SaveTestOutputFile( - image, - extension, - testOutputDetails: testOutputDetails, - grayscale: grayscale); - } - + string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); + if (!File.Exists(referenceOutputFile)) { throw new Exception("Reference output file missing: " + referenceOutputFile); diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs index 46cd86b5e..9796c3dc6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs @@ -36,6 +36,19 @@ namespace ImageSharp.Tests } } + [Theory] + [WithSolidFilledImages(10, 10, 0, 0, 255, PixelTypes.Rgba32)] + public void CompareToReferenceOutput_DoNotAppendPixelType( + TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + image.DebugSave(provider, appendPixelTypeToFileName: false); + image.CompareToReferenceOutput(provider, appendPixelTypeToFileName: false); + } + } + [Theory] [WithSolidFilledImages(10, 10, 0, 0, 255, PixelTypes.Rgba32)] public void CompareToReferenceOutput_WhenReferenceFileMissing_Throws(TestImageProvider provider) diff --git a/tests/Images/External b/tests/Images/External index f01bd8ace..d5a93a6e2 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit f01bd8ace62e69aa9be9fcdf8bc00789d8d75a53 +Subproject commit d5a93a6e271c05a6549730acafa7092ac2dc7f03