-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add string length out of range. (#333)
* feat: add string length out of range. * feat: Add min and max check. * Refactor LengthOutOfRange.
- Loading branch information
1 parent
36051ca
commit f1411c4
Showing
2 changed files
with
93 additions
and
0 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
57 changes: 57 additions & 0 deletions
57
test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.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,57 @@ | ||
using System; | ||
using Ardalis.GuardClauses; | ||
using Xunit; | ||
|
||
namespace GuardClauses.UnitTests; | ||
|
||
public class GuardAgainstStringLengthOutOfRange | ||
{ | ||
[Fact] | ||
public void DoesNothingGivenNonEmptyString() | ||
{ | ||
Guard.Against.LengthOutOfRange("a", 1, 4, "string"); | ||
Guard.Against.LengthOutOfRange("abc", 1, 4, "string"); | ||
Guard.Against.LengthOutOfRange("a", 1, 4, "string"); | ||
Guard.Against.LengthOutOfRange("a", 1, 4, "string"); | ||
Guard.Against.LengthOutOfRange("a", 1, 4, "string"); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsGivenEmptyString() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", 1, 2, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsGivenNonPositiveMinLength() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", 0, 0, "string")); | ||
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("", -1, -1, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsGivenStringShorterThanMinLength() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("a", 2, 2, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsGivenStringLongerThanMaxLength() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("abcd", 2, 2, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsWhenMinIsBiggerThanMax() | ||
{ | ||
Assert.Throws<ArgumentException>(() => Guard.Against.LengthOutOfRange("asd", 4, 2, "string")); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsExpectedValueWhenGivenLongerString() | ||
{ | ||
var expected = "abc"; | ||
var actual = Guard.Against.LengthOutOfRange("abc", 2, 5, "string"); | ||
Assert.Equal(expected, actual); | ||
} | ||
} |