Browse Source

introduce [WithBasicTestPatternImages]

af/merge-core
Anton Firszov 7 years ago
parent
commit
0387964192
  1. 17
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  2. 37
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithBasicTestPatternImagesAttribute.cs
  3. 65
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/BasicTestPatternProvider.cs
  4. 3
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/BlankProvider.cs
  5. 3
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/SolidProvider.cs
  6. 6
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  7. 5
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs
  8. 14
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

17
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -35,6 +35,23 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
private const PixelTypes CommonNonDefaultPixelTypes =
PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector;
[Theory]
[WithBasicTestPatternImages(15, 12, PixelTypes.Rgba32)]
public void Resize_BasicSmall<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
// Basic test case, very helpful for debugging
// resizing: (15, 12) -> (10, 6)
// kernel dimensions: (3, 4)
using (Image<TPixel> image = provider.GetImage())
{
var destSize = new Size(image.Width * 2 / 3, image.Height / 2);
image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false));
image.DebugSave(provider);
}
}
[Theory]
[WithFileCollection(nameof(CommonTestImages), nameof(AllResamplerNames), DefaultPixelType, 0.5f, null, null)]

37
tests/ImageSharp.Tests/TestUtilities/Attributes/WithBasicTestPatternImagesAttribute.cs

@ -0,0 +1,37 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Reflection;
namespace SixLabors.ImageSharp.Tests
{
public class WithBasicTestPatternImagesAttribute : ImageDataAttributeBase
{
public WithBasicTestPatternImagesAttribute(int width, int height, PixelTypes pixelTypes, params object[] additionalParameters)
: this(null, width, height, pixelTypes, additionalParameters)
{
}
public WithBasicTestPatternImagesAttribute(string memberData, int width, int height, PixelTypes pixelTypes, params object[] additionalParameters)
: base(memberData, pixelTypes, additionalParameters)
{
this.Width = width;
this.Height = height;
}
/// <summary>
/// Gets the width
/// </summary>
public int Width { get; }
/// <summary>
/// Gets the height
/// </summary>
public int Height { get; }
protected override string GetFactoryMethodName(MethodInfo testMethod) => "BasicTestPattern";
protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) => new object[] { this.Width, this.Height };
}
}

65
tests/ImageSharp.Tests/TestUtilities/ImageProviders/BasicTestPatternProvider.cs

@ -0,0 +1,65 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Tests
{
public abstract partial class TestImageProvider<TPixel>
{
private class BasicTestPatternProvider : BlankProvider
{
public BasicTestPatternProvider(int width, int height)
: base(width, height)
{
}
/// <summary>
/// This parameterless constructor is needed for xUnit deserialization
/// </summary>
public BasicTestPatternProvider()
{
}
public override string SourceFileOrDescription => TestUtils.AsInvariantString($"BasicTestPattern{this.Width}x{this.Height}");
public override Image<TPixel> GetImage()
{
var result = new Image<TPixel>(this.Width, this.Height);
TPixel topLeftColor = NamedColors<TPixel>.Red;
TPixel topRightColor = NamedColors<TPixel>.Green;
TPixel bottomLeftColor = NamedColors<TPixel>.Blue;
// Transparent purple:
TPixel bottomRightColor = default;
bottomRightColor.FromVector4(new Vector4(1f, 0f, 1f, 0.5f));
int midY = this.Height / 2;
int midX = this.Width / 2;
for (int y = 0; y < midY; y++)
{
Span<TPixel> row = result.GetPixelRowSpan(y);
row.Slice(0, midX).Fill(topLeftColor);
row.Slice(midX, this.Width-midX).Fill(topRightColor);
}
for (int y = midY; y < this.Height; y++)
{
Span<TPixel> row = result.GetPixelRowSpan(y);
row.Slice(0, midX).Fill(bottomLeftColor);
row.Slice(midX, this.Width-midX).Fill(bottomRightColor);
}
return result;
}
}
}
}

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

@ -20,6 +20,9 @@ namespace SixLabors.ImageSharp.Tests
this.Height = height;
}
/// <summary>
/// This parameterless constructor is needed for xUnit deserialization
/// </summary>
public BlankProvider()
{
this.Width = 100;

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

@ -35,6 +35,9 @@ namespace SixLabors.ImageSharp.Tests
this.a = a;
}
/// <summary>
/// This parameterless constructor is needed for xUnit deserialization
/// </summary>
public SolidProvider()
: base()
{

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

@ -44,6 +44,12 @@ namespace SixLabors.ImageSharp.Tests
public string MethodName { get; private set; }
public string OutputSubfolderName { get; private set; }
public static TestImageProvider<TPixel> BasicTestPattern(int width,
int height,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new BasicTestPatternProvider(width, height).Init(testMethod, pixelTypeOverride);
public static TestImageProvider<TPixel> TestPattern(
int width,
int height,

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

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Net.Mime;
using System.Numerics;
using SixLabors.ImageSharp.Memory;
@ -25,8 +26,10 @@ namespace SixLabors.ImageSharp.Tests
{
}
/// <summary>
/// This parameterless constructor is needed for xUnit deserialization
/// </summary>
public TestPatternProvider()
: base()
{
}

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

@ -325,6 +325,20 @@ namespace SixLabors.ImageSharp.Tests
}
}
[Theory]
[WithBasicTestPatternImages(50, 100, PixelTypes.Rgba32)]
[WithBasicTestPatternImages(49,17, PixelTypes.Rgba32)]
[WithBasicTestPatternImages(20, 10, PixelTypes.Rgba32)]
public void Use_WithBasicTestPatternImages<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> img = provider.GetImage())
{
img.DebugSave(provider);
}
}
public static readonly TheoryData<object> BasicData = new TheoryData<object>()
{
TestImageProvider<Rgba32>.Blank(10, 20),

Loading…
Cancel
Save