Skip to content

Commit 19637a0

Browse files
committed
More unit tests
1 parent b2f1dde commit 19637a0

File tree

3 files changed

+331
-0
lines changed

3 files changed

+331
-0
lines changed

tests/PrintZPL.Tests/Core/Services/PrintServiceTests.cs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ await Assert.ThrowsAsync<ArgumentNullException>(() =>
3939
_printService.PrintZPL(emptyZpl, "192.168.1.100", 9100, null, null));
4040
}
4141

42+
[Fact]
43+
public async Task PrintZPL_WithWhitespaceZpl_ThrowsArgumentNullException()
44+
{
45+
// Arrange
46+
string whitespaceZpl = " ";
47+
48+
// Act & Assert
49+
await Assert.ThrowsAsync<ArgumentNullException>(() =>
50+
_printService.PrintZPL(whitespaceZpl, "192.168.1.100", 9100, null, null));
51+
}
52+
4253
[Fact]
4354
public async Task PrintZPL_WithData_CallsTemplateService()
4455
{
@@ -59,12 +70,55 @@ public async Task PrintZPL_WithData_CallsTemplateService()
5970
}
6071
catch (Exception)
6172
{
73+
// Expected to fail due to no actual printer
6274
}
6375

6476
// Assert
6577
_mockTemplateService.Verify(x => x.PopulateZplTemplate(data, zpl, delimiter), Times.Once);
6678
}
6779

80+
[Fact]
81+
public async Task PrintZPL_WithNullData_DoesNotCallTemplateService()
82+
{
83+
// Arrange
84+
var zpl = "^XA^FO50,50^FDTest^FS^XZ";
85+
Dictionary<string, string>? nullData = null;
86+
87+
// Act
88+
try
89+
{
90+
await _printService.PrintZPL(zpl, "192.168.1.100", 9100, nullData, "$");
91+
}
92+
catch (Exception)
93+
{
94+
// Expected to fail due to no actual printer
95+
}
96+
97+
// Assert
98+
_mockTemplateService.Verify(x => x.PopulateZplTemplate(It.IsAny<Dictionary<string, string>>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
99+
}
100+
101+
[Fact]
102+
public async Task PrintZPL_WithEmptyData_DoesNotCallTemplateService()
103+
{
104+
// Arrange
105+
var zpl = "^XA^FO50,50^FDTest^FS^XZ";
106+
var emptyData = new Dictionary<string, string>();
107+
108+
// Act
109+
try
110+
{
111+
await _printService.PrintZPL(zpl, "192.168.1.100", 9100, emptyData, "$");
112+
}
113+
catch (Exception)
114+
{
115+
// Expected to fail due to no actual printer
116+
}
117+
118+
// Assert
119+
_mockTemplateService.Verify(x => x.PopulateZplTemplate(It.IsAny<Dictionary<string, string>>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
120+
}
121+
68122
[Fact]
69123
public async Task PrintZPL_WithInvalidIpAddress_ThrowsInvalidOperationException()
70124
{
@@ -94,4 +148,121 @@ public async Task PrintZPL_WithUnreachableIpAddress_ThrowsInvalidOperationExcept
94148
// Assert
95149
Assert.Contains("Failed to connect to printer", exception.Message);
96150
}
151+
152+
[Fact]
153+
public async Task PrintZPL_WithInvalidPort_ThrowsInvalidOperationException()
154+
{
155+
// Arrange
156+
var zpl = "^XA^FO50,50^FDTest^FS^XZ";
157+
var invalidPort = 99999; // Invalid port
158+
159+
// Act
160+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
161+
_printService.PrintZPL(zpl, "192.168.1.100", invalidPort, null, null));
162+
163+
// Assert
164+
Assert.Contains("Failed to connect to printer", exception.Message);
165+
}
166+
167+
[Fact]
168+
public async Task PrintZPL_WithZeroPort_ThrowsInvalidOperationException()
169+
{
170+
// Arrange
171+
var zpl = "^XA^FO50,50^FDTest^FS^XZ";
172+
var zeroPort = 0;
173+
174+
// Act
175+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
176+
_printService.PrintZPL(zpl, "192.168.1.100", zeroPort, null, null));
177+
178+
// Assert
179+
Assert.Contains("Failed to connect to printer", exception.Message);
180+
}
181+
182+
[Fact]
183+
public async Task PrintZPL_WithNegativePort_ThrowsInvalidOperationException()
184+
{
185+
// Arrange
186+
var zpl = "^XA^FO50,50^FDTest^FS^XZ";
187+
var negativePort = -1;
188+
189+
// Act
190+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
191+
_printService.PrintZPL(zpl, "192.168.1.100", negativePort, null, null));
192+
193+
// Assert
194+
Assert.Contains("Failed to connect to printer", exception.Message);
195+
}
196+
197+
[Theory]
198+
[InlineData("localhost")]
199+
[InlineData("127.0.0.1")]
200+
[InlineData("192.168.1.1")]
201+
[InlineData("10.0.0.1")]
202+
public async Task PrintZPL_WithValidIpFormats_AttemptsConnection(string ipAddress)
203+
{
204+
// Arrange
205+
var zpl = "^XA^FO50,50^FDTest^FS^XZ";
206+
207+
// Act & Assert
208+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
209+
_printService.PrintZPL(zpl, ipAddress, 9100, null, null));
210+
211+
Assert.Contains("Failed to connect to printer", exception.Message);
212+
}
213+
214+
[Theory]
215+
[InlineData("^XA^FO50,50^FDTest^FS^XZ")]
216+
[InlineData("^XA^FO50,50^FDTest^FS^XZ\n")]
217+
[InlineData("^XA^FO50,50^FDTest^FS^XZ\r\n")]
218+
public async Task PrintZPL_WithVariousZplFormats_AttemptsConnection(string zpl)
219+
{
220+
// Arrange & Act & Assert
221+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
222+
_printService.PrintZPL(zpl, "192.168.1.100", 9100, null, null));
223+
224+
Assert.Contains("Failed to connect to printer", exception.Message);
225+
}
226+
227+
[Fact]
228+
public async Task PrintZPL_WithComplexZpl_AttemptsConnection()
229+
{
230+
// Arrange
231+
var complexZpl = @"^XA
232+
^FO50,50^A0N,50,50^FDHello World!^FS
233+
^FO50,120^A0N,30,30^FDLine 2^FS
234+
^FO50,180^A0N,30,30^FDLine 3^FS
235+
^XZ";
236+
237+
// Act & Assert
238+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
239+
_printService.PrintZPL(complexZpl, "192.168.1.100", 9100, null, null));
240+
241+
Assert.Contains("Failed to connect to printer", exception.Message);
242+
}
243+
244+
[Fact]
245+
public async Task PrintZPL_WithTemplateAndData_ProcessesCorrectly()
246+
{
247+
// Arrange
248+
var zpl = "^XA^FO50,50^FD$Name$^FS^FO50,100^FD$Date$^FS^XZ";
249+
var data = new Dictionary<string, string>
250+
{
251+
{ "Name", "John Doe" },
252+
{ "Date", "2023-12-01" }
253+
};
254+
var delimiter = "$";
255+
var processedZpl = "^XA^FO50,50^FDJohn Doe^FS^FO50,100^FD2023-12-01^FS^XZ";
256+
257+
_mockTemplateService
258+
.Setup(x => x.PopulateZplTemplate(data, zpl, delimiter))
259+
.Returns(processedZpl);
260+
261+
// Act & Assert
262+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
263+
_printService.PrintZPL(zpl, "192.168.1.100", 9100, data, delimiter));
264+
265+
Assert.Contains("Failed to connect to printer", exception.Message);
266+
_mockTemplateService.Verify(x => x.PopulateZplTemplate(data, zpl, delimiter), Times.Once);
267+
}
97268
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using PrintZPL.Core.Services;
2+
3+
namespace PrintZPL.Tests.Core.Services;
4+
5+
public class TemplateServiceTests
6+
{
7+
private readonly TemplateService _templateService;
8+
9+
public TemplateServiceTests()
10+
{
11+
_templateService = new TemplateService();
12+
}
13+
14+
[Fact]
15+
public void PopulateZplTemplate_WithValidData_ReplacesPlaceholders()
16+
{
17+
// Arrange
18+
var template = "^XA^FO50,50^FD$Name$^FS^XZ";
19+
var data = new Dictionary<string, string> { { "Name", "TestPrinter" } };
20+
var delimiter = "$";
21+
var expected = "^XA^FO50,50^FDTestPrinter^FS^XZ";
22+
23+
// Act
24+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
25+
26+
// Assert
27+
Assert.Equal(expected, result);
28+
}
29+
30+
[Fact]
31+
public void PopulateZplTemplate_WithMultipleData_ReplacesAllPlaceholders()
32+
{
33+
// Arrange
34+
var template = "^XA^FO50,50^FD$Greeting$, $Name$!^FS^XZ";
35+
var data = new Dictionary<string, string>
36+
{
37+
{ "Greeting", "Hello" },
38+
{ "Name", "World" }
39+
};
40+
var delimiter = "$";
41+
var expected = "^XA^FO50,50^FDHello, World!^FS^XZ";
42+
43+
// Act
44+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
45+
46+
// Assert
47+
Assert.Equal(expected, result);
48+
}
49+
50+
[Fact]
51+
public void PopulateZplTemplate_WithEmptyData_ReturnsOriginalTemplate()
52+
{
53+
// Arrange
54+
var template = "^XA^FO50,50^FD$Name$^FS^XZ";
55+
var data = new Dictionary<string, string>();
56+
var delimiter = "$";
57+
58+
// Act
59+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
60+
61+
// Assert
62+
Assert.Equal(template, result);
63+
}
64+
65+
[Fact]
66+
public void PopulateZplTemplate_WithNullValues_ReplacesWithEmptyString()
67+
{
68+
// Arrange
69+
var template = "^XA^FO50,50^FD$Name$^FS^XZ";
70+
var data = new Dictionary<string, string> { { "Name", null! } };
71+
var delimiter = "$";
72+
var expected = "^XA^FO50,50^FD^FS^XZ";
73+
74+
// Act
75+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
76+
77+
// Assert
78+
Assert.Equal(expected, result);
79+
}
80+
81+
[Fact]
82+
public void PopulateZplTemplate_WithDifferentDelimiters_ReplacesCorrectly()
83+
{
84+
// Arrange
85+
var template = "^XA^FO50,50^FD$$Name$$^FS^XZ";
86+
var data = new Dictionary<string, string> { { "Name", "TestValue" } };
87+
var delimiter = "$$";
88+
var expected = "^XA^FO50,50^FDTestValue^FS^XZ";
89+
90+
// Act
91+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
92+
93+
// Assert
94+
Assert.Equal(expected, result);
95+
}
96+
97+
[Fact]
98+
public void PopulateZplTemplate_WithSpecialRegexCharacters_HandlesCorrectly()
99+
{
100+
// Arrange
101+
var template = "^XA^FO50,50^FD$$Name$$^FS^XZ";
102+
var data = new Dictionary<string, string> { { "Name", "TestValue" } };
103+
var delimiter = "$$";
104+
var expected = "^XA^FO50,50^FDTestValue^FS^XZ";
105+
106+
// Act
107+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
108+
109+
// Assert
110+
Assert.Equal(expected, result);
111+
}
112+
113+
[Fact]
114+
public void PopulateZplTemplate_WithCaseInsensitiveMatch_ReplacesCorrectly()
115+
{
116+
// Arrange
117+
var template = "^XA^FO50,50^FD$name$^FS^XZ";
118+
var data = new Dictionary<string, string> { { "Name", "TestValue" } };
119+
var delimiter = "$";
120+
var expected = "^XA^FO50,50^FDTestValue^FS^XZ";
121+
122+
// Act
123+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
124+
125+
// Assert
126+
Assert.Equal(expected, result);
127+
}
128+
129+
[Fact]
130+
public void PopulateZplTemplate_WithNoMatchingPlaceholders_ReturnsOriginalTemplate()
131+
{
132+
// Arrange
133+
var template = "^XA^FO50,50^FD$Name$^FS^XZ";
134+
var data = new Dictionary<string, string> { { "Age", "25" } };
135+
var delimiter = "$";
136+
137+
// Act
138+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
139+
140+
// Assert
141+
Assert.Equal(template, result);
142+
}
143+
144+
[Fact]
145+
public void PopulateZplTemplate_WithDuplicatePlaceholders_ReplacesAll()
146+
{
147+
// Arrange
148+
var template = "^XA^FO50,50^FD$Name$^FS^FO50,100^FD$Name$^FS^XZ";
149+
var data = new Dictionary<string, string> { { "Name", "Test" } };
150+
var delimiter = "$";
151+
var expected = "^XA^FO50,50^FDTest^FS^FO50,100^FDTest^FS^XZ";
152+
153+
// Act
154+
var result = _templateService.PopulateZplTemplate(data, template, delimiter);
155+
156+
// Assert
157+
Assert.Equal(expected, result);
158+
}
159+
}

tests/PrintZPL.Tests/PrintZPL.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
<ItemGroup>
2424
<ProjectReference Include="..\..\src\PrintZPL.Core\PrintZPL.Core.csproj" />
25+
<ProjectReference Include="..\..\src\PrintZPL.Host\PrintZPL.Host.csproj" />
2526
</ItemGroup>
2627

2728
</Project>

0 commit comments

Comments
 (0)