mirror of https://github.com/SixLabors/ImageSharp
8 changed files with 246 additions and 14 deletions
@ -0,0 +1,31 @@ |
|||
// <copyright file="Endianness.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
using System.IO; |
|||
|
|||
namespace ImageSharp.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 OpenWrite(string path); |
|||
} |
|||
#endif
|
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
|
|||
namespace ImageSharp.IO |
|||
{ |
|||
#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 OpenWrite(string path) |
|||
{ |
|||
return File.OpenWrite(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 (var r = new StreamReader(fs.OpenRead(path))) |
|||
{ |
|||
string data = r.ReadToEnd(); |
|||
|
|||
Assert.Equal(testData, data); |
|||
} |
|||
|
|||
File.Delete(path); |
|||
} |
|||
|
|||
[Fact] |
|||
public void OpenWrite() |
|||
{ |
|||
string path = Path.GetTempFileName(); |
|||
string testData = Guid.NewGuid().ToString(); |
|||
LocalFileSystem fs = new LocalFileSystem(); |
|||
|
|||
using (var r = new StreamWriter(fs.OpenWrite(path))) |
|||
{ |
|||
r.Write(testData); |
|||
} |
|||
|
|||
string data = File.ReadAllText(path); |
|||
Assert.Equal(testData, data); |
|||
|
|||
File.Delete(path); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
// <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 SaveWatchingImage Image; |
|||
private readonly Mock<IFileSystem> fileSystem; |
|||
|
|||
public ImageSaveTests() |
|||
{ |
|||
this.fileSystem = new Mock<IFileSystem>(); |
|||
this.Image = new SaveWatchingImage(1, 1, this.fileSystem.Object); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SavePath() |
|||
{ |
|||
Stream stream = new MemoryStream(); |
|||
this.fileSystem.Setup(x => x.OpenWrite("path")).Returns(stream); |
|||
this.Image.Save("path"); |
|||
|
|||
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); |
|||
Assert.Equal(stream, operation.stream); |
|||
Assert.Equal(this.Image.CurrentImageFormat.Encoder, operation.encoder); |
|||
Assert.Null(operation.options); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
this.Image.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using ImageSharp; |
|||
using Processing; |
|||
using System.Collections.Generic; |
|||
using ImageSharp.Formats; |
|||
using ImageSharp.IO; |
|||
|
|||
/// <summary>
|
|||
/// Watches but does not actually run the processors against the image.
|
|||
/// </summary>
|
|||
/// <seealso cref="ImageSharp.Image{ImageSharp.Color}" />
|
|||
public class SaveWatchingImage : Image<Color> |
|||
{ |
|||
public List<OperationDetails> Saves { get; } = new List<OperationDetails>(); |
|||
|
|||
public SaveWatchingImage(int width, int height, IFileSystem fs = null) |
|||
: base(width, height, Configuration.CreateDefaultInstance()) |
|||
{ |
|||
//switch out the file system for tests
|
|||
this.Configuration.FileSystem = fs ?? this.Configuration.FileSystem; |
|||
} |
|||
|
|||
internal override void SaveInternal(Stream stream, IImageEncoder encoder, IEncoderOptions options) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public struct OperationDetails |
|||
{ |
|||
public Stream stream; |
|||
public IImageEncoder encoder; |
|||
public IEncoderOptions options; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue