-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from feO2x/features/string-starts-with-ends-with
StartsWith and EndsWith assertions
- Loading branch information
Showing
14 changed files
with
718 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Code/Light.GuardClauses.Tests/StringAssertions/MustEndWithTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Code/Light.GuardClauses.Tests/StringAssertions/MustNotEndWithTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
85 changes: 85 additions & 0 deletions
85
Code/Light.GuardClauses.Tests/StringAssertions/MustNotStartWithTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
73 changes: 73 additions & 0 deletions
73
Code/Light.GuardClauses.Tests/StringAssertions/MustStartWithTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
Oops, something went wrong.