Skip to content

Commit

Permalink
Merge pull request #100 from feO2x/features/string-starts-with-ends-with
Browse files Browse the repository at this point in the history
StartsWith and EndsWith assertions
  • Loading branch information
feO2x authored Oct 27, 2024
2 parents 93cb102 + 8a53d51 commit 84f55c4
Show file tree
Hide file tree
Showing 14 changed files with 718 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public string ImperativeVersion()

[Benchmark]
public string LightGuardClausesCustomException() =>
X.MustBe(Y, StringComparisonType.OrdinalIgnoreWhiteSpace, (x, y) => new Exception("The strings are not equal."));
X.MustBe(Y, StringComparisonType.OrdinalIgnoreWhiteSpace, (_, _, _) => new Exception("The strings are not equal."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public string ImperativeVersion()

[Benchmark]
public string LightGuardClausesCustomException() =>
X.MustNotBe(Y, StringComparisonType.OrdinalIgnoreCaseIgnoreWhiteSpace, (x, y) => new Exception("The strings are equal."));
X.MustNotBe(Y, StringComparisonType.OrdinalIgnoreCaseIgnoreWhiteSpace, (_, _, _) => new Exception("The strings are equal."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public static void CustomException(string first, string second) =>
[InlineData("Foo", "FOO", StringComparison.Ordinal)]
[InlineData("Bar", null, StringComparison.OrdinalIgnoreCase)]
[InlineData(null, "Baz", StringComparison.CurrentCulture)]
[InlineData("Qux", "Quux", (StringComparison) 509)]
public static void CustomExceptionCustomComparisonType(string first, string second, StringComparison comparisonType) =>
Test.CustomException(first,
second,
Expand Down
6 changes: 4 additions & 2 deletions Code/Light.GuardClauses.Tests/StringAssertions/MustBeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static void StringsNotEqualIgnoreWhiteSpace(string first, string second,
public static void CustomException() =>
Test.CustomException("Foo",
"Bar",
(x, y, exceptionFactory) => x.MustBe(y, StringComparison.Ordinal, exceptionFactory));
StringComparison.Ordinal,
(x, y, ct, exceptionFactory) => x.MustBe(y, ct, exceptionFactory));

[Fact]
public static void CustomMessage() =>
Expand All @@ -60,7 +61,8 @@ public static void CustomMessage() =>
public static void CustomExceptionIgnoreWhiteSpace() =>
Test.CustomException("Foo",
" foo",
(x, y, exceptionFactory) => x.MustBe(y, StringComparisonType.OrdinalIgnoreWhiteSpace, exceptionFactory));
StringComparisonType.OrdinalIgnoreWhiteSpace,
(x, y, ct, exceptionFactory) => x.MustBe(y, ct, exceptionFactory));

[Fact]
public static void CustomMessageIgnoreWhiteSpace() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public static void CustomException(string first, string second) =>
[InlineData("Foo", "foo", StringComparison.Ordinal)]
[InlineData(null, "Bar", StringComparison.OrdinalIgnoreCase)]
[InlineData("Baz", null, StringComparison.CurrentCulture)]
[InlineData("Qux", "Qux", (StringComparison) 42)]
public static void CustomExceptionCustomSearch(string x, string y, StringComparison comparison) =>
Test.CustomException(x,
y,
Expand Down
73 changes: 73 additions & 0 deletions Code/Light.GuardClauses.Tests/StringAssertions/MustEndWithTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using FluentAssertions;
using Light.GuardClauses.Exceptions;
using Xunit;

namespace Light.GuardClauses.Tests.StringAssertions;

public static class MustEndWithTests
{
[Theory]
[InlineData("Foo", "Bar")]
[InlineData("When you play the game of thrones you win, or you die.", "you live")]
public static void DoesNotEndWith(string x, string y)
{
var act = () => x.MustEndWith(y);

var exception = act.Should().Throw<SubstringException>().Which;
exception.Message.Should().StartWith($"{nameof(x)} must end with \"{y}\" (CurrentCulture), but it actually is \"{x}\".");
exception.ParamName.Should().BeSameAs(nameof(x));
}

[Theory]
[InlineData("FooBar", "Bar", StringComparison.Ordinal)]
[InlineData("12345678", "5678", StringComparison.InvariantCultureIgnoreCase)]
public static void EndsWith(string x, string y, StringComparison comparisonType) =>
x.MustEndWith(y, comparisonType).Should().BeSameAs(x);

[Fact]
public static void ParameterNull()
{
var act = () => ((string) null).MustEndWith("Foo");

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void ValueNull()
{
var act = () => "Foo".MustEndWith(null!);

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void CustomMessage() =>
Test.CustomMessage<SubstringException>(message => "Foo".MustEndWith("Bar", message: message));

[Fact]
public static void CustomMessageParameterNull() =>
Test.CustomMessage<ArgumentNullException>(message => ((string) null).MustEndWith("Bar", message: message));

[Fact]
public static void EndsWithCustomException() =>
"Foo".MustEndWith("o", (_, _) => new Exception()).Should().Be("Foo");

[Fact]
public static void EndsWithCustomExceptionAndComparisonType() =>
"Foo".MustEndWith("O", StringComparison.OrdinalIgnoreCase, (_, _, _) => new Exception()).Should().Be("Foo");

[Theory]
[InlineData("Foo", "Bar")]
[InlineData("Foo", null)]
[InlineData(null, "Baz")]
public static void CustomException(string first, string second) =>
Test.CustomException(first, second, (s1, s2, exceptionFactory) => s1.MustEndWith(s2, exceptionFactory));

[Theory]
[InlineData("Foo", "Bar", StringComparison.Ordinal)]
[InlineData(null, "Bar", StringComparison.Ordinal)]
[InlineData("Baz", null, StringComparison.Ordinal)]
public static void CustomExceptionWithComparisonType(string a, string b, StringComparison comparisonType) =>
Test.CustomException(a, b, comparisonType, (s1, s2, ct, exceptionFactory) => s1.MustEndWith(s2, ct, exceptionFactory));
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public static void CustomException(string first, string second) =>
[InlineData("Full of Possibilities", "Death is so terribly final, while life is full of possibilities", StringComparison.OrdinalIgnoreCase)]
[InlineData(null, "Foo", StringComparison.Ordinal)]
[InlineData("Bar", null, StringComparison.CurrentCulture)]
[InlineData("Baz", "Qux", (StringComparison) (-14))]
public static void CustomExceptionCustomComparison(string a, string b, StringComparison comparisonType) =>
Test.CustomException(a,
b,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static void CustomException() =>
public static void CustomExceptionIgnoreWhiteSpace() =>
Test.CustomException("Foo",
" Foo",
(x, y, exceptionFactory) => x.MustNotBe(y, StringComparisonType.OrdinalIgnoreWhiteSpace, exceptionFactory));
StringComparisonType.OrdinalIgnoreWhiteSpace,
(x, y, ct, exceptionFactory) => x.MustNotBe(y, ct, exceptionFactory));

[Fact]
public static void CustomMessage() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public static void CustomException(string first, string second) =>
[InlineData("Foo", "O", StringComparison.OrdinalIgnoreCase)]
[InlineData("Bar", null, StringComparison.CurrentCulture)]
[InlineData(null, "Baz", StringComparison.CurrentCultureIgnoreCase)]
[InlineData("Qux", "Quux", (StringComparison) 504)]
public static void CustomExceptionCustomSearch(string first, string second, StringComparison comparisonType) =>
Test.CustomException(first,
second,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using FluentAssertions;
using Light.GuardClauses.Exceptions;
using Xunit;

namespace Light.GuardClauses.Tests.StringAssertions;

public static class MustNotEndWithTests
{
[Theory]
[InlineData("FooBar", "Bar")]
[InlineData("When you play the game of thrones you win, or you die.", "die.")]
public static void DoesEndWith(string x, string y)
{
var act = () => x.MustNotEndWith(y);

var exception = act.Should().Throw<SubstringException>().Which;
exception.Message.Should().StartWith($"{nameof(x)} must not end with \"{y}\" (CurrentCulture), but it actually is \"{x}\".");
exception.ParamName.Should().BeSameAs(nameof(x));
}

[Theory]
[InlineData("Foo", "Bar", StringComparison.Ordinal)]
[InlineData("12345", "1234", StringComparison.InvariantCultureIgnoreCase)]
public static void DoesNotEndWith(string x, string y, StringComparison comparisonType) =>
x.MustNotEndWith(y, comparisonType).Should().BeSameAs(x);

[Fact]
public static void ParameterNull()
{
var act = () => ((string) null).MustNotEndWith("Foo");

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void ValueNull()
{
var act = () => "Foo".MustNotEndWith(null!);

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void CustomMessage() =>
Test.CustomMessage<SubstringException>(message => "FooBar".MustNotEndWith("Bar", message: message));

[Fact]
public static void CustomMessageParameterNull() =>
Test.CustomMessage<ArgumentNullException>(message => ((string) null).MustNotEndWith("Bar", message: message));

[Fact]
public static void DoesNotEndWithCustomException() =>
"Foo".MustNotEndWith("r", (_, _) => new Exception()).Should().Be("Foo");

[Fact]
public static void DoesNotEndWithCustomExceptionAndComparisonType() =>
"Foo".MustNotEndWith("R", StringComparison.OrdinalIgnoreCase, (_, _, _) => new Exception()).Should().Be("Foo");

[Theory]
[InlineData("FooBar", "Bar")]
[InlineData("Foo", null)]
[InlineData(null, "Baz")]
public static void CustomException(string first, string second) =>
Test.CustomException(first, second, (s1, s2, exceptionFactory) => s1.MustNotEndWith(s2, exceptionFactory));

[Theory]
[InlineData("FooBar", "Bar", StringComparison.Ordinal)]
[InlineData(null, "Bar", StringComparison.Ordinal)]
[InlineData("Baz", null, StringComparison.Ordinal)]
public static void CustomExceptionWithComparisonType(string a, string b, StringComparison comparisonType) =>
Test.CustomException(a, b, comparisonType, (s1, s2, ct, exceptionFactory) => s1.MustNotEndWith(s2, ct, exceptionFactory));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using FluentAssertions;
using Light.GuardClauses.Exceptions;
using Xunit;

namespace Light.GuardClauses.Tests.StringAssertions;

public static class MustNotStartWithTests
{
[Theory]
[InlineData("Bar", "Foo")]
[InlineData("When you play the game of thrones you win, or you die.", "Love and peace")]
public static void DoesNotStartWith(string x, string y) =>
x.MustNotStartWith(y).Should().BeSameAs(x);

[Theory]
[InlineData("FooBar", "Foo")]
[InlineData("12345678", "1234")]
public static void StartsWith(string x, string y)
{
var act = () => x.MustNotStartWith(y);

var exception = act.Should().Throw<SubstringException>().Which;
exception.Message.Should().StartWith($"{nameof(x)} must not start with \"{y}\" (CurrentCulture), but it actually is \"{x}\"");
exception.ParamName.Should().BeSameAs(nameof(x));
}

[Theory]
[InlineData("FooBar", "Foo", StringComparison.Ordinal)]
[InlineData("12345678", "1234", StringComparison.InvariantCultureIgnoreCase)]
public static void StartsWithComparisonType(string x, string y, StringComparison comparisonType)
{
var act = () => x.MustNotStartWith(y, comparisonType);

var exception = act.Should().Throw<SubstringException>().Which;
exception.Message.Should().StartWith($"{nameof(x)} must not start with \"{y}\" ({comparisonType}), but it actually is \"{x}\"");
exception.ParamName.Should().BeSameAs(nameof(x));
}

[Fact]
public static void ParameterNull()
{
var act = () => ((string)null).MustNotStartWith("Foo");

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void ValueNull()
{
var act = () => "Foo".MustNotStartWith(null!);

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void CustomMessage() =>
Test.CustomMessage<SubstringException>(message => "FooBar".MustNotStartWith("Foo", message: message));

[Fact]
public static void CustomMessageParameterNull() =>
Test.CustomMessage<ArgumentNullException>(message => ((string)null).MustNotStartWith("Bar", message: message));

[Fact]
public static void NotStartsWithCustomException() =>
"Bar".MustNotStartWith("Foo", (_, _) => new Exception()).Should().Be("Bar");

[Fact]
public static void NotStartsWithCustomExceptionAndComparisonType() =>
"Bar".MustNotStartWith("foo", StringComparison.OrdinalIgnoreCase, (_, _, _) => new Exception()).Should().Be("Bar");

[Theory]
[InlineData("Foo", "Foo")]
[InlineData("Foo", null)]
[InlineData(null, "Baz")]
public static void CustomException(string first, string second) =>
Test.CustomException(first, second, (s1, s2, exceptionFactory) => s1.MustNotStartWith(s2, exceptionFactory));

[Theory]
[InlineData("Foo", "foo", StringComparison.OrdinalIgnoreCase)]
[InlineData(null, "Bar", StringComparison.Ordinal)]
[InlineData("Baz", null, StringComparison.Ordinal)]
public static void CustomExceptionWithComparisonType(string a, string b, StringComparison comparisonType) =>
Test.CustomException(a, b, comparisonType, (s1, s2, ct, exceptionFactory) => s1.MustNotStartWith(s2, ct, exceptionFactory));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using FluentAssertions;
using Light.GuardClauses.Exceptions;
using Xunit;

namespace Light.GuardClauses.Tests.StringAssertions;

public static class MustStartWithTests
{
[Theory]
[InlineData("Foo", "Bar")]
[InlineData("When you play the game of thrones you win, or you die. There is no middle ground.", "Love and peace")]
public static void DoesNotStartWith(string x, string y)
{
var act = () => x.MustStartWith(y);

var exception = act.Should().Throw<SubstringException>().Which;
exception.Message.Should().StartWith($"{nameof(x)} must start with \"{y}\" (CurrentCulture), but it actually is \"{x}\".");
exception.ParamName.Should().BeSameAs(nameof(x));
}

[Theory]
[InlineData("FooBar", "Foo", StringComparison.Ordinal)]
[InlineData("12345678", "1234", StringComparison.InvariantCultureIgnoreCase)]
public static void StartsWith(string x, string y, StringComparison comparisonType) =>
x.MustStartWith(y, comparisonType).Should().BeSameAs(x);

[Fact]
public static void ParameterNull()
{
var act = () => ((string) null).MustStartWith("Foo");

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void ValueNull()
{
var act = () => "Foo".MustStartWith(null!);

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public static void CustomMessage() =>
Test.CustomMessage<SubstringException>(message => "Foo".MustStartWith("Bar", message: message));

[Fact]
public static void CustomMessageParameterNull() =>
Test.CustomMessage<ArgumentNullException>(message => ((string) null).MustStartWith("Bar", message: message));

[Fact]
public static void StartsWithCustomException() =>
"Foo".MustStartWith("F", (_, _) => new Exception()).Should().Be("Foo");

[Fact]
public static void StartsWithCustomExceptionAndComparisonType() =>
"Foo".MustStartWith("f", StringComparison.OrdinalIgnoreCase, (_, _, _) => new Exception()).Should().Be("Foo");

[Theory]
[InlineData("Foo", "Bar")]
[InlineData("Foo", null)]
[InlineData(null, "Baz")]
public static void CustomException(string first, string second) =>
Test.CustomException(first, second, (s1, s2, exceptionFactory) => s1.MustStartWith(s2, exceptionFactory));

[Theory]
[InlineData("Foo", "Bar", StringComparison.Ordinal)]
[InlineData(null, "Bar", StringComparison.Ordinal)]
[InlineData("Baz", null, StringComparison.Ordinal)]
public static void CustomExceptionWithComparisonType(string a, string b, StringComparison comparisonType) =>
Test.CustomException(a, b, comparisonType, (s1, s2, ct, exceptionFactory) => s1.MustStartWith(s2, ct, exceptionFactory));
}
Loading

0 comments on commit 84f55c4

Please sign in to comment.