Skip to content

Commit 875c1c5

Browse files
Refactored and added testing
1 parent b34c7ec commit 875c1c5

File tree

5 files changed

+101
-32
lines changed

5 files changed

+101
-32
lines changed

EssentialCSharp.Web.Tests/SiteMappingTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,61 @@ public void FindCSyntaxFundamentalsSanitizedWithAnchorReturnsCorrectSiteMap()
7878
Assert.NotNull(foundSiteMap);
7979
Assert.Equal(CSyntaxFundamentalsSiteMapping, foundSiteMap);
8080
}
81+
82+
[Fact]
83+
public void FindPercentComplete_KeyIsNull_ThrowsArgumentNullException()
84+
{
85+
// Arrange
86+
87+
// Act
88+
89+
// Assert
90+
Assert.Throws<ArgumentNullException>(() =>
91+
{
92+
GetSiteMap().FindPercentComplete(null!);
93+
});
94+
}
95+
96+
[Theory]
97+
[InlineData(" ")]
98+
[InlineData("")]
99+
public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string key)
100+
{
101+
// Arrange
102+
103+
// Act
104+
105+
// Assert
106+
Assert.Throws<ArgumentException>(() =>
107+
{
108+
GetSiteMap().FindPercentComplete(key);
109+
});
110+
}
111+
112+
[Theory]
113+
[InlineData("hello-world", "50.00")]
114+
[InlineData("c-syntax-fundamentals", "100.00")]
115+
public void FindPercentComplete_ValidKey_Success(string key, string result)
116+
{
117+
// Arrange
118+
119+
// Act
120+
string percent = GetSiteMap().FindPercentComplete(key);
121+
122+
// Assert
123+
Assert.Equal(result, percent);
124+
}
125+
126+
[Fact]
127+
public void FindPercentComplete_EmptySiteMappings_ReturnsZeroPercent()
128+
{
129+
// Arrange
130+
IList<SiteMapping> siteMappings = new List<SiteMapping>();
131+
132+
// Act
133+
string percent = siteMappings.FindPercentComplete("test");
134+
135+
// Assert
136+
Assert.Equal("0.00", percent);
137+
}
81138
}

EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace EssentialCSharp.Web.Extensions;
1+
using EssentialCSharp.Common;
2+
using System.Globalization;
3+
4+
namespace EssentialCSharp.Web.Extensions;
25

36
public static class SiteMappingListExtensions
47
{
@@ -23,4 +26,43 @@ public static class SiteMappingListExtensions
2326
}
2427
return null;
2528
}
29+
/// <summary>
30+
/// Finds percent complete based on a key.
31+
/// </summary>
32+
/// <param name="siteMappings">IList of SiteMappings</param>
33+
/// <param name="key">If null, uses the first key in the list</param>
34+
/// <returns>Returns a formatted double for use as the percent complete.</returns>
35+
public static string FindPercentComplete(this IList<SiteMapping> siteMappings, string key)
36+
{
37+
ArgumentNullException.ThrowIfNullOrWhiteSpace(key);
38+
int currentMappingCount = 0;
39+
int overallMappingCount = 0;
40+
bool currentPageFound = false;
41+
IEnumerable<IGrouping<int, SiteMapping>> chapterGroupings = siteMappings.GroupBy(x => x.ChapterNumber).OrderBy(g => g.Key);
42+
foreach (IGrouping<int, SiteMapping> chapterGrouping in chapterGroupings)
43+
{
44+
IEnumerable<IGrouping<int, SiteMapping>> pageGroupings = chapterGrouping.GroupBy(x => x.PageNumber).OrderBy(g => g.Key);
45+
foreach (IGrouping<int, SiteMapping> pageGrouping in pageGroupings)
46+
{
47+
foreach (SiteMapping siteMapping in pageGrouping)
48+
{
49+
if (!currentPageFound)
50+
{
51+
currentMappingCount++;
52+
}
53+
overallMappingCount++;
54+
if (siteMapping.Key == key)
55+
{
56+
currentPageFound = true;
57+
}
58+
}
59+
}
60+
}
61+
if (overallMappingCount is 0)
62+
{
63+
return "0.00";
64+
}
65+
double result = (double)currentMappingCount / overallMappingCount * 100;
66+
return string.Format(CultureInfo.InvariantCulture, "{0:0.00}", result);
67+
}
2668
}

EssentialCSharp.Web/Services/ISiteMappingService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
public interface ISiteMappingService
44
{
55
IList<SiteMapping> SiteMappings { get; }
6-
string GetPercentComplete(string currentPageKey);
76
}

EssentialCSharp.Web/Services/SiteMappingService.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,4 @@ public SiteMappingService(IWebHostEnvironment webHostEnvironment)
1212
List<SiteMapping>? siteMappings = System.Text.Json.JsonSerializer.Deserialize<List<SiteMapping>>(File.OpenRead(path)) ?? throw new InvalidOperationException("No table of contents found");
1313
SiteMappings = siteMappings;
1414
}
15-
16-
public string GetPercentComplete(string currentPageKey)
17-
{
18-
int currentMappingCount = 1;
19-
int overallMappingCount = 1;
20-
bool currentPageFound = false;
21-
IEnumerable<IGrouping<int, SiteMapping>> chapterGroupings = SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(g => g.Key);
22-
foreach (IGrouping<int, SiteMapping> chapterGrouping in chapterGroupings)
23-
{
24-
IEnumerable<IGrouping<int, SiteMapping>> pageGroupings = chapterGrouping.GroupBy(x => x.PageNumber).OrderBy(g => g.Key);
25-
foreach (IGrouping<int, SiteMapping> pageGrouping in pageGroupings)
26-
{
27-
foreach (SiteMapping siteMapping in pageGrouping)
28-
{
29-
if (siteMapping.Key == currentPageKey)
30-
{
31-
currentPageFound = true;
32-
}
33-
if (!currentPageFound)
34-
{
35-
currentMappingCount++;
36-
}
37-
overallMappingCount++;
38-
}
39-
}
40-
}
41-
double result = (double)currentMappingCount / overallMappingCount * 100;
42-
return string.Format(CultureInfo.InvariantCulture, "{0:0.00}", result);
43-
}
4415
}

EssentialCSharp.Web/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
Items = GetItems(x, 1)
297297
});
298298
299-
var percentComplete = _SiteMappings.GetPercentComplete(ViewBag.CurrentPageKey);
299+
var percentComplete = _SiteMappings.SiteMappings.FindPercentComplete((string) ViewBag.CurrentPageKey);
300300
}
301301
PERCENT_COMPLETE = @Json.Serialize(percentComplete);
302302
PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage)

0 commit comments

Comments
 (0)