Add tests for OriginalFilenameFileOutputPathFormatter

This commit is contained in:
Denis-Cosmin Nutiu 2022-01-25 22:41:02 +02:00
parent f6de032d84
commit 6f74af0f1d
3 changed files with 38 additions and 3 deletions

View file

@ -0,0 +1,28 @@
using System;
using Image;
using Xunit;
namespace ImageCore.Tests
{
public class TestOriginalFilenameFileOutputPathFormatter
{
[Theory]
[InlineData("", "./", @".\.jpg")]
[InlineData("", @".\", @".\.jpg")]
[InlineData("", "asd", @".\asd.jpg")]
[InlineData("dir", "asd", @"dir\asd.jpg")]
public void TestGetOutputPath(string directory, string file, string expectedPath)
{
var outputPathFormatter = OriginalFilenameFileOutputPathFormatter.Create(directory);
Assert.Equal(expectedPath, outputPathFormatter.GetOutputPath(file));
}
[Fact]
public void TestGetOutputPathNull()
{
var outputPathFormatter = OriginalFilenameFileOutputPathFormatter.Create("directory");
Assert.Throws<ArgumentException>(() => outputPathFormatter.GetOutputPath(""));
}
}
}

View file

@ -6,6 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Guard.Net" Version="2.0.0" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="8.6.1" /> <PackageReference Include="Magick.NET-Q16-AnyCPU" Version="8.6.1" />
<PackageReference Include="Magick.NET.Core" Version="8.6.1" /> <PackageReference Include="Magick.NET.Core" Version="8.6.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />

View file

@ -1,4 +1,5 @@
using System.IO; using System.IO;
using GuardNet;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
@ -19,6 +20,10 @@ namespace Image
/// <param name="outputDirectory">The output directory.</param> /// <param name="outputDirectory">The output directory.</param>
public OriginalFilenameFileOutputPathFormatter(string outputDirectory) public OriginalFilenameFileOutputPathFormatter(string outputDirectory)
{ {
if (outputDirectory.Equals(""))
{
outputDirectory = ".";
}
_outputDirectory = outputDirectory; _outputDirectory = outputDirectory;
} }
@ -29,6 +34,7 @@ namespace Image
/// <returns>An absolute path of the form output_directory/initialFileName.jpg</returns> /// <returns>An absolute path of the form output_directory/initialFileName.jpg</returns>
public string GetOutputPath(string initialFilePath) public string GetOutputPath(string initialFilePath)
{ {
Guard.NotNullOrEmpty(initialFilePath, nameof(initialFilePath));
var fileName = Path.GetFileName(initialFilePath)?.Split(".")[0]; var fileName = Path.GetFileName(initialFilePath)?.Split(".")[0];
var path = Path.Join(_outputDirectory, $"{fileName}.jpg"); var path = Path.Join(_outputDirectory, $"{fileName}.jpg");
return path; return path;