diff --git a/Personnummer.Tests/PersonnummerTests.cs b/Personnummer.Tests/PersonnummerTests.cs index 68071b1..be5ecee 100644 --- a/Personnummer.Tests/PersonnummerTests.cs +++ b/Personnummer.Tests/PersonnummerTests.cs @@ -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); @@ -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(() => + { + Personnummer.Parse("11111111-11111"); // 14 greater than 13. + }).Message + ); + } + + [Fact] + public void TestParseTooShort() + { + Assert.Equal( + "Input string too short", + Assert.Throws(() => + { + Personnummer.Parse("111111111"); // 9 less than 10 + }).Message + ); + } } } diff --git a/Personnummer/Personnummer.cs b/Personnummer/Personnummer.cs index d8a553b..bf40892 100644 --- a/Personnummer/Personnummer.cs +++ b/Personnummer/Personnummer.cs @@ -52,6 +52,12 @@ public struct Options /// Options object. 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