Skip to content

Commit 84f55c4

Browse files
authored
Merge pull request #100 from feO2x/features/string-starts-with-ends-with
StartsWith and EndsWith assertions
2 parents 93cb102 + 8a53d51 commit 84f55c4

File tree

14 files changed

+718
-40
lines changed

14 files changed

+718
-40
lines changed

Code/Light.GuardClauses.Performance/StringAssertions/MustBeBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public string ImperativeVersion()
2222

2323
[Benchmark]
2424
public string LightGuardClausesCustomException() =>
25-
X.MustBe(Y, StringComparisonType.OrdinalIgnoreWhiteSpace, (x, y) => new Exception("The strings are not equal."));
25+
X.MustBe(Y, StringComparisonType.OrdinalIgnoreWhiteSpace, (_, _, _) => new Exception("The strings are not equal."));
2626
}
2727
}

Code/Light.GuardClauses.Performance/StringAssertions/MustNotBeBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public string ImperativeVersion()
2525

2626
[Benchmark]
2727
public string LightGuardClausesCustomException() =>
28-
X.MustNotBe(Y, StringComparisonType.OrdinalIgnoreCaseIgnoreWhiteSpace, (x, y) => new Exception("The strings are equal."));
28+
X.MustNotBe(Y, StringComparisonType.OrdinalIgnoreCaseIgnoreWhiteSpace, (_, _, _) => new Exception("The strings are equal."));
2929
}
3030
}

Code/Light.GuardClauses.Tests/StringAssertions/MustBeSubstringOfTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public static void CustomException(string first, string second) =>
9494
[InlineData("Foo", "FOO", StringComparison.Ordinal)]
9595
[InlineData("Bar", null, StringComparison.OrdinalIgnoreCase)]
9696
[InlineData(null, "Baz", StringComparison.CurrentCulture)]
97-
[InlineData("Qux", "Quux", (StringComparison) 509)]
9897
public static void CustomExceptionCustomComparisonType(string first, string second, StringComparison comparisonType) =>
9998
Test.CustomException(first,
10099
second,

Code/Light.GuardClauses.Tests/StringAssertions/MustBeTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public static void StringsNotEqualIgnoreWhiteSpace(string first, string second,
5050
public static void CustomException() =>
5151
Test.CustomException("Foo",
5252
"Bar",
53-
(x, y, exceptionFactory) => x.MustBe(y, StringComparison.Ordinal, exceptionFactory));
53+
StringComparison.Ordinal,
54+
(x, y, ct, exceptionFactory) => x.MustBe(y, ct, exceptionFactory));
5455

5556
[Fact]
5657
public static void CustomMessage() =>
@@ -60,7 +61,8 @@ public static void CustomMessage() =>
6061
public static void CustomExceptionIgnoreWhiteSpace() =>
6162
Test.CustomException("Foo",
6263
" foo",
63-
(x, y, exceptionFactory) => x.MustBe(y, StringComparisonType.OrdinalIgnoreWhiteSpace, exceptionFactory));
64+
StringComparisonType.OrdinalIgnoreWhiteSpace,
65+
(x, y, ct, exceptionFactory) => x.MustBe(y, ct, exceptionFactory));
6466

6567
[Fact]
6668
public static void CustomMessageIgnoreWhiteSpace() =>

