mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
8 changed files with 352 additions and 26 deletions
@ -0,0 +1,31 @@ |
|||
// <copyright file="IFileSystem.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.IO |
|||
{ |
|||
using System.IO; |
|||
|
|||
#if !NETSTANDARD1_1
|
|||
/// <summary>
|
|||
/// A simple interface representing the filesystem.
|
|||
/// </summary>
|
|||
public interface IFileSystem |
|||
{ |
|||
/// <summary>
|
|||
/// Returns a readable stream as defined by the path.
|
|||
/// </summary>
|
|||
/// <param name="path">Path to the file to open.</param>
|
|||
/// <returns>A stream representing the file to open.</returns>
|
|||
Stream OpenRead(string path); |
|||
|
|||
/// <summary>
|
|||
/// Creates or opens a file and returns it as a writeable stream as defined by the path.
|
|||
/// </summary>
|
|||
/// <param name="path">Path to the file to open.</param>
|
|||
/// <returns>A stream representing the file to open.</returns>
|
|||
Stream Create(string path); |
|||
} |
|||
#endif
|
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
// <copyright file="LocalFileSystem.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.IO |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
|
|||
#if !NETSTANDARD1_1
|
|||
/// <summary>
|
|||
/// A wrapper around the local File apis.
|
|||
/// </summary>
|
|||
public class LocalFileSystem : IFileSystem |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public Stream OpenRead(string path) |
|||
{ |
|||
return File.OpenRead(path); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public Stream Create(string path) |
|||
{ |
|||
return File.Create(path); |
|||
} |
|||
} |
|||
#endif
|
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
// <copyright file="LittleEndianBitConverterTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests.IO |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using ImageSharp.IO; |
|||
|
|||
using Xunit; |
|||
|
|||
public class LocalFileSystemTests |
|||
{ |
|||
[Fact] |
|||
public void OpenRead() |
|||
{ |
|||
string path = Path.GetTempFileName(); |
|||
string testData = Guid.NewGuid().ToString(); |
|||
File.WriteAllText(path, testData); |
|||
|
|||
LocalFileSystem fs = new LocalFileSystem(); |
|||
|
|||
using (StreamReader r = new StreamReader(fs.OpenRead(path))) |
|||
{ |
|||
string data = r.ReadToEnd(); |
|||
|
|||
Assert.Equal(testData, data); |
|||
} |
|||
|
|||
File.Delete(path); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Create() |
|||
{ |
|||
string path = Path.GetTempFileName(); |
|||
string testData = Guid.NewGuid().ToString(); |
|||
LocalFileSystem fs = new LocalFileSystem(); |
|||
|
|||
using (StreamWriter r = new StreamWriter(fs.Create(path))) |
|||
{ |
|||
r.Write(testData); |
|||
} |
|||
|
|||
string data = File.ReadAllText(path); |
|||
Assert.Equal(testData, data); |
|||
|
|||
File.Delete(path); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,186 @@ |
|||
// <copyright file="PixelAccessorTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using ImageSharp.Formats; |
|||
using ImageSharp.IO; |
|||
using Moq; |
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="Image"/> class.
|
|||
/// </summary>
|
|||
public class ImageSaveTests : IDisposable |
|||
{ |
|||
private readonly Image Image; |
|||
private readonly Mock<IFileSystem> fileSystem; |
|||
private readonly Mock<IImageFormat> format; |
|||
private readonly Mock<IImageFormat> formatNotRegistered; |
|||
private readonly Mock<IImageEncoder> encoder; |
|||
private readonly Mock<IImageEncoder> encoderNotInFormat; |
|||
private readonly IEncoderOptions encoderOptions; |
|||
|
|||
public ImageSaveTests() |
|||
{ |
|||
this.encoder = new Mock<IImageEncoder>(); |
|||
this.format = new Mock<IImageFormat>(); |
|||
this.format.Setup(x => x.Encoder).Returns(this.encoder.Object); |
|||
this.format.Setup(x => x.Decoder).Returns(new Mock<IImageDecoder>().Object); |
|||
this.format.Setup(x => x.MimeType).Returns("img/test"); |
|||
this.format.Setup(x => x.Extension).Returns("png"); |
|||
this.format.Setup(x => x.SupportedExtensions).Returns(new string[] { "png", "jpg" }); |
|||
|
|||
|
|||
this.encoderNotInFormat = new Mock<IImageEncoder>(); |
|||
this.formatNotRegistered = new Mock<IImageFormat>(); |
|||
this.formatNotRegistered.Setup(x => x.Encoder).Returns(this.encoderNotInFormat.Object); |
|||
this.formatNotRegistered.Setup(x => x.Decoder).Returns(new Mock<IImageDecoder>().Object); |
|||
this.formatNotRegistered.Setup(x => x.MimeType).Returns("img/test"); |
|||
this.formatNotRegistered.Setup(x => x.Extension).Returns("png"); |
|||
this.formatNotRegistered.Setup(x => x.SupportedExtensions).Returns(new string[] { "png", "jpg" }); |
|||
|
|||
this.fileSystem = new Mock<IFileSystem>(); |
|||
this.encoderOptions = new Mock<IEncoderOptions>().Object; |
|||
this.Image = new Image(1, 1, new Configuration(this.format.Object) { |
|||
FileSystem = this.fileSystem.Object |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SavePath() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.fileSystem.Setup(x => x.Create("path.png")).Returns(stream); |
|||
this.Image.Save("path.png"); |
|||
|
|||
this.encoder.Verify(x => x.Encode<Color>(this.Image, stream, null)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SavePathWithOptions() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); |
|||
|
|||
this.Image.Save("path.jpg", this.encoderOptions); |
|||
|
|||
this.encoder.Verify(x => x.Encode<Color>(this.Image, stream, this.encoderOptions)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SavePathWithEncoder() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); |
|||
|
|||
this.Image.Save("path.jpg", this.encoderNotInFormat.Object); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, null)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SavePathWithEncoderAndOptions() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); |
|||
|
|||
this.Image.Save("path.jpg", this.encoderNotInFormat.Object, this.encoderOptions); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, this.encoderOptions)); |
|||
} |
|||
|
|||
|
|||
|
|||
[Fact] |
|||
public void SavePathWithFormat() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); |
|||
|
|||
this.Image.Save("path.jpg", this.encoderNotInFormat.Object); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, null)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SavePathWithFormatAndOptions() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); |
|||
|
|||
this.Image.Save("path.jpg", this.encoderNotInFormat.Object, this.encoderOptions); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, this.encoderOptions)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SaveStream() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.Image.Save(stream); |
|||
|
|||
this.encoder.Verify(x => x.Encode<Color>(this.Image, stream, null)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SaveStreamWithOptions() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
|
|||
this.Image.Save(stream, this.encoderOptions); |
|||
|
|||
this.encoder.Verify(x => x.Encode<Color>(this.Image, stream, this.encoderOptions)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SaveStreamWithEncoder() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
|
|||
this.Image.Save(stream, this.encoderNotInFormat.Object); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, null)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SaveStreamWithEncoderAndOptions() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
|
|||
this.Image.Save(stream, this.encoderNotInFormat.Object, this.encoderOptions); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, this.encoderOptions)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SaveStreamWithFormat() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
|
|||
this.Image.Save(stream, this.formatNotRegistered.Object); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, null)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SaveStreamWithFormatAndOptions() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
|
|||
this.Image.Save(stream, this.formatNotRegistered.Object, this.encoderOptions); |
|||
|
|||
this.encoderNotInFormat.Verify(x => x.Encode<Color>(this.Image, stream, this.encoderOptions)); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
this.Image.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue