Browse Source

good by GenericFactory!

af/merge-core
Anton Firszov 9 years ago
parent
commit
de89c7d2f7
  1. 4
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  2. 12
      tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs
  3. 8
      tests/ImageSharp.Tests/Image/PixelAccessorTests.cs
  4. 3
      tests/ImageSharp.Tests/ImageSharp.Tests.csproj
  5. 3
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithMemberFactoryAttribute.cs
  6. 36
      tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs
  7. 24
      tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs
  8. 2
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageSimilarityReport.cs
  9. 2
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs
  10. 8
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/LambdaProvider.cs
  11. 12
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  12. 4
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs
  13. 8
      tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

4
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -76,8 +76,8 @@ namespace ImageSharp.Tests
image.Save(ms, encoder);
}
}
Image<TPixel> mirror = provider.Factory.CreateImage(data);
Image<TPixel> mirror = Image.Load<TPixel>(data);
mirror.DebugSave(provider, $"_{subsample}_Q{quality}");
}

12
tests/ImageSharp.Tests/Formats/Jpg/JpegUtilsTests.cs

@ -16,19 +16,19 @@ namespace ImageSharp.Tests
public class JpegUtilsTests : TestBase
{
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory)
public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
Image<TPixel> image = factory.CreateImage(10, 10);
var image = new Image<TPixel>(10, 10);
using (PixelAccessor<TPixel> pixels = image.Lock())
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Vector4 v = new Vector4(i / 10f, j / 10f, 0, 1);
var v = new Vector4(i / 10f, j / 10f, 0, 1);
TPixel color = default(TPixel);
var color = default(TPixel);
color.PackFromVector4(v);
pixels[i, j] = color;
@ -45,7 +45,7 @@ namespace ImageSharp.Tests
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> src = provider.GetImage())
using (Image<TPixel> dest = provider.Factory.CreateImage(8, 8))
using (Image<TPixel> dest = new Image<TPixel>(8,8))
using (PixelArea<TPixel> area = new PixelArea<TPixel>(8, 8, ComponentOrder.Xyz))
using (PixelAccessor<TPixel> s = src.Lock())
using (PixelAccessor<TPixel> d = dest.Lock())
@ -68,7 +68,7 @@ namespace ImageSharp.Tests
{
using (Image<TPixel> src = provider.GetImage())
using (PixelArea<TPixel> area = new PixelArea<TPixel>(8, 8, ComponentOrder.Xyz))
using (Image<TPixel> dest = provider.Factory.CreateImage(8, 8))
using (Image<TPixel> dest = new Image<TPixel>(8, 8))
using (PixelAccessor<TPixel> s = src.Lock())
using (PixelAccessor<TPixel> d = dest.Lock())
{

8
tests/ImageSharp.Tests/Image/PixelAccessorTests.cs

@ -17,20 +17,20 @@ namespace ImageSharp.Tests
/// </summary>
public class PixelAccessorTests
{
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory)
public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
Image<TPixel> image = factory.CreateImage(10, 10);
var image = new Image<TPixel>(10, 10);
using (PixelAccessor<TPixel> 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);
var v = new Vector4(i, j, 0, 1);
v /= 10;
TPixel color = default(TPixel);
var color = default(TPixel);
color.PackFromVector4(v);
pixels[i, j] = color;

3
tests/ImageSharp.Tests/ImageSharp.Tests.csproj

@ -28,4 +28,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="TestUtilities\Factories\" />
</ItemGroup>
</Project>

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

@ -39,9 +39,8 @@ namespace ImageSharp.Tests
Type colorType = args.Single();
Type imgType = typeof(Image<>).MakeGenericType(colorType);
Type genericFactoryType = (typeof(GenericFactory<>)).MakeGenericType(colorType);
Type funcType = typeof(Func<,>).MakeGenericType(genericFactoryType, imgType);
Type funcType = typeof(Func<>).MakeGenericType(imgType);
MethodInfo genericMethod = m.MakeGenericMethod(args);

36
tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs

@ -1,36 +0,0 @@
// <copyright file="GenericFactory.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 ImageSharp.PixelFormats;
/// <summary>
/// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here!
///
/// Utility class to create specialized subclasses of generic classes (eg. <see cref="Image"/>)
/// Used as parameter for <see cref="WithMemberFactoryAttribute"/> -based factory methods
/// </summary>
public class GenericFactory<TPixel>
where TPixel : struct, IPixel<TPixel>
{
public virtual Image<TPixel> CreateImage(int width, int height)
{
return new Image<TPixel>(width, height);
}
public virtual Image<TPixel> CreateImage(byte[] bytes)
{
return Image.Load<TPixel>(bytes);
}
public virtual Image<TPixel> CreateImage(Image<TPixel> other)
{
return other.Clone();
}
}
}

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

@ -1,24 +0,0 @@
// <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
{
using ImageSharp.PixelFormats;
/// <summary>
/// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here!
/// </summary>
public class ImageFactory : GenericFactory<Rgba32>
{
public override Image<Rgba32> CreateImage(byte[] bytes) => Image.Load<Rgba32>(bytes);
public override Image<Rgba32> CreateImage(int width, int height) => new Image<Rgba32>(width, height);
public override Image<Rgba32> CreateImage(Image<Rgba32> other)
{
return other.Clone();
}
}
}

2
tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageSimilarityReport.cs

@ -42,7 +42,7 @@
var sb = new StringBuilder();
if (this.TotalNormalizedDifference.HasValue)
{
sb.AppendLine($"Total difference: {this.TotalNormalizedDifference.Value * 100:0.00}%");
sb.AppendLine($"Total difference: {this.TotalNormalizedDifference.Value * 100:0.0000}%");
}
int max = Math.Min(5, this.Differences.Length);

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

@ -33,7 +33,7 @@ namespace ImageSharp.Tests
protected int Width { get; private set; }
public override Image<TPixel> GetImage() => this.Factory.CreateImage(this.Width, this.Height);
public override Image<TPixel> GetImage() => new Image<TPixel>(this.Width, this.Height);
public override void Deserialize(IXunitSerializationInfo info)

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

@ -18,14 +18,14 @@ namespace ImageSharp.Tests
{
private class LambdaProvider : TestImageProvider<TPixel>
{
private readonly Func<GenericFactory<TPixel>, Image<TPixel>> creator;
private readonly Func<Image<TPixel>> factoryFunc;
public LambdaProvider(Func<GenericFactory<TPixel>, Image<TPixel>> creator)
public LambdaProvider(Func<Image<TPixel>> factoryFunc)
{
this.creator = creator;
this.factoryFunc = factoryFunc;
}
public override Image<TPixel> GetImage() => this.creator(this.Factory);
public override Image<TPixel> GetImage() => this.factoryFunc();
}
}
}

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

