Browse Source

Moved some of the new files around.

Moved the private classes to separate files.
Removed the FlagsHelper class and only take what we need.
Some refactoring in some of the new classes.
af/merge-core
Dirk Lemstra 9 years ago
parent
commit
bfc2cb02e5
  1. 24
      tests/ImageSharp.Tests/FileTestBase.cs
  2. 2
      tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs
  3. 32
      tests/ImageSharp.Tests/TestBase.cs
  4. 6
      tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs
  5. 0
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs
  6. 0
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileAttribute.cs
  7. 1
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs
  8. 0
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs
  9. 1
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs
  10. 19
      tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs
  11. 7
      tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs
  12. 14
      tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs
  13. 242
      tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs
  14. 30
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs
  15. 42
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs
  16. 29
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs
  17. 49
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs
  18. 104
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  19. 36
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs
  20. 3
      tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs
  21. 13
      tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs
  22. 7
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs
  23. 3
      tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

24
tests/ImageSharp.Tests/FileTestBase.cs

@ -6,12 +6,11 @@
namespace ImageSharp.Tests
{
using System.Collections.Generic;
using System.IO;
/// <summary>
/// The test base class for reading and writing to files.
/// </summary>
public abstract class FileTestBase
public abstract class FileTestBase : TestBase
{
/// <summary>
/// The collection of image files to test against.
@ -45,26 +44,5 @@ namespace ImageSharp.Tests
// TestFile.Create(TestImages.Gif.Cheers),
// TestFile.Create(TestImages.Gif.Giphy) // Perf: Enable for local testing only
};
// TODO: Find a better place for this
internal const string TestOutputRoot = "TestOutput/";
protected string CreateOutputDirectory(string path, params string[] pathParts)
{
var postFix = "";
if (pathParts != null && pathParts.Length > 0)
{
postFix = "/" + string.Join("/", pathParts);
}
path = TestOutputRoot + path + postFix;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
}
}

2
tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs

@ -21,9 +21,7 @@ namespace ImageSharp.Tests
}
public static IEnumerable<string> AllJpegFiles => TestImages.Jpeg.All;
// TODO: Turned off PixelTypes.All to be CI-friendly, what should be the practice?
[Theory]
//[WithFileCollection(nameof(AllJpegFiles), PixelTypes.All)]
[WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.StandardImageClass | PixelTypes.Argb)]
public void OpenJpeg_SaveBmp<TColor>(TestImageProvider<TColor> provider)
where TColor : struct, IPackedPixel, IEquatable<TColor>

32
tests/ImageSharp.Tests/TestBase.cs

@ -0,0 +1,32 @@
// <copyright file="TestBase.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System.IO;
/// <summary>
/// The test base class.
/// </summary>
public abstract class TestBase
{
protected string CreateOutputDirectory(string path, params string[] pathParts)
{
path = Path.Combine("TestOutput", path);
if (pathParts != null && pathParts.Length > 0)
{
path = Path.Combine(path, Path.Combine(pathParts));
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
}
}

6
tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs → tests/ImageSharp.Tests/TestUtilities/Attributes/ImageDataAttributeBase.cs

@ -2,11 +2,11 @@
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
@ -29,8 +29,8 @@ namespace ImageSharp.Tests
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
var type = testMethod.GetParameters().First().ParameterType;
if (!typeof(ITestImageFactory).IsAssignableFrom(type))
var type = testMethod.GetParameters().First().ParameterType.GetTypeInfo();
if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(TestImageProvider<>))
{
yield return this.AdditionalParameters;
}

0
tests/ImageSharp.Tests/TestUtilities/WithBlankImageAttribute.cs → tests/ImageSharp.Tests/TestUtilities/Attributes/WithBlankImageAttribute.cs

0
tests/ImageSharp.Tests/TestUtilities/WithFileAttribute.cs → tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileAttribute.cs

1
tests/ImageSharp.Tests/TestUtilities/WithFileCollectionAttribute.cs → tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs

@ -2,6 +2,7 @@
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;

0
tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs → tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs

1
tests/ImageSharp.Tests/TestUtilities/WithSolidFilledImagesAttribute.cs → tests/ImageSharp.Tests/TestUtilities/Attributes/WithSolidFilledImagesAttribute.cs

@ -2,6 +2,7 @@
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;

19
tests/ImageSharp.Tests/TestUtilities/EnumHelper.cs

