Skip to content

Commit e0b02ae

Browse files
Joshua-Lester3BenjaminMichaelis
authored andcommitted
Refactored and added testing
1 parent 3646144 commit e0b02ae

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
@@ -82,4 +82,61 @@ public void FindCSyntaxFundamentalsSanitizedWithAnchorReturnsCorrectSiteMap()
8282
Assert.NotNull(foundSiteMap);
8383
Assert.Equivalent(CSyntaxFundamentalsSiteMapping, foundSiteMap);
8484
}
85+
86+
[Fact]
87+
public void FindPercentComplete_KeyIsNull_ThrowsArgumentNullException()
88+
{
89+
// Arrange
90+
91+
// Act
92+
93+
// Assert
94+
Assert.Throws<ArgumentNullException>(() =>
95+
{
96+
GetSiteMap().FindPercentComplete(null!);
97+
});
98+
}
99+
100+
[Theory]
101+
[InlineData(" ")]
102+
[InlineData("")]
103+
public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string key)
104+
{
105+
// Arrange
106+
107+
// Act
108+
109+
// Assert
110+
Assert.Throws<ArgumentException>(() =>
111+
{
112+
GetSiteMap().FindPercentComplete(key);
113+
});
114+
}
115+
116+
[Theory]
117+
[InlineData("hello-world", "50.00")]
118+
[InlineData("c-syntax-fundamentals", "100.00")]
119+
public void FindPercentComplete_ValidKey_Success(string key, string result)
120+
{
121+
// Arrange
122+
123+
// Act
124+
string percent = GetSiteMap().FindPercentComplete(key);
125+
126+
// Assert
127+
Assert.Equal(result, percent);
128+
}
129+
130+
[Fact]
131+
public void FindPercentComplete_EmptySiteMappings_ReturnsZeroPercent()
132+
{
133+
// Arrange
134+
IList<SiteMapping> siteMappings = new List<SiteMapping>();
135+
136+
// Act
137+
string percent = siteMappings.FindPercentComplete("test");
138+
139+
// Assert
140+
Assert.Equal("0.00", percent);
141+
}
85142
}

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
@@ -4,5 +4,4 @@ public interface ISiteMappingService
44
{
55
IList<SiteMapping> SiteMappings { get; }
66
IEnumerable<SiteMappingDto> GetTocData();
7-
string GetPercentComplete(string currentPageKey);
87
}

EssentialCSharp.Web/Services/SiteMappingService.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,4 @@ private static IEnumerable<SiteMappingDto> GetItems(IEnumerable<SiteMapping> cha
4949
Items = GetItems(chapterItems.SkipWhile(q => i.Keys.First() != q.Keys.First()).Skip(1), indentLevel + 1)
5050
});
5151
}
52-
53-
public string GetPercentComplete(string currentPageKey)
54-
{
55-
int currentMappingCount = 1;
56-
int overallMappingCount = 1;
57-
bool currentPageFound = false;
58-
IEnumerable<IGrouping<int, SiteMapping>> chapterGroupings = SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(g => g.Key);
59-
foreach (IGrouping<int, SiteMapping> chapterGrouping in chapterGroupings)
60-
{
61-
IEnumerable<IGrouping<int, SiteMapping>> pageGroupings = chapterGrouping.GroupBy(x => x.PageNumber).OrderBy(g => g.Key);
62-
foreach (IGrouping<int, SiteMapping> pageGrouping in pageGroupings)
63-
{
64-
foreach (SiteMapping siteMapping in pageGrouping)
65-
{
66-
if (siteMapping.Key == currentPageKey)
67-
{
68-
currentPageFound = true;
69-
}
70-
if (!currentPageFound)
71-
{
72-
currentMappingCount++;
73-
}
74-
overallMappingCount++;
75-
}
76-
}
77-
}
78-
double result = (double)currentMappingCount / overallMappingCount * 100;
79-
return string.Format(CultureInfo.InvariantCulture, "{0:0.00}", result);
80-
}
8152
}

EssentialCSharp.Web/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
<script>
272272
@{
273273
var tocData = _SiteMappings.GetTocData();
274-
var percentComplete = _SiteMappings.GetPercentComplete(ViewBag.CurrentPageKey);
274+
var percentComplete = _SiteMappings.SiteMappings.FindPercentComplete((string) ViewBag.CurrentPageKey);
275275
}
276276
PERCENT_COMPLETE = @Json.Serialize(percentComplete);
277277
PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage)

0 commit comments

Comments
 (0)