Skip to content

Commit

Permalink
- add boundary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KinNeko-De committed Feb 10, 2024
1 parent 1372fad commit 00706c0
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions Sample.HumanFriendlyId.UnitTest/HumanFriendlyIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,48 @@ public void Id_IsGenerated()
Assert.That(actualId, Does.Match($"^[{allowedChars}]+$"));
}

[Test]
public void DisplayId_IsGroupedBySpaces()
[TestCase(1, 1)]
[TestCase(4, 4)]
[TestCase(5, 6)]
[TestCase(8, 9)]
[TestCase(9, 11)]
[TestCase(12, 14)]
[TestCase(13, 16)]
public void DisplayId_IsGroupedBySpaces(int length, int expectedLength)
{
var length = 16;
var expectedLength = 19;

var sut = new HumanFriendlyId(length);
var actualDisplayId = sut.DisplayId;

string regex = CreateRegex(length);

Assert.That(actualDisplayId.Length, Is.EqualTo(expectedLength));
Assert.That(actualDisplayId, Does.Match($"^[{allowedChars}]{{4}} [{allowedChars}]{{4}} [{allowedChars}]{{4}} [{allowedChars}]{{4}}$"));
Assert.That(actualDisplayId, Does.Match(regex));
}

private static string CreateRegex(int length)
{
var rest = length % 4;
var expectedBlocks = length / 4;

const string regexTemplate = $"[{allowedChars}]{{4}}";
string lastRegex = rest > 0 ? $"[{allowedChars}]{{{rest}}}" : string.Empty;
string regex = $"^";

for (int i = 0; i < expectedBlocks; i++)
{
if (i != 0)
{
regex += " ";
}
regex += regexTemplate;
}

if (expectedBlocks > 0 && rest > 0)
{
regex += " ";
}
regex += lastRegex + "$";
return regex;
}

[TestCase("ABCDEFGHJKLM", "ABCDEFGHJKLM", TestName = nameof(Parse) + "_InputIsUpperCase")]
Expand Down

0 comments on commit 00706c0

Please sign in to comment.