Skip to content

Commit b7b324f

Browse files
committed
tests: add unit tests for FileService
1 parent 16777a4 commit b7b324f

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using PrivatePdfConverter.Services;
2+
3+
namespace PrivatePdfConverter.Tests.UnitTests;
4+
5+
public sealed class FileServiceUnitTests
6+
{
7+
[Theory]
8+
[InlineData(".jpg", true)]
9+
[InlineData(".jpeg", true)]
10+
[InlineData(".png", true)]
11+
[InlineData(".bmp", true)]
12+
[InlineData(".gif", true)]
13+
[InlineData(".tif", true)]
14+
[InlineData(".tiff", true)]
15+
[InlineData(".webp", true)]
16+
[InlineData(".JPG", true)]
17+
[InlineData(".txt", false)]
18+
[InlineData(".pdf", false)]
19+
[InlineData("", false)]
20+
[InlineData(".unknown", false)]
21+
[InlineData(null, false)]
22+
public void IsImage_ShouldReturnCorrectResult(string? extension, bool expected)
23+
{
24+
// Act
25+
var result = extension.IsImage();
26+
27+
// Assert
28+
result.Should().Be(expected);
29+
}
30+
31+
[Fact]
32+
public void PrepareOutputFileName_WithOutputProvided_ShouldReturnOutputWithPdfExtension()
33+
{
34+
// Arrange
35+
const string output = "test";
36+
const string sourcePath = "dummy.pdf";
37+
38+
// Act
39+
var result = output.PrepareOutputFileName(sourcePath);
40+
41+
// Assert
42+
result.Should().Be("test.pdf");
43+
}
44+
45+
[Fact]
46+
public void PrepareOutputFileName_WithOutputPdf_ShouldReturnAsIs()
47+
{
48+
// Arrange
49+
const string output = "test.pdf";
50+
const string sourcePath = "dummy.jpg";
51+
52+
// Act
53+
var result = output.PrepareOutputFileName(sourcePath);
54+
55+
// Assert
56+
result.Should().Be("test.pdf");
57+
}
58+
59+
[Fact]
60+
public void PrepareOutputFileName_WithNullOutput_ShouldDeriveFromSourcePath()
61+
{
62+
// Arrange
63+
string? output = null;
64+
var sourcePath = Path.Combine("images", "photo.jpg");
65+
66+
// Act
67+
var result = output.PrepareOutputFileName(sourcePath);
68+
69+
// Assert
70+
result.Should().Be("photo.pdf");
71+
}
72+
73+
[Fact]
74+
public void PrepareOutputFileName_WithEmptyOutput_ShouldDeriveFromSourcePath()
75+
{
76+
// Arrange
77+
const string output = "";
78+
var sourcePath = Path.Combine("path", "to", "file.png");
79+
80+
// Act
81+
var result = output.PrepareOutputFileName(sourcePath);
82+
83+
// Assert
84+
result.Should().Be("file.pdf");
85+
}
86+
87+
[Fact]
88+
public void PathCombine_WithPath_ShouldCombine()
89+
{
90+
// Arrange
91+
var path = $"C:{Path.DirectorySeparatorChar}dir";
92+
const string fileName = "output.pdf";
93+
94+
// Act
95+
var result = Path.Combine(path, fileName);
96+
97+
// Assert
98+
result.Should().Be($"C:{Path.DirectorySeparatorChar}dir{Path.DirectorySeparatorChar}output.pdf");
99+
}
100+
101+
[Fact]
102+
public void PathCombine_WithNullPath_ShouldReturnFileName()
103+
{
104+
// Arrange
105+
string? path = null;
106+
const string fileName = "output.pdf";
107+
108+
// Act
109+
var result = Path.Combine(path ?? string.Empty, fileName);
110+
111+
// Assert
112+
result.Should().Be("output.pdf");
113+
}
114+
}

0 commit comments

Comments
 (0)