Browse Source

LambdaProvider -> serializable MemberMethodProvider

af/octree-no-pixelmap
Anton Firszov 6 years ago
parent
commit
aeb0100361
  1. 16
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs
  2. 34
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs
  3. 69
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/MemberMethodProvider.cs
  4. 5
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs

16
tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs

@ -31,21 +31,9 @@ namespace SixLabors.ImageSharp.Tests
protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType)
{
MethodInfo m = testMethod.DeclaringType.GetMethod(this.memberMethodName);
Type[] args = factoryType.GetGenericArguments();
Type colorType = args.Single();
Type imgType = typeof(Image<>).MakeGenericType(colorType);
Type funcType = typeof(Func<>).MakeGenericType(imgType);
MethodInfo genericMethod = m.MakeGenericMethod(args);
Delegate d = genericMethod.CreateDelegate(funcType);
return new object[] { d };
return new object[] { testMethod.DeclaringType.FullName, this.memberMethodName};
}
protected override string GetFactoryMethodName(MethodInfo testMethod) => "Lambda";
}
}
}

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

@ -1,34 +0,0 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Tests
{
/// <summary>
/// Provides <see cref="Image{TPixel}" /> instances for parametric unit tests.
/// </summary>
/// <typeparam name="TPixel">The pixel format of the image</typeparam>
public abstract partial class TestImageProvider<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private class LambdaProvider : TestImageProvider<TPixel>
{
private readonly Func<Image<TPixel>> factoryFunc;
public LambdaProvider()
{
throw new NotSupportedException();
}
public LambdaProvider(Func<Image<TPixel>> factoryFunc)
{
this.factoryFunc = factoryFunc;
}
public override Image<TPixel> GetImage() => this.factoryFunc();
}
}
}

69
tests/ImageSharp.Tests/TestUtilities/ImageProviders/MemberMethodProvider.cs

@ -0,0 +1,69 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Linq;
using System.Reflection;
using SixLabors.ImageSharp.PixelFormats;
using Xunit.Abstractions;
namespace SixLabors.ImageSharp.Tests
{
/// <summary>
/// Provides <see cref="Image{TPixel}" /> instances for parametric unit tests.
/// </summary>
/// <typeparam name="TPixel">The pixel format of the image</typeparam>
public abstract partial class TestImageProvider<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private class MemberMethodProvider : TestImageProvider<TPixel>
{
private string declaringTypeName;
private string methodName;
private Func<Image<TPixel>> factoryFunc;
public MemberMethodProvider()
{
}
public MemberMethodProvider(string declaringTypeName, string methodName)
{
this.declaringTypeName = declaringTypeName;
this.methodName = methodName;
}
public override Image<TPixel> GetImage()
{
this.factoryFunc ??= this.GetFactory();
return this.factoryFunc();
}
public override void Serialize(IXunitSerializationInfo info)
{
base.Serialize(info);
info.AddValue(nameof(this.declaringTypeName), this.declaringTypeName);
info.AddValue(nameof(this.methodName), this.methodName);
}
public override void Deserialize(IXunitSerializationInfo info)
{
base.Deserialize(info);
this.methodName = info.GetValue<string>(nameof(this.methodName));
this.declaringTypeName = info.GetValue<string>(nameof(this.declaringTypeName));
}
private Func<Image<TPixel>> GetFactory()
{
var declaringType = Type.GetType(this.declaringTypeName);
MethodInfo m = declaringType.GetMethod(this.methodName);
Type pixelType = typeof(TPixel);
Type imgType = typeof(Image<>).MakeGenericType(pixelType);
Type funcType = typeof(Func<>).MakeGenericType(imgType);
MethodInfo genericMethod = m.MakeGenericMethod(pixelType);
return (Func<Image<TPixel>>) genericMethod.CreateDelegate(funcType);
}
}
}
}

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

@ -73,10 +73,11 @@ namespace SixLabors.ImageSharp.Tests
}
public static TestImageProvider<TPixel> Lambda(
Func<Image<TPixel>> factoryFunc,
string declaringTypeName,
string methodName,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new LambdaProvider(factoryFunc).Init(testMethod, pixelTypeOverride);
=> new MemberMethodProvider(declaringTypeName, methodName).Init(testMethod, pixelTypeOverride);
public static TestImageProvider<TPixel> Solid(
int width,

Loading…
Cancel
Save