Code/Light.GuardClauses.Tests/StringAssertions/MustContainTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public static void CustomException(string first, string second) =>
7979
[InlineData("Foo", "foo", StringComparison.Ordinal)]
8080
[InlineData(null, "Bar", StringComparison.OrdinalIgnoreCase)]
8181
[InlineData("Baz", null, StringComparison.CurrentCulture)]
82-
[InlineData("Qux", "Qux", (StringComparison) 42)]
8382
public static void CustomExceptionCustomSearch(string x, string y, StringComparison comparison) =>
8483
Test.CustomException(x,
8584
y,
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using FluentAssertions;
3+
using Light.GuardClauses.Exceptions;
4+
using Xunit;
5+
6+
namespace Light.GuardClauses.Tests.StringAssertions;
7+
8+
public static class MustEndWithTests
9+
{
10+
[Theory]
11+
[InlineData("Foo", "Bar")]
12+
[InlineData("When you play the game of thrones you win, or you die.", "you live")]
13+
public static void DoesNotEndWith(string x, string y)
14+
{
15+
var act = () => x.MustEndWith(y);
16+
17+
var exception = act.Should().Throw<SubstringException>().Which;
18+
exception.Message.Should().StartWith($"{nameof(x)} must end with \"{y}\" (CurrentCulture), but it actually is \"{x}\".");
19+
exception.ParamName.Should().BeSameAs(nameof(x));
20+
}
21+
22+
[Theory]
23+
[InlineData("FooBar", "Bar", StringComparison.Ordinal)]
24+
[InlineData("12345678", "5678", StringComparison.InvariantCultureIgnoreCase)]
25+
public static void EndsWith(string x, string y, StringComparison comparisonType) =>
26+
x.MustEndWith(y, comparisonType).Should().BeSameAs(x);
27+
28+
[Fact]
29+
public static void ParameterNull()
30+
{
31+
var act = () => ((string) null).MustEndWith("Foo");
32+
33+
act.Should().Throw<ArgumentNullException>();
34+
}
35+
36+
[Fact]
37+
public static void ValueNull()
38+
{
39+
var act = () => "Foo".MustEndWith(null!);
40+
41+
act.Should().Throw<ArgumentNullException>();
42+
}
43+
44+
[Fact]
45+
public static void CustomMessage() =>
46+
Test.CustomMessage<SubstringException>(message => "Foo".MustEndWith("Bar", message: message));
47+
48+
[Fact]
49+
public static void CustomMessageParameterNull() =>
50+
Test.CustomMessage<ArgumentNullException>(message => ((string) null).MustEndWith("Bar", message: message));
51+
52+
[Fact]
53+
public static void EndsWithCustomException() =>
54+
"Foo".MustEndWith("o", (_, _) => new Exception()).Should().Be("Foo");
55+
56+
[Fact]
57+
public static void EndsWithCustomExceptionAndComparisonType() =>
58+
"Foo".MustEndWith("O", StringComparison.OrdinalIgnoreCase, (_, _, _) => new Exception()).Should().Be("Foo");
59+
60+
[Theory]
61+
[InlineData("Foo", "Bar")]
62+
[InlineData("Foo", null)]
63+
[InlineData(null, "Baz")]
64+
public static void CustomException(string first, string second) =>
65+
Test.CustomException(first, second, (s1, s2, exceptionFactory) => s1.MustEndWith(s2, exceptionFactory));
66+
67+
[Theory]
68+
[InlineData("Foo", "Bar", StringComparison.Ordinal)]
69+
[InlineData(null, "Bar", StringComparison.Ordinal)]
70+
[InlineData("Baz", null, StringComparison.Ordinal)]
71+
public static void CustomExceptionWithComparisonType(string a, string b, StringComparison comparisonType) =>
72+
Test.CustomException(a, b, comparisonType, (s1, s2, ct, exceptionFactory) => s1.MustEndWith(s2, ct, exceptionFactory));
73+
}

Code/Light.GuardClauses.Tests/StringAssertions/MustNotBeSubstringOfTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public static void CustomException(string first, string second) =>
9393
[InlineData("Full of Possibilities", "Death is so terribly final, while life is full of possibilities", StringComparison.OrdinalIgnoreCase)]
9494
[InlineData(null, "Foo", StringComparison.Ordinal)]
9595
[InlineData("Bar", null, StringComparison.CurrentCulture)]
96-
[InlineData("Baz", "Qux", (StringComparison) (-14))]
9796
public static void CustomExceptionCustomComparison(string a, string b, StringComparison comparisonType) =>
9897
Test.CustomException(a,
9998
b,

Code/Light.GuardClauses.Tests/StringAssertions/MustNotBeTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static void CustomException() =>
5656
public static void CustomExceptionIgnoreWhiteSpace() =>
5757
Test.CustomException("Foo",
5858
" Foo",
59-
(x, y, exceptionFactory) => x.MustNotBe(y, StringComparisonType.OrdinalIgnoreWhiteSpace, exceptionFactory));
59+
StringComparisonType.OrdinalIgnoreWhiteSpace,
60+
(x, y, ct, exceptionFactory) => x.MustNotBe(y, ct, exceptionFactory));
6061

6162
[Fact]
6263
public static void CustomMessage() =>

Code/Light.GuardClauses.Tests/StringAssertions/MustNotContainTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public static void CustomException(string first, string second) =>
6464
[InlineData("Foo", "O", StringComparison.OrdinalIgnoreCase)]
6565
[InlineData("Bar", null, StringComparison.CurrentCulture)]
6666
[InlineData(null, "Baz", StringComparison.CurrentCultureIgnoreCase)]
67-
[InlineData("Qux", "Quux", (StringComparison) 504)]
6867
public static void CustomExceptionCustomSearch(string first, string second, StringComparison comparisonType) =>
6968
Test.CustomException(first,
7069
second,
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using FluentAssertions;
3+
using Light.GuardClauses.Exceptions;
4+
using Xunit;
5+
6+
namespace Light.GuardClauses.Tests.StringAssertions;
7+
8+
public static class MustNotEndWithTests
9+
{
10+
[Theory]
11+
[InlineData("FooBar", "Bar")]
12+
[InlineData("When you play the game of thrones you win, or you die.", "die.")]
13+
public static void DoesEndWith(string x, string y)
14+
{
15+
var act = () => x.MustNotEndWith(y);
16+
17+
var exception = act.Should().Throw<SubstringException>().Which;
18+
exception.Message.Should().StartWith($"{nameof(x)} must not end with \"{y}\" (CurrentCulture), but it actually is \"{x}\".");
19+
exception.ParamName.Should().BeSameAs(nameof(x));
20+
}
21+
22+
[Theory]
23+
[InlineData("Foo", "Bar", StringComparison.Ordinal)]
24+
[InlineData("12345", "1234", StringComparison.InvariantCultureIgnoreCase)]
25+
public static void DoesNotEndWith(string x, string y, StringComparison comparisonType) =>
26+
x.MustNotEndWith(y, comparisonType).Should().BeSameAs(x);
27+
28+
[Fact]
29+
public static void ParameterNull()
30+
{
31+
var act = () => ((string) null).MustNotEndWith("Foo");
32+
33+
act.Should().Throw<ArgumentNullException>();
34+
}
35+
36+
[Fact]
37+
public static void ValueNull()
38+
{
39+
var act = () => "Foo".MustNotEndWith(null!);
40+
41+
act.Should().Throw<ArgumentNullException>();
42+
}
43+
44+
[Fact]
45+
public static void CustomMessage() =>
46+
Test.CustomMessage<SubstringException>(message => "FooBar".MustNotEndWith("Bar", message: message));
47+
48+
[Fact]
49+
public static void CustomMessageParameterNull() =>
50+
Test.CustomMessage<ArgumentNullException>(message => ((string) null).MustNotEndWith("Bar", message: message));
51+
52+
[Fact]
53+
public static void DoesNotEndWithCustomException() =>
54+
"Foo".MustNotEndWith("r", (_, _) => new Exception()).Should().Be("Foo");
55+
56+
[Fact]
57+
public static void DoesNotEndWithCustomExceptionAndComparisonType() =>
58+
"Foo".MustNotEndWith("R", StringComparison.OrdinalIgnoreCase, (_, _, _) => new Exception()).Should().Be("Foo");
59+
60+
[Theory]
61+
[InlineData("FooBar", "Bar")]
62+
[InlineData("Foo", null)]
63+
[InlineData(null, "Baz")]
64+
public static void CustomException(string first, string second) =>
65+
Test.CustomException(first, second, (s1, s2, exceptionFactory) => s1.MustNotEndWith(s2, exceptionFactory));
66+
67+
[Theory]
68+
[InlineData("FooBar", "Bar", StringComparison.Ordinal)]
69+
[InlineData(null, "Bar", StringComparison.Ordinal)]
70+
[InlineData("Baz", null, StringComparison.Ordinal)]
71+
public static void CustomExceptionWithComparisonType(string a, string b, StringComparison comparisonType) =>
72+
Test.CustomException(a, b, comparisonType, (s1, s2, ct, exceptionFactory) => s1.MustNotEndWith(s2, ct, exceptionFactory));
73+
}

0 commit comments

Comments
 (0)