Browse Source

allow test cases to be serialized to they show in VS

pull/152/head
Scott Williams 9 years ago
parent
commit
72e88f3a33
  1. 25
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs
  2. 20
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs
  3. 38
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs
  4. 44
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  5. 21
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs
  6. 15
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

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

@ -6,6 +6,7 @@
namespace ImageSharp.Tests
{
using System;
using Xunit.Abstractions;
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPixel<TColor>
@ -17,14 +18,34 @@ namespace ImageSharp.Tests
this.Width = width;
this.Height = height;
}
public BlankProvider()
{
this.Width = 100;
this.Height = 100;
}
public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}";
protected int Height { get; }
protected int Height { get; private set; }
protected int Width { get; }
protected int Width { get; private set; }
public override Image<TColor> GetImage() => this.Factory.CreateImage(this.Width, this.Height);
public override void Deserialize(IXunitSerializationInfo info)
{
this.Width = info.GetValue<int>("width");
this.Height = info.GetValue<int>("height");
base.Deserialize(info);
}
public override void Serialize(IXunitSerializationInfo info)
{
info.AddValue("width", this.Width);
info.AddValue("height", this.Height);
base.Serialize(info);
}
}
}
}

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

@ -7,11 +7,12 @@ namespace ImageSharp.Tests
{
using System;
using System.Collections.Concurrent;
using Xunit.Abstractions;
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPixel<TColor>
{
private class FileProvider : TestImageProvider<TColor>
private class FileProvider : TestImageProvider<TColor>, IXunitSerializable
{
// Need PixelTypes in the dictionary key, because result images of TestImageProvider<TColor>.FileProvider
// are shared between PixelTypes.Color & PixelTypes.StandardImageClass
@ -33,6 +34,10 @@ namespace ImageSharp.Tests
this.filePath = filePath;
}
public FileProvider()
{
}
public override string SourceFileOrDescription => this.filePath;
public override Image<TColor> GetImage()
@ -49,6 +54,19 @@ namespace ImageSharp.Tests
return this.Factory.CreateImage(cachedImage);
}
public void Deserialize(IXunitSerializationInfo info)
{
this.filePath = info.GetValue<string>("path");
base.Deserialize(info); // must be called last
}
public void Serialize(IXunitSerializationInfo info)
{
base.Serialize(info);
info.AddValue("path", this.filePath);
}
}
}
}

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

@ -6,6 +6,7 @@
namespace ImageSharp.Tests
{
using System;
using Xunit.Abstractions;
/// <summary>
/// Provides <see cref="Image{TColor}" /> instances for parametric unit tests.
@ -14,15 +15,15 @@ namespace ImageSharp.Tests
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPixel<TColor>
{
private class SolidProvider : BlankProvider
private class SolidProvider : BlankProvider
{
private readonly byte a;
private byte a;
private readonly byte b;
private byte b;
private readonly byte g;
private byte g;
private readonly byte r;
private byte r;
public SolidProvider(int width, int height, byte r, byte g, byte b, byte a)
: base(width, height)
@ -33,6 +34,15 @@ namespace ImageSharp.Tests
this.a = a;
}
public SolidProvider()
: base()
{
this.r = 0;
this.g = 0;
this.b = 0;
this.a = 0;
}
public override string SourceFileOrDescription
=> $"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})";
@ -44,6 +54,24 @@ namespace ImageSharp.Tests
return image.Fill(color);
}
public override void Serialize(IXunitSerializationInfo info)
{
info.AddValue("red", this.r);
info.AddValue("green", this.g);
info.AddValue("blue", this.b);
info.AddValue("alpha", this.a);
base.Serialize(info);
}
public override void Deserialize(IXunitSerializationInfo info)
{
this.r = info.GetValue<byte>("red");
this.g = info.GetValue<byte>("green");
this.b = info.GetValue<byte>("blue");
this.a = info.GetValue<byte>("alpha");
base.Deserialize(info);
}
}
}
}

44
tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs

@ -7,12 +7,13 @@ namespace ImageSharp.Tests
{
using System;
using System.Reflection;
using Xunit.Abstractions;
/// <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>
public abstract partial class TestImageProvider<TColor> : IXunitSerializable
where TColor : struct, IPixel<TColor>
{
public PixelTypes PixelType { get; private set; } = typeof(TColor).GetPixelType();
@ -25,7 +26,9 @@ namespace ImageSharp.Tests
public ImagingTestCaseUtility Utility { get; private set; }
public GenericFactory<TColor> Factory { get; private set; } = new GenericFactory<TColor>();
public string TypeName { get; private set; }
public string MethodName { get; private set; }
public static TestImageProvider<TColor> TestPattern(
int width,
int height,
@ -72,12 +75,30 @@ namespace ImageSharp.Tests
/// </summary>
public abstract Image<TColor> GetImage();
protected TestImageProvider<TColor> Init(MethodInfo testMethod, PixelTypes pixelTypeOverride)
public virtual void Deserialize(IXunitSerializationInfo info)
{
PixelTypes pixelType = info.GetValue<PixelTypes>("PixelType");
string typeName = info.GetValue<string>("TypeName");
string methodName = info.GetValue<string>("MethodName");
this.Init(typeName, methodName, pixelType);
}
public virtual void Serialize(IXunitSerializationInfo info)
{
info.AddValue("PixelType", this.PixelType);
info.AddValue("TypeName", this.TypeName);
info.AddValue("MethodName", this.MethodName);
}
protected TestImageProvider<TColor> Init(string typeName, string methodName, PixelTypes pixelTypeOverride)
{
if (pixelTypeOverride != PixelTypes.Undefined)
{
this.PixelType = pixelTypeOverride;
}
this.TypeName = typeName;
this.MethodName = methodName;
if (pixelTypeOverride == PixelTypes.StandardImageClass)
{
@ -85,19 +106,24 @@ namespace ImageSharp.Tests
}
this.Utility = new ImagingTestCaseUtility()
{
SourceFileOrDescription = this.SourceFileOrDescription,
PixelTypeName = this.PixelType.ToString()
};
{
SourceFileOrDescription = this.SourceFileOrDescription,
PixelTypeName = this.PixelType.ToString()
};
if (testMethod != null)
if (methodName != null)
{
this.Utility.Init(testMethod);
this.Utility.Init(typeName, methodName);
}
return this;
}
protected TestImageProvider<TColor> Init(MethodInfo testMethod, PixelTypes pixelTypeOverride)
{
return Init(testMethod?.DeclaringType.Name, testMethod?.Name, pixelTypeOverride);
}
public override string ToString()
{
string provName = this.GetType().Name.Replace("Provider", "");

21
tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs

@ -8,6 +8,7 @@ namespace ImageSharp.Tests
using System;
using System.Collections.Generic;
using System.Numerics;
using Xunit.Abstractions;
public abstract partial class TestImageProvider<TColor>
where TColor : struct, IPixel<TColor>
@ -17,21 +18,21 @@ namespace ImageSharp.Tests
/// A test image provider that produces test patterns.
/// </summary>
/// <typeparam name="TColor"></typeparam>
private class TestPatternProvider : TestImageProvider<TColor>
private class TestPatternProvider : BlankProvider
{
static Dictionary<string, Image<TColor>> testImages = new Dictionary<string, Image<TColor>>();
public TestPatternProvider(int width, int height)
: base(width, height)
{
this.Width = width;
this.Height = height;
}
public override string SourceFileOrDescription => $"TestPattern{this.Width}x{this.Height}";
protected int Height { get; }
public TestPatternProvider()
: base()
{
}
protected int Width { get; }
public override string SourceFileOrDescription => $"TestPattern{this.Width}x{this.Height}";
public override Image<TColor> GetImage()
{
@ -43,9 +44,9 @@ namespace ImageSharp.Tests
DrawTestPattern(image);
testImages.Add(this.SourceFileOrDescription, image);
}
return new Image<TColor>(testImages[this.SourceFileOrDescription]);
}
return new Image<TColor>(testImages[this.SourceFileOrDescription]);
}
/// <summary>
@ -132,7 +133,7 @@ namespace ImageSharp.Tests
p = pstart;
}
}
/// <summary>
/// Fills the bottom left quadrent with 3 horizental bars in Red, Green and Blue with a alpha gradient from left (transparent) to right (solid).
/// </summary>

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

@ -62,7 +62,7 @@ namespace ImageSharp.Tests
if (string.IsNullOrWhiteSpace(extension))
{
extension = ".bmp";
extension = ".bmp";
}
if (extension[0] != '.')
@ -81,9 +81,9 @@ namespace ImageSharp.Tests
tag = tag ?? string.Empty;
if (tag != string.Empty)
{
tag= '_' + tag;
tag = '_' + tag;
}
return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{tag}{extension}";
}
@ -111,10 +111,15 @@ namespace ImageSharp.Tests
}
}
internal void Init(string typeName, string methodName)
{
this.TestGroupName = typeName;
this.TestName = methodName;
}
internal void Init(MethodInfo method)
{
this.TestGroupName = method.DeclaringType.Name;
this.TestName = method.Name;
this.Init(method.DeclaringType.Name, method.Name);
}
private static IImageFormat GetImageFormatByExtension(string extension)

Loading…
Cancel
Save