Browse Source

full coverage for Image.Load (I hope)

pull/904/head
Anton Firszov 7 years ago
parent
commit
fcaaa6fa39
  1. 83
      tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs
  2. 100
      tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_PassLocalConfiguration.cs
  3. 103
      tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs
  4. 4
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs
  5. 60
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs
  6. 3
      tests/ImageSharp.Tests/ImageSharp.Tests.csproj

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

@ -1,83 +0,0 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
using Moq;
using SixLabors.ImageSharp.IO;
public partial class ImageTests
{
public class Load_FileSystemPath : ImageLoadTestBase
{
[Fact]
public void BasicCase()
{
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.MockFilePath);
Assert.NotNull(img);
this.TestFormat.VerifySpecificDecodeCall<Rgba32>(this.Marker, this.TopLevelConfiguration);
}
[Fact]
public void UseLocalConfiguration()
{
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.MockFilePath);
Assert.NotNull(img);
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, this.DataStream));
}
[Fact]
public void UseCustomDecoder()
{
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));
}
[Fact]
public void UseGlobalConfigration()
{
var file = TestFile.Create(TestImages.Bmp.Car);
using (var image = Image.Load<Rgba32>(file.FullPath))
{
Assert.Equal(600, image.Width);
Assert.Equal(450, image.Height);
}
}
[Fact]
public void WhenFileNotFound_Throws()
{
System.IO.FileNotFoundException ex = Assert.Throws<System.IO.FileNotFoundException>(
() =>
{
Image.Load<Rgba32>(Guid.NewGuid().ToString());
});
}
[Fact]
public void WhenPathIsNull_Throws()
{
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(
() =>
{
Image.Load<Rgba32>((string)null);
});
}
}
}
}

100
tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_PassLocalConfiguration.cs

@ -0,0 +1,100 @@
// // Copyright (c) Six Labors and contributors.
// // Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
namespace SixLabors.ImageSharp.Tests
{
public partial class ImageTests
{
public class Load_FileSystemPath_PassLocalConfiguration : ImageLoadTestBase
{
[Fact]
public void Configuration_Path_Specific()
{
var img = Image.Load<Rgb24>(this.TopLevelConfiguration, this.MockFilePath);
Assert.NotNull(img);
Assert.Equal(this.TestFormat.Sample<Rgb24>(), img);
this.TestFormat.VerifySpecificDecodeCall<Rgb24>(this.Marker, this.TopLevelConfiguration);
}
[Fact]
public void Configuration_Path_Agnostic()
{
var img = Image.Load(this.TopLevelConfiguration, this.MockFilePath);
Assert.NotNull(img);
Assert.Equal(this.TestFormat.SampleAgnostic(), img);
this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration);
}
[Fact]
public void Configuration_Path_Decoder_Specific()
{
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));
}
[Fact]
public void Configuration_Path_Decoder_Agnostic()
{
var img = Image.Load(this.TopLevelConfiguration, this.MockFilePath, this.localDecoder.Object);
Assert.NotNull(img);
this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, this.DataStream));
}
[Fact]
public void Configuration_Path_OutFormat_Specific()
{
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.MockFilePath, out IImageFormat format);
Assert.NotNull(img);
Assert.Equal(this.TestFormat, format);
this.TestFormat.VerifySpecificDecodeCall<Rgba32>(this.Marker, this.TopLevelConfiguration);
}
[Fact]
public void Configuration_Path_OutFormat_Agnostic()
{
var img = Image.Load(this.TopLevelConfiguration, this.MockFilePath, out IImageFormat format);
Assert.NotNull(img);
Assert.Equal(this.TestFormat, format);
this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration);
}
[Fact]
public void WhenFileNotFound_Throws()
{
System.IO.FileNotFoundException ex = Assert.Throws<System.IO.FileNotFoundException>(
() =>
{
Image.Load<Rgba32>(this.TopLevelConfiguration, Guid.NewGuid().ToString());
});
}
[Fact]
public void WhenPathIsNull_Throws()
{
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(
() =>
{
Image.Load<Rgba32>(this.TopLevelConfiguration,(string)null);
});
}
}
}
}

103
tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs

@ -0,0 +1,103 @@
// // Copyright (c) Six Labors and contributors.
// // Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
using Xunit;
namespace SixLabors.ImageSharp.Tests
{
public partial class ImageTests
{
public class Load_FileSystemPath_UseDefaultConfiguration
{
private string Path { get; } = TestFile.GetInputFileFullPath(TestImages.Bmp.Bit8);
private static void VerifyDecodedImage(Image img)
{
Assert.Equal(new Size(127, 64), img.Size());
}
[Fact]
public void Path_Specific()
{
using (var img = Image.Load<Rgba32>(this.Path))
{
VerifyDecodedImage(img);
}
}
[Fact]
public void Path_Agnostic()
{
using (var img = Image.Load(this.Path))
{
VerifyDecodedImage(img);
}
}
[Fact]
public void Path_Decoder_Specific()
{
using (var img = Image.Load<Rgba32>(this.Path, new BmpDecoder()))
{
VerifyDecodedImage(img);
}
}
[Fact]
public void Path_Decoder_Agnostic()
{
using (var img = Image.Load(this.Path, new BmpDecoder()))
{
VerifyDecodedImage(img);
}
}
[Fact]
public void Path_OutFormat_Specific()
{
using (var img = Image.Load<Rgba32>(this.Path, out IImageFormat format))
{
VerifyDecodedImage(img);
Assert.IsType<BmpFormat>(format);
}
}
[Fact]
public void Path_OutFormat_Agnostic()
{
using (var img = Image.Load(this.Path, out IImageFormat format))
{
VerifyDecodedImage(img);
Assert.IsType<BmpFormat>(format);
}
}
[Fact]
public void WhenFileNotFound_Throws()
{
System.IO.FileNotFoundException ex = Assert.Throws<System.IO.FileNotFoundException>(
() =>
{
Image.Load<Rgba32>(Guid.NewGuid().ToString());
});
}
[Fact]
public void WhenPathIsNull_Throws()
{
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(
() =>
{
Image.Load<Rgba32>((string)null);
});
}
}
}
}

4
tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs

@ -17,13 +17,13 @@ namespace SixLabors.ImageSharp.Tests
{
public class Load_FromBytes_UseGlobalConfiguration
{
private static byte[] ByteArray { get; } = TestFile.Create(TestImages.Bmp.F).Bytes;
private static byte[] ByteArray { get; } = TestFile.Create(TestImages.Bmp.Bit8).Bytes;
private static Span<byte> ByteSpan => new Span<byte>(ByteArray);
private static void VerifyDecodedImage(Image img)
{
Assert.Equal(new Size(108, 202), img.Size());
Assert.Equal(new Size(127, 64), img.Size());
}
[Theory]

60
tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using SixLabors.ImageSharp.Formats;
@ -14,42 +15,41 @@ namespace SixLabors.ImageSharp.Tests
{
public partial class ImageTests
{
public class Load_FromStream_UseDefaultConfiguration
public class Load_FromStream_UseDefaultConfiguration : IDisposable
{
private static readonly byte[] Data = TestFile.Create(TestImages.Bmp.Bit8).Bytes;
private MemoryStream Stream { get; } = new MemoryStream(Data);
private static void VerifyDecodedImage(Image img)
{
Assert.Equal(new Size(127, 64), img.Size());
}
[Fact]
public void Stream_Specific()
{
byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes;
using (var stream = new MemoryStream(data))
using (var img = Image.Load<Rgba32>(stream))
using (var img = Image.Load<Rgba32>(this.Stream))
{
Assert.Equal(new Size(108, 202), img.Size());
VerifyDecodedImage(img);
}
}
[Fact]
public void Stream_Agnostic()
{
byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes;
using (var stream = new MemoryStream(data))
using (var img = Image.Load(stream))
using (var img = Image.Load(this.Stream))
{
Assert.Equal(new Size(108, 202), img.Size());
VerifyDecodedImage(img);
}
}
[Fact]
public void Stream_OutFormat_Specific()
{
byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes;
using (var stream = new MemoryStream(data))
using (var img = Image.Load<Rgba32>(stream, out IImageFormat format))
using (var img = Image.Load<Rgba32>(this.Stream, out IImageFormat format))
{
Assert.Equal(new Size(108, 202), img.Size());
VerifyDecodedImage(img);
Assert.IsType<BmpFormat>(format);
}
}
@ -57,39 +57,35 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void Stream_Decoder_Specific()
{
byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes;
using (var stream = new MemoryStream(data))
using (var img = Image.Load<Rgba32>(stream, new BmpDecoder()))
using (var img = Image.Load<Rgba32>(this.Stream, new BmpDecoder()))
{
Assert.Equal(new Size(108, 202), img.Size());
VerifyDecodedImage(img);
}
}
[Fact]
public void Stream_Decoder_Agnostic()
{
byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes;
using (var stream = new MemoryStream(data))
using (var img = Image.Load(stream, new BmpDecoder()))
using (var img = Image.Load(this.Stream, new BmpDecoder()))
{
Assert.Equal(new Size(108, 202), img.Size());
VerifyDecodedImage(img);
}
}
[Fact]
public void Stream_OutFormat_Agnostic()
{
byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes;
using (var stream = new MemoryStream(data))
using (var img = Image.Load(stream, out IImageFormat format))
using (var img = Image.Load(this.Stream, out IImageFormat format))
{
Assert.Equal(new Size(108, 202), img.Size());
VerifyDecodedImage(img);
Assert.IsType<BmpFormat>(format);
}
}
public void Dispose()
{
this.Stream?.Dispose();
}
}
}
}

3
tests/ImageSharp.Tests/ImageSharp.Tests.csproj

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<TargetFrameworks>netcoreapp2.1;net462;net472</TargetFrameworks>-->
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFrameworks>netcoreapp2.1;net462;net472</TargetFrameworks>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<DebugType Condition="$(codecov) != ''">full</DebugType>

Loading…
Cancel
Save