From 09f2ad0b14409960a54f614773f03625311e4391 Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Fri, 23 Dec 2016 02:41:22 +0100 Subject: [PATCH 1/6] re-added & refactored TestUtilities --- tests/ImageSharp.Tests/FileTestBase.cs | 5 +- tests/ImageSharp.Tests/TestFile.cs | 22 +- .../TestUtilities/FlagsHelper.cs | 242 ++++++++++++++++++ .../TestUtilities/ImageDataAttributeBase.cs | 73 ++++++ .../TestUtilities/ImagingTestCaseUtility.cs | 115 +++++++++ .../TestUtilities/PixelTypes.cs | 57 +++++ .../TestUtilities/TestImageFactory.cs | 165 ++++++++++++ .../TestUtilities/TestImageFactoryTests.cs | 167 ++++++++++++ .../TestUtilities/TestUtilityExtensions.cs | 119 +++++++++ .../TestUtilityExtensionsTests.cs | 123 +++++++++ .../TestUtilities/WithBlankImageAttribute.cs | 38 +++ .../TestUtilities/WithFileAttribute.cs | 36 +++ .../WithFileCollectionAttribute.cs | 86 +++++++ .../WithMemberFactoryAttribute.cs | 48 ++++ .../WithSolidFilledImagesAttribute.cs | 93 +++++++ 15 files changed, 1384 insertions(+), 5 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/TestImageFactory.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/TestImageFactoryTests.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensionsTests.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs create mode 100644 tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs diff --git a/tests/ImageSharp.Tests/FileTestBase.cs b/tests/ImageSharp.Tests/FileTestBase.cs index 38a499a02..e0e84dbda 100644 --- a/tests/ImageSharp.Tests/FileTestBase.cs +++ b/tests/ImageSharp.Tests/FileTestBase.cs @@ -46,6 +46,9 @@ namespace ImageSharp.Tests // 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 = ""; @@ -54,7 +57,7 @@ namespace ImageSharp.Tests postFix = "/" + string.Join("/", pathParts); } - path = "TestOutput/" + path + postFix; + path = TestOutputRoot + path + postFix; if (!Directory.Exists(path)) { diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index d4d4a0a89..5325f7db5 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -4,6 +4,7 @@ // namespace ImageSharp.Tests { + using System; using System.Collections.Concurrent; using System.IO; @@ -26,19 +27,28 @@ namespace ImageSharp.Tests private readonly Image image; private readonly string file; - private TestFile(string file) + private TestFile(string file, bool decodeImage) { this.file = file; this.Bytes = File.ReadAllBytes(file); - this.image = new Image(this.Bytes); + if (decodeImage) + { + this.image = new Image(this.Bytes); + } + } - public static TestFile Create(string file) + public static TestFile Create(string file) => CreateImpl(file, true); + + // No need to decode the image when used by TestImageProvider! + internal static TestFile CreateWithoutImage(string file) => CreateImpl(file, false); + + private static TestFile CreateImpl(string file, bool decodeImage) { return cache.GetOrAdd(file, (string fileName) => { - return new TestFile(FormatsDirectory + fileName); + return new TestFile(FormatsDirectory + fileName, decodeImage); }); } @@ -72,6 +82,10 @@ namespace ImageSharp.Tests public Image CreateImage() { + if (this.image == null) + { + throw new InvalidOperationException("TestFile.CreateImage() is invalid because instance has been created with decodeImage = false!"); + } return new Image(this.image); } } diff --git a/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs b/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs new file mode 100644 index 000000000..858989b20 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs @@ -0,0 +1,242 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +namespace ImageSharp.Tests.TestUtilities +{ + 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/ImageDataAttributeBase.cs b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs new file mode 100644 index 000000000..494e3bd17 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs @@ -0,0 +1,73 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + using Xunit.Sdk; + + /// + /// Base class for Theory Data attributes which pass an instance of to the test cases. + /// + public abstract class ImageDataAttributeBase : DataAttribute + { + protected readonly object[] AdditionalParameters; + + protected readonly PixelTypes PixelTypes; + + protected ImageDataAttributeBase(PixelTypes pixelTypes, object[] additionalParameters) + { + this.PixelTypes = pixelTypes; + this.AdditionalParameters = additionalParameters; + } + + public override IEnumerable GetData(MethodInfo testMethod) + { + var type = testMethod.GetParameters().First().ParameterType; + if (!typeof(ITestImageFactory).IsAssignableFrom(type)) + { + yield return this.AdditionalParameters; + } + else + { + foreach (var pixelType in this.PixelTypes.ToTypes()) + { + var factoryType = typeof(TestImageFactory<>).MakeGenericType(pixelType); + + foreach (object[] originalFacoryMethodArgs in this.GetAllFactoryMethodArgs(testMethod, factoryType)) + { + var actualFactoryMethodArgs = new object[originalFacoryMethodArgs.Length + 1]; + Array.Copy(originalFacoryMethodArgs, actualFactoryMethodArgs, originalFacoryMethodArgs.Length); + actualFactoryMethodArgs[actualFactoryMethodArgs.Length - 1] = testMethod; + + var factory = factoryType.GetMethod(this.GetFactoryMethodName(testMethod)) + .Invoke(null, actualFactoryMethodArgs); + + object[] result = new object[this.AdditionalParameters.Length + 1]; + result[0] = factory; + Array.Copy(this.AdditionalParameters, 0, result, 1, this.AdditionalParameters.Length); + yield return result; + } + } + } + } + + protected virtual IEnumerable GetAllFactoryMethodArgs(MethodInfo testMethod, Type factoryType) + { + var args = this.GetFactoryMethodArgs(testMethod, factoryType); + return Enumerable.Repeat(args, 1); + } + + protected virtual object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) + { + throw new InvalidOperationException("Semi-abstract method"); + } + + protected abstract string GetFactoryMethodName(MethodInfo testMethod); + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs new file mode 100644 index 000000000..798e397bd --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -0,0 +1,115 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.IO; + using System.Linq; + using System.Reflection; + + using ImageSharp.Formats; + + /// + /// Utility class to provide information about the test image & the test case for the test code, + /// and help managing IO. + /// + public class ImagingTestCaseUtility + { + /// + /// Name of the TColor in the owner + /// + 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; + + /// + /// The name of the test class (by default) + /// + public string TestGroupName { get; set; } = string.Empty; + + /// + /// The name of the test case (by default) + /// + 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 + /// + /// + /// The required extension + public string GetTestOutputFileName(string extension = null) + { + string fn = string.Empty; + + fn = Path.GetFileNameWithoutExtension(this.SourceFileOrDescription); + extension = extension ?? Path.GetExtension(this.SourceFileOrDescription); + extension = extension ?? ".bmp"; + + if (extension[0] != '.') + { + extension = '.' + extension; + } + + if (fn != string.Empty) fn = '_' + fn; + + string pixName = this.PixelTypeName; + if (pixName != string.Empty) + { + pixName = '_' + pixName + ' '; + } + + 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. + /// + /// The pixel format of the image + /// The image instance + /// The requested extension + public void SaveTestOutputFile(Image image, string extension = null) + where TColor : struct, IPackedPixel, IEquatable + { + string path = this.GetTestOutputFileName(extension); + + var format = GetImageFormatByExtension(extension); + + using (var stream = File.OpenWrite(path)) + { + image.Save(stream, format); + } + } + + internal void Init(MethodInfo method) + { + this.TestGroupName = method.DeclaringType.Name; + this.TestName = method.Name; + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs new file mode 100644 index 000000000..ad224564a --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -0,0 +1,57 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +namespace ImageSharp.Tests.TestUtilities +{ + using System; + + /// + /// Flags that are mapped to PackedPixel types. + /// They trigger the desired parametrization for . + /// + [Flags] + public enum PixelTypes : uint + { + None = 0, + + Alpha8 = 1 << 0, + + Argb = 1 << 1, + + Bgr565 = 1 << 2, + + Bgra4444 = 1 << 3, + + Byte4 = 1 << 4, + + Color = 1 << 5, + + HalfSingle = 1 << 6, + + HalfVector2 = 1 << 7, + + HalfVector4 = 1 << 8, + + NormalizedByte2 = 1 << 9, + + NormalizedByte4 = 1 << 10, + + NormalizedShort4 = 1 << 11, + + Rg32 = 1 << 12, + + Rgba1010102 = 1 << 13, + + Rgba64 = 1 << 14, + + Short2 = 1 << 15, + + Short4 = 1 << 16, + + // TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper + + // "All" is handled as a separate, individual case instead of using bitwise OR + All = 30 + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageFactory.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageFactory.cs new file mode 100644 index 000000000..49e8cc528 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageFactory.cs @@ -0,0 +1,165 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Collections.Concurrent; + using System.Reflection; + + /// + /// Provides instances for parametric unit tests. + /// + /// The pixel format of the image + public abstract class TestImageFactory : ITestImageFactory + where TColor : struct, IPackedPixel, IEquatable + { + public abstract Image Create(); + + public virtual string SourceFileOrDescription => ""; + + /// + /// Utility instance to provide informations about the test image & manage input/output + /// + public ImagingTestCaseUtility Utility { get; private set; } + + protected TestImageFactory() + { + } + + protected virtual TestImageFactory InitUtility(MethodInfo testMethod) + { + this.Utility = new ImagingTestCaseUtility() + { + SourceFileOrDescription = this.SourceFileOrDescription, + PixelTypeName = typeof(TColor).Name + }; + + if (testMethod != null) + { + this.Utility.Init(testMethod); + } + + return this; + } + + private class BlankFactory : TestImageFactory + { + protected int Width { get; } + + protected int Height { get; } + + public BlankFactory(int width, int height) + { + this.Width = width; + this.Height = height; + } + + public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; + + public override Image Create() => new Image(this.Width, this.Height); + } + + public static TestImageFactory Blank(int width, int height, MethodInfo testMethod = null) + => new BlankFactory(width, height).InitUtility(testMethod); + + private class LambdaFactory : TestImageFactory + { + private readonly Func> creator; + + public LambdaFactory(Func> creator) + { + this.creator = creator; + } + + public override Image Create() => this.creator(); + } + + public static TestImageFactory Lambda( + Func> func, + MethodInfo testMethod = null) => new LambdaFactory(func).InitUtility(testMethod); + + private class FileFactory : TestImageFactory + { + private static ConcurrentDictionary> cache = + new ConcurrentDictionary>(); + + private string filePath; + + public FileFactory(string filePath) + { + this.filePath = filePath; + } + + public override string SourceFileOrDescription => this.filePath; + + public override Image Create() + { + var cachedImage = cache.GetOrAdd( + this.filePath, + fn => + { + var testFile = TestFile.CreateWithoutImage(this.filePath); + return new Image(testFile.Bytes); + }); + + return new Image(cachedImage); + } + } + + public static TestImageFactory File(string filePath, MethodInfo testMethod = null) + { + return new FileFactory(filePath).InitUtility(testMethod); + } + + private class SolidFactory : BlankFactory + { + private readonly byte r; + + private readonly byte g; + + private readonly byte b; + + private readonly byte a; + + public override Image Create() + { + var image = base.Create(); + TColor color = default(TColor); + color.PackFromBytes(this.r, this.g, this.b, this.a); + + return image.Fill(color); + } + + public SolidFactory(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 static TestImageFactory Solid( + int width, + int height, + byte r, + byte g, + byte b, + byte a = 255, + MethodInfo testMethod = null) + { + return new SolidFactory(width, height, r, g, b, a).InitUtility(testMethod); + } + } + + /// + /// Marker + /// + public interface ITestImageFactory + { + } +} diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageFactoryTests.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageFactoryTests.cs new file mode 100644 index 000000000..9ced0bb78 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageFactoryTests.cs @@ -0,0 +1,167 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +// ReSharper disable InconsistentNaming +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.IO; + + using Xunit; + using Xunit.Abstractions; + + public class TestImageFactoryTests + { + + public TestImageFactoryTests(ITestOutputHelper output) + { + this.Output = output; + } + + private ITestOutputHelper Output { get; } + + + [Theory] + [WithBlankImages(42, 666, PixelTypes.Color | PixelTypes.Argb | PixelTypes.HalfSingle, "hello")] + public void Use_WithEmptyImageAttribute( + TestImageFactory factory, + string message) + where TColor : struct, IPackedPixel, IEquatable + { + var img = factory.Create(); + + Assert.Equal(42, img.Width); + Assert.Equal(666, img.Height); + Assert.Equal("hello", message); + } + + [Theory] + [WithBlankImages(42, 666, PixelTypes.All, "hello")] + public void Use_WithBlankImagesAttribute_WithAllPixelTypes( + TestImageFactory factory, + string message) + where TColor : struct, IPackedPixel, IEquatable + { + var img = factory.Create(); + + Assert.Equal(42, img.Width); + Assert.Equal(666, img.Height); + Assert.Equal("hello", message); + } + + // TODO: @dlemstra this works only with constant strings! + [Theory] + [WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)] + [WithFile(TestImages.Bmp.F, PixelTypes.All, 88)] + public void Use_WithFileAttribute(TestImageFactory factory, int yo) + where TColor : struct, IPackedPixel, IEquatable + { + Assert.NotNull(factory.Utility.SourceFileOrDescription); + var img = factory.Create(); + Assert.True(img.Width * img.Height > 0); + + Assert.Equal(88, yo); + + string fn = factory.Utility.GetTestOutputFileName("jpg"); + this.Output.WriteLine(fn); + } + + public static string[] AllBmpFiles => TestImages.Bmp.All; + + [Theory] + [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb)] + public void Use_WithFileCollection(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable + { + Assert.NotNull(factory.Utility.SourceFileOrDescription); + var image = factory.Create(); + factory.Utility.SaveTestOutputFile(image, "png"); + } + + [Theory] + [WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Color | PixelTypes.Argb)] + public void Use_WithSolidFilledImagesAttribute(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable + { + var img = factory.Create(); + Assert.Equal(img.Width, 10); + Assert.Equal(img.Height, 20); + + byte[] colors = new byte[4]; + + using (var pixels = img.Lock()) + { + for (int y = 0; y < pixels.Height; y++) + { + for (int x = 0; x < pixels.Width; x++) + { + pixels[x, y].ToBytes(colors, 0, ComponentOrder.XYZW); + + Assert.Equal(colors[0], 255); + Assert.Equal(colors[1], 100); + Assert.Equal(colors[2], 50); + Assert.Equal(colors[3], 200); + } + } + } + } + + public static Image TestMemberFactory() + where TColor : struct, IPackedPixel, IEquatable + { + return new Image(3, 3); + } + + [Theory] + [WithMemberFactory(nameof(TestMemberFactory), PixelTypes.All)] + public void Use_WithMemberFactoryAttribute(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable + { + var img = factory.Create(); + Assert.Equal(img.Width, 3); + } + + + public static readonly TheoryData BasicData = new TheoryData() + { + TestImageFactory.Blank(10, 20), + TestImageFactory.Blank( + 10, + 20) + }; + + + [Theory] + [MemberData(nameof(BasicData))] + public void Blank_MemberData(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable + { + var img = factory.Create(); + + Assert.True(img.Width * img.Height > 0); + } + + public static readonly TheoryData FileData = new TheoryData() + { + TestImageFactory.File( + TestImages.Bmp.Car), + TestImageFactory.File( + TestImages.Bmp.F) + }; + + [Theory] + [MemberData(nameof(FileData))] + public void File_MemberData(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable + { + this.Output.WriteLine("SRC: " + factory.Utility.SourceFileOrDescription); + this.Output.WriteLine("OUT: " + factory.Utility.GetTestOutputFileName()); + + var img = factory.Create(); + + Assert.True(img.Width * img.Height > 0); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs new file mode 100644 index 000000000..b4d8bcb32 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -0,0 +1,119 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Collections.Generic; + using System.Globalization; + using System.Linq; + using System.Reflection; + + /// + /// Extension methods for TestUtilities + /// + public static class TestUtilityExtensions + { + private static readonly Assembly ImageSharpAssembly = typeof(Color).GetTypeInfo().Assembly; + + private static readonly Dictionary PixelTypes2ClrTypes = new Dictionary(); + + private static readonly PixelTypes[] PixelTypesExpanded = + FlagsHelper.GetSortedValues().Where(t => t != PixelTypes.All && t != PixelTypes.None).ToArray(); + + static TestUtilityExtensions() + { + Assembly assembly = typeof(Color).GetTypeInfo().Assembly; + string nameSpace = typeof(Color).FullName; + nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Color).Name.Length - 1); + foreach (PixelTypes pt in PixelTypesExpanded) + { + string typeName = $"{nameSpace}.{FlagsHelper.ToString(pt)}"; + var t = assembly.GetType(typeName); + if (t != null) + { + PixelTypes2ClrTypes[pt] = t; + } + } + } + + public static Type GetPackedType(Type pixelType) + { + var intrfcType = + pixelType.GetInterfaces() + .Single(i => i.IsConstructedGenericType && i.GetGenericTypeDefinition() == typeof(IPackedPixel<>)); + + return intrfcType.GetGenericArguments().Single(); + } + + public static bool HasFlag(this PixelTypes pixelTypes, PixelTypes flag) => (pixelTypes & flag) == flag; + + public static bool IsEquivalentTo( + this Image a, + Image b, + bool compareAlpha = true) + where TColor : struct, IPackedPixel, IEquatable + { + if (a.Width != b.Width || a.Height != b.Height) + { + return false; + } + + byte[] bytesA = new byte[3]; + byte[] bytesB = new byte[3]; + + using (var pixA = a.Lock()) + { + using (var pixB = b.Lock()) + { + for (int y = 0; y < a.Height; y++) + { + for (int x = 0; x < a.Width; x++) + { + var ca = pixA[x, y]; + var cb = pixB[x, y]; + + if (compareAlpha) + { + if (!ca.Equals(cb)) + { + return false; + } + } + else + { + ca.ToBytes(bytesA, 0, ComponentOrder.XYZ); + cb.ToBytes(bytesB, 0, ComponentOrder.XYZ); + } + } + } + } + } + + return true; + } + + public static string ToCsv(this IEnumerable items, string separator = ",") + { + return string.Join(separator, items.Select(o => string.Format(CultureInfo.InvariantCulture, "{0}", o))); + } + + public static Type ToType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType]; + + public static IEnumerable ToTypes(this PixelTypes pixelTypes) + { + if (pixelTypes == PixelTypes.None) + { + return Enumerable.Empty(); + } + else if (pixelTypes == PixelTypes.All) + { + // TODO: Need to return unknown types here without forcing CLR to load all types in ImageSharp assembly + return PixelTypes2ClrTypes.Values; + } + + return PixelTypesExpanded.Where(pt => pixelTypes.HasFlag(pt)).Select(pt => pt.ToType()); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensionsTests.cs new file mode 100644 index 000000000..ddb8e549b --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensionsTests.cs @@ -0,0 +1,123 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +// ReSharper disable InconsistentNaming +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Linq; + using System.Numerics; + using System.Reflection; + + using Xunit; + using Xunit.Abstractions; + + public class TestUtilityExtensionsTests + { + public TestUtilityExtensionsTests(ITestOutputHelper output) + { + this.Output = output; + } + + private ITestOutputHelper Output { get; } + + public static Image CreateTestImage() + where TColor : struct, IPackedPixel, IEquatable + { + Image image = new Image(10, 10); + + using (var pixels = image.Lock()) + { + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 10; j++) + { + Vector4 v = new Vector4(i, j, 0, 1); + v /= 10; + + TColor color = default(TColor); + color.PackFromVector4(v); + + pixels[i, j] = color; + } + } + } + + return image; + } + + [Fact] + public void Baz() + { + var type = typeof(Color).GetTypeInfo().Assembly.GetType("ImageSharp.Color"); + this.Output.WriteLine(type.ToString()); + + var fake = typeof(Color).GetTypeInfo().Assembly.GetType("ImageSharp.dsaada_DASqewrr"); + Assert.Null(fake); + } + + [Fact] + public void GetPackedType() + { + Type shouldBeUIint32 = TestUtilityExtensions.GetPackedType(typeof(Color)); + + Assert.Equal(shouldBeUIint32, typeof(uint)); + } + + [Theory] + [WithFile(TestImages.Bmp.Car, PixelTypes.Color)] + public void IsEquivalentTo_WhenFalse(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable + { + var a = factory.Create(); + var b = factory.Create(); + b = b.OilPaint(3, 2); + + Assert.False(a.IsEquivalentTo(b)); + } + + [Theory] + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.Color | PixelTypes.Bgr565)] + public void IsEquivalentTo_WhenTrue(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable + { + var a = factory.Create(); + var b = factory.Create(); + + Assert.True(a.IsEquivalentTo(b)); + } + + [Theory] + [InlineData(PixelTypes.Color, typeof(Color))] + [InlineData(PixelTypes.Argb, typeof(Argb))] + [InlineData(PixelTypes.HalfVector4, typeof(HalfVector4))] + public void ToType(PixelTypes pt, Type expectedType) + { + Assert.Equal(pt.ToType(), expectedType); + } + + [Fact] + public void ToTypes() + { + PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.Color | PixelTypes.HalfVector2; + + var clrTypes = pixelTypes.ToTypes().ToArray(); + + Assert.Equal(clrTypes.Length, 4); + Assert.Contains(typeof(Alpha8), clrTypes); + Assert.Contains(typeof(Bgr565), clrTypes); + Assert.Contains(typeof(Color), clrTypes); + Assert.Contains(typeof(HalfVector2), clrTypes); + } + + [Fact] + public void ToTypes_All() + { + var clrTypes = PixelTypes.All.ToTypes().ToArray(); + + Assert.True(clrTypes.Length >= FlagsHelper.GetSortedValues().Length - 2); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs new file mode 100644 index 000000000..a7266a615 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Reflection; + + /// + /// Triggers passing instances which produce a blank image of size width * height. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + public class WithBlankImagesAttribute : ImageDataAttributeBase + { + /// + /// Triggers passing an that produces a blank image of size width * height + /// + /// The required width + /// The required height + /// The requested parameter + /// Additional theory parameter values + public WithBlankImagesAttribute(int width, int height, PixelTypes pixelTypes, params object[] additionalParameters) + : base(pixelTypes, additionalParameters) + { + this.Width = width; + this.Height = height; + } + + public int Width { get; } + public int Height { get; } + + protected override string GetFactoryMethodName(MethodInfo testMethod) => "Blank"; + + protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) => new object[] { this.Width, this.Height }; + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs new file mode 100644 index 000000000..ff8a7a30d --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs @@ -0,0 +1,36 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Reflection; + + /// + /// Triggers passing instances which read an image from the given file + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + public class WithFileAttribute : ImageDataAttributeBase + { + private readonly string fileName; + + /// + /// Triggers passing instances which read an image from the given file + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The name of the file + /// The requested pixel types + /// Additional theory parameter values + public WithFileAttribute(string fileName, PixelTypes pixelTypes, params object[] additionalParameters) + : base(pixelTypes, additionalParameters) + { + this.fileName = fileName; + } + + protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) => new object[] { this.fileName }; + + protected override string GetFactoryMethodName(MethodInfo testMethod) => "File"; + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs new file mode 100644 index 000000000..7ac19007b --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs @@ -0,0 +1,86 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + /// + /// Triggers passing instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName + /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// + public class WithFileCollectionAttribute : ImageDataAttributeBase + { + private readonly string enumeratorMemberName; + + /// + /// Triggers passing instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName + /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The name of the static test class field/property enumerating the files + /// The requested pixel types + /// Additional theory parameter values + public WithFileCollectionAttribute( + string enumeratorMemberName, + PixelTypes pixelTypes, + params object[] additionalParameters) + : base(pixelTypes, additionalParameters) + { + this.enumeratorMemberName = enumeratorMemberName; + } + + protected override IEnumerable GetAllFactoryMethodArgs(MethodInfo testMethod, Type factoryType) + { + var accessor = this.GetPropertyAccessor(testMethod.DeclaringType); + + accessor = accessor ?? this.GetFieldAccessor(testMethod.DeclaringType); + + IEnumerable files = (IEnumerable)accessor(); + return files.Select(f => new object[] { f }); + } + + protected override string GetFactoryMethodName(MethodInfo testMethod) => "File"; + + /// + /// Based on MemberData implementation + /// + private Func GetFieldAccessor(Type type) + { + FieldInfo fieldInfo = null; + for (var reflectionType = type; + reflectionType != null; + reflectionType = reflectionType.GetTypeInfo().BaseType) + { + fieldInfo = reflectionType.GetRuntimeField(this.enumeratorMemberName); + if (fieldInfo != null) break; + } + + if (fieldInfo == null || !fieldInfo.IsStatic) return null; + + return () => fieldInfo.GetValue(null); + } + + /// + /// Based on MemberData implementation + /// + private Func GetPropertyAccessor(Type type) + { + PropertyInfo propInfo = null; + for (var reflectionType = type; + reflectionType != null; + reflectionType = reflectionType.GetTypeInfo().BaseType) + { + propInfo = reflectionType.GetRuntimeProperty(this.enumeratorMemberName); + if (propInfo != null) break; + } + + if (propInfo == null || propInfo.GetMethod == null || !propInfo.GetMethod.IsStatic) return null; + + return () => propInfo.GetValue(null, null); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs new file mode 100644 index 000000000..b71db5198 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs @@ -0,0 +1,48 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Reflection; + + /// + /// Triggers passing instances which return the image produced by the given test class member method + /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// + public class WithMemberFactoryAttribute : ImageDataAttributeBase + { + private readonly string memberMethodName; + + /// + /// Triggers passing instances which return the image produced by the given test class member method + /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The name of the static test class which returns the image + /// The requested pixel types + /// Additional theory parameter values + public WithMemberFactoryAttribute(string memberMethodName, PixelTypes pixelTypes, params object[] additionalParameters) + : base(pixelTypes, additionalParameters) + { + this.memberMethodName = memberMethodName; + } + + protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) + { + var m = testMethod.DeclaringType.GetMethod(this.memberMethodName); + + var args = factoryType.GetGenericArguments(); + var imgType = typeof(Image<>).MakeGenericType(args); + var funcType = typeof(Func<>).MakeGenericType(imgType); + + var genericMethod = m.MakeGenericMethod(args); + + var d = genericMethod.CreateDelegate(funcType); + return new object[] { d }; + } + + protected override string GetFactoryMethodName(MethodInfo testMethod) => "Lambda"; + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs new file mode 100644 index 000000000..bd8abe1f2 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs @@ -0,0 +1,93 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// +namespace ImageSharp.Tests.TestUtilities +{ + using System; + using System.Reflection; + + /// + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + public class WithSolidFilledImagesAttribute : WithBlankImagesAttribute + { + /// + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The width of the requested image + /// The height of the requested image + /// Red + /// Green + /// Blue + /// The requested pixel types + /// Additional theory parameter values + public WithSolidFilledImagesAttribute( + int width, + int height, + byte r, + byte g, + byte b, + PixelTypes pixelTypes, + params object[] additionalParameters) + : this(width, height, r, g, b, 255, pixelTypes, additionalParameters) + { + } + + /// + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// + /// The width of the requested image + /// The height of the requested image + /// Red + /// Green + /// Blue + /// /// Alpha + /// The requested pixel types + /// Additional theory parameter values + public WithSolidFilledImagesAttribute( + int width, + int height, + byte r, + byte g, + byte b, + byte a, + PixelTypes pixelTypes, + params object[] additionalParameters) + : base(width, height, pixelTypes, additionalParameters) + { + this.R = r; + this.G = g; + this.B = b; + this.A = a; + } + + /// + /// Red + /// + public byte R { get; } + + /// + /// Green + /// + public byte G { get; } + + /// + /// Blue + /// + public byte B { get; } + + /// + /// Alpha + /// + public byte A { get; } + + protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) + => new object[] { this.Width, this.Height, this.R, this.G, this.B, this.A }; + + protected override string GetFactoryMethodName(MethodInfo testMethod) => "Solid"; + } +} \ No newline at end of file From ff3e9e9894120e7e74af346ae5235dbc0e708e28 Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Fri, 23 Dec 2016 02:58:18 +0100 Subject: [PATCH 2/6] Re-applied JpegTests and PixelAccessorTests changes. + added new constructor to PixelArea --- src/ImageSharp/Image/PixelArea{TColor}.cs | 11 ++ .../ImageSharp.Tests/Formats/Jpg/JpegTests.cs | 69 +++++------ .../Image/PixelAccessorTests.cs | 111 ++++++++++++++++++ .../TestUtilities/ImagingTestCaseUtility.cs | 8 +- 4 files changed, 153 insertions(+), 46 deletions(-) diff --git a/src/ImageSharp/Image/PixelArea{TColor}.cs b/src/ImageSharp/Image/PixelArea{TColor}.cs index 2f631f66e..673fe5500 100644 --- a/src/ImageSharp/Image/PixelArea{TColor}.cs +++ b/src/ImageSharp/Image/PixelArea{TColor}.cs @@ -79,6 +79,17 @@ namespace ImageSharp this.PixelBase = (byte*)this.dataPointer.ToPointer(); } + /// + /// Initializes a new instance of the class. + /// + /// The width. + /// The height. + /// The component order. + public PixelArea(int width, int height, ComponentOrder componentOrder) + : this(width, height, componentOrder, 0) + { + } + /// /// Initializes a new instance of the class. /// diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs index 08efe5c89..6e0977541 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs @@ -8,6 +8,8 @@ using Xunit.Abstractions; namespace ImageSharp.Tests.Formats.Jpg { + using ImageSharp.Tests.TestUtilities; + public class JpegTests { @@ -19,60 +21,43 @@ namespace ImageSharp.Tests.Formats.Jpg { Output = output; } + public static IEnumerable AllJpegFiles => TestImages.Jpeg.All; - protected string CreateTestOutputFile(string fileName) - { - if (!Directory.Exists(TestOutputDirectory)) - { - Directory.CreateDirectory(TestOutputDirectory); - } - - //string id = Guid.NewGuid().ToString().Substring(0, 4); - - string ext = Path.GetExtension(fileName); - fileName = Path.GetFileNameWithoutExtension(fileName); - - return $"{TestOutputDirectory}/{fileName}{ext}"; - } - - protected Stream CreateOutputStream(string fileName) - { - fileName = CreateTestOutputFile(fileName); - Output?.WriteLine("Opened for write: "+fileName); - return File.OpenWrite(fileName); - } - - public static IEnumerable AllJpegFiles - => TestImages.Jpeg.All.Select(file => new object[] {TestFile.Create(file)}); - + // TODO: Turned off PixelTypes.All to be CI-friendly, what should be the practice? [Theory] - [MemberData(nameof(AllJpegFiles))] - public void OpenJpeg_SaveBmp(TestFile file) + //[WithFileCollection(nameof(AllJpegFiles), PixelTypes.All)] + [WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.Argb)] + public void OpenJpeg_SaveBmp(TestImageFactory factory) + where TColor : struct, IPackedPixel, IEquatable { - string bmpFileName = file.FileNameWithoutExtension + ".bmp"; + var image = factory.Create(); - var image = file.CreateImage(); - - using (var outputStream = CreateOutputStream(bmpFileName)) - { - image.Save(outputStream, new BmpFormat()); - } + factory.Utility.SaveTestOutputFile(image, "bmp"); } - public static IEnumerable AllBmpFiles - => TestImages.Bmp.All.Select(file => new object[] { TestFile.Create(file) }); + + public static IEnumerable AllBmpFiles => TestImages.Bmp.All; [Theory] - [MemberData(nameof(AllBmpFiles))] - public void OpenBmp_SaveJpeg(TestFile file) + [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio420, 75)] + [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio444, 75)] + public void OpenBmp_SaveJpeg(TestImageFactory factory, JpegSubsample subSample, int quality) + where TColor : struct, IPackedPixel, IEquatable { - string jpegPath = file.FileNameWithoutExtension + ".jpeg"; + var image = factory.Create(); - var image = file.CreateImage(); + var utility = factory.Utility; + utility.TestName += "_" + subSample + "_Q" + quality; - using (var outputStream = CreateOutputStream(jpegPath)) + using (var outputStream = File.OpenWrite(utility.GetTestOutputFileName("jpg"))) { - image.Save(outputStream, new JpegFormat()); + var encoder = new JpegEncoder() + { + Subsample = subSample, + Quality = quality + }; + + image.Save(outputStream, encoder); } } } diff --git a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs index e47678d0c..39503d1da 100644 --- a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs +++ b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs @@ -6,6 +6,9 @@ namespace ImageSharp.Tests { using System; + using System.Numerics; + + using ImageSharp.Tests.TestUtilities; using Xunit; @@ -14,6 +17,114 @@ namespace ImageSharp.Tests /// public class PixelAccessorTests { + public static Image CreateTestImage() + where TColor : struct, IPackedPixel, IEquatable + { + Image image = new Image(10, 10); + + using (var pixels = image.Lock()) + { + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 10; j++) + { + Vector4 v = new Vector4(i, j, 0, 1); + v /= 10; + + TColor color = default(TColor); + color.PackFromVector4(v); + + pixels[i, j] = color; + } + } + } + return image; + } + + [Theory] + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All, ComponentOrder.XYZ)] + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All, ComponentOrder.ZYX)] + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All, ComponentOrder.XYZW)] + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All, ComponentOrder.ZYXW)] + public void CopyTo_Then_CopyFrom_OnFullImageRect(TestImageFactory factory, ComponentOrder order) + where TColor : struct, IPackedPixel, IEquatable + { + var src = factory.Create(); + + var dest = new Image(src.Width, src.Height); + + using (PixelArea area = new PixelArea(src.Width, src.Height, order)) + { + using (var srcPixels = src.Lock()) + { + srcPixels.CopyTo(area, 0, 0); + } + + using (var destPixels = dest.Lock()) + { + destPixels.CopyFrom(area, 0, 0); + } + } + + Assert.True(src.IsEquivalentTo(dest, false)); + } + + // TODO: Need a processor in the library with this signature + private static void Fill(Image image, Rectangle region, TColor color) + where TColor : struct, IPackedPixel, IEquatable + { + using (var pixels = image.Lock()) + { + for (int y = region.Top; y < region.Bottom; y++) + { + for (int x = region.Left; x < region.Right; x++) + { + pixels[x, y] = color; + } + } + } + } + + [Theory] + [WithBlankImages(16, 16, PixelTypes.All, ComponentOrder.XYZ)] + [WithBlankImages(16, 16, PixelTypes.All, ComponentOrder.ZYX)] + [WithBlankImages(16, 16, PixelTypes.All, ComponentOrder.XYZW)] + [WithBlankImages(16, 16, PixelTypes.All, ComponentOrder.ZYXW)] + public void CopyTo_Then_CopyFrom_WithOffset(TestImageFactory factory, ComponentOrder order) + where TColor : struct, IPackedPixel, IEquatable + + { + var srcImage = factory.Create(); + + var color = default(TColor); + color.PackFromBytes(255, 0, 0, 255); + + Fill(srcImage, new Rectangle(4, 4, 8, 8), color); + + var destImage = new Image(8, 8); + + using (var srcPixels = srcImage.Lock()) + { + using (var area = new PixelArea(8, 8, order)) + { + srcPixels.CopyTo(area, 4, 4); + + using (var destPixels = destImage.Lock()) + { + destPixels.CopyFrom(area, 0, 0); + } + } + } + + factory.Utility.SourceFileOrDescription = order.ToString(); + factory.Utility.SaveTestOutputFile(destImage, "bmp"); + + var expectedImage = new Image(8, 8).Fill(color); + + Assert.True(destImage.IsEquivalentTo(expectedImage)); + } + + [Fact] public void CopyFromZYX() { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 798e397bd..5c41ca248 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -18,18 +18,18 @@ namespace ImageSharp.Tests.TestUtilities public class ImagingTestCaseUtility { /// - /// Name of the TColor in the owner + /// Name of the TColor in the owner /// public string PixelTypeName { get; set; } = string.Empty; /// - /// The name of the file which is provided by + /// 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; /// - /// The name of the test class (by default) + /// By default this is the name of the test class, but it's possible to change it /// public string TestGroupName { get; set; } = string.Empty; @@ -75,7 +75,7 @@ namespace ImageSharp.Tests.TestUtilities string pixName = this.PixelTypeName; if (pixName != string.Empty) { - pixName = '_' + pixName + ' '; + pixName = '_' + pixName; } return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{extension}"; From c62e12248a8b49e932a8ada16cce711a48b7da57 Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Fri, 23 Dec 2016 03:34:50 +0100 Subject: [PATCH 3/6] renamed TestImageFactory --to--> TestImageProvider --- .../ImageSharp.Tests/Formats/Jpg/JpegTests.cs | 12 ++-- .../Image/PixelAccessorTests.cs | 12 ++-- .../TestUtilities/ImageDataAttributeBase.cs | 4 +- .../TestUtilities/ImagingTestCaseUtility.cs | 4 +- .../TestUtilities/PixelTypes.cs | 4 +- ...stImageFactory.cs => TestImageProvider.cs} | 57 +++++++++---------- .../{ => Tests}/TestImageFactoryTests.cs | 55 +++++++++--------- .../{ => Tests}/TestUtilityExtensionsTests.cs | 14 ++--- .../TestUtilities/WithBlankImageAttribute.cs | 6 +- .../TestUtilities/WithFileAttribute.cs | 8 +-- .../WithFileCollectionAttribute.cs | 8 +-- .../WithMemberFactoryAttribute.cs | 8 +-- .../WithSolidFilledImagesAttribute.cs | 12 ++-- 13 files changed, 101 insertions(+), 103 deletions(-) rename tests/ImageSharp.Tests/TestUtilities/{TestImageFactory.cs => TestImageProvider.cs} (63%) rename tests/ImageSharp.Tests/TestUtilities/{ => Tests}/TestImageFactoryTests.cs (76%) rename tests/ImageSharp.Tests/TestUtilities/{ => Tests}/TestUtilityExtensionsTests.cs (89%) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs index 6e0977541..d02c2d76e 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs @@ -27,12 +27,12 @@ namespace ImageSharp.Tests.Formats.Jpg [Theory] //[WithFileCollection(nameof(AllJpegFiles), PixelTypes.All)] [WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.Argb)] - public void OpenJpeg_SaveBmp(TestImageFactory factory) + public void OpenJpeg_SaveBmp(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - var image = factory.Create(); + var image = provider.GetImage(); - factory.Utility.SaveTestOutputFile(image, "bmp"); + provider.Utility.SaveTestOutputFile(image, "bmp"); } @@ -41,12 +41,12 @@ namespace ImageSharp.Tests.Formats.Jpg [Theory] [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio420, 75)] [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio444, 75)] - public void OpenBmp_SaveJpeg(TestImageFactory factory, JpegSubsample subSample, int quality) + public void OpenBmp_SaveJpeg(TestImageProvider provider, JpegSubsample subSample, int quality) where TColor : struct, IPackedPixel, IEquatable { - var image = factory.Create(); + var image = provider.GetImage(); - var utility = factory.Utility; + var utility = provider.Utility; utility.TestName += "_" + subSample + "_Q" + quality; using (var outputStream = File.OpenWrite(utility.GetTestOutputFileName("jpg"))) diff --git a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs index 39503d1da..409184303 100644 --- a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs +++ b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs @@ -46,10 +46,10 @@ namespace ImageSharp.Tests [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All, ComponentOrder.ZYX)] [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All, ComponentOrder.XYZW)] [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All, ComponentOrder.ZYXW)] - public void CopyTo_Then_CopyFrom_OnFullImageRect(TestImageFactory factory, ComponentOrder order) + public void CopyTo_Then_CopyFrom_OnFullImageRect(TestImageProvider provider, ComponentOrder order) where TColor : struct, IPackedPixel, IEquatable { - var src = factory.Create(); + var src = provider.GetImage(); var dest = new Image(src.Width, src.Height); @@ -90,11 +90,11 @@ namespace ImageSharp.Tests [WithBlankImages(16, 16, PixelTypes.All, ComponentOrder.ZYX)] [WithBlankImages(16, 16, PixelTypes.All, ComponentOrder.XYZW)] [WithBlankImages(16, 16, PixelTypes.All, ComponentOrder.ZYXW)] - public void CopyTo_Then_CopyFrom_WithOffset(TestImageFactory factory, ComponentOrder order) + public void CopyTo_Then_CopyFrom_WithOffset(TestImageProvider provider, ComponentOrder order) where TColor : struct, IPackedPixel, IEquatable { - var srcImage = factory.Create(); + var srcImage = provider.GetImage(); var color = default(TColor); color.PackFromBytes(255, 0, 0, 255); @@ -116,8 +116,8 @@ namespace ImageSharp.Tests } } - factory.Utility.SourceFileOrDescription = order.ToString(); - factory.Utility.SaveTestOutputFile(destImage, "bmp"); + provider.Utility.SourceFileOrDescription = order.ToString(); + provider.Utility.SaveTestOutputFile(destImage, "bmp"); var expectedImage = new Image(8, 8).Fill(color); diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs index 494e3bd17..fc27f32a0 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs @@ -12,7 +12,7 @@ namespace ImageSharp.Tests.TestUtilities using Xunit.Sdk; /// - /// Base class for Theory Data attributes which pass an instance of to the test cases. + /// Base class for Theory Data attributes which pass an instance of to the test cases. /// public abstract class ImageDataAttributeBase : DataAttribute { @@ -37,7 +37,7 @@ namespace ImageSharp.Tests.TestUtilities { foreach (var pixelType in this.PixelTypes.ToTypes()) { - var factoryType = typeof(TestImageFactory<>).MakeGenericType(pixelType); + var factoryType = typeof(TestImageProvider<>).MakeGenericType(pixelType); foreach (object[] originalFacoryMethodArgs in this.GetAllFactoryMethodArgs(testMethod, factoryType)) { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 5c41ca248..8225e08ef 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -18,12 +18,12 @@ namespace ImageSharp.Tests.TestUtilities public class ImagingTestCaseUtility { /// - /// Name of the TColor in the owner + /// Name of the TColor in the owner /// public string PixelTypeName { get; set; } = string.Empty; /// - /// The name of the file which is provided by + /// 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; diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs index ad224564a..627715679 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -8,7 +8,7 @@ namespace ImageSharp.Tests.TestUtilities /// /// Flags that are mapped to PackedPixel types. - /// They trigger the desired parametrization for . + /// They trigger the desired parametrization for . /// [Flags] public enum PixelTypes : uint @@ -48,7 +48,7 @@ namespace ImageSharp.Tests.TestUtilities Short2 = 1 << 15, Short4 = 1 << 16, - + // TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper // "All" is handled as a separate, individual case instead of using bitwise OR diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageFactory.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs similarity index 63% rename from tests/ImageSharp.Tests/TestUtilities/TestImageFactory.cs rename to tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs index 49e8cc528..5664c7595 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -13,10 +13,13 @@ namespace ImageSharp.Tests.TestUtilities /// Provides instances for parametric unit tests. /// /// The pixel format of the image - public abstract class TestImageFactory : ITestImageFactory + public abstract class TestImageProvider : ITestImageFactory where TColor : struct, IPackedPixel, IEquatable { - public abstract Image Create(); + /// + /// Returns an instance to the test case with the necessary traits. + /// + public abstract Image GetImage(); public virtual string SourceFileOrDescription => ""; @@ -25,11 +28,7 @@ namespace ImageSharp.Tests.TestUtilities /// public ImagingTestCaseUtility Utility { get; private set; } - protected TestImageFactory() - { - } - - protected virtual TestImageFactory InitUtility(MethodInfo testMethod) + protected virtual TestImageProvider InitUtility(MethodInfo testMethod) { this.Utility = new ImagingTestCaseUtility() { @@ -45,13 +44,13 @@ namespace ImageSharp.Tests.TestUtilities return this; } - private class BlankFactory : TestImageFactory + private class BlankProvider : TestImageProvider { protected int Width { get; } protected int Height { get; } - public BlankFactory(int width, int height) + public BlankProvider(int width, int height) { this.Width = width; this.Height = height; @@ -59,43 +58,43 @@ namespace ImageSharp.Tests.TestUtilities public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; - public override Image Create() => new Image(this.Width, this.Height); + public override Image GetImage() => new Image(this.Width, this.Height); } - public static TestImageFactory Blank(int width, int height, MethodInfo testMethod = null) - => new BlankFactory(width, height).InitUtility(testMethod); + public static TestImageProvider Blank(int width, int height, MethodInfo testMethod = null) + => new BlankProvider(width, height).InitUtility(testMethod); - private class LambdaFactory : TestImageFactory + private class LambdaProvider : TestImageProvider { private readonly Func> creator; - public LambdaFactory(Func> creator) + public LambdaProvider(Func> creator) { this.creator = creator; } - public override Image Create() => this.creator(); + public override Image GetImage() => this.creator(); } - public static TestImageFactory Lambda( + public static TestImageProvider Lambda( Func> func, - MethodInfo testMethod = null) => new LambdaFactory(func).InitUtility(testMethod); + MethodInfo testMethod = null) => new LambdaProvider(func).InitUtility(testMethod); - private class FileFactory : TestImageFactory + private class FileProvider : TestImageProvider { private static ConcurrentDictionary> cache = new ConcurrentDictionary>(); private string filePath; - public FileFactory(string filePath) + public FileProvider(string filePath) { this.filePath = filePath; } public override string SourceFileOrDescription => this.filePath; - public override Image Create() + public override Image GetImage() { var cachedImage = cache.GetOrAdd( this.filePath, @@ -109,12 +108,12 @@ namespace ImageSharp.Tests.TestUtilities } } - public static TestImageFactory File(string filePath, MethodInfo testMethod = null) + public static TestImageProvider File(string filePath, MethodInfo testMethod = null) { - return new FileFactory(filePath).InitUtility(testMethod); + return new FileProvider(filePath).InitUtility(testMethod); } - private class SolidFactory : BlankFactory + private class SolidProvider : BlankProvider { private readonly byte r; @@ -124,16 +123,16 @@ namespace ImageSharp.Tests.TestUtilities private readonly byte a; - public override Image Create() + public override Image GetImage() { - var image = base.Create(); + var image = base.GetImage(); TColor color = default(TColor); color.PackFromBytes(this.r, this.g, this.b, this.a); return image.Fill(color); } - public SolidFactory(int width, int height, byte r, byte g, byte b, byte a) + public SolidProvider(int width, int height, byte r, byte g, byte b, byte a) : base(width, height) { this.r = r; @@ -143,7 +142,7 @@ namespace ImageSharp.Tests.TestUtilities } } - public static TestImageFactory Solid( + public static TestImageProvider Solid( int width, int height, byte r, @@ -152,7 +151,7 @@ namespace ImageSharp.Tests.TestUtilities byte a = 255, MethodInfo testMethod = null) { - return new SolidFactory(width, height, r, g, b, a).InitUtility(testMethod); + return new SolidProvider(width, height, r, g, b, a).InitUtility(testMethod); } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageFactoryTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageFactoryTests.cs similarity index 76% rename from tests/ImageSharp.Tests/TestUtilities/TestImageFactoryTests.cs rename to tests/ImageSharp.Tests/TestUtilities/Tests/TestImageFactoryTests.cs index 9ced0bb78..1cc8ac35b 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageFactoryTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageFactoryTests.cs @@ -4,10 +4,9 @@ // // ReSharper disable InconsistentNaming -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests.TestUtilities.Tests { using System; - using System.IO; using Xunit; using Xunit.Abstractions; @@ -26,11 +25,11 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [WithBlankImages(42, 666, PixelTypes.Color | PixelTypes.Argb | PixelTypes.HalfSingle, "hello")] public void Use_WithEmptyImageAttribute( - TestImageFactory factory, + TestImageProvider provider, string message) where TColor : struct, IPackedPixel, IEquatable { - var img = factory.Create(); + var img = provider.GetImage(); Assert.Equal(42, img.Width); Assert.Equal(666, img.Height); @@ -40,11 +39,11 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [WithBlankImages(42, 666, PixelTypes.All, "hello")] public void Use_WithBlankImagesAttribute_WithAllPixelTypes( - TestImageFactory factory, + TestImageProvider provider, string message) where TColor : struct, IPackedPixel, IEquatable { - var img = factory.Create(); + var img = provider.GetImage(); Assert.Equal(42, img.Width); Assert.Equal(666, img.Height); @@ -55,16 +54,16 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)] [WithFile(TestImages.Bmp.F, PixelTypes.All, 88)] - public void Use_WithFileAttribute(TestImageFactory factory, int yo) + public void Use_WithFileAttribute(TestImageProvider provider, int yo) where TColor : struct, IPackedPixel, IEquatable { - Assert.NotNull(factory.Utility.SourceFileOrDescription); - var img = factory.Create(); + Assert.NotNull(provider.Utility.SourceFileOrDescription); + var img = provider.GetImage(); Assert.True(img.Width * img.Height > 0); Assert.Equal(88, yo); - string fn = factory.Utility.GetTestOutputFileName("jpg"); + string fn = provider.Utility.GetTestOutputFileName("jpg"); this.Output.WriteLine(fn); } @@ -72,20 +71,20 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb)] - public void Use_WithFileCollection(TestImageFactory factory) + public void Use_WithFileCollection(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - Assert.NotNull(factory.Utility.SourceFileOrDescription); - var image = factory.Create(); - factory.Utility.SaveTestOutputFile(image, "png"); + Assert.NotNull(provider.Utility.SourceFileOrDescription); + var image = provider.GetImage(); + provider.Utility.SaveTestOutputFile(image, "png"); } [Theory] [WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Color | PixelTypes.Argb)] - public void Use_WithSolidFilledImagesAttribute(TestImageFactory factory) + public void Use_WithSolidFilledImagesAttribute(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - var img = factory.Create(); + var img = provider.GetImage(); Assert.Equal(img.Width, 10); Assert.Equal(img.Height, 20); @@ -116,18 +115,18 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [WithMemberFactory(nameof(TestMemberFactory), PixelTypes.All)] - public void Use_WithMemberFactoryAttribute(TestImageFactory factory) + public void Use_WithMemberFactoryAttribute(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - var img = factory.Create(); + var img = provider.GetImage(); Assert.Equal(img.Width, 3); } public static readonly TheoryData BasicData = new TheoryData() { - TestImageFactory.Blank(10, 20), - TestImageFactory.Blank( + TestImageProvider.Blank(10, 20), + TestImageProvider.Blank( 10, 20) }; @@ -135,31 +134,31 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [MemberData(nameof(BasicData))] - public void Blank_MemberData(TestImageFactory factory) + public void Blank_MemberData(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - var img = factory.Create(); + var img = provider.GetImage(); Assert.True(img.Width * img.Height > 0); } public static readonly TheoryData FileData = new TheoryData() { - TestImageFactory.File( + TestImageProvider.File( TestImages.Bmp.Car), - TestImageFactory.File( + TestImageProvider.File( TestImages.Bmp.F) }; [Theory] [MemberData(nameof(FileData))] - public void File_MemberData(TestImageFactory factory) + public void File_MemberData(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - this.Output.WriteLine("SRC: " + factory.Utility.SourceFileOrDescription); - this.Output.WriteLine("OUT: " + factory.Utility.GetTestOutputFileName()); + this.Output.WriteLine("SRC: " + provider.Utility.SourceFileOrDescription); + this.Output.WriteLine("OUT: " + provider.Utility.GetTestOutputFileName()); - var img = factory.Create(); + var img = provider.GetImage(); Assert.True(img.Width * img.Height > 0); } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs similarity index 89% rename from tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensionsTests.cs rename to tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index ddb8e549b..948479d7a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -4,7 +4,7 @@ // // ReSharper disable InconsistentNaming -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests.TestUtilities.Tests { using System; using System.Linq; @@ -68,11 +68,11 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [WithFile(TestImages.Bmp.Car, PixelTypes.Color)] - public void IsEquivalentTo_WhenFalse(TestImageFactory factory) + public void IsEquivalentTo_WhenFalse(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - var a = factory.Create(); - var b = factory.Create(); + var a = provider.GetImage(); + var b = provider.GetImage(); b = b.OilPaint(3, 2); Assert.False(a.IsEquivalentTo(b)); @@ -80,11 +80,11 @@ namespace ImageSharp.Tests.TestUtilities [Theory] [WithMemberFactory(nameof(CreateTestImage), PixelTypes.Color | PixelTypes.Bgr565)] - public void IsEquivalentTo_WhenTrue(TestImageFactory factory) + public void IsEquivalentTo_WhenTrue(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { - var a = factory.Create(); - var b = factory.Create(); + var a = provider.GetImage(); + var b = provider.GetImage(); Assert.True(a.IsEquivalentTo(b)); } diff --git a/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs index a7266a615..9a4b03636 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs @@ -9,13 +9,13 @@ namespace ImageSharp.Tests.TestUtilities using System.Reflection; /// - /// Triggers passing instances which produce a blank image of size width * height. - /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which produce a blank image of size width * height. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter /// public class WithBlankImagesAttribute : ImageDataAttributeBase { /// - /// Triggers passing an that produces a blank image of size width * height + /// Triggers passing an that produces a blank image of size width * height /// /// The required width /// The required height diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs index ff8a7a30d..49cbeab5e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs @@ -9,16 +9,16 @@ namespace ImageSharp.Tests.TestUtilities using System.Reflection; /// - /// Triggers passing instances which read an image from the given file - /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which read an image from the given file + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter /// public class WithFileAttribute : ImageDataAttributeBase { private readonly string fileName; /// - /// Triggers passing instances which read an image from the given file - /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which read an image from the given file + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter /// /// The name of the file /// The requested pixel types diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs index 7ac19007b..bd80ae66b 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs @@ -10,16 +10,16 @@ namespace ImageSharp.Tests.TestUtilities using System.Reflection; /// - /// Triggers passing instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName - /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName + /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// public class WithFileCollectionAttribute : ImageDataAttributeBase { private readonly string enumeratorMemberName; /// - /// Triggers passing instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName - /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName + /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// /// The name of the static test class field/property enumerating the files /// The requested pixel types diff --git a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs index b71db5198..3860880c3 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs @@ -9,16 +9,16 @@ namespace ImageSharp.Tests.TestUtilities using System.Reflection; /// - /// Triggers passing instances which return the image produced by the given test class member method - /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which return the image produced by the given test class member method + /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// public class WithMemberFactoryAttribute : ImageDataAttributeBase { private readonly string memberMethodName; /// - /// Triggers passing instances which return the image produced by the given test class member method - /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which return the image produced by the given test class member method + /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// /// The name of the static test class which returns the image /// The requested pixel types diff --git a/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs index bd8abe1f2..864ac80b3 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs @@ -8,14 +8,14 @@ namespace ImageSharp.Tests.TestUtilities using System.Reflection; /// - /// Triggers passing instances which produce an image of size width * height filled with the requested color. - /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter /// public class WithSolidFilledImagesAttribute : WithBlankImagesAttribute { /// - /// Triggers passing instances which produce an image of size width * height filled with the requested color. - /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter /// /// The width of the requested image /// The height of the requested image @@ -37,8 +37,8 @@ namespace ImageSharp.Tests.TestUtilities } /// - /// Triggers passing instances which produce an image of size width * height filled with the requested color. - /// One instance will be passed for each the pixel format defined by the pixelTypes parameter + /// Triggers passing instances which produce an image of size width * height filled with the requested color. + /// One instance will be passed for each the pixel format defined by the pixelTypes parameter /// /// The width of the requested image /// The height of the requested image From 67487f0c18e523ef905c0aea997e7f06aeb10209 Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Fri, 23 Dec 2016 05:25:08 +0100 Subject: [PATCH 4/6] introduced PixelTypes.ColorWithDefaultImageClass --- .../ImageSharp.Tests/Formats/Jpg/JpegTests.cs | 6 +- .../Image/PixelAccessorTests.cs | 4 +- tests/ImageSharp.Tests/TestFile.cs | 21 +-- .../TestUtilities/GenericFactory.cs | 41 ++++++ .../TestUtilities/ImageDataAttributeBase.cs | 10 +- .../TestUtilities/PixelTypes.cs | 7 +- .../TestUtilities/TestImageProvider.cs | 138 +++++++++++------- .../TestUtilities/TestUtilityExtensions.cs | 39 +++-- ...toryTests.cs => TestImageProviderTests.cs} | 53 +++++-- .../Tests/TestUtilityExtensionsTests.cs | 44 ++++-- .../WithMemberFactoryAttribute.cs | 9 +- 11 files changed, 248 insertions(+), 124 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs rename tests/ImageSharp.Tests/TestUtilities/Tests/{TestImageFactoryTests.cs => TestImageProviderTests.cs} (77%) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs index d02c2d76e..d9dafe81c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs @@ -26,7 +26,7 @@ namespace ImageSharp.Tests.Formats.Jpg // 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.Argb)] + [WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb)] public void OpenJpeg_SaveBmp(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { @@ -39,8 +39,8 @@ namespace ImageSharp.Tests.Formats.Jpg public static IEnumerable AllBmpFiles => TestImages.Bmp.All; [Theory] - [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio420, 75)] - [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio444, 75)] + [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb, JpegSubsample.Ratio420, 75)] + [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb, JpegSubsample.Ratio444, 75)] public void OpenBmp_SaveJpeg(TestImageProvider provider, JpegSubsample subSample, int quality) where TColor : struct, IPackedPixel, IEquatable { diff --git a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs index 409184303..70573e4f6 100644 --- a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs +++ b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs @@ -17,10 +17,10 @@ namespace ImageSharp.Tests /// public class PixelAccessorTests { - public static Image CreateTestImage() + public static Image CreateTestImage(GenericFactory factory) where TColor : struct, IPackedPixel, IEquatable { - Image image = new Image(10, 10); + Image image = factory.CreateImage(10, 10); using (var pixels = image.Lock()) { diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index 5325f7db5..aab7c2620 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -27,28 +27,19 @@ namespace ImageSharp.Tests private readonly Image image; private readonly string file; - private TestFile(string file, bool decodeImage) + private TestFile(string file) { this.file = file; this.Bytes = File.ReadAllBytes(file); - if (decodeImage) - { - this.image = new Image(this.Bytes); - } - + this.image = new Image(this.Bytes); } - - public static TestFile Create(string file) => CreateImpl(file, true); - - // No need to decode the image when used by TestImageProvider! - internal static TestFile CreateWithoutImage(string file) => CreateImpl(file, false); - private static TestFile CreateImpl(string file, bool decodeImage) + public static TestFile Create(string file) { return cache.GetOrAdd(file, (string fileName) => { - return new TestFile(FormatsDirectory + fileName, decodeImage); + return new TestFile(FormatsDirectory + fileName); }); } @@ -82,10 +73,6 @@ namespace ImageSharp.Tests public Image CreateImage() { - if (this.image == null) - { - throw new InvalidOperationException("TestFile.CreateImage() is invalid because instance has been created with decodeImage = false!"); - } return new Image(this.image); } } diff --git a/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs b/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs new file mode 100644 index 000000000..6b4d8ecc7 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs @@ -0,0 +1,41 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests.TestUtilities +{ + using System; + + /// + /// Utility class to create specialized subclasses generic classes (eg. ) + /// + public class GenericFactory + where TColor : struct, IPackedPixel, IEquatable + { + public virtual Image CreateImage(int width, int height) + { + return new Image(width, height); + } + + public virtual Image CreateImage(byte[] bytes) + { + return new Image(bytes); + } + + public virtual PixelArea CreatePixelArea(int width, int height, ComponentOrder componentOrder) + { + return new PixelArea(width, height, componentOrder); + } + } + + 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); + + public override PixelArea CreatePixelArea(int width, int height, ComponentOrder componentOrder) + => new PixelArea(width, height, componentOrder); + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs index fc27f32a0..094a66149 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs @@ -6,6 +6,7 @@ namespace ImageSharp.Tests.TestUtilities { using System; using System.Collections.Generic; + using System.Diagnostics; using System.Linq; using System.Reflection; @@ -35,15 +36,16 @@ namespace ImageSharp.Tests.TestUtilities } else { - foreach (var pixelType in this.PixelTypes.ToTypes()) + foreach (var kv in this.PixelTypes.ExpandAllTypes()) { - var factoryType = typeof(TestImageProvider<>).MakeGenericType(pixelType); + var factoryType = typeof(TestImageProvider<>).MakeGenericType(kv.Value); foreach (object[] originalFacoryMethodArgs in this.GetAllFactoryMethodArgs(testMethod, factoryType)) { - var actualFactoryMethodArgs = new object[originalFacoryMethodArgs.Length + 1]; + var actualFactoryMethodArgs = new object[originalFacoryMethodArgs.Length + 2]; Array.Copy(originalFacoryMethodArgs, actualFactoryMethodArgs, originalFacoryMethodArgs.Length); - actualFactoryMethodArgs[actualFactoryMethodArgs.Length - 1] = testMethod; + actualFactoryMethodArgs[actualFactoryMethodArgs.Length - 2] = testMethod; + actualFactoryMethodArgs[actualFactoryMethodArgs.Length - 1] = kv.Key; var factory = factoryType.GetMethod(this.GetFactoryMethodName(testMethod)) .Invoke(null, actualFactoryMethodArgs); diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs index 627715679..5ddc8cbcc 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -13,7 +13,7 @@ namespace ImageSharp.Tests.TestUtilities [Flags] public enum PixelTypes : uint { - None = 0, + Undefined = 0, Alpha8 = 1 << 0, @@ -48,6 +48,11 @@ namespace ImageSharp.Tests.TestUtilities Short2 = 1 << 15, Short4 = 1 << 16, + + /// + /// Triggers instantiating the subclass of + /// + ColorWithDefaultImageClass = 1 << 29, // TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs index 5664c7595..5c58751ea 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs @@ -16,10 +16,7 @@ namespace ImageSharp.Tests.TestUtilities public abstract class TestImageProvider : ITestImageFactory where TColor : struct, IPackedPixel, IEquatable { - /// - /// Returns an instance to the test case with the necessary traits. - /// - public abstract Image GetImage(); + public PixelTypes PixelType { get; private set; } = typeof(TColor).GetPixelType(); public virtual string SourceFileOrDescription => ""; @@ -28,8 +25,60 @@ namespace ImageSharp.Tests.TestUtilities /// public ImagingTestCaseUtility Utility { get; private set; } - protected virtual TestImageProvider InitUtility(MethodInfo testMethod) + public GenericFactory Factory { get; private set; } = new GenericFactory(); + + public static TestImageProvider Blank( + int width, + int height, + MethodInfo testMethod = null, + PixelTypes pixelTypeOverride = PixelTypes.Undefined) + => new BlankProvider(width, height).Init(testMethod, pixelTypeOverride); + + public static TestImageProvider File( + string filePath, + MethodInfo testMethod = null, + PixelTypes pixelTypeOverride = PixelTypes.Undefined) + { + return new FileProvider(filePath).Init(testMethod, pixelTypeOverride); + } + + public static TestImageProvider Lambda( + Func, Image> func, + MethodInfo testMethod = null, + PixelTypes pixelTypeOverride = PixelTypes.Undefined) + => new LambdaProvider(func).Init(testMethod, pixelTypeOverride); + + public static TestImageProvider Solid( + int width, + int height, + byte r, + byte g, + byte b, + byte a = 255, + MethodInfo testMethod = null, + PixelTypes pixelTypeOverride = PixelTypes.Undefined) + { + return new SolidProvider(width, height, r, g, b, a).Init(testMethod, pixelTypeOverride); + } + + /// + /// 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) + { + this.PixelType = pixelTypeOverride; + } + + if (pixelTypeOverride == PixelTypes.ColorWithDefaultImageClass) + { + this.Factory = new DefaultImageClassSpecificFactory() as GenericFactory; + } + + this.Utility = new ImagingTestCaseUtility() { SourceFileOrDescription = this.SourceFileOrDescription, @@ -46,10 +95,6 @@ namespace ImageSharp.Tests.TestUtilities private class BlankProvider : TestImageProvider { - protected int Width { get; } - - protected int Height { get; } - public BlankProvider(int width, int height) { this.Width = width; @@ -58,28 +103,13 @@ namespace ImageSharp.Tests.TestUtilities public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; - public override Image GetImage() => new Image(this.Width, this.Height); - } - - public static TestImageProvider Blank(int width, int height, MethodInfo testMethod = null) - => new BlankProvider(width, height).InitUtility(testMethod); - - private class LambdaProvider : TestImageProvider - { - private readonly Func> creator; + protected int Height { get; } - public LambdaProvider(Func> creator) - { - this.creator = creator; - } + protected int Width { get; } - public override Image GetImage() => this.creator(); + public override Image GetImage() => this.Factory.CreateImage(this.Width, this.Height); } - public static TestImageProvider Lambda( - Func> func, - MethodInfo testMethod = null) => new LambdaProvider(func).InitUtility(testMethod); - private class FileProvider : TestImageProvider { private static ConcurrentDictionary> cache = @@ -100,37 +130,35 @@ namespace ImageSharp.Tests.TestUtilities this.filePath, fn => { - var testFile = TestFile.CreateWithoutImage(this.filePath); - return new Image(testFile.Bytes); + var testFile = TestFile.Create(this.filePath); + return this.Factory.CreateImage(testFile.Bytes); }); return new Image(cachedImage); } } - public static TestImageProvider File(string filePath, MethodInfo testMethod = null) + private class LambdaProvider : TestImageProvider { - return new FileProvider(filePath).InitUtility(testMethod); + 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 r; - - private readonly byte g; + private readonly byte a; private readonly byte b; - private readonly byte a; - - public override Image GetImage() - { - var image = base.GetImage(); - TColor color = default(TColor); - color.PackFromBytes(this.r, this.g, this.b, this.a); + private readonly byte g; - return image.Fill(color); - } + private readonly byte r; public SolidProvider(int width, int height, byte r, byte g, byte b, byte a) : base(width, height) @@ -140,18 +168,18 @@ namespace ImageSharp.Tests.TestUtilities this.b = b; this.a = a; } - } - public static TestImageProvider Solid( - int width, - int height, - byte r, - byte g, - byte b, - byte a = 255, - MethodInfo testMethod = null) - { - return new SolidProvider(width, height, r, g, b, a).InitUtility(testMethod); + 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); + } } } @@ -161,4 +189,4 @@ namespace ImageSharp.Tests.TestUtilities public interface ITestImageFactory { } -} +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs index b4d8bcb32..670db96d9 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -2,6 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // + namespace ImageSharp.Tests.TestUtilities { using System; @@ -15,27 +16,32 @@ namespace ImageSharp.Tests.TestUtilities /// public static class TestUtilityExtensions { + private static readonly Dictionary ClrTypes2PixelTypes = new Dictionary(); + private static readonly Assembly ImageSharpAssembly = typeof(Color).GetTypeInfo().Assembly; private static readonly Dictionary PixelTypes2ClrTypes = new Dictionary(); - - private static readonly PixelTypes[] PixelTypesExpanded = - FlagsHelper.GetSortedValues().Where(t => t != PixelTypes.All && t != PixelTypes.None).ToArray(); + + private static readonly PixelTypes[] AllConcretePixelTypes = FlagsHelper + .GetSortedValues() + .Except(new [] {PixelTypes.Undefined, PixelTypes.All }) + .ToArray(); static TestUtilityExtensions() { - Assembly assembly = typeof(Color).GetTypeInfo().Assembly; string nameSpace = typeof(Color).FullName; nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Color).Name.Length - 1); - foreach (PixelTypes pt in PixelTypesExpanded) + foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.ColorWithDefaultImageClass)) { string typeName = $"{nameSpace}.{FlagsHelper.ToString(pt)}"; - var t = assembly.GetType(typeName); + var t = ImageSharpAssembly.GetType(typeName); if (t != null) { PixelTypes2ClrTypes[pt] = t; + ClrTypes2PixelTypes[t] = pt; } } + PixelTypes2ClrTypes[PixelTypes.ColorWithDefaultImageClass] = typeof(Color); } public static Type GetPackedType(Type pixelType) @@ -46,13 +52,10 @@ namespace ImageSharp.Tests.TestUtilities return intrfcType.GetGenericArguments().Single(); } - + public static bool HasFlag(this PixelTypes pixelTypes, PixelTypes flag) => (pixelTypes & flag) == flag; - public static bool IsEquivalentTo( - this Image a, - Image b, - bool compareAlpha = true) + public static bool IsEquivalentTo(this Image a, Image b, bool compareAlpha = true) where TColor : struct, IPackedPixel, IEquatable { if (a.Width != b.Width || a.Height != b.Height) @@ -101,19 +104,23 @@ namespace ImageSharp.Tests.TestUtilities public static Type ToType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType]; - public static IEnumerable ToTypes(this PixelTypes pixelTypes) + public static PixelTypes GetPixelType(this Type colorStructClrType) => ClrTypes2PixelTypes[colorStructClrType]; + + public static IEnumerable> ExpandAllTypes(this PixelTypes pixelTypes) { - if (pixelTypes == PixelTypes.None) + if (pixelTypes == PixelTypes.Undefined) { - return Enumerable.Empty(); + return Enumerable.Empty>(); } else if (pixelTypes == PixelTypes.All) { // TODO: Need to return unknown types here without forcing CLR to load all types in ImageSharp assembly - return PixelTypes2ClrTypes.Values; + return PixelTypes2ClrTypes; } - return PixelTypesExpanded.Where(pt => pixelTypes.HasFlag(pt)).Select(pt => pt.ToType()); + return AllConcretePixelTypes + .Where(pt => pixelTypes.HasFlag(pt)) + .Select(pt => new KeyValuePair(pt, pt.ToType())); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageFactoryTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs similarity index 77% rename from tests/ImageSharp.Tests/TestUtilities/Tests/TestImageFactoryTests.cs rename to tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 1cc8ac35b..8a7985b38 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageFactoryTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -4,6 +4,7 @@ // // ReSharper disable InconsistentNaming + namespace ImageSharp.Tests.TestUtilities.Tests { using System; @@ -11,22 +12,18 @@ namespace ImageSharp.Tests.TestUtilities.Tests using Xunit; using Xunit.Abstractions; - public class TestImageFactoryTests + public class TestImageProviderTests { - - public TestImageFactoryTests(ITestOutputHelper output) + public TestImageProviderTests(ITestOutputHelper output) { this.Output = output; } private ITestOutputHelper Output { get; } - [Theory] [WithBlankImages(42, 666, PixelTypes.Color | PixelTypes.Argb | PixelTypes.HalfSingle, "hello")] - public void Use_WithEmptyImageAttribute( - TestImageProvider provider, - string message) + public void Use_WithEmptyImageAttribute(TestImageProvider provider, string message) where TColor : struct, IPackedPixel, IEquatable { var img = provider.GetImage(); @@ -50,6 +47,27 @@ namespace ImageSharp.Tests.TestUtilities.Tests Assert.Equal("hello", message); } + [Theory] + [WithBlankImages(1, 1, PixelTypes.Color, PixelTypes.Color)] + [WithBlankImages(1, 1, PixelTypes.Alpha8, PixelTypes.Alpha8)] + [WithBlankImages(1, 1, PixelTypes.ColorWithDefaultImageClass, PixelTypes.ColorWithDefaultImageClass)] + public void PixelType_PropertyValueIsCorrect(TestImageProvider provider, PixelTypes expected) + where TColor : struct, IPackedPixel, IEquatable + { + Assert.Equal(expected, provider.PixelType); + } + + [Theory] + [WithBlankImages(1, 1, PixelTypes.ColorWithDefaultImageClass)] + public void PixelTypes_ColorWithDefaultImageClass_TriggersCreatingTheNonGenericDerivedImageClass( + TestImageProvider provider) + where TColor : struct, IPackedPixel, IEquatable + { + var img = provider.GetImage(); + + Assert.IsType(img); + } + // TODO: @dlemstra this works only with constant strings! [Theory] [WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)] @@ -78,7 +96,7 @@ namespace ImageSharp.Tests.TestUtilities.Tests var image = provider.GetImage(); provider.Utility.SaveTestOutputFile(image, "png"); } - + [Theory] [WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Color | PixelTypes.Argb)] public void Use_WithSolidFilledImagesAttribute(TestImageProvider provider) @@ -107,21 +125,31 @@ namespace ImageSharp.Tests.TestUtilities.Tests } } - public static Image TestMemberFactory() + /// + /// Need to us to create instance of when pixelType is ColorWithDefaultImageClass + /// + /// + /// + /// + public static Image CreateTestImage(GenericFactory factory) where TColor : struct, IPackedPixel, IEquatable { - return new Image(3, 3); + return factory.CreateImage(3, 3); } [Theory] - [WithMemberFactory(nameof(TestMemberFactory), PixelTypes.All)] + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All)] public void Use_WithMemberFactoryAttribute(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { var img = provider.GetImage(); Assert.Equal(img.Width, 3); - } + if (provider.PixelType == PixelTypes.ColorWithDefaultImageClass) + { + Assert.IsType(img); + } + } public static readonly TheoryData BasicData = new TheoryData() { @@ -131,7 +159,6 @@ namespace ImageSharp.Tests.TestUtilities.Tests 20) }; - [Theory] [MemberData(nameof(BasicData))] public void Blank_MemberData(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 948479d7a..7d8d0cc6b 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Tests.TestUtilities.Tests { using System; + using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Reflection; @@ -23,10 +24,10 @@ namespace ImageSharp.Tests.TestUtilities.Tests private ITestOutputHelper Output { get; } - public static Image CreateTestImage() + public static Image CreateTestImage(GenericFactory factory) where TColor : struct, IPackedPixel, IEquatable { - Image image = new Image(10, 10); + Image image = factory.CreateImage(10, 10); using (var pixels = image.Lock()) { @@ -93,31 +94,52 @@ namespace ImageSharp.Tests.TestUtilities.Tests [InlineData(PixelTypes.Color, typeof(Color))] [InlineData(PixelTypes.Argb, typeof(Argb))] [InlineData(PixelTypes.HalfVector4, typeof(HalfVector4))] + [InlineData(PixelTypes.ColorWithDefaultImageClass, typeof(Color))] public void ToType(PixelTypes pt, Type expectedType) { Assert.Equal(pt.ToType(), expectedType); } + [Theory] + [InlineData(typeof(Color), PixelTypes.Color)] + [InlineData(typeof(Argb), PixelTypes.Argb)] + public void GetPixelType(Type clrType, PixelTypes expectedPixelType) + { + Assert.Equal(expectedPixelType, clrType.GetPixelType()); + } + + private static void AssertContainsPixelType( + PixelTypes pt, + IEnumerable> pixelTypesExp) + { + Assert.Contains(new KeyValuePair(pt, typeof(T)), pixelTypesExp); + + } + [Fact] public void ToTypes() { - PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.Color | PixelTypes.HalfVector2; + PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.Color | PixelTypes.HalfVector2 | PixelTypes.ColorWithDefaultImageClass; - var clrTypes = pixelTypes.ToTypes().ToArray(); + var expanded = pixelTypes.ExpandAllTypes(); - Assert.Equal(clrTypes.Length, 4); - Assert.Contains(typeof(Alpha8), clrTypes); - Assert.Contains(typeof(Bgr565), clrTypes); - Assert.Contains(typeof(Color), clrTypes); - Assert.Contains(typeof(HalfVector2), clrTypes); + Assert.Equal(expanded.Count(), 5); + + AssertContainsPixelType(PixelTypes.Alpha8, expanded); + AssertContainsPixelType(PixelTypes.Bgr565, expanded); + AssertContainsPixelType(PixelTypes.Color, expanded); + AssertContainsPixelType(PixelTypes.HalfVector2, expanded); + AssertContainsPixelType(PixelTypes.ColorWithDefaultImageClass, expanded); } [Fact] public void ToTypes_All() { - var clrTypes = PixelTypes.All.ToTypes().ToArray(); + var expanded = PixelTypes.All.ExpandAllTypes().ToArray(); - Assert.True(clrTypes.Length >= FlagsHelper.GetSortedValues().Length - 2); + Assert.True(expanded.Length >= FlagsHelper.GetSortedValues().Length - 2); + AssertContainsPixelType(PixelTypes.Color, expanded); + AssertContainsPixelType(PixelTypes.ColorWithDefaultImageClass, expanded); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs index 3860880c3..598b01b45 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs @@ -6,6 +6,7 @@ namespace ImageSharp.Tests.TestUtilities { using System; + using System.Linq; using System.Reflection; /// @@ -34,8 +35,12 @@ namespace ImageSharp.Tests.TestUtilities var m = testMethod.DeclaringType.GetMethod(this.memberMethodName); var args = factoryType.GetGenericArguments(); - var imgType = typeof(Image<>).MakeGenericType(args); - var funcType = typeof(Func<>).MakeGenericType(imgType); + var colorType = args.Single(); + + var imgType = typeof(Image<>).MakeGenericType(colorType); + var genericFactoryType = (typeof(GenericFactory<>)).MakeGenericType(colorType); + + var funcType = typeof(Func<,>).MakeGenericType(genericFactoryType, imgType); var genericMethod = m.MakeGenericMethod(args); From 799c00a639879b8b03c88b644b7bff7afcb68a46 Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Fri, 23 Dec 2016 05:40:51 +0100 Subject: [PATCH 5/6] the folder TestUtilities is no longer a namespace provider --- tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs | 6 ++---- tests/ImageSharp.Tests/Image/PixelAccessorTests.cs | 2 -- tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs | 2 +- .../TestUtilities/GenericFactory.cs | 13 +++---------- .../TestUtilities/ImageDataAttributeBase.cs | 4 ++-- .../TestUtilities/ImagingTestCaseUtility.cs | 2 +- tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs | 2 +- .../TestUtilities/TestImageProvider.cs | 2 +- .../TestUtilities/TestUtilityExtensions.cs | 2 +- .../TestUtilities/Tests/TestImageProviderTests.cs | 2 +- .../Tests/TestUtilityExtensionsTests.cs | 2 +- .../TestUtilities/WithBlankImageAttribute.cs | 2 +- .../TestUtilities/WithFileAttribute.cs | 2 +- .../TestUtilities/WithFileCollectionAttribute.cs | 2 +- .../TestUtilities/WithMemberFactoryAttribute.cs | 3 ++- .../TestUtilities/WithSolidFilledImagesAttribute.cs | 2 +- 16 files changed, 20 insertions(+), 30 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs index d9dafe81c..7cbd6cedb 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs @@ -6,10 +6,8 @@ using ImageSharp.Formats; using Xunit; using Xunit.Abstractions; -namespace ImageSharp.Tests.Formats.Jpg +namespace ImageSharp.Tests { - using ImageSharp.Tests.TestUtilities; - public class JpegTests { @@ -19,7 +17,7 @@ namespace ImageSharp.Tests.Formats.Jpg public JpegTests(ITestOutputHelper output) { - Output = output; + this.Output = output; } public static IEnumerable AllJpegFiles => TestImages.Jpeg.All; diff --git a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs index 70573e4f6..fbba55815 100644 --- a/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs +++ b/tests/ImageSharp.Tests/Image/PixelAccessorTests.cs @@ -8,8 +8,6 @@ namespace ImageSharp.Tests using System; using System.Numerics; - using ImageSharp.Tests.TestUtilities; - using Xunit; /// diff --git a/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs b/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs index 858989b20..6c2589b82 100644 --- a/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs +++ b/tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs @@ -2,7 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Collections.Generic; diff --git a/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs b/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs index 6b4d8ecc7..92fe8e16c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs @@ -3,12 +3,13 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; /// - /// Utility class to create specialized subclasses generic classes (eg. ) + /// Utility class to create specialized subclasses of generic classes (eg. ) + /// Used as parameter for -based factory methods /// public class GenericFactory where TColor : struct, IPackedPixel, IEquatable @@ -22,11 +23,6 @@ namespace ImageSharp.Tests.TestUtilities { return new Image(bytes); } - - public virtual PixelArea CreatePixelArea(int width, int height, ComponentOrder componentOrder) - { - return new PixelArea(width, height, componentOrder); - } } public class DefaultImageClassSpecificFactory : GenericFactory @@ -34,8 +30,5 @@ namespace ImageSharp.Tests.TestUtilities public override Image CreateImage(byte[] bytes) => new Image(bytes); public override Image CreateImage(int width, int height) => new Image(width, height); - - public override PixelArea CreatePixelArea(int width, int height, ComponentOrder componentOrder) - => new PixelArea(width, height, componentOrder); } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs index 094a66149..18600085d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs @@ -2,7 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Collections.Generic; @@ -13,7 +13,7 @@ namespace ImageSharp.Tests.TestUtilities using Xunit.Sdk; /// - /// Base class for Theory Data attributes which pass an instance of to the test cases. + /// Base class for Theory Data attributes which pass an instance of to the test case. /// public abstract class ImageDataAttributeBase : DataAttribute { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs index 8225e08ef..e2b885034 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs @@ -2,7 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.IO; diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs index 5ddc8cbcc..75b9030ed 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -2,7 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs index 5c58751ea..9d17c32e6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Collections.Concurrent; diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs index 670db96d9..3be70dc1f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Collections.Generic; diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 8a7985b38..ab491c38f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -5,7 +5,7 @@ // ReSharper disable InconsistentNaming -namespace ImageSharp.Tests.TestUtilities.Tests +namespace ImageSharp.Tests.Tests { using System; diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 7d8d0cc6b..9375aabaa 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -4,7 +4,7 @@ // // ReSharper disable InconsistentNaming -namespace ImageSharp.Tests.TestUtilities.Tests +namespace ImageSharp.Tests.Tests { using System; using System.Collections.Generic; diff --git a/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs index 9a4b03636..e1f8f4c55 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Reflection; diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs index 49cbeab5e..617a9a237 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Reflection; diff --git a/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs index bd80ae66b..bca69eea7 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs @@ -2,7 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Collections.Generic; diff --git a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs index 598b01b45..e658e817d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Linq; @@ -12,6 +12,7 @@ namespace ImageSharp.Tests.TestUtilities /// /// Triggers passing instances which return the image produced by the given test class member method /// instances will be passed for each the pixel format defined by the pixelTypes parameter + /// The parameter of the factory method must be a instance /// public class WithMemberFactoryAttribute : ImageDataAttributeBase { diff --git a/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs b/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs index 864ac80b3..46f18e08c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs +++ b/tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs @@ -2,7 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.Tests.TestUtilities +namespace ImageSharp.Tests { using System; using System.Reflection; From 7818a9ce4805b23878bb15f755456d760932f758 Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Fri, 23 Dec 2016 15:23:22 +0100 Subject: [PATCH 6/6] fixed IsEquivalentTo, renamed ColorWithDefaultImageClass to StandardImageClass --- .../ImageSharp.Tests/Formats/Jpg/JpegTests.cs | 6 ++--- .../TestUtilities/PixelTypes.cs | 2 +- .../TestUtilities/TestImageProvider.cs | 2 +- .../TestUtilities/TestUtilityExtensions.cs | 11 +++++++-- .../Tests/TestImageProviderTests.cs | 10 ++++---- .../Tests/TestUtilityExtensionsTests.cs | 24 ++++++++++--------- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs index 7cbd6cedb..85bc18d11 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs @@ -24,7 +24,7 @@ namespace ImageSharp.Tests // 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.ColorWithDefaultImageClass | PixelTypes.Argb)] + [WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.StandardImageClass | PixelTypes.Argb)] public void OpenJpeg_SaveBmp(TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable { @@ -37,8 +37,8 @@ namespace ImageSharp.Tests public static IEnumerable AllBmpFiles => TestImages.Bmp.All; [Theory] - [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb, JpegSubsample.Ratio420, 75)] - [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb, JpegSubsample.Ratio444, 75)] + [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.StandardImageClass | PixelTypes.Argb, JpegSubsample.Ratio420, 75)] + [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.StandardImageClass | PixelTypes.Argb, JpegSubsample.Ratio444, 75)] public void OpenBmp_SaveJpeg(TestImageProvider provider, JpegSubsample subSample, int quality) where TColor : struct, IPackedPixel, IEquatable { diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs index 75b9030ed..dd1460678 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -52,7 +52,7 @@ namespace ImageSharp.Tests /// /// Triggers instantiating the subclass of /// - ColorWithDefaultImageClass = 1 << 29, + StandardImageClass = 1 << 29, // TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs index 9d17c32e6..453e03af2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs @@ -73,7 +73,7 @@ namespace ImageSharp.Tests this.PixelType = pixelTypeOverride; } - if (pixelTypeOverride == PixelTypes.ColorWithDefaultImageClass) + if (pixelTypeOverride == PixelTypes.StandardImageClass) { this.Factory = new DefaultImageClassSpecificFactory() as GenericFactory; } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs index 3be70dc1f..a4e0a959a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -31,7 +31,7 @@ namespace ImageSharp.Tests { string nameSpace = typeof(Color).FullName; nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Color).Name.Length - 1); - foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.ColorWithDefaultImageClass)) + foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.StandardImageClass)) { string typeName = $"{nameSpace}.{FlagsHelper.ToString(pt)}"; var t = ImageSharpAssembly.GetType(typeName); @@ -41,7 +41,7 @@ namespace ImageSharp.Tests ClrTypes2PixelTypes[t] = pt; } } - PixelTypes2ClrTypes[PixelTypes.ColorWithDefaultImageClass] = typeof(Color); + PixelTypes2ClrTypes[PixelTypes.StandardImageClass] = typeof(Color); } public static Type GetPackedType(Type pixelType) @@ -88,6 +88,13 @@ namespace ImageSharp.Tests { ca.ToBytes(bytesA, 0, ComponentOrder.XYZ); cb.ToBytes(bytesB, 0, ComponentOrder.XYZ); + + if (bytesA[0] != bytesB[0] || + bytesA[1] != bytesB[1] || + bytesA[2] != bytesB[2]) + { + return false; + } } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index ab491c38f..974fe35cc 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -5,7 +5,7 @@ // ReSharper disable InconsistentNaming -namespace ImageSharp.Tests.Tests +namespace ImageSharp.Tests { using System; @@ -50,7 +50,7 @@ namespace ImageSharp.Tests.Tests [Theory] [WithBlankImages(1, 1, PixelTypes.Color, PixelTypes.Color)] [WithBlankImages(1, 1, PixelTypes.Alpha8, PixelTypes.Alpha8)] - [WithBlankImages(1, 1, PixelTypes.ColorWithDefaultImageClass, PixelTypes.ColorWithDefaultImageClass)] + [WithBlankImages(1, 1, PixelTypes.StandardImageClass, PixelTypes.StandardImageClass)] public void PixelType_PropertyValueIsCorrect(TestImageProvider provider, PixelTypes expected) where TColor : struct, IPackedPixel, IEquatable { @@ -58,7 +58,7 @@ namespace ImageSharp.Tests.Tests } [Theory] - [WithBlankImages(1, 1, PixelTypes.ColorWithDefaultImageClass)] + [WithBlankImages(1, 1, PixelTypes.StandardImageClass)] public void PixelTypes_ColorWithDefaultImageClass_TriggersCreatingTheNonGenericDerivedImageClass( TestImageProvider provider) where TColor : struct, IPackedPixel, IEquatable @@ -126,7 +126,7 @@ namespace ImageSharp.Tests.Tests } /// - /// Need to us to create instance of when pixelType is ColorWithDefaultImageClass + /// Need to us to create instance of when pixelType is StandardImageClass /// /// /// @@ -144,7 +144,7 @@ namespace ImageSharp.Tests.Tests { var img = provider.GetImage(); Assert.Equal(img.Width, 3); - if (provider.PixelType == PixelTypes.ColorWithDefaultImageClass) + if (provider.PixelType == PixelTypes.StandardImageClass) { Assert.IsType(img); } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 9375aabaa..371934127 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -4,7 +4,7 @@ // // ReSharper disable InconsistentNaming -namespace ImageSharp.Tests.Tests +namespace ImageSharp.Tests { using System; using System.Collections.Generic; @@ -68,33 +68,35 @@ namespace ImageSharp.Tests.Tests } [Theory] - [WithFile(TestImages.Bmp.Car, PixelTypes.Color)] - public void IsEquivalentTo_WhenFalse(TestImageProvider provider) + [WithFile(TestImages.Bmp.Car, PixelTypes.Color, true)] + [WithFile(TestImages.Bmp.Car, PixelTypes.Color, false)] + public void IsEquivalentTo_WhenFalse(TestImageProvider provider, bool compareAlpha) where TColor : struct, IPackedPixel, IEquatable { var a = provider.GetImage(); var b = provider.GetImage(); b = b.OilPaint(3, 2); - Assert.False(a.IsEquivalentTo(b)); + Assert.False(a.IsEquivalentTo(b, compareAlpha)); } [Theory] - [WithMemberFactory(nameof(CreateTestImage), PixelTypes.Color | PixelTypes.Bgr565)] - public void IsEquivalentTo_WhenTrue(TestImageProvider provider) + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.Color | PixelTypes.Bgr565, true)] + [WithMemberFactory(nameof(CreateTestImage), PixelTypes.Color | PixelTypes.Bgr565, false)] + public void IsEquivalentTo_WhenTrue(TestImageProvider provider, bool compareAlpha) where TColor : struct, IPackedPixel, IEquatable { var a = provider.GetImage(); var b = provider.GetImage(); - Assert.True(a.IsEquivalentTo(b)); + Assert.True(a.IsEquivalentTo(b, compareAlpha)); } [Theory] [InlineData(PixelTypes.Color, typeof(Color))] [InlineData(PixelTypes.Argb, typeof(Argb))] [InlineData(PixelTypes.HalfVector4, typeof(HalfVector4))] - [InlineData(PixelTypes.ColorWithDefaultImageClass, typeof(Color))] + [InlineData(PixelTypes.StandardImageClass, typeof(Color))] public void ToType(PixelTypes pt, Type expectedType) { Assert.Equal(pt.ToType(), expectedType); @@ -119,7 +121,7 @@ namespace ImageSharp.Tests.Tests [Fact] public void ToTypes() { - PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.Color | PixelTypes.HalfVector2 | PixelTypes.ColorWithDefaultImageClass; + PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.Color | PixelTypes.HalfVector2 | PixelTypes.StandardImageClass; var expanded = pixelTypes.ExpandAllTypes(); @@ -129,7 +131,7 @@ namespace ImageSharp.Tests.Tests AssertContainsPixelType(PixelTypes.Bgr565, expanded); AssertContainsPixelType(PixelTypes.Color, expanded); AssertContainsPixelType(PixelTypes.HalfVector2, expanded); - AssertContainsPixelType(PixelTypes.ColorWithDefaultImageClass, expanded); + AssertContainsPixelType(PixelTypes.StandardImageClass, expanded); } [Fact] @@ -139,7 +141,7 @@ namespace ImageSharp.Tests.Tests Assert.True(expanded.Length >= FlagsHelper.GetSortedValues().Length - 2); AssertContainsPixelType(PixelTypes.Color, expanded); - AssertContainsPixelType(PixelTypes.ColorWithDefaultImageClass, expanded); + AssertContainsPixelType(PixelTypes.StandardImageClass, expanded); } } } \ No newline at end of file