-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db5d2d6
commit efe4121
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/Tests/Tests.Dybal.Utils.Guards/ArgumentGuard/NullOrWhiteSpaceTests.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,110 @@ | ||
using Dybal.Utils.Guards; | ||
using Xunit; | ||
|
||
namespace Tests.Dybal.Utils.Guards.ArgumentGuard; | ||
|
||
public class NullOrWhiteSpaceTests : UnitTestsBase | ||
{ | ||
[Fact] | ||
public void NotThrow_When_null() | ||
{ | ||
// Arrange | ||
string? value = null; | ||
|
||
// Act | ||
Guard.Argument(value).NullOrWhiteSpace(); | ||
|
||
// Assert | ||
// doesn't throw any exception | ||
} | ||
|
||
[Fact] | ||
public void NotThrow_When_empty_string() | ||
{ | ||
// Arrange | ||
var value = string.Empty; | ||
|
||
// Act | ||
Guard.Argument(value).NullOrWhiteSpace(); | ||
|
||
// Assert | ||
// doesn't throw any exception | ||
} | ||
|
||
|
||
[Theory] | ||
[InlineData(" ")] | ||
[InlineData("\t")] | ||
[InlineData("\n")] | ||
public void NotThrow_When_white_space(string value) | ||
{ | ||
// Act | ||
Guard.Argument(value).NullOrWhiteSpace(); | ||
|
||
// Assert | ||
// doesn't throw any exception | ||
} | ||
|
||
[Fact] | ||
public void Throw_ArgumentException_When_is_not_empty() | ||
{ | ||
// Arrange | ||
var value = "non-empty"; | ||
|
||
void Act() | ||
{ | ||
Guard.Argument(value).NullOrWhiteSpace(); | ||
} | ||
|
||
// Assert | ||
var ex = Assert.Throws<ArgumentException>(Act); | ||
Assert.Equal("Value must be null or white space string. (Parameter 'value')", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void Throw_ArgumentException_with_custom_message_When_was_used() | ||
{ | ||
// Arrange | ||
var value = "non-empty"; | ||
var customMessage = "Custom message."; | ||
|
||
void Act() | ||
{ | ||
value = Guard.Argument(value).NullOrWhiteSpace(customMessage); | ||
} | ||
|
||
// Assert | ||
var ex = Assert.Throws<ArgumentException>(Act); | ||
Assert.Equal($"{customMessage} (Parameter 'value')", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void Throw_CustomException_When_Throws_was_used() | ||
{ | ||
// Arrange | ||
var value = "non-empty"; | ||
var customMessage = "Custom message."; | ||
|
||
void Act() | ||
{ | ||
ThrowHelper.TryRegister((paramName, message) => new CustomException(paramName, message)); | ||
value = Guard.Argument(value).Throws<CustomException>().NullOrWhiteSpace(customMessage); | ||
} | ||
|
||
// Assert | ||
var ex = Assert.Throws<CustomException>(Act); | ||
Assert.Equal(customMessage, ex.Message); | ||
Assert.Equal(nameof(value), ex.ParamName); | ||
} | ||
|
||
class CustomException : Exception | ||
{ | ||
public string ParamName { get; } | ||
|
||
public CustomException(string paramName, string? message) | ||
: base(message) | ||
{ | ||
ParamName = paramName; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Utils/Dybal.Utils.Guards/ArgumentGuardExtensions.NullOrWhiteSpace.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,15 @@ | ||
namespace Dybal.Utils.Guards; | ||
|
||
public static partial class ArgumentGuardExtensions | ||
{ | ||
public static ArgumentGuard<string> NullOrWhiteSpace(this ICovariantArgumentGuard<string?> covariantGuard, string? message = null) | ||
{ | ||
var guard = ArgumentGuard<string>.From(covariantGuard, covariantGuard.Argument!); | ||
if (!string.IsNullOrWhiteSpace(guard.Argument.Value)) | ||
{ | ||
ThrowHelper.Throw<ArgumentException>(guard, message ?? "Value must be null or white space string."); | ||
} | ||
|
||
return guard; | ||
} | ||
} |