diff --git a/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs b/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs index db6c1157c..79ebf4778 100644 --- a/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs @@ -1,16 +1,14 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; using System.Numerics; -using SixLabors.ImageSharp; using SixLabors.ImageSharp.Drawing; using SixLabors.ImageSharp.Drawing.Pens; using SixLabors.ImageSharp.Drawing.Processors; -using SixLabors.ImageSharp.PixelFormats; using Moq; using Xunit; using SixLabors.ImageSharp.Drawing.Brushes; +using SixLabors.Primitives; namespace SixLabors.ImageSharp.Tests.Drawing { @@ -25,18 +23,18 @@ namespace SixLabors.ImageSharp.Tests.Drawing [InlineData(false, 16, 4)] // we always do 4 sub=pixels when antialising is off. public void MinimumAntialiasSubpixelDepth(bool antialias, int antialiasSubpixelDepth, int expectedAntialiasSubpixelDepth) { - SixLabors.Primitives.Rectangle bounds = new SixLabors.Primitives.Rectangle(0, 0, 1, 1); + var bounds = new SixLabors.Primitives.Rectangle(0, 0, 1, 1); - Mock> brush = new Mock>(); - Mock region = new Mock(); + var brush = new Mock>(); + var region = new Mock(); region.Setup(x => x.Bounds).Returns(bounds); - GraphicsOptions options = new GraphicsOptions(antialias) + var options = new GraphicsOptions(antialias) { AntialiasSubpixelDepth = 1 }; - FillRegionProcessor processor = new FillRegionProcessor(brush.Object, region.Object, options); - Image img = new Image(1, 1); + var processor = new FillRegionProcessor(brush.Object, region.Object, options); + var img = new Image(1, 1); processor.Apply(img, bounds); region.Verify(x => x.Scan(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(4)); @@ -45,31 +43,11 @@ namespace SixLabors.ImageSharp.Tests.Drawing [Fact] public void FillOffCanvas() { - - SixLabors.Primitives.Rectangle bounds = new SixLabors.Primitives.Rectangle(-100, -10, 10, 10); - - Mock> brush = new Mock>(); - Mock region = new Mock(); - region.Setup(x => x.Bounds).Returns(bounds); - - region.Setup(x => x.MaxIntersections).Returns(10); - region.Setup(x => x.Scan(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns>((y, span) => - { - if (y < 5) - { - span[0] = -10f; - span[1] = 100f; - return 2; - } - return 0; - }); - - GraphicsOptions options = new GraphicsOptions(true) - { - }; - FillRegionProcessor processor = new FillRegionProcessor(brush.Object, region.Object, options); - Image img = new Image(10, 10); + var bounds = new Rectangle(-100, -10, 10, 10); + var brush = new Mock>(); + var options = new GraphicsOptions(true); + var processor = new FillRegionProcessor(brush.Object, new MockRegion(), options); + var img = new Image(10, 10); processor.Apply(img, bounds); } @@ -85,5 +63,24 @@ namespace SixLabors.ImageSharp.Tests.Drawing })); } } + + // Mocking the region throws an error in netcore2.0 + private class MockRegion : Region + { + public override Rectangle Bounds => new Rectangle(-100, -10, 10, 10); + + public override int MaxIntersections => 10; + + public override int Scan(float y, float[] buffer, int offset) + { + if (y < 5) + { + buffer[0] = -10f; + buffer[1] = 100f; + return 2; + } + return 0; + } + } } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 7e0dc915c..f141905ef 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -39,10 +39,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [MemberData(nameof(CommonConversionData))] public void ConvertFromYCbCrBasic(int inputBufferLength, int resultBufferLength, int seed) { - ValidateConversion(new JpegColorConverter.FromYCbCrBasic(), 3, inputBufferLength, resultBufferLength, seed, ValidateYCbCr); + ValidateRgbToYCbCrConversion( + new JpegColorConverter.FromYCbCrBasic(), + 3, + inputBufferLength, + resultBufferLength, + seed); } - private static void ValidateYCbCr(JpegColorConverter.ComponentValues values, Span result, int i) + private static void ValidateYCbCr(JpegColorConverter.ComponentValues values, Vector4[] result, int i) { float y = values.Component0[i]; float cb = values.Component1[i]; @@ -63,20 +68,27 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [InlineData(8, 3)] public void FromYCbCrSimd_ConvertCore(int size, int seed) { - ValidateConversion(JpegColorConverter.FromYCbCrSimd.ConvertCore, 3, size, size, seed, ValidateYCbCr); + JpegColorConverter.ComponentValues values = CreateRandomValues(3, size, seed); + Vector4[] result = new Vector4[size]; + + JpegColorConverter.FromYCbCrSimd.ConvertCore(values, result); + + for (int i = 0; i < size; i++) + { + ValidateYCbCr(values, result, i); + } } [Theory] [MemberData(nameof(CommonConversionData))] public void FromYCbCrSimd(int inputBufferLength, int resultBufferLength, int seed) { - ValidateConversion( + ValidateRgbToYCbCrConversion( new JpegColorConverter.FromYCbCrSimd(), 3, inputBufferLength, resultBufferLength, - seed, - ValidateYCbCr); + seed); } [Theory] @@ -91,13 +103,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg //JpegColorConverter.FromYCbCrSimdAvx2.LogPlz = s => this.Output.WriteLine(s); - ValidateConversion( + ValidateRgbToYCbCrConversion( new JpegColorConverter.FromYCbCrSimdAvx2(), 3, inputBufferLength, resultBufferLength, - seed, - ValidateYCbCr); + seed); } @@ -105,10 +116,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [MemberData(nameof(CommonConversionData))] public void ConvertFromYCbCr_WithDefaultConverter(int inputBufferLength, int resultBufferLength, int seed) { - ValidateConversion(JpegColorSpace.YCbCr, 3, inputBufferLength, resultBufferLength, seed, ValidateYCbCr); + ValidateConversion( + JpegColorSpace.YCbCr, + 3, + inputBufferLength, + resultBufferLength, + seed); } - // Becnhmark, for local execution only + // Benchmark, for local execution only //[Theory] //[InlineData(false)] //[InlineData(true)] @@ -120,11 +136,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg JpegColorConverter.ComponentValues values = CreateRandomValues(3, count, 1); Vector4[] result = new Vector4[count]; - JpegColorConverter converter = simd ? (JpegColorConverter)new JpegColorConverter.FromYCbCrSimd() : new JpegColorConverter.FromYCbCrBasic(); - + JpegColorConverter converter = simd ? (JpegColorConverter)new JpegColorConverter.FromYCbCrSimd() : new JpegColorConverter.FromYCbCrBasic(); + // Warm up: converter.ConvertToRGBA(values, result); - + using (new MeasureGuard(this.Output, $"{converter.GetType().Name} x {times}")) { for (int i = 0; i < times; i++) @@ -141,79 +157,79 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg var v = new Vector4(0, 0, 0, 1F); var scale = new Vector4(1 / 255F, 1 / 255F, 1 / 255F, 1F); - ValidateConversion( - JpegColorSpace.Cmyk, - 4, - inputBufferLength, - resultBufferLength, - seed, - (values, result, i) => - { - float c = values.Component0[i]; - float m = values.Component1[i]; - float y = values.Component2[i]; - float k = values.Component3[i] / 255F; - - v.X = c * k; - v.Y = m * k; - v.Z = y * k; - v.W = 1F; - - v *= scale; - - Vector4 rgba = result[i]; - var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); - var expected = new Rgb(v.X, v.Y, v.Z); - - Assert.True(actual.AlmostEquals(expected, Precision)); - Assert.Equal(1, rgba.W); - }); + var converter = JpegColorConverter.GetConverter(JpegColorSpace.Cmyk); + JpegColorConverter.ComponentValues values = CreateRandomValues(4, inputBufferLength, seed); + Vector4[] result = new Vector4[resultBufferLength]; + + converter.ConvertToRGBA(values, result); + + for (int i = 0; i < resultBufferLength; i++) + { + float c = values.Component0[i]; + float m = values.Component1[i]; + float y = values.Component2[i]; + float k = values.Component3[i] / 255F; + + v.X = c * k; + v.Y = m * k; + v.Z = y * k; + v.W = 1F; + + v *= scale; + + Vector4 rgba = result[i]; + var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); + var expected = new Rgb(v.X, v.Y, v.Z); + + Assert.True(actual.AlmostEquals(expected, Precision)); + Assert.Equal(1, rgba.W); + } } [Theory] [MemberData(nameof(CommonConversionData))] public void ConvertFromGrayScale(int inputBufferLength, int resultBufferLength, int seed) { - ValidateConversion( - JpegColorSpace.GrayScale, - 1, - inputBufferLength, - resultBufferLength, - seed, - (values, result, i) => - { - float y = values.Component0[i]; - Vector4 rgba = result[i]; - var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); - var expected = new Rgb(y / 255F, y / 255F, y / 255F); - - Assert.True(actual.AlmostEquals(expected, Precision)); - Assert.Equal(1, rgba.W); - }); + var converter = JpegColorConverter.GetConverter(JpegColorSpace.GrayScale); + JpegColorConverter.ComponentValues values = CreateRandomValues(1, inputBufferLength, seed); + Vector4[] result = new Vector4[resultBufferLength]; + + converter.ConvertToRGBA(values, result); + + for (int i = 0; i < resultBufferLength; i++) + { + float y = values.Component0[i]; + Vector4 rgba = result[i]; + var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); + var expected = new Rgb(y / 255F, y / 255F, y / 255F); + + Assert.True(actual.AlmostEquals(expected, Precision)); + Assert.Equal(1, rgba.W); + } } [Theory] [MemberData(nameof(CommonConversionData))] public void ConvertFromRgb(int inputBufferLength, int resultBufferLength, int seed) { - ValidateConversion( - JpegColorSpace.RGB, - 3, - inputBufferLength, - resultBufferLength, - seed, - (values, result, i) => - { - float r = values.Component0[i]; - float g = values.Component1[i]; - float b = values.Component2[i]; - Vector4 rgba = result[i]; - var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); - var expected = new Rgb(r / 255F, g / 255F, b / 255F); - - Assert.True(actual.AlmostEquals(expected, Precision)); - Assert.Equal(1, rgba.W); - }); + var converter = JpegColorConverter.GetConverter(JpegColorSpace.RGB); + JpegColorConverter.ComponentValues values = CreateRandomValues(3, inputBufferLength, seed); + Vector4[] result = new Vector4[resultBufferLength]; + + converter.ConvertToRGBA(values, result); + + for (int i = 0; i < resultBufferLength; i++) + { + float r = values.Component0[i]; + float g = values.Component1[i]; + float b = values.Component2[i]; + Vector4 rgba = result[i]; + var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); + var expected = new Rgb(r / 255F, g / 255F, b / 255F); + + Assert.True(actual.AlmostEquals(expected, Precision)); + Assert.Equal(1, rgba.W); + } } [Theory] @@ -223,35 +239,35 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg var v = new Vector4(0, 0, 0, 1F); var scale = new Vector4(1 / 255F, 1 / 255F, 1 / 255F, 1F); - ValidateConversion( - JpegColorSpace.Ycck, - 4, - inputBufferLength, - resultBufferLength, - seed, - (values, result, i) => - { - float y = values.Component0[i]; - float cb = values.Component1[i] - 128F; - float cr = values.Component2[i] - 128F; - float k = values.Component3[i] / 255F; - - v.X = (255F - (float)Math.Round(y + (1.402F * cr), MidpointRounding.AwayFromZero)) * k; - v.Y = (255F - (float)Math.Round( - y - (0.344136F * cb) - (0.714136F * cr), - MidpointRounding.AwayFromZero)) * k; - v.Z = (255F - (float)Math.Round(y + (1.772F * cb), MidpointRounding.AwayFromZero)) * k; - v.W = 1F; - - v *= scale; - - Vector4 rgba = result[i]; - var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); - var expected = new Rgb(v.X, v.Y, v.Z); - - Assert.True(actual.AlmostEquals(expected, Precision)); - Assert.Equal(1, rgba.W); - }); + var converter = JpegColorConverter.GetConverter(JpegColorSpace.Ycck); + JpegColorConverter.ComponentValues values = CreateRandomValues(4, inputBufferLength, seed); + Vector4[] result = new Vector4[resultBufferLength]; + + converter.ConvertToRGBA(values, result); + + for (int i = 0; i < resultBufferLength; i++) + { + float y = values.Component0[i]; + float cb = values.Component1[i] - 128F; + float cr = values.Component2[i] - 128F; + float k = values.Component3[i] / 255F; + + v.X = (255F - (float)Math.Round(y + (1.402F * cr), MidpointRounding.AwayFromZero)) * k; + v.Y = (255F - (float)Math.Round( + y - (0.344136F * cb) - (0.714136F * cr), + MidpointRounding.AwayFromZero)) * k; + v.Z = (255F - (float)Math.Round(y + (1.772F * cb), MidpointRounding.AwayFromZero)) * k; + v.W = 1F; + + v *= scale; + + Vector4 rgba = result[i]; + var actual = new Rgb(rgba.X, rgba.Y, rgba.Z); + var expected = new Rgb(v.X, v.Y, v.Z); + + Assert.True(actual.AlmostEquals(expected, Precision)); + Assert.Equal(1, rgba.W); + } } private static JpegColorConverter.ComponentValues CreateRandomValues( @@ -269,7 +285,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg for (int j = 0; j < inputBufferLength; j++) { - values[j] = (float)rnd.NextDouble() * (maxVal-minVal)+minVal; + values[j] = (float)rnd.NextDouble() * (maxVal - minVal) + minVal; } // no need to dispose when buffer is not array owner @@ -283,51 +299,31 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg int componentCount, int inputBufferLength, int resultBufferLength, - int seed, - Action, int> validatePixelValue) + int seed) { - ValidateConversion( + ValidateRgbToYCbCrConversion( JpegColorConverter.GetConverter(colorSpace), componentCount, inputBufferLength, resultBufferLength, - seed, - validatePixelValue); + seed); } - private static void ValidateConversion( + private static void ValidateRgbToYCbCrConversion( JpegColorConverter converter, int componentCount, int inputBufferLength, int resultBufferLength, - int seed, - Action, int> validatePixelValue) - { - ValidateConversion( - converter.ConvertToRGBA, - componentCount, - inputBufferLength, - resultBufferLength, - seed, - validatePixelValue); - } - - private static void ValidateConversion( - Action> doConvert, - int componentCount, - int inputBufferLength, - int resultBufferLength, - int seed, - Action, int> validatePixelValue) + int seed) { JpegColorConverter.ComponentValues values = CreateRandomValues(componentCount, inputBufferLength, seed); Vector4[] result = new Vector4[resultBufferLength]; - doConvert(values, result); + converter.ConvertToRGBA(values, result); for (int i = 0; i < resultBufferLength; i++) { - validatePixelValue(values, result, i); + ValidateYCbCr(values, result, i); } } } diff --git a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs index aefa32f46..f19fa1990 100644 --- a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs +++ b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs @@ -5,7 +5,6 @@ using System; using System.IO; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.IO; -using SixLabors.ImageSharp.PixelFormats; using Moq; using Xunit; @@ -18,10 +17,10 @@ namespace SixLabors.ImageSharp.Tests { private readonly Mock fileSystem; private readonly string FilePath; - private readonly Mock localMimeTypeDetector; + private readonly IImageFormatDetector localMimeTypeDetector; private readonly Mock localImageFormatMock; - public IImageFormat localImageFormat => localImageFormatMock.Object; + public IImageFormat localImageFormat => this.localImageFormatMock.Object; public Configuration LocalConfiguration { get; private set; } public byte[] Marker { get; private set; } public MemoryStream DataStream { get; private set; } @@ -32,9 +31,7 @@ namespace SixLabors.ImageSharp.Tests { this.localImageFormatMock = new Mock(); - this.localMimeTypeDetector = new Mock(); - this.localMimeTypeDetector.Setup(x => x.HeaderSize).Returns(1); - this.localMimeTypeDetector.Setup(x => x.DetectFormat(It.IsAny>())).Returns(localImageFormatMock.Object); + this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.fileSystem = new Mock(); @@ -42,7 +39,8 @@ namespace SixLabors.ImageSharp.Tests { FileSystem = this.fileSystem.Object }; - this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector.Object); + + this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector); TestFormat.RegisterGlobalTestFormat(); this.Marker = Guid.NewGuid().ToByteArray(); @@ -58,49 +56,49 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void DiscoverImageFormatByteArray() { - var type = Image.DetectFormat(DataStream.ToArray()); + IImageFormat type = Image.DetectFormat(this.DataStream.ToArray()); Assert.Equal(TestFormat.GlobalTestFormat, type); } [Fact] public void DiscoverImageFormatByteArray_WithConfig() { - var type = Image.DetectFormat(this.LocalConfiguration, DataStream.ToArray()); - Assert.Equal(localImageFormat, type); + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream.ToArray()); + Assert.Equal(this.localImageFormat, type); } [Fact] public void DiscoverImageFormatFile() { - var type = Image.DetectFormat(this.FilePath); + IImageFormat type = Image.DetectFormat(this.FilePath); Assert.Equal(TestFormat.GlobalTestFormat, type); } [Fact] public void DiscoverImageFormatFilePath_WithConfig() { - var type = Image.DetectFormat(this.LocalConfiguration, FilePath); - Assert.Equal(localImageFormat, type); + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.FilePath); + Assert.Equal(this.localImageFormat, type); } [Fact] public void DiscoverImageFormatStream() { - var type = Image.DetectFormat(this.DataStream); + IImageFormat type = Image.DetectFormat(this.DataStream); Assert.Equal(TestFormat.GlobalTestFormat, type); } [Fact] public void DiscoverImageFormatFileStream_WithConfig() { - var type = Image.DetectFormat(this.LocalConfiguration, DataStream); - Assert.Equal(localImageFormat, type); + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream); + Assert.Equal(this.localImageFormat, type); } [Fact] public void DiscoverImageFormatNoDetectorsRegisterdShouldReturnNull() { - var type = Image.DetectFormat(new Configuration(), DataStream); + IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream); Assert.Null(type); } } diff --git a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs index 2c0a30b15..de18714e2 100644 --- a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs @@ -14,13 +14,13 @@ namespace SixLabors.ImageSharp.Tests /// /// Tests the class. /// - public class ImageLoadTests : IDisposable + public partial class ImageLoadTests : IDisposable { private readonly Mock fileSystem; private Image returnImage; private Mock localDecoder; private readonly string FilePath; - private readonly Mock localMimeTypeDetector; + private readonly IImageFormatDetector localMimeTypeDetector; private readonly Mock localImageFormatMock; public Configuration LocalConfiguration { get; private set; } @@ -35,10 +35,7 @@ namespace SixLabors.ImageSharp.Tests this.localImageFormatMock = new Mock(); this.localDecoder = new Mock(); - this.localMimeTypeDetector = new Mock(); - this.localMimeTypeDetector.Setup(x => x.HeaderSize).Returns(1); - this.localMimeTypeDetector.Setup(x => x.DetectFormat(It.IsAny>())).Returns(localImageFormatMock.Object); - + this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.localDecoder.Setup(x => x.Decode(It.IsAny(), It.IsAny())) .Callback((c, s) => @@ -57,8 +54,8 @@ namespace SixLabors.ImageSharp.Tests { FileSystem = this.fileSystem.Object }; - this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector.Object); - this.LocalConfiguration.SetDecoder(localImageFormatMock.Object, this.localDecoder.Object); + this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector); + this.LocalConfiguration.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); TestFormat.RegisterGlobalTestFormat(); this.Marker = Guid.NewGuid().ToByteArray(); diff --git a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs index 5b672059c..7f6e3b7da 100644 --- a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs @@ -24,17 +24,14 @@ namespace SixLabors.ImageSharp.Tests private readonly Mock fileSystem; private readonly Mock encoder; private readonly Mock encoderNotInFormat; - private Mock localMimeTypeDetector; + private IImageFormatDetector localMimeTypeDetector; private Mock localImageFormat; public ImageSaveTests() { this.localImageFormat = new Mock(); this.localImageFormat.Setup(x => x.FileExtensions).Returns(new[] { "png" }); - - this.localMimeTypeDetector = new Mock(); - this.localMimeTypeDetector.Setup(x => x.HeaderSize).Returns(1); - this.localMimeTypeDetector.Setup(x => x.DetectFormat(It.IsAny>())).Returns(localImageFormat.Object); + this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormat.Object); this.encoder = new Mock(); @@ -45,8 +42,8 @@ namespace SixLabors.ImageSharp.Tests { FileSystem = this.fileSystem.Object }; - config.AddImageFormatDetector(this.localMimeTypeDetector.Object); - config.SetEncoder(localImageFormat.Object, this.encoder.Object); + config.AddImageFormatDetector(this.localMimeTypeDetector); + config.SetEncoder(this.localImageFormat.Object, this.encoder.Object); this.Image = new Image(config, 1, 1); } @@ -57,7 +54,7 @@ namespace SixLabors.ImageSharp.Tests { using (Image image = provider.GetImage()) { - TPixel[] buffer = new TPixel[image.Width*image.Height]; + TPixel[] buffer = new TPixel[image.Width * image.Height]; image.SavePixelData(buffer); image.ComparePixelBufferTo(buffer); @@ -73,14 +70,14 @@ namespace SixLabors.ImageSharp.Tests { using (Image image = provider.GetImage()) { - byte[] buffer = new byte[image.Width*image.Height*Unsafe.SizeOf()]; + byte[] buffer = new byte[image.Width * image.Height * Unsafe.SizeOf()]; image.SavePixelData(buffer); image.ComparePixelBufferTo(buffer.AsSpan().NonPortableCast()); } } - + [Fact] public void SavePixelData_Rgba32_WhenBufferIsTooSmall_Throws() { @@ -91,7 +88,7 @@ namespace SixLabors.ImageSharp.Tests img[0, 1] = Rgba32.Red; img[1, 1] = Rgba32.Blue; - var buffer = new byte[2 * 2]; // width * height * bytes per pixel + byte[] buffer = new byte[2 * 2]; // width * height * bytes per pixel Assert.Throws(() => { @@ -125,7 +122,7 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void ToBase64String() { - var str = this.Image.ToBase64String(localImageFormat.Object); + string str = this.Image.ToBase64String(this.localImageFormat.Object); this.encoder.Verify(x => x.Encode(this.Image, It.IsAny())); } @@ -134,7 +131,7 @@ namespace SixLabors.ImageSharp.Tests public void SaveStreamWithMime() { Stream stream = new MemoryStream(); - this.Image.Save(stream, localImageFormat.Object); + this.Image.Save(stream, this.localImageFormat.Object); this.encoder.Verify(x => x.Encode(this.Image, stream)); } diff --git a/tests/ImageSharp.Tests/Image/MockImageFormatDetector.cs b/tests/ImageSharp.Tests/Image/MockImageFormatDetector.cs new file mode 100644 index 000000000..cb09fa010 --- /dev/null +++ b/tests/ImageSharp.Tests/Image/MockImageFormatDetector.cs @@ -0,0 +1,28 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using SixLabors.ImageSharp.Formats; + +namespace SixLabors.ImageSharp.Tests +{ + /// + /// You can't mock the "DetectFormat" method due to the ReadOnlySpan{byte} parameter. + /// + public class MockImageFormatDetector : IImageFormatDetector + { + private IImageFormat localImageFormatMock; + + public MockImageFormatDetector(IImageFormat imageFormat) + { + this.localImageFormatMock = imageFormat; + } + + public int HeaderSize => 1; + + public IImageFormat DetectFormat(ReadOnlySpan header) + { + return this.localImageFormatMock; + } + } +}