@ -33,7 +33,6 @@ namespace ImageSharp.Tests
/// </summary>
public ImagingTestCaseUtility Utility { get; private set; }
public GenericFactory<TPixel> Factory { get; private set; } = new GenericFactory<TPixel>();
public string TypeName { get; private set; }
public string MethodName { get; private set; }
@ -60,10 +59,10 @@ namespace ImageSharp.Tests
}
public static TestImageProvider<TPixel> Lambda(
Func<GenericFactory<TPixel>, Image<TPixel>> func,
Func<Image<TPixel>> factoryFunc,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new LambdaProvider(func).Init(testMethod, pixelTypeOverride);
=> new LambdaProvider(factoryFunc).Init(testMethod, pixelTypeOverride);
public static TestImageProvider<TPixel> Solid(
int width,
@ -122,12 +121,7 @@ namespace ImageSharp.Tests
}
this.TypeName = typeName;
this.MethodName = methodName;
if (pixelTypeOverride == PixelTypes.Rgba32)
{
this.Factory = new ImageFactory() as GenericFactory<TPixel>;
}
this.Utility = new ImagingTestCaseUtility
{
SourceFileOrDescription = this.SourceFileOrDescription,

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

@ -165,10 +165,10 @@ namespace ImageSharp.Tests
/// <typeparam name="TPixel"></typeparam>
/// <param name="factory"></param>
/// <returns></returns>
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory)
public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
return factory.CreateImage(3, 3);
return new Image<TPixel>(3, 3);
}
[Theory]

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

@ -27,10 +27,10 @@ namespace ImageSharp.Tests
private ITestOutputHelper Output { get; }
public static Image<TPixel> CreateTestImage<TPixel>(GenericFactory<TPixel> factory)
public static Image<TPixel> CreateTestImage<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
Image<TPixel> image = factory.CreateImage(10, 10);
var image = new Image<TPixel>(10, 10);
using (PixelAccessor<TPixel> pixels = image.Lock())
{
@ -38,10 +38,10 @@ namespace ImageSharp.Tests
{
for (int j = 0; j < 10; j++)
{
Vector4 v = new Vector4(i, j, 0, 1);
var v = new Vector4(i, j, 0, 1);
v /= 10;
TPixel color = default(TPixel);
var color = default(TPixel);
color.PackFromVector4(v);
pixels[i, j] = color;

Loading…
Cancel
Save