@ -0,0 +1,19 @@
// <copyright file="EnumHelper.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
public class EnumHelper
{
public static T[] GetSortedValues<T>()
{
T[] vals = (T[])Enum.GetValues(typeof(T));
Array.Sort(vals);
return vals;
}
}
}

7
tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs → tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs

@ -24,11 +24,4 @@ namespace ImageSharp.Tests
return new Image<TColor>(bytes);
}
}
public class DefaultImageClassSpecificFactory : GenericFactory<Color>
{
public override Image<Color> CreateImage(byte[] bytes) => new Image(bytes);
public override Image<Color> CreateImage(int width, int height) => new Image(width, height);
}
}

14
tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs

@ -0,0 +1,14 @@
// <copyright file="ImageFactory.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
public class ImageFactory : GenericFactory<Color>
{
public override Image<Color> CreateImage(byte[] bytes) => new Image(bytes);
public override Image<Color> CreateImage(int width, int height) => new Image(width, height);
}
}

242
tests/ImageSharp.Tests/TestUtilities/FlagsHelper.cs

@ -1,242 +0,0 @@
// <copyright file="FlagsHelper.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// Helper class for flag manipulation, based on
/// <see>
/// <cref>http://www.codeproject.com/KB/dotnet/enum.aspx</cref>
/// </see>
/// </summary>
/// <typeparam name="T">Must be enum type (declared using <c>enum</c> keyword)</typeparam>
public class FlagsHelper<T>
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));
}
/// <summary>
/// Converts enum value to string
/// </summary>
/// <param name="value">Enum value converted to int</param>
/// <returns>If <paramref name="value"/> is defined, the enum member name; otherwise the string representation of the <paramref name="value"/>.
/// If <see cref="FlagsAttribute"/> is applied, can return comma-separated list of values</returns>
public static string ToString(int value)
{
return Converter.ToStringInternal(value);
}
/// <summary>
/// Converts enum value to string
/// </summary>
/// <param name="value">Enum value</param>
/// <returns>If <paramref name="value"/> is defined, the enum member name; otherwise the string representation of the <paramref name="value"/>.
/// If <see cref="FlagsAttribute"/> is applied, can return comma-separated list of values</returns>
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<int, string> Dic;
protected DictionaryEnumConverter(string[] names, T[] values)
{
this.Dic = new Dictionary<int, string>(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<int, string> 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<int, string> 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;
}
}
}
}

30
tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs

@ -0,0 +1,30 @@
// <copyright file="BlankProvider.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
private class BlankProvider : TestImageProvider<TColor>
{
public BlankProvider(int width, int height)
{
this.Width = width;
this.Height = height;
}
public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}";
protected int Height { get; }
protected int Width { get; }
public override Image<TColor> GetImage() => this.Factory.CreateImage(this.Width, this.Height);
}
}
}

42
tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs

@ -0,0 +1,42 @@
// <copyright file="FileProvider.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using System.Collections.Concurrent;
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
private class FileProvider : TestImageProvider<TColor>
{
private static ConcurrentDictionary<string, Image<TColor>> cache =
new ConcurrentDictionary<string, Image<TColor>>();
private string filePath;
public FileProvider(string filePath)
{
this.filePath = filePath;
}
public override string SourceFileOrDescription => this.filePath;
public override Image<TColor> GetImage()
{
var cachedImage = cache.GetOrAdd(
this.filePath,
fn =>
{
var testFile = TestFile.Create(this.filePath);
return this.Factory.CreateImage(testFile.Bytes);
});
return new Image<TColor>(cachedImage);
}
}
}
}

29
tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs

@ -0,0 +1,29 @@
// <copyright file="LambdaProvider.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
/// <summary>
/// Provides <see cref="Image{TColor}" /> instances for parametric unit tests.
/// </summary>
/// <typeparam name="TColor">The pixel format of the image</typeparam>
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
private class LambdaProvider : TestImageProvider<TColor>
{
private readonly Func<GenericFactory<TColor>, Image<TColor>> creator;
public LambdaProvider(Func<GenericFactory<TColor>, Image<TColor>> creator)
{
this.creator = creator;
}
public override Image<TColor> GetImage() => this.creator(this.Factory);
}
}
}

49
tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs

@ -0,0 +1,49 @@
// <copyright file="SolidProvider.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
/// <summary>
/// Provides <see cref="Image{TColor}" /> instances for parametric unit tests.
/// </summary>
/// <typeparam name="TColor">The pixel format of the image</typeparam>
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
private class SolidProvider : BlankProvider
{
private readonly byte a;
private readonly byte b;
private readonly byte g;
private readonly byte r;
public SolidProvider(int width, int height, byte r, byte g, byte b, byte a)
: base(width, height)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public override string SourceFileOrDescription
=> $"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})";
public override Image<TColor> GetImage()
{
var image = base.GetImage();
TColor color = default(TColor);
color.PackFromBytes(this.r, this.g, this.b, this.a);
return image.Fill(color);
}
}
}
}

104
tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs → tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs

@ -6,14 +6,13 @@
namespace ImageSharp.Tests
{
using System;
using System.Collections.Concurrent;
using System.Reflection;
/// <summary>
/// Provides <see cref="Image{TColor}" /> instances for parametric unit tests.
/// </summary>
/// <typeparam name="TColor">The pixel format of the image</typeparam>
public abstract class TestImageProvider<TColor> : ITestImageFactory
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
public PixelTypes PixelType { get; private set; } = typeof(TColor).GetPixelType();
@ -65,7 +64,7 @@ namespace ImageSharp.Tests
/// Returns an <see cref="Image{TColor}"/> instance to the test case with the necessary traits.
/// </summary>
public abstract Image<TColor> GetImage();
protected TestImageProvider<TColor> Init(MethodInfo testMethod, PixelTypes pixelTypeOverride)
{
if (pixelTypeOverride != PixelTypes.Undefined)
@ -75,10 +74,9 @@ namespace ImageSharp.Tests
if (pixelTypeOverride == PixelTypes.StandardImageClass)
{
this.Factory = new DefaultImageClassSpecificFactory() as GenericFactory<TColor>;
this.Factory = new ImageFactory() as GenericFactory<TColor>;
}
this.Utility = new ImagingTestCaseUtility()
{
SourceFileOrDescription = this.SourceFileOrDescription,
@ -92,101 +90,5 @@ namespace ImageSharp.Tests
return this;
}
private class BlankProvider : TestImageProvider<TColor>
{
public BlankProvider(int width, int height)
{
this.Width = width;
this.Height = height;
}
public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}";
protected int Height { get; }
protected int Width { get; }
public override Image<TColor> GetImage() => this.Factory.CreateImage(this.Width, this.Height);
}
private class FileProvider : TestImageProvider<TColor>
{
private static ConcurrentDictionary<string, Image<TColor>> cache =
new ConcurrentDictionary<string, Image<TColor>>();
private string filePath;
public FileProvider(string filePath)
{
this.filePath = filePath;
}
public override string SourceFileOrDescription => this.filePath;
public override Image<TColor> GetImage()
{
var cachedImage = cache.GetOrAdd(
this.filePath,
fn =>
{
var testFile = TestFile.Create(this.filePath);
return this.Factory.CreateImage(testFile.Bytes);
});
return new Image<TColor>(cachedImage);
}
}
private class LambdaProvider : TestImageProvider<TColor>
{
private readonly Func<GenericFactory<TColor>, Image<TColor>> creator;
public LambdaProvider(Func<GenericFactory<TColor>, Image<TColor>> creator)
{
this.creator = creator;
}
public override Image<TColor> GetImage() => this.creator(this.Factory);
}
private class SolidProvider : BlankProvider
{
private readonly byte a;
private readonly byte b;
private readonly byte g;
private readonly byte r;
public SolidProvider(int width, int height, byte r, byte g, byte b, byte a)
: base(width, height)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public override string SourceFileOrDescription
=> $"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})";
public override Image<TColor> GetImage()
{
var image = base.GetImage();
TColor color = default(TColor);
color.PackFromBytes(this.r, this.g, this.b, this.a);
return image.Fill(color);
}
}
}
/// <summary>
/// Marker
/// </summary>
public interface ITestImageFactory
{
}
}

36
tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

