Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.RegularExpressions;
using FluentAssertions;
using FunctionalStringExtensions;

namespace FunctionalStringExtensionsTests;
Expand All @@ -15,7 +14,7 @@ public void ShouldReturnDefaultValueWhenStringIsInvalid(string? value)
var result = value.OrDefault("abc");

//Assert
result.Should().Be("abc");
Assert.Equal("abc", result);
}

[Theory]
Expand All @@ -26,7 +25,7 @@ public async Task ShouldReturnValueAsyncWhenStringIsValid(string value)
var result = await value.OrDefaultAsync(Task.FromResult("abc"));

//Assert
result.Should().Be(value);
Assert.Equal(value, result);
}

[Theory]
Expand All @@ -38,7 +37,7 @@ public async Task ShouldReturnDefaultValueAsyncWhenStringIsInvalid(string? value
var result = await value.OrDefaultAsync(Task.FromResult("abc"));

//Assert
result.Should().Be("abc");
Assert.Equal("abc", result);
}

[Theory]
Expand All @@ -49,7 +48,7 @@ public void ShouldReturnValueWhenStringIsValid(string value)
var result = value.OrDefault("abc");

//Assert
result.Should().Be(value);
Assert.Equal(value, result);
}

[Theory]
Expand All @@ -61,7 +60,7 @@ public void ShouldReturnFuncValueWhenStringIsInvalid(string? value)
var result = value.WhenNullOrEmpty(() => "abc");

//Assert
result.Should().Be("abc");
Assert.Equal("abc", result);
}

[Theory]
Expand All @@ -72,7 +71,7 @@ public void ShouldNotReturnFuncWhenStringIsValid(string value)
var result = value.WhenNullOrEmpty(() => "abc");

//Assert
result.Should().Be(value);
Assert.Equal(value, result);
}

[Theory]
Expand All @@ -84,7 +83,7 @@ public async Task ShouldReturnFuncValueAsyncWhenStringIsInvalid(string? value)
var result = await value.WhenNullOrEmptyAsync(() => Task.FromResult("abc"));

//Assert
result.Should().Be("abc");
Assert.Equal("abc", result);
}

[Theory]
Expand All @@ -95,7 +94,7 @@ public async Task ShouldNotReturnFuncAsyncWhenStringIsValid(string value)
var result = await value.WhenNullOrEmptyAsync(() => Task.FromResult("abc"));

//Assert
result.Should().Be(value);
Assert.Equal(value, result);
}

[Theory]
Expand All @@ -110,7 +109,7 @@ public void ShouldExecuteActionValueWhenStringIsInvalid(string? value)
value.OnNullOrEmpty(Act);

//Assert
initValue.Should().BeGreaterThan(0);
Assert.True(initValue > 0);
return;

