From bfc2cb02e59e695950c9605d336d2628acbc4109 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 23 Dec 2016 22:04:45 +0100 Subject: [PATCH] Moved some of the new files around. Moved the private classes to separate files. Removed the FlagsHelper class and only take what we need. Some refactoring in some of the new classes. --- tests/ImageSharp.Tests/FileTestBase.cs | 24 +- .../ImageSharp.Tests/Formats/Jpg/JpegTests.cs | 2 - tests/ImageSharp.Tests/TestBase.cs | 32 +++ .../ImageDataAttributeBase.cs | 6 +- .../WithBlankImageAttribute.cs | 0 .../{ => Attributes}/WithFileAttribute.cs | 0 .../WithFileCollectionAttribute.cs | 1 + .../WithMemberFactoryAttribute.cs | 0 .../WithSolidFilledImagesAttribute.cs | 1 + .../TestUtilities/EnumHelper.cs | 19 ++ .../{ => Factories}/GenericFactory.cs | 7 - .../TestUtilities/Factories/ImageFactory.cs | 14 + .../TestUtilities/FlagsHelper.cs | 242 ------------------ .../ImageProviders/BlankProvider.cs | 30 +++ .../ImageProviders/FileProvider.cs | 42 +++ .../ImageProviders/LambdaProvider.cs | 29 +++ .../ImageProviders/SolidProvider.cs | 49 ++++ .../{ => ImageProviders}/TestImageProvider.cs | 104 +------- .../TestUtilities/ImagingTestCaseUtility.cs | 36 ++- .../TestUtilities/PixelTypes.cs | 3 +- .../TestUtilities/TestUtilityExtensions.cs | 13 +- .../Tests/TestImageProviderTests.cs | 7 +- .../Tests/TestUtilityExtensionsTests.cs | 3 +- 23 files changed, 251 insertions(+), 413 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestBase.cs rename tests/ImageSharp.Tests/TestUtilities/{ => Attributes}/ImageDataAttributeBase.cs (95%) rename tests/ImageSharp.Tests/TestUtilities/{ => Attributes}/WithBlankImageAttribute.cs (100%) rename tests/ImageSharp.Tests/TestUtilities/{ => Attributes}/WithFileAttribute.cs (100%) rename tests/ImageSharp.Tests/TestUtilities/{ => Attributes}/WithFileCollectionAttribute.cs (99%) rename tests/ImageSharp.Tests/TestUtilities/{ => Attributes}/WithMemberFactoryAttribute.cs (100%) rename tests/ImageSharp.Tests/TestUtilities/{ => Attributes}/WithSolidFilledImagesAttribute.cs (99%) create mode 100644 tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs rename tests/ImageSharp.Tests/TestUtilities/{ => Factories}/GenericFactory.cs (75%) create mode 100644 tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs delete mode 100644 tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs rename tests/ImageSharp.Tests/TestUtilities/{ => ImageProviders}/TestImageProvider.cs (51%) diff --git a/tests/ImageSharp.Tests/FileTestBase.cs b/tests/ImageSharp.Tests/FileTestBase.cs index e0e84dbda..0dbf13299 100644 --- a/tests/ImageSharp.Tests/FileTestBase.cs +++ b/tests/ImageSharp.Tests/FileTestBase.cs @@ -6,12 +6,11 @@ namespace ImageSharp.Tests { using System.Collections.Generic; - using System.IO; /// /// The test base class for reading and writing to files. /// - public abstract class FileTestBase + public abstract class FileTestBase : TestBase { /// /// The collection of image files to test against. @@ -45,26 +44,5 @@ namespace ImageSharp.Tests // TestFile.Create(TestImages.Gif.Cheers), // TestFile.Create(TestImages.Gif.Giphy) // Perf: Enable for local testing only }; - - // TODO: Find a better place for this - internal const string TestOutputRoot = "TestOutput/"; - - protected string CreateOutputDirectory(string path, params string[] pathParts) - { - var postFix = ""; - if (pathParts != null && pathParts.Length > 0) - { - postFix = "/" + string.Join("/", pathParts); - } - - path = TestOutputRoot + path + postFix; - - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - - return path; - } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs index 85bc18d11..11d535fb8 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs @@ -21,9 +21,7 @@ namespace ImageSharp.Tests } public static IEnumerable AllJpegFiles => TestImages.Jpeg.All; - // TODO: Turned off PixelTypes.All to be CI-friendly, what should be the practice? [Theory] - //[WithFileCollection(nameof(AllJpegFiles), PixelTypes.All)] [WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.StandardImageClass | PixelTypes.Argb)] public void OpenJpeg_SaveBmp(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable diff --git a/tests/ImageSharp.Tests/TestBase.cs b/tests/ImageSharp.Tests/TestBase.cs new file mode 100644 index 000000000..91c7adef1 --- /dev/null +++ b/tests/ImageSharp.Tests/TestBase.cs @@ -0,0 +1,32 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System.IO; + + /// + /// The test base class. + /// + public abstract class TestBase + { + protected string CreateOutputDirectory(string path, params string[] pathParts) + { + path = Path.Combine("TestOutput", path); + + if (pathParts != null && pathParts.Length > 0) + { + path = Path.Combine(path, Path.Combine(pathParts)); + } + + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + return path; + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs similarity index 95% rename from tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs rename to tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs index 18600085d..4d18da025 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs @@ -2,11 +2,11 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // + namespace ImageSharp.Tests { using System; using System.Collections.Generic; - using System.Diagnostics; using System.Linq; using System.Reflection; @@ -29,8 +29,8 @@ namespace ImageSharp.Tests public override IEnumerable GetData(MethodInfo testMethod) { - var type = testMethod.GetParameters().First().ParameterType; - if (!typeof(ITestImageFactory).IsAssignableFrom(type)) + var type = testMethod.GetParameters().First().ParameterType.GetTypeInfo(); + if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(TestImageProvider<>)) { yield return this.AdditionalParameters; } diff --git a/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs similarity index 100% rename from tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs rename to tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileAttribute.cs similarity index 100% rename from tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs rename to tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileAttribute.cs diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs similarity index 99% rename from tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs rename to tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs index bca69eea7..c75cf71bf 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs @@ -2,6 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // + namespace ImageSharp.Tests { using System; diff --git a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs similarity index 100% rename from tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs rename to tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs diff --git a/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs similarity index 99% rename from tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs rename to tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs index 46f18e08c..d225f8a77 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs @@ -2,6 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // + namespace ImageSharp.Tests { using System; diff --git a/tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs b/tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs new file mode 100644 index 000000000..7b6b26e94 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs @@ -0,0 +1,19 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System; + + public class EnumHelper + { + public static T[] GetSortedValues() + { + T[] vals = (T[])Enum.GetValues(typeof(T)); + Array.Sort(vals); + return vals; + } + } +} diff --git a/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs similarity index 75% rename from tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs rename to tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs index 92fe8e16c..8e164cb3c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs @@ -24,11 +24,4 @@ namespace ImageSharp.Tests return new Image(bytes); } } - - public class DefaultImageClassSpecificFactory : GenericFactory - { - public override Image CreateImage(byte[] bytes) => new Image(bytes); - - public override Image CreateImage(int width, int height) => new Image(width, height); - } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs new file mode 100644 index 000000000..288dbdd8c --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs @@ -0,0 +1,14 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + public class ImageFactory : GenericFactory + { + public override Image CreateImage(byte[] bytes) => new Image(bytes); + + public override Image CreateImage(int width, int height) => new Image(width, height); + } +} diff --git a/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs b/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs deleted file mode 100644 index 6c2589b82..000000000 --- a/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs +++ /dev/null @@ -1,242 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// -namespace ImageSharp.Tests -{ - using System; - using System.Collections.Generic; - using System.Text; - - /// - /// Helper class for flag manipulation, based on - /// - /// http://www.codeproject.com/KB/dotnet/enum.aspx - /// - /// - /// Must be enum type (declared using enum keyword) - public class FlagsHelper - where T : struct, IConvertible - { - private static readonly EnumConverter Converter; - - static FlagsHelper() - { - Type type = typeof(T); - string[] names = Enum.GetNames(type); - var values = (T[])Enum.GetValues(type); - - Converter = new FlagsEnumConverter(names, values); - } - - public static T[] GetSortedValues() - { - T[] vals = (T[])Enum.GetValues(typeof(T)); - Array.Sort(vals); - return vals; - } - - public static T Parse(string value, bool ignoreCase = false, bool parseNumeric = true) - { - return (T)Enum.ToObject(typeof(T), Converter.ParseInternal(value, ignoreCase, parseNumeric)); - } - - /// - /// Converts enum value to string - /// - /// Enum value converted to int - /// If is defined, the enum member name; otherwise the string representation of the . - /// If is applied, can return comma-separated list of values - public static string ToString(int value) - { - return Converter.ToStringInternal(value); - } - - /// - /// Converts enum value to string - /// - /// Enum value - /// If is defined, the enum member name; otherwise the string representation of the . - /// If is applied, can return comma-separated list of values - public static string ToString(T value) - { - return Converter.ToStringInternal(value.ToInt32(null)); - } - - public static bool TryParse(string value, bool ignoreCase, bool parseNumeric, out T result) - { - int ir; - bool b = Converter.TryParseInternal(value, ignoreCase, parseNumeric, out ir); - result = (T)Enum.ToObject(typeof(T), ir); - return b; - } - - public static bool TryParse(string value, bool ignoreCase, out T result) - { - int ir; - bool b = Converter.TryParseInternal(value, ignoreCase, true, out ir); - result = (T)Enum.ToObject(typeof(T), ir); - return b; - } - - public static bool TryParse(string value, out T result) - { - int ir; - bool b = Converter.TryParseInternal(value, false, true, out ir); - result = (T)Enum.ToObject(typeof(T), ir); - return b; - } - - class DictionaryEnumConverter : EnumConverter - { - protected readonly Dictionary Dic; - - protected DictionaryEnumConverter(string[] names, T[] values) - { - this.Dic = new Dictionary(names.Length); - for (int j = 0; j < names.Length; j++) this.Dic.Add(Convert.ToInt32(values[j], null), names[j]); - } - - public override int ParseInternal(string value, bool ignoreCase, bool parseNumber) - { - if (value == null) throw new ArgumentNullException(nameof(value)); - if (value.Length == 0) throw new ArgumentException("Value is empty", nameof(value)); - char f = value[0]; - if (parseNumber && (char.IsDigit(f) || f == '+' || f == '-')) return int.Parse(value); - StringComparison stringComparison = ignoreCase - ? StringComparison.OrdinalIgnoreCase - : StringComparison.Ordinal; - foreach (KeyValuePair pair in this.Dic) - { - if (pair.Value.Equals(value, stringComparison)) return pair.Key; - } - - throw new ArgumentException("Enum value wasn't found", nameof(value)); - } - - public override string ToStringInternal(int value) - { - string n; - return this.Dic.TryGetValue(value, out n) ? n : value.ToString(); - } - - public override bool TryParseInternal(string value, bool ignoreCase, bool parseNumber, out int result) - { - result = 0; - if (string.IsNullOrEmpty(value)) return false; - char f = value[0]; - if (parseNumber && (char.IsDigit(f) || f == '+' || f == '-')) - { - int i; - if (int.TryParse(value, out i)) - { - result = i; - return true; - } - - return false; - } - - StringComparison stringComparison = ignoreCase - ? StringComparison.OrdinalIgnoreCase - : StringComparison.Ordinal; - foreach (KeyValuePair pair in this.Dic) - { - if (pair.Value.Equals(value, stringComparison)) - { - result = pair.Key; - return true; - } - } - - return false; - } - } - - abstract class EnumConverter - { - public abstract int ParseInternal(string value, bool ignoreCase, bool parseNumber); - - public abstract string ToStringInternal(int value); - - public abstract bool TryParseInternal(string value, bool ignoreCase, bool parseNumber, out int result); - } - - class FlagsEnumConverter : DictionaryEnumConverter - { - private static readonly string[] Seps = new[] { "," }; - - private readonly uint[] values; - - public FlagsEnumConverter(string[] names, T[] values) - : base(names, values) - { - this.values = new uint[values.Length]; - for (int i = 0; i < values.Length; i++) this.values[i] = values[i].ToUInt32(null); - } - - public override int ParseInternal(string value, bool ignoreCase, bool parseNumber) - { - string[] parts = value.Split(Seps, StringSplitOptions.RemoveEmptyEntries); - if (parts.Length == 1) return base.ParseInternal(value, ignoreCase, parseNumber); - int val = 0; - for (int i = 0; i < parts.Length; i++) - { - string part = parts[i]; - int t = base.ParseInternal(part.Trim(), ignoreCase, parseNumber); - val |= t; - } - - return val; - } - - public override string ToStringInternal(int value) - { - string n; - if (this.Dic.TryGetValue(value, out n)) return n; - var sb = new StringBuilder(); - const string sep = ", "; - uint uval; - unchecked - { - uval = (uint)value; - - for (int i = this.values.Length - 1; i >= 0; i--) - { - uint v = this.values[i]; - if (v == 0) continue; - if ((v & uval) == v) - { - uval &= ~v; - sb.Insert(0, sep).Insert(0, this.Dic[(int)v]); - } - } - } - - return uval == 0 && sb.Length > sep.Length ? sb.ToString(0, sb.Length - sep.Length) : value.ToString(); - } - - public override bool TryParseInternal(string value, bool ignoreCase, bool parseNumber, out int result) - { - string[] parts = value.Split(Seps, StringSplitOptions.RemoveEmptyEntries); - if (parts.Length == 1) return base.TryParseInternal(value, ignoreCase, parseNumber, out result); - int val = 0; - for (int i = 0; i < parts.Length; i++) - { - string part = parts[i]; - int t; - if (!base.TryParseInternal(part.Trim(), ignoreCase, parseNumber, out t)) - { - result = 0; - return false; - } - - val |= t; - } - - result = val; - return true; - } - } - } -} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs new file mode 100644 index 000000000..c14f56588 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs @@ -0,0 +1,30 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System; + + public abstract partial class TestImageProvider + where TColor : struct, IPackedPixel, IEquatable + { + private class BlankProvider : TestImageProvider + { + public BlankProvider(int width, int height) + { + this.Width = width; + this.Height = height; + } + + public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; + + protected int Height { get; } + + protected int Width { get; } + + public override Image GetImage() => this.Factory.CreateImage(this.Width, this.Height); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs new file mode 100644 index 000000000..edd6d03a1 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -0,0 +1,42 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System; + using System.Collections.Concurrent; + + public abstract partial class TestImageProvider + where TColor : struct, IPackedPixel, IEquatable + { + private class FileProvider : TestImageProvider + { + private static ConcurrentDictionary> cache = + new ConcurrentDictionary>(); + + private string filePath; + + public FileProvider(string filePath) + { + this.filePath = filePath; + } + + public override string SourceFileOrDescription => this.filePath; + + public override Image GetImage() + { + var cachedImage = cache.GetOrAdd( + this.filePath, + fn => + { + var testFile = TestFile.Create(this.filePath); + return this.Factory.CreateImage(testFile.Bytes); + }); + + return new Image(cachedImage); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs new file mode 100644 index 000000000..c8bc705f1 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs @@ -0,0 +1,29 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System; + + /// + /// Provides instances for parametric unit tests. + /// + /// The pixel format of the image + public abstract partial class TestImageProvider + where TColor : struct, IPackedPixel, IEquatable + { + private class LambdaProvider : TestImageProvider + { + private readonly Func, Image> creator; + + public LambdaProvider(Func, Image> creator) + { + this.creator = creator; + } + + public override Image GetImage() => this.creator(this.Factory); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs new file mode 100644 index 000000000..f7d3a0bf8 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs @@ -0,0 +1,49 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System; + + /// + /// Provides instances for parametric unit tests. + /// + /// The pixel format of the image + public abstract partial class TestImageProvider + where TColor : struct, IPackedPixel, IEquatable + { + private class SolidProvider : BlankProvider + { + private readonly byte a; + + private readonly byte b; + + private readonly byte g; + + private readonly byte r; + + public SolidProvider(int width, int height, byte r, byte g, byte b, byte a) + : base(width, height) + { + this.r = r; + this.g = g; + this.b = b; + this.a = a; + } + + public override string SourceFileOrDescription + => $"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})"; + + public override Image GetImage() + { + var image = base.GetImage(); + TColor color = default(TColor); + color.PackFromBytes(this.r, this.g, this.b, this.a); + + return image.Fill(color); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs similarity index 51% rename from tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs rename to tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs index 453e03af2..8a8db3ee8 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs @@ -6,14 +6,13 @@ namespace ImageSharp.Tests { using System; - using System.Collections.Concurrent; using System.Reflection; /// /// Provides instances for parametric unit tests. /// /// The pixel format of the image - public abstract class TestImageProvider : ITestImageFactory + public abstract partial class TestImageProvider where TColor : struct, IPackedPixel, IEquatable { public PixelTypes PixelType { get; private set; } = typeof(TColor).GetPixelType(); @@ -65,7 +64,7 @@ namespace ImageSharp.Tests /// Returns an instance to the test case with the necessary traits. /// public abstract Image GetImage(); - + protected TestImageProvider Init(MethodInfo testMethod, PixelTypes pixelTypeOverride) { if (pixelTypeOverride != PixelTypes.Undefined) @@ -75,10 +74,9 @@ namespace ImageSharp.Tests if (pixelTypeOverride == PixelTypes.StandardImageClass) { - this.Factory = new DefaultImageClassSpecificFactory() as GenericFactory; + this.Factory = new ImageFactory() as GenericFactory; } - this.Utility = new ImagingTestCaseUtility() { SourceFileOrDescription = this.SourceFileOrDescription, @@ -92,101 +90,5 @@ namespace ImageSharp.Tests return this; } - - private class BlankProvider : TestImageProvider - { - public BlankProvider(int width, int height) - { - this.Width = width; - this.Height = height; - } - - public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; - - protected int Height { get; } - - protected int Width { get; } - - public override Image GetImage() => this.Factory.CreateImage(this.Width, this.Height); - } - - private class FileProvider : TestImageProvider - { - private static ConcurrentDictionary> cache = - new ConcurrentDictionary>(); - - private string filePath; - - public FileProvider(string filePath) - { - this.filePath = filePath; - } - - public override string SourceFileOrDescription => this.filePath; - - public override Image GetImage() - { - var cachedImage = cache.GetOrAdd( - this.filePath, - fn => - { - var testFile = TestFile.Create(this.filePath); - return this.Factory.CreateImage(testFile.Bytes); - }); - - return new Image(cachedImage); - } - } - - private class LambdaProvider : TestImageProvider - { - private readonly Func, Image> creator; - - public LambdaProvider(Func, Image> creator) - { - this.creator = creator; - } - - public override Image GetImage() => this.creator(this.Factory); - } - - private class SolidProvider : BlankProvider - { - private readonly byte a; - - private readonly byte b; - - private readonly byte g; - - private readonly byte r; - - public SolidProvider(int width, int height, byte r, byte g, byte b, byte a) - : base(width, height) - { - this.r = r; - this.g = g; - this.b = b; - this.a = a; - } - - public override string SourceFileOrDescription - => $"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})"; - - public override Image GetImage() - { - var image = base.GetImage(); - TColor color = default(TColor); - color.PackFromBytes(this.r, this.g, this.b, this.a); - - return image.Fill(color); - } - } - } - - /// - /// Marker - /// - public interface ITestImageFactory - { } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index e2b885034..ad8cfe157 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -2,6 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // + namespace ImageSharp.Tests { using System; @@ -15,7 +16,7 @@ namespace ImageSharp.Tests /// Utility class to provide information about the test image & the test case for the test code, /// and help managing IO. /// - public class ImagingTestCaseUtility + public class ImagingTestCaseUtility : TestBase { /// /// Name of the TColor in the owner @@ -38,20 +39,6 @@ namespace ImageSharp.Tests /// public string TestName { get; set; } = string.Empty; - /// - /// Root directory for output images - /// - public string TestOutputRoot { get; set; } = FileTestBase.TestOutputRoot; - - public string GetTestOutputDir() - { - string testGroupName = Path.GetFileNameWithoutExtension(this.TestGroupName); - - string dir = $@"{this.TestOutputRoot}{testGroupName}"; - Directory.CreateDirectory(dir); - return dir; - } - /// /// Gets the recommended file name for the output of the test /// @@ -81,12 +68,6 @@ namespace ImageSharp.Tests return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{extension}"; } - private static IImageFormat GetImageFormatByExtension(string extension) - { - extension = extension.ToLower(); - return Bootstrapper.ImageFormats.First(f => f.SupportedExtensions.Contains(extension)); - } - /// /// Encodes image by the format matching the required extension, than saves it to the recommended output file. /// @@ -111,5 +92,18 @@ namespace ImageSharp.Tests this.TestGroupName = method.DeclaringType.Name; this.TestName = method.Name; } + + private static IImageFormat GetImageFormatByExtension(string extension) + { + extension = extension.ToLower(); + return Bootstrapper.ImageFormats.First(f => f.SupportedExtensions.Contains(extension)); + } + + private string GetTestOutputDir() + { + string testGroupName = Path.GetFileNameWithoutExtension(this.TestGroupName); + + return CreateOutputDirectory(testGroupName); + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs index dd1460678..547efaa6f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -2,12 +2,13 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // + namespace ImageSharp.Tests { using System; /// - /// Flags that are mapped to PackedPixel types. + /// Flags that are mapped to PackedPixel types. /// They trigger the desired parametrization for . /// [Flags] diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs index a4e0a959a..bb517dda9 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -22,8 +22,7 @@ namespace ImageSharp.Tests private static readonly Dictionary PixelTypes2ClrTypes = new Dictionary(); - private static readonly PixelTypes[] AllConcretePixelTypes = FlagsHelper - .GetSortedValues() + private static readonly PixelTypes[] AllConcretePixelTypes = EnumHelper.GetSortedValues() .Except(new [] {PixelTypes.Undefined, PixelTypes.All }) .ToArray(); @@ -33,13 +32,15 @@ namespace ImageSharp.Tests nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Color).Name.Length - 1); foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.StandardImageClass)) { - string typeName = $"{nameSpace}.{FlagsHelper.ToString(pt)}"; + string typeName = $"{nameSpace}.{pt.ToString()}"; var t = ImageSharpAssembly.GetType(typeName); - if (t != null) + if (t == null) { - PixelTypes2ClrTypes[pt] = t; - ClrTypes2PixelTypes[t] = pt; + throw new InvalidOperationException($"Could not find: {typeName}"); } + + PixelTypes2ClrTypes[pt] = t; + ClrTypes2PixelTypes[t] = pt; } PixelTypes2ClrTypes[PixelTypes.StandardImageClass] = typeof(Color); } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 974fe35cc..28e5ad2c2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -3,8 +3,6 @@ // Licensed under the Apache License, Version 2.0. // -// ReSharper disable InconsistentNaming - namespace ImageSharp.Tests { using System; @@ -68,7 +66,6 @@ namespace ImageSharp.Tests Assert.IsType(img); } - // TODO: @dlemstra this works only with constant strings! [Theory] [WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)] [WithFile(TestImages.Bmp.F, PixelTypes.All, 88)] @@ -151,7 +148,7 @@ namespace ImageSharp.Tests } - public static readonly TheoryData BasicData = new TheoryData() + public static readonly TheoryData BasicData = new TheoryData() { TestImageProvider.Blank(10, 20), TestImageProvider.Blank( @@ -169,7 +166,7 @@ namespace ImageSharp.Tests Assert.True(img.Width * img.Height > 0); } - public static readonly TheoryData FileData = new TheoryData() + public static readonly TheoryData FileData = new TheoryData() { TestImageProvider.File( TestImages.Bmp.Car), diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 371934127..93d6eab16 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -3,7 +3,6 @@ // Licensed under the Apache License, Version 2.0. // -// ReSharper disable InconsistentNaming namespace ImageSharp.Tests { using System; @@ -139,7 +138,7 @@ namespace ImageSharp.Tests { var expanded = PixelTypes.All.ExpandAllTypes().ToArray(); - Assert.True(expanded.Length >= FlagsHelper.GetSortedValues().Length - 2); + Assert.True(expanded.Length >= EnumHelper.GetSortedValues().Length - 2); AssertContainsPixelType(PixelTypes.Color, expanded); AssertContainsPixelType(PixelTypes.StandardImageClass, expanded); }