mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
apply Roy Osherove unit test naming standard
This commit is contained in:
parent
e6df789dcc
commit
2aa9906202
14 changed files with 192 additions and 210 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ public static async Task<SnapshotMetaDto> 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<string> sut = new Collection<string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<string> sut = new Collection<string>();
|
||||
private IEnumerable<string> range;
|
||||
|
||||
[Fact]
|
||||
public void AddCollectionOfStrings()
|
||||
{
|
||||
range = new Collection<string> {"item1", "item2"};
|
||||
sut.AddRange(range);
|
||||
Assert.True(!range.Except(sut).Any());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddListOfStrings()
|
||||
{
|
||||
range = new List<string> {"item1", "item2"};
|
||||
sut.AddRange(range);
|
||||
Assert.True(!range.Except(sut).Any());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotAddAnyItemsFromEmptyCollection()
|
||||
{
|
||||
range = new Collection<string>();
|
||||
sut.AddRange(range);
|
||||
Assert.Empty(sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotAddAnyItemsFromEmptyList()
|
||||
{
|
||||
range = new List<string>();
|
||||
sut.AddRange(range);
|
||||
Assert.Empty(sut);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<string> sut = new Collection<string>();
|
||||
private IEnumerable<string> 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<string>();
|
||||
sut.AddRange(range);
|
||||
Assert.Empty(sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRange_EmptyList_AddsNothing()
|
||||
{
|
||||
range = new List<string>();
|
||||
sut.AddRange(range);
|
||||
Assert.Empty(sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRange_StringCollection_AddsParam()
|
||||
{
|
||||
range = new Collection<string> {"item1", "item2"};
|
||||
sut.AddRange(range);
|
||||
Assert.True(!range.Except(sut).Any());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRange_StringList_AddsParam()
|
||||
{
|
||||
range = new List<string> {"item1", "item2"};
|
||||
sut.AddRange(range);
|
||||
Assert.True(!range.Except(sut).Any());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<ArgumentOutOfRangeException>(() => Sut.GetNthIndexOfChar(n, c));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<ArgumentOutOfRangeException>(() => 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<TimeoutException>(() =>
|
||||
sut.TimeoutAfter(oneTickTimeout));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<TimeoutException>(() => taskWithTimeout);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void TimeoutAfter_TimeToCompleteIsLessThanTimeout_CompletesSuccessfully()
|
||||
{
|
||||
var twoSecondTimeout = new TimeSpan(0, 0, 2);
|
||||
await sut.TimeoutAfter(twoSecondTimeout);
|
||||
Assert.True(sut.IsCompletedSuccessfully);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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");
|
||||
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue