-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathListingSourceCodeServiceTests.cs
More file actions
131 lines (106 loc) · 3.95 KB
/
ListingSourceCodeServiceTests.cs
File metadata and controls
131 lines (106 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using EssentialCSharp.Web.Models;
using EssentialCSharp.Web.Services;
namespace EssentialCSharp.Web.Tests;
public class ListingSourceCodeServiceTests
{
[Test]
public async Task GetListingAsync_WithValidChapterAndListing_ReturnsCorrectListing()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
ListingSourceCodeResponse? result = await service.GetListingAsync(1, 1);
// Assert
await Assert.That(result).IsNotNull();
using (Assert.Multiple())
{
await Assert.That(result.ChapterNumber).IsEqualTo(1);
await Assert.That(result.ListingNumber).IsEqualTo(1);
await Assert.That(result.FileExtension).IsEqualTo("cs");
await Assert.That(result.Content).IsNotEmpty();
}
}
[Test]
public async Task GetListingAsync_WithInvalidChapter_ReturnsNull()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
ListingSourceCodeResponse? result = await service.GetListingAsync(999, 1);
// Assert
await Assert.That(result).IsNull();
}
[Test]
public async Task GetListingAsync_WithInvalidListing_ReturnsNull()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
ListingSourceCodeResponse? result = await service.GetListingAsync(1, 999);
// Assert
await Assert.That(result).IsNull();
}
[Test]
public async Task GetListingAsync_DifferentFileExtension_AutoDiscoversFileExtension()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act - Get an XML file (01.02.xml exists in Chapter 1)
ListingSourceCodeResponse? result = await service.GetListingAsync(1, 2);
// Assert
await Assert.That(result).IsNotNull();
await Assert.That(result.FileExtension).IsEqualTo("xml");
}
[Test]
public async Task GetListingsByChapterAsync_WithValidChapter_ReturnsAllListings()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(1);
// Assert
await Assert.That(results).IsNotEmpty();
foreach (var r in results)
{
using (Assert.Multiple())
{
await Assert.That(r.ChapterNumber).IsEqualTo(1);
await Assert.That(r.Content).IsNotEmpty();
await Assert.That(r.FileExtension).IsNotEmpty();
}
}
// Verify results are ordered
await Assert.That(results).IsOrderedBy(r => r.ListingNumber);
}
[Test]
public async Task GetListingsByChapterAsync_DirectoryContainsNonListingFiles_ExcludesNonListingFiles()
{
// Arrange - Chapter 10 has Employee.cs which doesn't match the pattern
ListingSourceCodeService service = CreateService();
// Act
IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(10);
// Assert
await Assert.That(results).IsNotEmpty();
// Ensure all results match the {CC}.{LL}.{ext} pattern
foreach (var r in results)
{
using (Assert.Multiple())
{
await Assert.That(r.ChapterNumber).IsEqualTo(10);
await Assert.That(r.ListingNumber).IsBetween(1, 99);
}
}
}
[Test]
public async Task GetListingsByChapterAsync_WithInvalidChapter_ReturnsEmptyList()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(999);
// Assert
await Assert.That(results).IsEmpty();
}
private static ListingSourceCodeService CreateService() =>
TestListingSourceCodeServiceHelper.CreateService();
}