@ -2,6 +2,7 @@
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
@ -15,7 +16,7 @@ namespace ImageSharp.Tests
/// Utility class to provide information about the test image & the test case for the test code,
/// and help managing IO.
/// </summary>
public class ImagingTestCaseUtility
public class ImagingTestCaseUtility : TestBase
{
/// <summary>
/// Name of the TColor in the owner <see cref="TestImageProvider{TColor}"/>
@ -38,20 +39,6 @@ namespace ImageSharp.Tests
/// </summary>
public string TestName { get; set; } = string.Empty;
/// <summary>
/// Root directory for output images
/// </summary>
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;
}
/// <summary>
/// Gets the recommended file name for the output of the test
/// </summary>
@ -81,12 +68,6 @@ namespace ImageSharp.Tests
return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{extension}";
}
private static IImageFormat GetImageFormatByExtension(string extension)
{
extension = extension.ToLower();
return Bootstrapper.ImageFormats.First(f => f.SupportedExtensions.Contains(extension));
}
/// <summary>
/// Encodes image by the format matching the required extension, than saves it to the recommended output file.
/// </summary>
@ -111,5 +92,18 @@ namespace ImageSharp.Tests
this.TestGroupName = method.DeclaringType.Name;
this.TestName = method.Name;
}
private static IImageFormat GetImageFormatByExtension(string extension)
{
extension = extension.ToLower();
return Bootstrapper.ImageFormats.First(f => f.SupportedExtensions.Contains(extension));
}
private string GetTestOutputDir()
{
string testGroupName = Path.GetFileNameWithoutExtension(this.TestGroupName);
return CreateOutputDirectory(testGroupName);
}
}
}

3
tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs

@ -2,12 +2,13 @@
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
/// <summary>
/// Flags that are mapped to PackedPixel types.
/// Flags that are mapped to PackedPixel types.
/// They trigger the desired parametrization for <see cref="TestImageProvider{TColor}"/>.
/// </summary>
[Flags]

13
tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs

@ -22,8 +22,7 @@ namespace ImageSharp.Tests
private static readonly Dictionary<PixelTypes, Type> PixelTypes2ClrTypes = new Dictionary<PixelTypes, Type>();
private static readonly PixelTypes[] AllConcretePixelTypes = FlagsHelper<PixelTypes>
.GetSortedValues()
private static readonly PixelTypes[] AllConcretePixelTypes = EnumHelper.GetSortedValues<PixelTypes>()
.Except(new [] {PixelTypes.Undefined, PixelTypes.All })
.ToArray();
@ -33,13 +32,15 @@ namespace ImageSharp.Tests
nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Color).Name.Length - 1);
foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.StandardImageClass))
{
string typeName = $"{nameSpace}.{FlagsHelper<PixelTypes>.ToString(pt)}";
string typeName = $"{nameSpace}.{pt.ToString()}";
var t = ImageSharpAssembly.GetType(typeName);
if (t != null)
if (t == null)
{
PixelTypes2ClrTypes[pt] = t;
ClrTypes2PixelTypes[t] = pt;
throw new InvalidOperationException($"Could not find: {typeName}");
}
PixelTypes2ClrTypes[pt] = t;
ClrTypes2PixelTypes[t] = pt;
}
PixelTypes2ClrTypes[PixelTypes.StandardImageClass] = typeof(Color);
}

7
tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

@ -3,8 +3,6 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// ReSharper disable InconsistentNaming
namespace ImageSharp.Tests
{
using System;
@ -68,7 +66,6 @@ namespace ImageSharp.Tests
Assert.IsType<Image>(img);
}
// TODO: @dlemstra this works only with constant strings!
[Theory]
[WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)]
[WithFile(TestImages.Bmp.F, PixelTypes.All, 88)]
@ -151,7 +148,7 @@ namespace ImageSharp.Tests
}
public static readonly TheoryData<ITestImageFactory> BasicData = new TheoryData<ITestImageFactory>()
public static readonly TheoryData<object> BasicData = new TheoryData<object>()
{
TestImageProvider<Color>.Blank(10, 20),
TestImageProvider<HalfVector4>.Blank(
@ -169,7 +166,7 @@ namespace ImageSharp.Tests
Assert.True(img.Width * img.Height > 0);
}
public static readonly TheoryData<ITestImageFactory> FileData = new TheoryData<ITestImageFactory>()
public static readonly TheoryData<object> FileData = new TheoryData<object>()
{
TestImageProvider<Color>.File(
TestImages.Bmp.Car),

3
tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

@ -3,7 +3,6 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// ReSharper disable InconsistentNaming
namespace ImageSharp.Tests
{
using System;
@ -139,7 +138,7 @@ namespace ImageSharp.Tests
{
var expanded = PixelTypes.All.ExpandAllTypes().ToArray();
Assert.True(expanded.Length >= FlagsHelper<PixelTypes>.GetSortedValues().Length - 2);
Assert.True(expanded.Length >= EnumHelper.GetSortedValues<PixelTypes>().Length - 2);
AssertContainsPixelType<Color>(PixelTypes.Color, expanded);
AssertContainsPixelType<Color>(PixelTypes.StandardImageClass, expanded);
}

Loading…
Cancel
Save