void Act()
Expand All @@ -131,7 +130,7 @@ public async Task ShouldExecuteActionValueAsyncWhenStringIsInvalid(string? value
await value.OnNullOrEmptyAsync(Task.Run(() => Act(ref initValue)));

//Assert
initValue.Should().BeGreaterThan(0);
Assert.True(initValue > 0);
return;

void Act(ref int change)
Expand All @@ -149,62 +148,60 @@ public void ShouldMakeStringAsSlugs(string? text, string expected)
{
//Arrange Act
var result = text.ToSlug();

//Assert
result.Should().Be(expected);
Assert.Equal(expected, result);
}

[Fact]
public void ToSlugShouldThrowRegexMatchTimeoutExceptionWhenStringIsToLong()
{
//Arrange
var text = string.Concat(Enumerable.Repeat("a", 10_000_000));

// Act
Action act = () => text.ToSlug();

//Assert
act.Should().Throw<RegexMatchTimeoutException>();
Assert.Throws<RegexMatchTimeoutException>(() => text.ToSlug());
}

[Fact]
public void ToEnumShouldTransformToEnumStruct()
{
//Arrange
var stringEnum = "Value1";
const string stringEnum = "Value1";

//Act
var result = stringEnum.ToEnum<FakeEnum>();

//Assert
result.Should().Be(FakeEnum.Value1);
Assert.Equal(FakeEnum.Value1, result);
}

[Theory]
[InlineData(FakeEnum.Value2)]
public void ToEnumShouldReturnChosenDefaultValueWhenStringIsInvalid(FakeEnum fakeEnum)
{
//Arrange
var stringEnum = "Invalid";
const string stringEnum = "Invalid";

//Act
var result = stringEnum.ToEnum<FakeEnum>(fakeEnum);

//Assert
result.Should().Be(fakeEnum);
Assert.Equal(fakeEnum, result);
}

[Fact]
public void ToEnumShouldReturnDefaultValueWhenStringIsInvalid()
{
//Arrange
var stringEnum = "Invalid";
const string stringEnum = "Invalid";

//Act
var result = stringEnum.ToEnum<FakeEnum>();

//Assert
result.Should().Be(FakeEnum.Value1);
Assert.Equal(FakeEnum.Value1, result);
}

[Theory]
Expand All @@ -218,11 +215,11 @@ public void OnlyLettersShouldReturnOnlyLetters(string? value, string expected)
{
//Arrange & Act
var result = value.OnlyLetters();

//Assert
result.Should().Be(expected);
Assert.Equal(expected, result);
}

[Theory]
[InlineData(null, "")]
[InlineData("", "")]
Expand All @@ -235,11 +232,11 @@ public void OnlyNumbersShouldReturnOnlyNumbers(string? value, string expected)
{
//Arrange & Act
var result = value.OnlyNumbers();

//Assert
result.Should().Be(expected);
Assert.Equal(expected, result);
}

[Theory]
[InlineData(null, "")]
[InlineData("", "")]
Expand All @@ -251,11 +248,11 @@ public void OnlyCharactersAndNumbersShouldReturnOnlyCharactersAndNumbers(string?
{
//Arrange & Act
var result = value.OnlyCharactersAndNumbers();

//Assert
result.Should().Be(expected);
Assert.Equal(expected, result);
}

[Theory]
[InlineData(null, "")]
[InlineData("", "")]
Expand All @@ -268,9 +265,9 @@ public void OnlySpecialCharactersShouldReturnOnlySpecialCharacters(string? value
{
//Arrange & Act
var result = value.OnlySpecialCharacters();

//Assert
result.Should().Be(expected);
Assert.Equal(expected, result);
}

[Theory]
Expand All @@ -284,11 +281,11 @@ public void ParseQueryString_ShouldReturnEmptyListWhenNotAValidInput(string? val
{
var result = value.ParseQueryString();

result.Should().BeEmpty();
Assert.Empty(result);
}

[Theory]
[InlineData("http://yoursite.com?variable1=false", "variable1", false)]
[InlineData("http://yoursite.com?variable1=false", "variable1", false)]
[InlineData("http://yoursite.com?variable1=False", "variable1", false)]
[InlineData("http://yoursite.com?variable1=1", "variable1", 1)]
[InlineData("http://yoursite.com?variable1=19.86", "variable1", 19.86)]
Expand All @@ -297,19 +294,23 @@ public void ParseQueryString_ShouldReturnEmptyListWhenNotAValidInput(string? val
[InlineData("?variable1=1", "variable1", 1)]
[InlineData("?variable1=0.77", "variable1", 0.77)]
[InlineData("?variable1=test", "variable1", "test")]
public void ParseQueryString_ConvertType_ShouldReturnListOfKeyValues(string? value, string expectedKey, object expectedValue)
public void ParseQueryString_ConvertType_ShouldReturnListOfKeyValues(string? value, string expectedKey,
object expectedValue)
{
var result = value.ParseQueryString(autoConvertType: true);

result.Should().NotBeEmpty();
result.Should().HaveCount(1);
result.First().Key.Should().Be(expectedKey);
result.First().Value.Should().Be(expectedValue);
result.First().Value.Should().BeOfType(expectedValue.GetType());
Assert.NotEmpty(result);
Assert.Single(result);
Assert.Equal(expectedKey, result.First()
.Key);
Assert.Equal(expectedValue, result.First()
.Value);
Assert.IsType(expectedValue.GetType(), result.First()
.Value);
}

[Theory]
[InlineData("http://yoursite.com?variable1=false", "variable1", "false")]
[InlineData("http://yoursite.com?variable1=false", "variable1", "false")]
[InlineData("http://yoursite.com?variable1=False", "variable1", "False")]
[InlineData("http://yoursite.com?variable1=1", "variable1", "1")]
[InlineData("http://yoursite.com?variable1=19.86", "variable1", "19.86")]
Expand All @@ -318,50 +319,57 @@ public void ParseQueryString_ConvertType_ShouldReturnListOfKeyValues(string? val
[InlineData("?variable1=1", "variable1", "1")]
[InlineData("?variable1=0.77", "variable1", "0.77")]
[InlineData("?variable1=test", "variable1", "test")]
public void ParseQueryString_WithoutConversion_ShouldReturnListOfKeyValues(string? value, string expectedKey, string expectedValue)
public void ParseQueryString_WithoutConversion_ShouldReturnListOfKeyValues(string? value, string expectedKey,
string expectedValue)
{
var result = value.ParseQueryString(autoConvertType: false);

result.Should().NotBeEmpty();
result.Should().HaveCount(1);
result.First().Key.Should().Be(expectedKey);
result.First().Value.Should().Be(expectedValue);
result.First().Value.Should().BeOfType<string>();
Assert.NotEmpty(result);
Assert.Single(result);
Assert.Equal(expectedKey, result.First()
.Key);
Assert.Equal(expectedValue, result.First()
.Value);
Assert.IsType<string>(result.First()
.Value);
}

[Theory]
[InlineData("?variable1=15&variable2=is that a question?&variable3=true&variable4=33.88&variable5=", "variable1=15|variable2=is that a question?|variable3=true|variable4=33.88|variable5=")]
[InlineData("?variable1=15&variable2=is that a question?&variable3=true&variable4=33.88&variable5=",
"variable1=15|variable2=is that a question?|variable3=true|variable4=33.88|variable5=")]
public void ParseQueryString_ShouldReturnListOfKeyValues(string? value, string expectedPipedString)
{
var result = value.ParseQueryString(autoConvertType: false);

var expected = expectedPipedString
.Split('|', StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Split('='))
.ToDictionary(k => k[0], v => v[1]);

result.Should().NotBeEmpty();
result.Should().BeEquivalentTo(expected);
var result = value.ParseQueryString(autoConvertType: false);

var expected = expectedPipedString.Split('|', StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Split('='))
.ToDictionary(k => k[0], v => v[1]);

Assert.NotEmpty(result);
Assert.Equivalent(expected, result);
}

[Theory]
[InlineData("?not a valid url", "not a valid url=")]
[InlineData("?not a valid url&&&&&", "not a valid url=")]
[InlineData("?not a valid url&&&&&=value", "not a valid url=|=value")]
[InlineData(" ?not a valid url&&&&&=value ", "not a valid url=|=value ")]
[InlineData("variable1=15&variable2=is that a question?&variable3=true&variable4=33.88&variable5=", "variable3=true|variable4=33.88|variable5=")]

[InlineData("variable1=15&variable2=is that a question?&variable3=true&variable4=33.88&variable5=",
"variable3=true|variable4=33.88|variable5=")]
public void ParseQueryString_EdgeCasesShouldReturnListOfKeyValues(string? value, string expectedPipedString)
{
var result = value.ParseQueryString(autoConvertType: false);

var expected = expectedPipedString
.Split('|')

var expected = expectedPipedString.Split('|')
.Select(s => s.Split('='))
.ToDictionary(k => k.Length > 0? k[0] : string.Empty, v => v.Length > 1 ? v[1]: string.Empty);

result.Should().NotBeEmpty();
result.Should().BeEquivalentTo(expected);
.ToDictionary(k => k.Length > 0
? k[0]
: string.Empty, v => v.Length > 1
? v[1]
: string.Empty);

Assert.NotEmpty(result);
Assert.Equivalent(expected, result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="6.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading
Loading