Browse Source

refactor tests for DetectFormat

pull/618/head
Anton Firszov 8 years ago
parent
commit
e63535ee69
  1. 104
      tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs
  2. 90
      tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs
  3. 12
      tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs
  4. 19
      tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs
  5. 19
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs
  6. 14
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs

104
tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs

@ -1,104 +0,0 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.IO;
using Moq;
using Xunit;
namespace SixLabors.ImageSharp.Tests
{
/// <summary>
/// Tests the <see cref="Image"/> class.
/// </summary>
public class DiscoverImageFormatTests
{
private readonly Mock<IFileSystem> fileSystem;
private readonly string FilePath;
private readonly IImageFormatDetector localMimeTypeDetector;
private readonly Mock<IImageFormat> localImageFormatMock;
public IImageFormat localImageFormat => this.localImageFormatMock.Object;
public Configuration LocalConfiguration { get; private set; }
public byte[] Marker { get; private set; }
public MemoryStream DataStream { get; private set; }
public byte[] DecodedData { get; private set; }
private const string localMimeType = "image/local";
public DiscoverImageFormatTests()
{
this.localImageFormatMock = new Mock<IImageFormat>();
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object);
this.fileSystem = new Mock<IFileSystem>();
this.LocalConfiguration = new Configuration
{
FileSystem = this.fileSystem.Object
};
this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector);
this.Marker = Guid.NewGuid().ToByteArray();
this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker);
this.FilePath = Guid.NewGuid().ToString();
this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream);
TestFileSystem.RegisterGlobalTestFormat();
TestFileSystem.Global.AddFile(this.FilePath, this.DataStream);
}
[Fact]
public void DiscoverImageFormatByteArray()
{
IImageFormat type = Image.DetectFormat(this.DataStream.ToArray());
Assert.Equal(TestFormat.GlobalTestFormat, type);
}
[Fact]
public void DiscoverImageFormatByteArray_WithConfig()
{
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream.ToArray());
Assert.Equal(this.localImageFormat, type);
}
[Fact]
public void DiscoverImageFormatFile()
{
IImageFormat type = Image.DetectFormat(this.FilePath);
Assert.Equal(TestFormat.GlobalTestFormat, type);
}
[Fact]
public void DiscoverImageFormatFilePath_WithConfig()
{
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.FilePath);
Assert.Equal(this.localImageFormat, type);
}
[Fact]
public void DiscoverImageFormatStream()
{
IImageFormat type = Image.DetectFormat(this.DataStream);
Assert.Equal(TestFormat.GlobalTestFormat, type);
}
[Fact]
public void DiscoverImageFormatFileStream_WithConfig()
{
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream);
Assert.Equal(this.localImageFormat, type);
}
[Fact]
public void DiscoverImageFormatNoDetectorsRegisterdShouldReturnNull()
{
IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream);
Assert.Null(type);
}
}
}

90
tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs

@ -0,0 +1,90 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.IO;
using Moq;
using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
public partial class ImageTests
{
/// <summary>
/// Tests the <see cref="Image"/> class.
/// </summary>
public class DetectFormat : ImageLoadTestBase
{
private static readonly string ActualImagePath = TestFile.GetInputFileFullPath(TestImages.Bmp.F);
private byte[] ActualImageBytes => TestFile.Create(TestImages.Bmp.F).Bytes;
private ReadOnlySpan<byte> ActualImageSpan => this.ActualImageBytes.AsSpan();
private byte[] ByteArray => this.DataStream.ToArray();
private ReadOnlySpan<byte> ByteSpan => this.ByteArray.AsSpan();
private IImageFormat LocalImageFormat => this.localImageFormatMock.Object;
private static readonly IImageFormat ExpectedGlobalFormat =
Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp");
[Fact]
public void FromBytes_GlobalConfiguration()
{
IImageFormat type = Image.DetectFormat(this.ActualImageBytes);
Assert.Equal(ExpectedGlobalFormat, type);
}
[Fact]
public void FromBytes_CustomConfiguration()
{
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.ByteArray);
Assert.Equal(this.LocalImageFormat, type);
}
[Fact]
public void FromFileSystemPath_GlobalConfiguration()
{
IImageFormat type = Image.DetectFormat(ActualImagePath);
Assert.Equal(ExpectedGlobalFormat, type);
}
[Fact]
public void FromFileSystemPath_CustomConfiguration()
{
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.MockFilePath);
Assert.Equal(this.LocalImageFormat, type);
}
[Fact]
public void FromStream_GlobalConfiguration()
{
using (var stream = new MemoryStream(this.ActualImageBytes))
{
IImageFormat type = Image.DetectFormat(stream);
Assert.Equal(ExpectedGlobalFormat, type);
}
}
[Fact]
public void FromStream_CustomConfiguration()
{
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream);
Assert.Equal(this.LocalImageFormat, type);
}
[Fact]
public void WhenNoMatchingFormatFound_ReturnsNull()
{
IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream);
Assert.Null(type);
}
}
}
}

12
tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs

@ -21,6 +21,12 @@
protected Mock<IImageFormat> localImageFormatMock;
protected readonly string MockFilePath = Guid.NewGuid().ToString();
internal readonly Mock<IFileSystem> localFileSystemMock = new Mock<IFileSystem>();
protected readonly TestFileSystem topLevelFileSystem = new TestFileSystem();
public Configuration LocalConfiguration { get; }
public TestFormat TestFormat { get; } = new TestFormat();
@ -67,8 +73,12 @@
this.Marker = Guid.NewGuid().ToByteArray();
this.DataStream = this.TestFormat.CreateStream(this.Marker);
this.localFileSystemMock.Setup(x => x.OpenRead(this.MockFilePath)).Returns(this.DataStream);
this.topLevelFileSystem.AddFile(this.MockFilePath, this.DataStream);
this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object;
this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem;
}
public void Dispose()
{
// clean up the global object;

19
tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs

@ -16,23 +16,10 @@ namespace SixLabors.ImageSharp.Tests
{
public class Load_FileSystemPath : ImageLoadTestBase
{
private readonly string filePath = Guid.NewGuid().ToString();
private readonly Mock<IFileSystem> localFileSystemMock = new Mock<IFileSystem>();
private readonly TestFileSystem topLevelFileSystem = new TestFileSystem();
public Load_FileSystemPath()
{
this.localFileSystemMock.Setup(x => x.OpenRead(this.filePath)).Returns(this.DataStream);
this.topLevelFileSystem.AddFile(this.filePath, this.DataStream);
this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object;
this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem;
}
[Fact]
public void BasicCase()
{
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.filePath);
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.MockFilePath);
Assert.NotNull(img);
@ -42,7 +29,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void UseLocalConfiguration()
{
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.filePath);
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.MockFilePath);
Assert.NotNull(img);
@ -52,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void UseCustomDecoder()
{
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.filePath, this.localDecoder.Object);
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.MockFilePath, this.localDecoder.Object);
Assert.NotNull(img);
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.TopLevelConfiguration, this.DataStream));

19
tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs

@ -95,9 +95,11 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(true)]
public void UseGlobalConfiguration(bool useSpan)
{
Image<Rgba32> img = useSpan ? Image.Load(this.ActualImageSpan) : Image.Load(this.ActualImageBytes);
Assert.Equal(new Size(108, 202), img.Size());
using (Image<Rgba32> img =
useSpan ? Image.Load(this.ActualImageSpan) : Image.Load(this.ActualImageBytes))
{
Assert.Equal(new Size(108, 202), img.Size());
}
}
[Theory]
@ -105,11 +107,12 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(true)]
public void UseGlobalConfiguration_NonDefaultPixelType(bool useSpan)
{
Image<Rgb24> img = useSpan
? Image.Load<Rgb24>(this.ActualImageSpan)
: Image.Load<Rgb24>(this.ActualImageBytes);
Assert.Equal(new Size(108, 202), img.Size());
using (Image<Rgb24> img = useSpan
? Image.Load<Rgb24>(this.ActualImageSpan)
: Image.Load<Rgb24>(this.ActualImageBytes))
{
Assert.Equal(new Size(108, 202), img.Size());
}
}
}
}

14
tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs

@ -11,6 +11,8 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests
{
using SixLabors.Primitives;
public partial class ImageTests
{
/// <summary>
@ -29,6 +31,18 @@ namespace SixLabors.ImageSharp.Tests
this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration);
}
[Fact]
public void UseGlobalConfiguration()
{
byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes;
using (var stream = new MemoryStream(data))
using (var img = Image.Load(stream))
{
Assert.Equal(new Size(108, 202), img.Size());
}
}
[Fact]
public void NonDefaultPixelTypeImage()
{

Loading…
Cancel
Save