Browse Source

refactor

pull/298/head
Anton Firszov 9 years ago
parent
commit
09d8face82
  1. 25
      tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs
  2. 60
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs
  3. 20
      tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs
  4. 57
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

25
tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs

@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ImageSharp.Tests.TestUtilities.Integration
{
using System.IO;
using ImageSharp.Formats;
using ImageSharp.PixelFormats;
public class ReferencePngEncoder : IImageEncoder
{
public static ReferencePngEncoder Instance { get; } = new ReferencePngEncoder();
public void Encode<TPixel>(Image<TPixel> image, Stream stream, IEncoderOptions options)
where TPixel : struct, IPixel<TPixel>
{
using (System.Drawing.Bitmap sdBitmap = IntegrationTestUtils.ToSystemDrawingBitmap(image))
{
sdBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}

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

@ -6,6 +6,7 @@
namespace ImageSharp.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
@ -40,12 +41,8 @@ namespace ImageSharp.Tests
/// </summary>
public string TestName { get; set; } = string.Empty;
/// <summary>
/// Gets the recommended file name for the output of the test
/// </summary>
/// <param name="extension"></param>
/// <returns>The required extension</returns>
public string GetTestOutputFileName(string extension = null, string tag = null)
private string GetTestOutputFileNameImpl(string extension, string tag)
{
string fn = string.Empty;
@ -89,6 +86,40 @@ namespace ImageSharp.Tests
return $"{this.GetTestOutputDir()}/{this.TestName}{pixName}{fn}{tag}{extension}";
}
/// <summary>
/// Gets the recommended file name for the output of the test
/// </summary>
/// <param name="extension">The required extension</param>
/// <param name="settings">The settings modifying the output path</param>
/// <returns>The file test name</returns>
public string GetTestOutputFileName(string extension = null, object settings = null)
{
string tag = null;
string s = settings as string;
if (s != null)
{
tag = s;
}
else if (settings != null)
{
Type type = settings.GetType();
TypeInfo info = type.GetTypeInfo();
if (info.IsPrimitive || info.IsEnum || type == typeof(decimal))
{
tag = settings.ToString();
}
else
{
IEnumerable<PropertyInfo> properties = settings.GetType().GetRuntimeProperties();
tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}"));
}
}
return this.GetTestOutputFileNameImpl(extension, tag);
}
/// <summary>
/// Encodes image by the format matching the required extension, than saves it to the recommended output file.
/// </summary>
@ -97,12 +128,17 @@ namespace ImageSharp.Tests
/// <param name="extension">The requested extension</param>
/// <param name="encoder">Optional encoder</param>
/// <param name="options">Optional encoder options</param>
public void SaveTestOutputFile<TPixel>(Image<TPixel> image, string extension = null, IImageEncoder encoder = null, IEncoderOptions options = null, string tag = null)
public void SaveTestOutputFile<TPixel>(
Image<TPixel> image,
string extension = null,
IImageEncoder encoder = null,
IEncoderOptions options = null,
object settings = null)
where TPixel : struct, IPixel<TPixel>
{
string path = this.GetTestOutputFileName(extension: extension, tag:tag);
extension = Path.GetExtension(path);
IImageFormat format = GetImageFormatByExtension(extension);
string path = this.GetTestOutputFileName(extension: extension, settings: settings);
string extension1 = Path.GetExtension(path);
IImageFormat format = GetImageFormatByExtension(extension1);
encoder = encoder ?? format.Encoder;
@ -112,8 +148,8 @@ namespace ImageSharp.Tests
}
}
internal string GetReferenceOutputFileName(string extension = null, string tag = null)
=> this.GetTestOutputFileName(extension, tag).Replace("TestOutput", "ReferenceOutput");
internal string GetReferenceOutputFileName(string extension = null, object settings = null)
=> this.GetTestOutputFileName(extension, settings).Replace("TestOutput", "ReferenceOutput");
internal void Init(string typeName, string methodName)
{

20
tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs

@ -0,0 +1,20 @@
namespace ImageSharp.Tests
{
using System;
public static class TestEnvironment
{
private static Lazy<bool> runsOnCi = new Lazy<bool>(
() =>
{
bool isCi;
return bool.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi;
});
// ReSharper disable once InconsistentNaming
/// <summary>
/// Gets a value indicating whether test execution runs on CI.
/// </summary>
internal static bool RunsOnCI => runsOnCi.Value;
}
}

57
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -27,36 +27,13 @@ namespace ImageSharp.Tests
public static Image<TPixel> DebugSave<TPixel>(this Image<TPixel> image, ITestImageProvider provider, object settings = null, string extension = "png")
where TPixel : struct, IPixel<TPixel>
{
if (bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) && isCi)
if (TestEnvironment.RunsOnCI)
{
return image;
}
// We are running locally then we want to save it out
string tag = null;
string s = settings as string;
if (s != null)
{
tag = s;
}
else if (settings != null)
{
Type type = settings.GetType();
TypeInfo info = type.GetTypeInfo();
if (info.IsPrimitive || info.IsEnum || type == typeof(decimal))
{
tag = settings.ToString();
}
else
{
IEnumerable<PropertyInfo> properties = settings.GetType().GetRuntimeProperties();
tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}"));
}
}
provider.Utility.SaveTestOutputFile(image, extension, tag: tag);
provider.Utility.SaveTestOutputFile(image, extension, settings: settings);
return image;
}
@ -70,35 +47,11 @@ namespace ImageSharp.Tests
int scalingFactor = ImageComparer.DefaultScalingFactor)
where TPixel : struct, IPixel<TPixel>
{
// We are running locally then we want to save it out
string tag = null;
string s = settings as string;
if (s != null)
{
tag = s;
}
else if (settings != null)
{
Type type = settings.GetType();
TypeInfo info = type.GetTypeInfo();
if (info.IsPrimitive || info.IsEnum || type == typeof(decimal))
{
tag = settings.ToString();
}
else
{
IEnumerable<PropertyInfo> properties = settings.GetType().GetRuntimeProperties();
tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}"));
}
}
string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, tag);
string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, settings);
if (!(bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) && isCi))
if (!TestEnvironment.RunsOnCI)
{
provider.Utility.SaveTestOutputFile(image, extension, tag: tag);
provider.Utility.SaveTestOutputFile(image, extension, settings: settings);
}
if (!File.Exists(referenceOutputFile))

Loading…
Cancel
Save