Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check on length as first test on validation/parse. #65

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions Personnummer.Tests/PersonnummerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,23 @@ public void TestSeparator(PersonnummerData ssn)
Assert.Equal(sep, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = true }).Separator);
// Getting the separator from a short formatted none-separated person number is not actually possible if it is intended to be a +.
}

[Theory]
[ClassData(typeof(ValidSsnDataProvider))]
[ClassData(typeof(ValidCnDataProvider))]
public void TestIgnoreSeparatorWhenFormatting(PersonnummerData ssn)
{
var separators = "+-".ToCharArray();

var ps1 = Personnummer.Parse(ssn.LongFormat, new Personnummer.Options { AllowCoordinationNumber = true });
var ps2 = Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = true });
var ps3 = Personnummer.Parse(ssn.SeparatedFormat,
new Personnummer.Options { AllowCoordinationNumber = true });

Assert.True(ps1.Format(false, true).IndexOfAny(separators) == -1);
Assert.True(ps2.Format(false, true).IndexOfAny(separators) == -1);
Assert.True(ps3.Format(false, true).IndexOfAny(separators) == -1);

Assert.True(ps1.Format().IndexOfAny(separators) >= 0);
Assert.True(ps2.Format().IndexOfAny(separators) >= 0);
Assert.True(ps3.Format().IndexOfAny(separators) >= 0);
Expand Down Expand Up @@ -334,5 +334,29 @@ public void TestDateCn(PersonnummerData data)
var pn = new Personnummer(data.LongFormat);
Assert.Equal(expect, $"{pn.Date.Year:0000}{pn.Date.Month:00}{pn.Date.Day:00}");
}

[Fact]
public void TestParseTooLong()
{
Assert.Equal(
"Input string too long",
Assert.Throws<PersonnummerException>(() =>
{
Personnummer.Parse("11111111-11111"); // 14 greater than 13.
}).Message
);
}

[Fact]
public void TestParseTooShort()
{
Assert.Equal(
"Input string too short",
Assert.Throws<PersonnummerException>(() =>
{
Personnummer.Parse("111111111"); // 9 less than 10
}).Message
);
}
}
}
6 changes: 6 additions & 0 deletions Personnummer/Personnummer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public struct Options
/// <param name="options">Options object.</param>
public Personnummer(string ssn, Options? options = null)
{
if (ssn.Length > 13 || ssn.Length < 10)
{
var state = ssn.Length < 10 ? "short" : "long";
throw new PersonnummerException($"Input string too {state}");
}

options ??= new Options() { AllowCoordinationNumber = true };
MatchCollection matches;
try
Expand Down