From 2aa9906202cce5f9c203831a05c412a08cbad4f2 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Mon, 15 Oct 2018 14:09:42 -0500 Subject: [PATCH] apply Roy Osherove unit test naming standard --- .../Snapshot/SnapshotWayback.cs | 2 +- .../Wayback/Models/SnapshotMetaDto.cs | 2 +- .../Wayback/WaybackService.cs | 2 +- .../Collection/AddIfNotNullOrEmptyShould.cs | 37 ---------- .../Extensions/Collection/AddRangeShould.cs | 46 ------------ .../Extensions/CollectionExtensionsTests.cs | 71 +++++++++++++++++++ .../String/GetNthIndexOfCharShould.cs | 35 --------- .../String/IsValidHttpOrHttpsUrlShould.cs | 31 -------- .../Extensions/StringExtensionsTests.cs | 60 ++++++++++++++++ .../Extensions/Task/TimeoutAfterShould.cs | 29 -------- .../Extensions/TaskExtensionsTests.cs | 29 ++++++++ ...esAsyncShould.cs => GitHubServiceTests.cs} | 26 +++---- ...syncShould.cs => UserAgentServiceTests.cs} | 6 +- ...aAsyncShould.cs => WaybackServiceTests.cs} | 26 +++---- 14 files changed, 192 insertions(+), 210 deletions(-) delete mode 100644 tests/FilterLists.Services.Tests/Extensions/Collection/AddIfNotNullOrEmptyShould.cs delete mode 100644 tests/FilterLists.Services.Tests/Extensions/Collection/AddRangeShould.cs create mode 100644 tests/FilterLists.Services.Tests/Extensions/CollectionExtensionsTests.cs delete mode 100644 tests/FilterLists.Services.Tests/Extensions/String/GetNthIndexOfCharShould.cs delete mode 100644 tests/FilterLists.Services.Tests/Extensions/String/IsValidHttpOrHttpsUrlShould.cs create mode 100644 tests/FilterLists.Services.Tests/Extensions/StringExtensionsTests.cs delete mode 100644 tests/FilterLists.Services.Tests/Extensions/Task/TimeoutAfterShould.cs create mode 100644 tests/FilterLists.Services.Tests/Extensions/TaskExtensionsTests.cs rename tests/FilterLists.Services.Tests/{GitHub/GetCommitDatesAsyncShould.cs => GitHubServiceTests.cs} (78%) rename tests/FilterLists.Services.Tests/{UserAgent/GetMostPopularStringAsyncShould.cs => UserAgentServiceTests.cs} (55%) rename tests/FilterLists.Services.Tests/{Wayback/GetMostRecentSnapshotMetaAsyncShould.cs => WaybackServiceTests.cs} (70%) diff --git a/src/FilterLists.Services/Snapshot/SnapshotWayback.cs b/src/FilterLists.Services/Snapshot/SnapshotWayback.cs index 7070e03c6..04b61a017 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotWayback.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotWayback.cs @@ -30,7 +30,7 @@ private async Task UpdateWaybackData() var snapshotMeta = await WaybackService.GetMostRecentSnapshotMetaAsync(ListUrl); if (snapshotMeta != null) { - ListUrl = SnapEntity.WaybackUrl = snapshotMeta.UrlRaw; + ListUrl = SnapEntity.WaybackUrl = snapshotMeta.RawUrl; SnapEntity.WaybackTimestamp = snapshotMeta.TimestampUtc; } else diff --git a/src/FilterLists.Services/Wayback/Models/SnapshotMetaDto.cs b/src/FilterLists.Services/Wayback/Models/SnapshotMetaDto.cs index 8ebceb370..cd6b63901 100644 --- a/src/FilterLists.Services/Wayback/Models/SnapshotMetaDto.cs +++ b/src/FilterLists.Services/Wayback/Models/SnapshotMetaDto.cs @@ -4,7 +4,7 @@ namespace FilterLists.Services.Wayback.Models { public class SnapshotMetaDto { - public string UrlRaw { get; set; } + public string RawUrl { get; set; } public DateTime TimestampUtc { get; set; } } } \ No newline at end of file diff --git a/src/FilterLists.Services/Wayback/WaybackService.cs b/src/FilterLists.Services/Wayback/WaybackService.cs index e5755e9eb..1e9c337f9 100644 --- a/src/FilterLists.Services/Wayback/WaybackService.cs +++ b/src/FilterLists.Services/Wayback/WaybackService.cs @@ -15,7 +15,7 @@ public static async Task GetMostRecentSnapshotMetaAsync(string { var closest = (await GetWaybackAvailability(url))?.ArchivedSnapshots?.Closest; return closest != null - ? new SnapshotMetaDto {TimestampUtc = ParseTimestampUtc(closest), UrlRaw = ParseUrlRaw(closest)} + ? new SnapshotMetaDto {TimestampUtc = ParseTimestampUtc(closest), RawUrl = ParseUrlRaw(closest)} : null; } diff --git a/tests/FilterLists.Services.Tests/Extensions/Collection/AddIfNotNullOrEmptyShould.cs b/tests/FilterLists.Services.Tests/Extensions/Collection/AddIfNotNullOrEmptyShould.cs deleted file mode 100644 index bd7dcbff6..000000000 --- a/tests/FilterLists.Services.Tests/Extensions/Collection/AddIfNotNullOrEmptyShould.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Collections.Generic; -using System.Collections.ObjectModel; -using FilterLists.Services.Extensions; -using Xunit; - -namespace FilterLists.Services.Tests.Extensions.Collection -{ - public class AddIfNotNullOrEmptyShould - { - private readonly ICollection sut = new Collection(); - private string item; - - [Fact] - public void AddIfNotNullOrEmpty() - { - item = "item"; - sut.AddIfNotNullOrEmpty(item); - Assert.Contains(item, sut); - } - - [Fact] - public void NotAddIfEmpty() - { - item = ""; - sut.AddIfNotNullOrEmpty(item); - Assert.DoesNotContain(item, sut); - } - - [Fact] - public void NotAddIfNull() - { - item = null; - sut.AddIfNotNullOrEmpty(item); - Assert.DoesNotContain(item, sut); - } - } -} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/Extensions/Collection/AddRangeShould.cs b/tests/FilterLists.Services.Tests/Extensions/Collection/AddRangeShould.cs deleted file mode 100644 index 59f294d45..000000000 --- a/tests/FilterLists.Services.Tests/Extensions/Collection/AddRangeShould.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using FilterLists.Services.Extensions; -using Xunit; - -namespace FilterLists.Services.Tests.Extensions.Collection -{ - public class AddRangeShould - { - private readonly ICollection sut = new Collection(); - private IEnumerable range; - - [Fact] - public void AddCollectionOfStrings() - { - range = new Collection {"item1", "item2"}; - sut.AddRange(range); - Assert.True(!range.Except(sut).Any()); - } - - [Fact] - public void AddListOfStrings() - { - range = new List {"item1", "item2"}; - sut.AddRange(range); - Assert.True(!range.Except(sut).Any()); - } - - [Fact] - public void NotAddAnyItemsFromEmptyCollection() - { - range = new Collection(); - sut.AddRange(range); - Assert.Empty(sut); - } - - [Fact] - public void NotAddAnyItemsFromEmptyList() - { - range = new List(); - sut.AddRange(range); - Assert.Empty(sut); - } - } -} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/Extensions/CollectionExtensionsTests.cs b/tests/FilterLists.Services.Tests/Extensions/CollectionExtensionsTests.cs new file mode 100644 index 000000000..4d544e513 --- /dev/null +++ b/tests/FilterLists.Services.Tests/Extensions/CollectionExtensionsTests.cs @@ -0,0 +1,71 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using FilterLists.Services.Extensions; +using Xunit; + +namespace FilterLists.Services.Tests.Extensions +{ + public class CollectionExtensionsTests + { + private readonly ICollection sut = new Collection(); + private IEnumerable range; + private string item; + + [Fact] + public void AddIfNotNullOrEmpty_EmptyString_AddsNothing() + { + item = ""; + sut.AddIfNotNullOrEmpty(item); + Assert.DoesNotContain(item, sut); + } + + [Fact] + public void AddIfNotNullOrEmpty_NonNullOrEmptyString_AddsParam() + { + item = "item"; + sut.AddIfNotNullOrEmpty(item); + Assert.Contains(item, sut); + } + + [Fact] + public void AddIfNotNullOrEmpty_NullString_AddsNothing() + { + item = null; + sut.AddIfNotNullOrEmpty(item); + Assert.DoesNotContain(item, sut); + } + + [Fact] + public void AddRange_EmptyCollection_AddsNothing() + { + range = new Collection(); + sut.AddRange(range); + Assert.Empty(sut); + } + + [Fact] + public void AddRange_EmptyList_AddsNothing() + { + range = new List(); + sut.AddRange(range); + Assert.Empty(sut); + } + + [Fact] + public void AddRange_StringCollection_AddsParam() + { + range = new Collection {"item1", "item2"}; + sut.AddRange(range); + Assert.True(!range.Except(sut).Any()); + } + + [Fact] + public void AddRange_StringList_AddsParam() + { + range = new List {"item1", "item2"}; + sut.AddRange(range); + Assert.True(!range.Except(sut).Any()); + } + } +} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/Extensions/String/GetNthIndexOfCharShould.cs b/tests/FilterLists.Services.Tests/Extensions/String/GetNthIndexOfCharShould.cs deleted file mode 100644 index 051d9ed0f..000000000 --- a/tests/FilterLists.Services.Tests/Extensions/String/GetNthIndexOfCharShould.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using FilterLists.Services.Extensions; -using Xunit; - -namespace FilterLists.Services.Tests.Extensions.String -{ - public class GetNthIndexOfCharShould - { - private const string Sut = "abcdeabcde"; - - [Theory] - [InlineData(1, 'a', 0)] - [InlineData(2, 'a', 5)] - public void ReturnNthIndexOfCharIfExists(int n, char c, int expectedIndex) - { - Assert.Equal(expectedIndex, Sut.GetNthIndexOfChar(n, c)); - } - - [Theory] - [InlineData(3, 'a')] - [InlineData(1, 'f')] - public void ReturnNegativeOneIfDoesNotExist(int n, char c) - { - Assert.Equal(-1, Sut.GetNthIndexOfChar(n, c)); - } - - [Theory] - [InlineData(0, 'a')] - [InlineData(-1, 'a')] - public void ThrowArgumentOutOfRangeExceptionIfInvalidN(int n, char c) - { - Assert.Throws(() => Sut.GetNthIndexOfChar(n, c)); - } - } -} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/Extensions/String/IsValidHttpOrHttpsUrlShould.cs b/tests/FilterLists.Services.Tests/Extensions/String/IsValidHttpOrHttpsUrlShould.cs deleted file mode 100644 index 55176e62f..000000000 --- a/tests/FilterLists.Services.Tests/Extensions/String/IsValidHttpOrHttpsUrlShould.cs +++ /dev/null @@ -1,31 +0,0 @@ -using FilterLists.Services.Extensions; -using Xunit; - -namespace FilterLists.Services.Tests.Extensions.String -{ - public class IsValidHttpOrHttpsUrlShould - { - [Theory] - [InlineData("abp://www.google.com")] - [InlineData("www.google.com")] - [InlineData("google")] - public void ReturnFalseIfNotHttpOrHttps(string url) - { - Assert.False(url.IsValidHttpOrHttpsUrl()); - } - - [Fact] - public void ReturnTrueIfValidHttpsUrl() - { - const string httpsUrl = "https://www.google.com"; - Assert.True(httpsUrl.IsValidHttpOrHttpsUrl()); - } - - [Fact] - public void ReturnTrueIfValidHttpUrl() - { - const string httpUrl = "http://www.google.com"; - Assert.True(httpUrl.IsValidHttpOrHttpsUrl()); - } - } -} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/Extensions/StringExtensionsTests.cs b/tests/FilterLists.Services.Tests/Extensions/StringExtensionsTests.cs new file mode 100644 index 000000000..0353942f2 --- /dev/null +++ b/tests/FilterLists.Services.Tests/Extensions/StringExtensionsTests.cs @@ -0,0 +1,60 @@ +using System; +using FilterLists.Services.Extensions; +using Xunit; + +namespace FilterLists.Services.Tests.Extensions +{ + public class StringExtensionsTests + { + private const string Sut = "abcdeabcde"; + + [Theory] + [InlineData(1, 'a', 0)] + [InlineData(2, 'a', 5)] + public void GetNthIndexOfChar_NthIndexOfCharExists_ReturnsNthIndexOfChar(int n, char c, int expectedIndex) + { + var actualIndex = Sut.GetNthIndexOfChar(n, c); + Assert.Equal(expectedIndex, actualIndex); + } + + [Theory] + [InlineData(3, 'a')] + [InlineData(1, 'f')] + public void GetNthIndexOfChar_NthIndexOfCharDoesNotExist_ReturnsNegativeOne(int n, char c) + { + var actualIndex = Sut.GetNthIndexOfChar(n, c); + Assert.Equal(-1, actualIndex); + } + + [Theory] + [InlineData(0, 'a')] + [InlineData(-1, 'a')] + public void GetNthIndexOfChar_NLessThanOne_ThrowsArgumentOutOfRangeException(int n, char c) + { + Assert.Throws(() => Sut.GetNthIndexOfChar(n, c)); + } + + [Theory] + [InlineData("abp://www.google.com")] + [InlineData("www.google.com")] + [InlineData("google")] + public void IsValidHttpOrHttpsUrl_NonHttpOrHttpsPrefixedUrl_ReturnsFalse(string url) + { + Assert.False(url.IsValidHttpOrHttpsUrl()); + } + + [Fact] + public void IsValidHttpOrHttpsUrl_ValidHttpsUrl_ReturnsTrue() + { + const string httpsUrl = "https://www.google.com"; + Assert.True(httpsUrl.IsValidHttpOrHttpsUrl()); + } + + [Fact] + public void IsValidHttpOrHttpsUrl_ValidHttpUrl_ReturnsTrue() + { + const string httpUrl = "http://www.google.com"; + Assert.True(httpUrl.IsValidHttpOrHttpsUrl()); + } + } +} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/Extensions/Task/TimeoutAfterShould.cs b/tests/FilterLists.Services.Tests/Extensions/Task/TimeoutAfterShould.cs deleted file mode 100644 index d7129f70f..000000000 --- a/tests/FilterLists.Services.Tests/Extensions/Task/TimeoutAfterShould.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Threading; -using FilterLists.Services.Extensions; -using Xunit; - -namespace FilterLists.Services.Tests.Extensions.Task -{ - public class TimeoutAfterShould - { - private readonly System.Threading.Tasks.Task sut = - System.Threading.Tasks.Task.Run(() => { Thread.Sleep(1000); }); - - [Fact] - public async void CompleteSuccessfullyIfTimeToCompleteIsLessThanTimeout() - { - var fifteenSecondTimeout = new TimeSpan(0, 0, 15); - await sut.TimeoutAfter(fifteenSecondTimeout); - Assert.True(sut.IsCompletedSuccessfully); - } - - [Fact] - public async void ThrowTimeoutExceptionIfTimeToCompleteIsGreaterThanTimeout() - { - var oneTickTimeout = new TimeSpan(1); - await Assert.ThrowsAsync(() => - sut.TimeoutAfter(oneTickTimeout)); - } - } -} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/Extensions/TaskExtensionsTests.cs b/tests/FilterLists.Services.Tests/Extensions/TaskExtensionsTests.cs new file mode 100644 index 000000000..18982bb11 --- /dev/null +++ b/tests/FilterLists.Services.Tests/Extensions/TaskExtensionsTests.cs @@ -0,0 +1,29 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using FilterLists.Services.Extensions; +using Xunit; + +namespace FilterLists.Services.Tests.Extensions +{ + public class TaskExtensionsTests + { + private readonly Task sut = Task.Run(() => { Thread.Sleep(TimeSpan.FromSeconds(1)); }); + + [Fact] + public async void TimeoutAfter_TimeToCompleteIsGreaterThanTimeout_ThrowsTimeoutException() + { + var oneTickTimeout = new TimeSpan(1); + var taskWithTimeout = sut.TimeoutAfter(oneTickTimeout); + await Assert.ThrowsAsync(() => taskWithTimeout); + } + + [Fact] + public async void TimeoutAfter_TimeToCompleteIsLessThanTimeout_CompletesSuccessfully() + { + var twoSecondTimeout = new TimeSpan(0, 0, 2); + await sut.TimeoutAfter(twoSecondTimeout); + Assert.True(sut.IsCompletedSuccessfully); + } + } +} \ No newline at end of file diff --git a/tests/FilterLists.Services.Tests/GitHub/GetCommitDatesAsyncShould.cs b/tests/FilterLists.Services.Tests/GitHubServiceTests.cs similarity index 78% rename from tests/FilterLists.Services.Tests/GitHub/GetCommitDatesAsyncShould.cs rename to tests/FilterLists.Services.Tests/GitHubServiceTests.cs index 6ade397a0..466ea290a 100644 --- a/tests/FilterLists.Services.Tests/GitHub/GetCommitDatesAsyncShould.cs +++ b/tests/FilterLists.Services.Tests/GitHubServiceTests.cs @@ -4,12 +4,20 @@ using FilterLists.Services.GitHub.Models; using Xunit; -namespace FilterLists.Services.Tests.GitHub +namespace FilterLists.Services.Tests { - public class GetCommitDatesAsyncShould + public class GitHubServiceTests { [Fact] - public async Task ReturnCorrectFirstAndLastCommitDatesForValidGitHubRawUrl() + public async Task GetCommitDatesAsync_InvalidGitHubRawUrl_ReturnsNull() + { + const string url = "https://github.com/collinbarrett/FilterLists/blob/master/.gitattributes"; + var actualDates = await new GitHubService().GetCommitDatesAsync(url); + Assert.Null(actualDates); + } + + [Fact] + public async Task GetCommitDatesAsync_ValidGitHubRawUrl_ReturnsCorrectFirstAndLastCommitDates() { const string url = "https://raw.githubusercontent.com/collinbarrett/FilterLists/master/LICENSE"; var expectedDates = new CommitDates @@ -23,15 +31,7 @@ public async Task ReturnCorrectFirstAndLastCommitDatesForValidGitHubRawUrl() } [Fact] - public async Task ReturnNullForInvalidGitHubRawUrl() - { - const string url = "https://github.com/collinbarrett/FilterLists/blob/master/.gitattributes"; - var actualDates = await new GitHubService().GetCommitDatesAsync(url); - Assert.Null(actualDates); - } - - [Fact] - public async Task ReturnNullForValidGitHubRawUrlToNonexistentFile() + public async Task GetCommitDatesAsync_ValidGitHubRawUrlToNonexistentFile_ReturnsNull() { const string url = "https://github.com/collinbarrett/FilterLists/blob/master/doesnotexist"; var actualDates = await new GitHubService().GetCommitDatesAsync(url); @@ -39,7 +39,7 @@ public async Task ReturnNullForValidGitHubRawUrlToNonexistentFile() } [Fact] - public async Task ReturnSameFirstAndLastCommitDatesForValidGitHubRawUrlWithOnlyOneCommit() + public async Task GetCommitDatesAsync_ValidGitHubRawUrlWithOnlyOneCommit_ReturnsSameFirstAndLastCommitDates() { const string url = "https://raw.githubusercontent.com/collinbarrett/FilterLists/master/.gitattributes"; var expectedDate = DateTime.Parse("2017-04-08T00:05:26.0000000"); diff --git a/tests/FilterLists.Services.Tests/UserAgent/GetMostPopularStringAsyncShould.cs b/tests/FilterLists.Services.Tests/UserAgentServiceTests.cs similarity index 55% rename from tests/FilterLists.Services.Tests/UserAgent/GetMostPopularStringAsyncShould.cs rename to tests/FilterLists.Services.Tests/UserAgentServiceTests.cs index 7df3e9927..4011d1261 100644 --- a/tests/FilterLists.Services.Tests/UserAgent/GetMostPopularStringAsyncShould.cs +++ b/tests/FilterLists.Services.Tests/UserAgentServiceTests.cs @@ -1,11 +1,11 @@ using Xunit; -namespace FilterLists.Services.Tests.UserAgent +namespace FilterLists.Services.Tests { - public class GetMostPopularStringAsyncShould + public class UserAgentServiceTests { [Fact] - public async void ReturnStringContainingMozilla() + public async void GetMostPopularStringAsync_ReturnsStringContainingMozilla() { var actualString = await UserAgentService.GetMostPopularStringAsync(); Assert.Contains("Mozilla", actualString); diff --git a/tests/FilterLists.Services.Tests/Wayback/GetMostRecentSnapshotMetaAsyncShould.cs b/tests/FilterLists.Services.Tests/WaybackServiceTests.cs similarity index 70% rename from tests/FilterLists.Services.Tests/Wayback/GetMostRecentSnapshotMetaAsyncShould.cs rename to tests/FilterLists.Services.Tests/WaybackServiceTests.cs index 8929a7286..49f1ce906 100644 --- a/tests/FilterLists.Services.Tests/Wayback/GetMostRecentSnapshotMetaAsyncShould.cs +++ b/tests/FilterLists.Services.Tests/WaybackServiceTests.cs @@ -3,37 +3,37 @@ using FilterLists.Services.Wayback; using Xunit; -namespace FilterLists.Services.Tests.Wayback +namespace FilterLists.Services.Tests { - public class GetMostRecentSnapshotMetaAsyncShould + public class WaybackServiceTests { [Fact] - public async void ReturnAccessibleUrlRawIfInWaybackMachine() + public async void GetMostRecentSnapshotMetaAsync_UrlInWaybackMachine_ReturnsAccessibleRawUrl() { const string url = "https://github.com/collinbarrett/FilterLists"; var meta = await WaybackService.GetMostRecentSnapshotMetaAsync(url); using (var client = new HttpClient()) { - var response = await client.GetAsync(meta.UrlRaw); + var response = await client.GetAsync(meta.RawUrl); Assert.True(response.IsSuccessStatusCode); } } [Fact] - public async void ReturnNullIfUrlNotInWaybackMachine() - { - const string url = "https://raw.githubusercontent.com/collinbarrett/FilterLists/master/doesnotexist"; - var meta = await WaybackService.GetMostRecentSnapshotMetaAsync(url); - Assert.Null(meta); - } - - [Fact] - public async void ReturnRecentTimestampUtcIfInWaybackMachine() + public async void GetMostRecentSnapshotMetaAsync_UrlInWaybackMachine_ReturnsRecentTimestampUtc() { const string url = "https://github.com/collinbarrett/FilterLists"; var meta = await WaybackService.GetMostRecentSnapshotMetaAsync(url); Assert.True(meta.TimestampUtc > DateTime.Now.AddYears(-5).ToUniversalTime() && meta.TimestampUtc < DateTime.UtcNow); } + + [Fact] + public async void GetMostRecentSnapshotMetaAsync_UrlNotInWaybackMachine_ReturnsNull() + { + const string url = "https://raw.githubusercontent.com/collinbarrett/FilterLists/master/doesnotexist"; + var meta = await WaybackService.GetMostRecentSnapshotMetaAsync(url); + Assert.Null(meta); + } } } \ No newline at end of file