From b13521114c6ec63c71687298ccaafab28be21fb0 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Thu, 22 Dec 2016 22:42:35 +0100 Subject: [PATCH] Refactored TestImages again so they can be used in Attributes. --- .../ImageSharp.Tests/Drawing/DrawImageTest.cs | 2 +- .../Drawing/SolidPolygonTests.cs | 2 +- tests/ImageSharp.Tests/FileTestBase.cs | 52 ++++----- .../ImageSharp.Tests/Formats/Jpg/JpegTests.cs | 4 +- tests/ImageSharp.Tests/Image/ImageTests.cs | 2 +- .../Processors/Filters/AutoOrientTests.cs | 2 +- .../Profiles/Exif/ExifProfileTests.cs | 8 +- .../Profiles/Exif/ExifValueTests.cs | 2 +- tests/ImageSharp.Tests/TestFile.cs | 30 +++++- tests/ImageSharp.Tests/TestImages.cs | 100 +++++++----------- 10 files changed, 102 insertions(+), 102 deletions(-) diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs index 89d9f55726..5e5a6897f7 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs @@ -17,7 +17,7 @@ namespace ImageSharp.Tests { string path = CreateOutputDirectory("Drawing", "DrawImage"); - Image blend = TestImages.Bmp.Car.CreateImage(); + Image blend = TestFile.Create(TestImages.Bmp.Car).CreateImage(); foreach (TestFile file in Files) { diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs index 2d8548c5a0..a70782478b 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs @@ -88,7 +88,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(50, 300) }; - var brush = new ImageBrush(TestImages.Bmp.Car.CreateImage()); + var brush = new ImageBrush(TestFile.Create(TestImages.Bmp.Car).CreateImage()); var image = new Image(500, 500); using (FileStream output = File.OpenWrite($"{path}/Image.png")) diff --git a/tests/ImageSharp.Tests/FileTestBase.cs b/tests/ImageSharp.Tests/FileTestBase.cs index bc2c7b8e83..38a499a02f 100644 --- a/tests/ImageSharp.Tests/FileTestBase.cs +++ b/tests/ImageSharp.Tests/FileTestBase.cs @@ -18,32 +18,32 @@ namespace ImageSharp.Tests /// protected static readonly List Files = new List { - // TestImages.Png.P1, // Perf: Enable for local testing only - // TestImages.Png.Pd, // Perf: Enable for local testing only - // TestImages.Jpeg.Floorplan, // Perf: Enable for local testing only - TestImages.Jpeg.Calliphora, - // TestImages.Jpeg.Ycck, // Perf: Enable for local testing only - // TestImages.Jpeg.Cmyk, // Perf: Enable for local testing only - TestImages.Jpeg.Turtle, - // TestImages.Jpeg.Fb, // Perf: Enable for local testing only - // TestImages.Jpeg.Progress, // Perf: Enable for local testing only - // TestImages.Jpeg.GammaDalaiLamaGray, // Perf: Enable for local testing only - TestImages.Bmp.Car, - // TestImages.Bmp.Neg_height, // Perf: Enable for local testing only - // TestImages.Png.Blur, // Perf: Enable for local testing only - // TestImages.Png.Indexed, // Perf: Enable for local testing only - TestImages.Png.Splash, - // TestImages.Png.SplashInterlaced, // Perf: Enable for local testing only - // TestImages.Png.Interlaced, // Perf: Enable for local testing only - // TestImages.Png.Filter0, // Perf: Enable for local testing only - // TestImages.Png.Filter1, // Perf: Enable for local testing only - // TestImages.Png.Filter2, // Perf: Enable for local testing only - // TestImages.Png.Filter3, // Perf: Enable for local testing only - // TestImages.Png.Filter4, // Perf: Enable for local testing only - // TestImages.Png.FilterVar, // Perf: Enable for local testing only - TestImages.Gif.Rings, // Perf: Enable for local testing only - // TestImages.Gif.Cheers, - // TestImages.Gif.Giphy // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.P1), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Pd), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Jpeg.Floorplan), // Perf: Enable for local testing only + TestFile.Create(TestImages.Jpeg.Calliphora), + // TestFile.Create(TestImages.Jpeg.Ycck), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Jpeg.Cmyk), // Perf: Enable for local testing only + TestFile.Create(TestImages.Jpeg.Turtle), + // TestFile.Create(TestImages.Jpeg.Fb), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Jpeg.Progress), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Jpeg.GammaDalaiLamaGray), // Perf: Enable for local testing only + TestFile.Create(TestImages.Bmp.Car), + // TestFile.Create(TestImages.Bmp.Neg_height), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Blur), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Indexed), // Perf: Enable for local testing only + TestFile.Create(TestImages.Png.Splash), + // TestFile.Create(TestImages.Png.SplashInterlaced), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Interlaced), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Filter0), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Filter1), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Filter2), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Filter3), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.Filter4), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Png.FilterVar), // Perf: Enable for local testing only + TestFile.Create(TestImages.Gif.Rings), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Gif.Cheers), + // TestFile.Create(TestImages.Gif.Giphy) // Perf: Enable for local testing only }; protected string CreateOutputDirectory(string path, params string[] pathParts) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs index d09a13b103..08efe5c899 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs @@ -43,7 +43,7 @@ namespace ImageSharp.Tests.Formats.Jpg } public static IEnumerable AllJpegFiles - => TestImages.Jpeg.All.Select(file => new object[] {file}); + => TestImages.Jpeg.All.Select(file => new object[] {TestFile.Create(file)}); [Theory] [MemberData(nameof(AllJpegFiles))] @@ -60,7 +60,7 @@ namespace ImageSharp.Tests.Formats.Jpg } public static IEnumerable AllBmpFiles - => TestImages.Bmp.All.Select(file => new object[] {file}); + => TestImages.Bmp.All.Select(file => new object[] { TestFile.Create(file) }); [Theory] [MemberData(nameof(AllBmpFiles))] diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs index 5a5348d005..829dce7486 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Tests new Image((byte[])null); }); - TestFile file = TestImages.Bmp.Car; + TestFile file = TestFile.Create(TestImages.Bmp.Car); Image image = new Image(file.Bytes); Assert.Equal(600, image.Width); diff --git a/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs b/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs index 6b3cacb274..d2e6dcbc1a 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs @@ -30,7 +30,7 @@ namespace ImageSharp.Tests { string path = CreateOutputDirectory("AutoOrient"); - TestFile file = TestImages.Bmp.F; + TestFile file = TestFile.Create(TestImages.Bmp.F); Image image = file.CreateImage(); image.ExifProfile = new ExifProfile(); diff --git a/tests/ImageSharp.Tests/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/Profiles/Exif/ExifProfileTests.cs index fca903605a..9539ccb3b9 100644 --- a/tests/ImageSharp.Tests/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/Profiles/Exif/ExifProfileTests.cs @@ -17,7 +17,7 @@ namespace ImageSharp.Tests [Fact] public void Constructor() { - Image image = TestImages.Jpeg.Calliphora.CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Calliphora).CreateImage(); Assert.Null(image.ExifProfile); @@ -104,7 +104,7 @@ namespace ImageSharp.Tests [Fact] public void ReadWriteInfinity() { - Image image = TestImages.Jpeg.Floorplan.CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Floorplan).CreateImage(); image.ExifProfile.SetValue(ExifTag.ExposureBiasValue, new SignedRational(double.PositiveInfinity)); image = WriteAndRead(image); @@ -132,7 +132,7 @@ namespace ImageSharp.Tests { Rational[] latitude = new Rational[] { new Rational(12.3), new Rational(4.56), new Rational(789.0) }; - Image image = TestImages.Jpeg.Floorplan.CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Floorplan).CreateImage(); image.ExifProfile.SetValue(ExifTag.Software, "ImageSharp"); ExifValue value = image.ExifProfile.GetValue(ExifTag.Software); @@ -234,7 +234,7 @@ namespace ImageSharp.Tests private static ExifProfile GetExifProfile() { - Image image = TestImages.Jpeg.Floorplan.CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Floorplan).CreateImage(); ExifProfile profile = image.ExifProfile; Assert.NotNull(profile); diff --git a/tests/ImageSharp.Tests/Profiles/Exif/ExifValueTests.cs b/tests/ImageSharp.Tests/Profiles/Exif/ExifValueTests.cs index 2a2d1937e9..a1c4ed9f12 100644 --- a/tests/ImageSharp.Tests/Profiles/Exif/ExifValueTests.cs +++ b/tests/ImageSharp.Tests/Profiles/Exif/ExifValueTests.cs @@ -12,7 +12,7 @@ namespace ImageSharp.Tests { private static ExifValue GetExifValue() { - Image image = TestImages.Jpeg.Floorplan.CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Floorplan).CreateImage(); ExifProfile profile = image.ExifProfile; Assert.NotNull(profile); diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index 89a724191b..d4d4a0a89d 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -2,17 +2,31 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // - -using System.IO; - namespace ImageSharp.Tests { + using System.Collections.Concurrent; + using System.IO; + public class TestFile { + private static readonly ConcurrentDictionary cache = new ConcurrentDictionary(); + private static readonly string FormatsDirectory = GetFormatsDirectory(); + + private static string GetFormatsDirectory() + { + // Here for code coverage tests. + string directory = "TestImages/Formats/"; + if (Directory.Exists(directory)) + { + return directory; + } + return "../../../../TestImages/Formats/"; + } + private readonly Image image; private readonly string file; - public TestFile(string file) + private TestFile(string file) { this.file = file; @@ -20,6 +34,14 @@ namespace ImageSharp.Tests this.image = new Image(this.Bytes); } + public static TestFile Create(string file) + { + return cache.GetOrAdd(file, (string fileName) => + { + return new TestFile(FormatsDirectory + fileName); + }); + } + public byte[] Bytes { get; } public string FileName diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 671d0c1dd1..e5b46b0e1f 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -12,68 +12,50 @@ namespace ImageSharp.Tests /// public static class TestImages { - private static readonly string FormatsDirectory = GetFormatsDirectory(); - - private static string GetFormatsDirectory() - { - // Here for code coverage tests. - string directory = "TestImages/Formats/"; - if (Directory.Exists(directory)) - { - return directory; - } - return "../../../../TestImages/Formats/"; - } - public static class Png { - private static readonly string folder = FormatsDirectory + "Png/"; + public const string P1 = "Png/pl.png"; + public const string Pd = "Png/pd.png"; + public const string Blur = "Png/blur.png"; + public const string Indexed = "Png/indexed.png"; + public const string Splash = "Png/splash.png"; - public static TestFile P1 => new TestFile(folder + "pl.png"); - public static TestFile Pd => new TestFile(folder + "pd.png"); - public static TestFile Blur => new TestFile(folder + "blur.png"); - public static TestFile Indexed => new TestFile(folder + "indexed.png"); - public static TestFile Splash => new TestFile(folder + "splash.png"); + public const string SplashInterlaced = "Png/splash-interlaced.png"; - public static TestFile SplashInterlaced => new TestFile(folder + "splash-interlaced.png"); - - public static TestFile Interlaced => new TestFile(folder + "interlaced.png"); + public const string Interlaced = "Png/interlaced.png"; // filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html - public static TestFile Filter0 => new TestFile(folder + "filter0.png"); - public static TestFile Filter1 => new TestFile(folder + "filter1.png"); - public static TestFile Filter2 => new TestFile(folder + "filter2.png"); - public static TestFile Filter3 => new TestFile(folder + "filter3.png"); - public static TestFile Filter4 => new TestFile(folder + "filter4.png"); + public const string Filter0 = "Png/filter0.png"; + public const string Filter1 = "Png/filter1.png"; + public const string Filter2 = "Png/filter2.png"; + public const string Filter3 = "Png/filter3.png"; + public const string Filter4 = "Png/filter4.png"; // filter changing per scanline - public static TestFile FilterVar => new TestFile(folder + "filterVar.png"); + public const string FilterVar = "Png/filterVar.png"; } public static class Jpeg { - private static readonly string folder = FormatsDirectory + "Jpg/"; - - public static TestFile Cmyk => new TestFile(folder + "cmyk.jpg"); - public static TestFile Exif => new TestFile(folder + "exif.jpg"); - public static TestFile Floorplan => new TestFile(folder + "Floorplan.jpg"); - public static TestFile Calliphora => new TestFile(folder + "Calliphora.jpg"); - public static TestFile Ycck => new TestFile(folder + "ycck.jpg"); - public static TestFile Turtle => new TestFile(folder + "turtle.jpg"); - public static TestFile Fb => new TestFile(folder + "fb.jpg"); - public static TestFile Progress => new TestFile(folder + "progress.jpg"); - public static TestFile GammaDalaiLamaGray => new TestFile(folder + "gamma_dalai_lama_gray.jpg"); - - public static TestFile Festzug => new TestFile(folder + "Festzug.jpg"); - public static TestFile Hiyamugi => new TestFile(folder + "Hiyamugi.jpg"); - - public static TestFile Jpeg400 => new TestFile(folder + "baseline/jpeg400jfif.jpg"); - public static TestFile Jpeg420 => new TestFile(folder + "baseline/jpeg420exif.jpg"); - public static TestFile Jpeg422 => new TestFile(folder + "baseline/jpeg422jfif.jpg"); - public static TestFile Jpeg444 => new TestFile(folder + "baseline/jpeg444.jpg"); - - - public static readonly TestFile[] All = { + public const string Cmyk = "Jpg/cmyk.jpg"; + public const string Exif = "Jpg/exif.jpg"; + public const string Floorplan = "Jpg/Floorplan.jpg"; + public const string Calliphora = "Jpg/Calliphora.jpg"; + public const string Ycck = "Jpg/ycck.jpg"; + public const string Turtle = "Jpg/turtle.jpg"; + public const string Fb = "Jpg/fb.jpg"; + public const string Progress ="Jpg/progress.jpg"; + public const string GammaDalaiLamaGray = "Jpg/gamma_dalai_lama_gray.jpg"; + + public const string Festzug = "Jpg/Festzug.jpg"; + public const string Hiyamugi = "Jpg/Hiyamugi.jpg"; + + public const string Jpeg400 = "Jpg/baseline/jpeg400jfif.jpg"; + public const string Jpeg420 = "Jpg/baseline/jpeg420exif.jpg"; + public const string Jpeg422 = "Jpg/baseline/jpeg422jfif.jpg"; + public const string Jpeg444 = "Jpg/baseline/jpeg444.jpg"; + + public static readonly string[] All = { Cmyk, Exif, Floorplan, Calliphora, Turtle, Fb, Progress, GammaDalaiLamaGray, Festzug, Hiyamugi, Jpeg400, Jpeg420, Jpeg444, @@ -82,27 +64,23 @@ namespace ImageSharp.Tests public static class Bmp { - private static readonly string folder = FormatsDirectory + "Bmp/"; - - public static TestFile Car => new TestFile(folder + "Car.bmp"); + public const string Car = "Bmp/Car.bmp"; - public static TestFile F => new TestFile(folder + "F.bmp"); + public const string F = "Bmp/F.bmp"; - public static TestFile NegHeight => new TestFile(folder + "neg_height.bmp"); + public const string NegHeight = "Bmp/neg_height.bmp"; - public static readonly TestFile[] All = { + public static readonly string[] All = { Car, F, NegHeight }; } public static class Gif { - private static readonly string folder = FormatsDirectory + "Gif/"; - - public static TestFile Rings => new TestFile(folder + "rings.gif"); - public static TestFile Giphy => new TestFile(folder + "giphy.gif"); + public const string Rings = "Gif/rings.gif"; + public const string Giphy = "Gif/giphy.gif"; - public static TestFile Cheers => new TestFile(folder + "cheers.gif"); + public const string Cheers = "Gif/cheers.gif"; } } }