add GetNthIndexOfCharShould tests (#581)

This commit is contained in:
Collin M. Barrett 2018-10-13 17:46:36 +00:00 committed by GitHub
parent 334666c335
commit 51481d2da2
5 changed files with 50 additions and 14 deletions

View file

@ -9,7 +9,12 @@ public static bool IsValidHttpOrHttpsUrl(this string source) =>
Uri.TryCreate(source, UriKind.Absolute, out var uriResult) &&
new[] {Uri.UriSchemeHttps, Uri.UriSchemeHttp}.Contains(uriResult.Scheme);
public static int GetNthIndexOfChar(this string s, int n, char t) =>
s.TakeWhile(c => (n -= c == t ? 1 : 0) > 0).Count();
public static int GetNthIndexOfChar(this string s, int n, char t)
{
if (n < 1)
throw new ArgumentOutOfRangeException();
var nthIndexOfChar = s.TakeWhile(c => (n -= c == t ? 1 : 0) > 0).Count();
return nthIndexOfChar == s.Length ? -1 : nthIndexOfChar;
}
}
}

View file

@ -7,10 +7,7 @@ namespace FilterLists.Services.Tests.Extensions.Collection
{
public class AddIfNotNullOrEmptyShould
{
public AddIfNotNullOrEmptyShould() =>
sut = new Collection<string>();
private readonly ICollection<string> sut;
private readonly ICollection<string> sut = new Collection<string>();
private string item;
[Fact]

View file

@ -8,10 +8,7 @@ namespace FilterLists.Services.Tests.Extensions.Collection
{
public class AddRangeShould
{
public AddRangeShould() =>
sut = new Collection<string>();
private readonly ICollection<string> sut;
private readonly ICollection<string> sut = new Collection<string>();
private IEnumerable<string> range;
[Fact]

View file

@ -0,0 +1,35 @@
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));
}
}
}

View file

@ -5,11 +5,13 @@ namespace FilterLists.Services.Tests.Extensions.String
{
public class IsValidHttpOrHttpsUrlShould
{
[Fact]
public void ReturnFalseIfNotHttpOrHttps()
[Theory]
[InlineData("abp://www.google.com")]
[InlineData("www.google.com")]
[InlineData("google")]
public void ReturnFalseIfNotHttpOrHttps(string url)
{
const string abp = "abp://www.google.com";
Assert.False(abp.IsValidHttpOrHttpsUrl());
Assert.False(url.IsValidHttpOrHttpsUrl());
}
[Fact]