diff --git a/DotNetTypes.sln b/DotNetTypes.sln index a54ab0e..20e01a2 100644 --- a/DotNetTypes.sln +++ b/DotNetTypes.sln @@ -3,13 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FA9762F1-6DC4-4722-8A92-962C1EDA0557}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{38E7358A-E518-4E9D-AB95-3D6BA0F97CE0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwedishPersonalNumber", "src\SwedishPersonalNumber\SwedishPersonalNumber.csproj", "{973F55C0-3CF0-4810-873E-ED48E6F52042}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwedishPersonalIdentityNumber", "src\SwedishPersonalIdentityNumber\SwedishPersonalIdentityNumber.csproj", "{200F4052-CB53-4151-8E4C-D430C653255B}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{8768CBBD-D786-4FBA-9F42-B49D093DC30D}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F742414A-CF15-46C5-8EC8-1962F15FB76B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.SwedishPersonalNumber", "tests\Tests.SwedishPersonalNumber\Tests.SwedishPersonalNumber.csproj", "{FDC03A90-AC82-499A-A5F1-C467B080C46B}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.SwedishPersonalIdentityNumber", "tests\Tests.SwedishPersonalIdentityNumber\Tests.SwedishPersonalIdentityNumber.csproj", "{B24DF638-F187-427C-863E-26A04272CF40}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -24,33 +24,33 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|Any CPU.Build.0 = Debug|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x64.ActiveCfg = Debug|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x64.Build.0 = Debug|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x86.ActiveCfg = Debug|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x86.Build.0 = Debug|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|Any CPU.ActiveCfg = Release|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|Any CPU.Build.0 = Release|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x64.ActiveCfg = Release|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x64.Build.0 = Release|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x86.ActiveCfg = Release|Any CPU - {973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x86.Build.0 = Release|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x64.ActiveCfg = Debug|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x64.Build.0 = Debug|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x86.ActiveCfg = Debug|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x86.Build.0 = Debug|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|Any CPU.Build.0 = Release|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x64.ActiveCfg = Release|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x64.Build.0 = Release|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x86.ActiveCfg = Release|Any CPU - {FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x86.Build.0 = Release|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x64.ActiveCfg = Debug|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x64.Build.0 = Debug|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x86.ActiveCfg = Debug|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x86.Build.0 = Debug|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Release|Any CPU.Build.0 = Release|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Release|x64.ActiveCfg = Release|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Release|x64.Build.0 = Release|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Release|x86.ActiveCfg = Release|Any CPU + {200F4052-CB53-4151-8E4C-D430C653255B}.Release|x86.Build.0 = Release|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Debug|x64.ActiveCfg = Debug|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Debug|x64.Build.0 = Debug|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Debug|x86.ActiveCfg = Debug|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Debug|x86.Build.0 = Debug|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Release|Any CPU.Build.0 = Release|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Release|x64.ActiveCfg = Release|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Release|x64.Build.0 = Release|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Release|x86.ActiveCfg = Release|Any CPU + {B24DF638-F187-427C-863E-26A04272CF40}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution - {973F55C0-3CF0-4810-873E-ED48E6F52042} = {FA9762F1-6DC4-4722-8A92-962C1EDA0557} - {FDC03A90-AC82-499A-A5F1-C467B080C46B} = {8768CBBD-D786-4FBA-9F42-B49D093DC30D} + {200F4052-CB53-4151-8E4C-D430C653255B} = {38E7358A-E518-4E9D-AB95-3D6BA0F97CE0} + {B24DF638-F187-427C-863E-26A04272CF40} = {F742414A-CF15-46C5-8EC8-1962F15FB76B} EndGlobalSection EndGlobal diff --git a/src/SwedishPersonalNumber/IsExternalInit.cs b/src/SwedishPersonalIdentityNumber/IsExternalInit.cs similarity index 100% rename from src/SwedishPersonalNumber/IsExternalInit.cs rename to src/SwedishPersonalIdentityNumber/IsExternalInit.cs diff --git a/src/SwedishPersonalNumber/SwedishPersonalNumber.Parse.cs b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.Parse.cs similarity index 65% rename from src/SwedishPersonalNumber/SwedishPersonalNumber.Parse.cs rename to src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.Parse.cs index 0e41efc..f574b6a 100644 --- a/src/SwedishPersonalNumber/SwedishPersonalNumber.Parse.cs +++ b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.Parse.cs @@ -5,7 +5,7 @@ namespace InsightArchitectures.Types { - public partial record SwedishPersonalNumber + public partial record SwedishPersonalIdentityNumber { private static readonly Regex _personalNumberRegex = new Regex(@"^(?(\d{2})?(\d{2}))\-?(?\d{2})\-?(?\d{2})[\-\+]?(?\d{4})$", RegexOptions.Compiled); @@ -15,7 +15,7 @@ public partial record SwedishPersonalNumber /// The string to parse. /// The result of the operation. /// True if the parsing was successful. - public static bool TryParse(string input, [NotNullWhen(true)] out SwedishPersonalNumber? result) + public static bool TryParse(string input, [NotNullWhen(true)] out SwedishPersonalIdentityNumber? result) { _ = input ?? throw new ArgumentNullException(nameof(input)); @@ -34,7 +34,7 @@ public static bool TryParse(string input, [NotNullWhen(true)] out SwedishPersona if (DateTime.TryParse($"{year}-{month}-{day}", out var date) && int.TryParse(match.Groups["extra"].Value, NumberStyles.None, CultureInfo.InvariantCulture.NumberFormat, out var extra)) { - result = new SwedishPersonalNumber(date, extra); + result = new SwedishPersonalIdentityNumber(date, extra); return true; } @@ -52,5 +52,21 @@ static string getYear(Group group) return str; } } + + /// + /// Parses a string into a Swedish personal number. + /// + /// The string to parse. + /// The parsed Swedish Personal Identity Number. + /// Thrown if does not contain a valid Swedish Personal Identity Number. + public static SwedishPersonalIdentityNumber Parse(string input) + { + if (TryParse(input, out var result)) + { + return result; + } + + throw new FormatException($"'{input}' is not a valid format for a Swedish Personal Identity Number"); + } } } diff --git a/src/SwedishPersonalNumber/SwedishPersonalNumber.cs b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.cs similarity index 65% rename from src/SwedishPersonalNumber/SwedishPersonalNumber.cs rename to src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.cs index 3d5b8ca..c1f957f 100644 --- a/src/SwedishPersonalNumber/SwedishPersonalNumber.cs +++ b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.cs @@ -5,14 +5,14 @@ namespace InsightArchitectures.Types /// /// A representation of the Swedish personal number (personnummer). /// - public partial record SwedishPersonalNumber + public partial record SwedishPersonalIdentityNumber { /// - /// Creates a new instance of from a date and an ordinal number. + /// Creates a new instance of from a date and an ordinal number. /// - /// The date part of the Swedish personal number. - /// The ordinal number part of the Swedish personal number. - public SwedishPersonalNumber(DateTime dateOfBirth, int ordinalNumber) + /// The date part of the Swedish personal identity number. + /// The ordinal number part of the Swedish personal identity number. + public SwedishPersonalIdentityNumber(DateTime dateOfBirth, int ordinalNumber) { DateOfBirth = dateOfBirth; @@ -35,13 +35,13 @@ public SwedishPersonalNumber(DateTime dateOfBirth, int ordinalNumber) public int OrdinalNumber { get; init; } /// - /// Returns the value of this instance of as a according to the specified concrete implementation. + /// Returns the value of this instance of as a according to the specified concrete implementation. /// - /// The formatter to use to formatter the Swedish personal number. - /// A string containing this formatted as a . - public string ToFormattedString(SwedishPersonalNumberFormatter formatter) => formatter?.Format(this) ?? throw new ArgumentNullException(nameof(formatter)); + /// The formatter to use to formatter the Swedish personal identity number. + /// A string containing this formatted as a . + public string ToFormattedString(SwedishPersonalIdentityNumberFormatter formatter) => formatter?.Format(this) ?? throw new ArgumentNullException(nameof(formatter)); /// - public override string ToString() => ToFormattedString(SwedishPersonalNumberFormatter.TwelveDigits); + public override string ToString() => ToFormattedString(SwedishPersonalIdentityNumberFormats.TwelveDigits); } } diff --git a/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.csproj b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.csproj new file mode 100644 index 0000000..b3d6a7a --- /dev/null +++ b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumber.csproj @@ -0,0 +1,17 @@ + + + + net5.0;netstandard2.1 + + + + This package contains a representation of the Swedish Personal Identity Number that can be reused across projects. It supports parsing a string and printing it as a string according to different formats. + + + + + SwedishPersonalIdentityNumber.cs + + + + diff --git a/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormats.cs b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormats.cs new file mode 100644 index 0000000..a18b782 --- /dev/null +++ b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormats.cs @@ -0,0 +1,38 @@ +namespace InsightArchitectures.Types +{ + /// + /// A set of formats that can be used to represent a Swedish Personal Identity Number. + /// + public static class SwedishPersonalIdentityNumberFormats + { + /// + /// A formatter that formats a using the formatter yyyyMMdd-xxxx. + /// + public static readonly SwedishPersonalIdentityNumberFormatter TwelveDigits = new TwelveDigitsSwedishPersonalIdentityNumberFormatter(); + + /// + /// A formatter that formats a using the formatter yyMMdd-xxxx. + /// + public static readonly SwedishPersonalIdentityNumberFormatter TenDigits = new TenDigitsSwedishPersonalIdentityNumberFormatter(); + + /// + /// A formatter that formats a using the formatter yyyyMMddxxxx. + /// + public static readonly SwedishPersonalIdentityNumberFormatter NoSplitTwelveDigits = new NoSplitTwelveDigitsSwedishPersonalIdentityNumberFormatter(); + + /// + /// A formatter that formats a using the formatter yyMMddxxxx. + /// + public static readonly SwedishPersonalIdentityNumberFormatter NoSplitTenDigits = new NoSplitTenDigitsSwedishPersonalIdentityNumberFormatter(); + + /// + /// A formatter that formats a using the formatter yyyy-MM-dd-xxxx. + /// + public static readonly SwedishPersonalIdentityNumberFormatter SplitTwelveDigits = new SplitTwelveDigitsSwedishPersonalIdentityNumberFormatter(); + + /// + /// A formatter that formats a using the formatter yy-MM-dd-xxxx. + /// + public static readonly SwedishPersonalIdentityNumberFormatter SplitTenDigits = new SplitTenDigitsSwedishPersonalIdentityNumberFormatter(); + } +} diff --git a/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormatter.cs b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormatter.cs new file mode 100644 index 0000000..6b4da7a --- /dev/null +++ b/src/SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormatter.cs @@ -0,0 +1,101 @@ +using System; + +namespace InsightArchitectures.Types +{ + /// + /// An abstract class that represents the different ways to formatter a Swedish personal identity number as a string. + /// + public abstract class SwedishPersonalIdentityNumberFormatter + { + /// + /// Formats the according to the specified concrete implementation. + /// + /// The Swedish personal identity number to formatter as string. + /// The string formatted according to the concrete implementation. + public abstract string Format(SwedishPersonalIdentityNumber pin); + } + + /// + /// Formats a Swedish personal identity number using the formatter yyyyMMddxxxx. + /// + public class NoSplitTwelveDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter + { + /// + public override string Format(SwedishPersonalIdentityNumber pn) + { + _ = pn ?? throw new ArgumentNullException(nameof(pn)); + + return $"{pn.DateOfBirth:yyyyMMdd}{pn.OrdinalNumber:0000}"; + } + } + + /// + /// Formats a Swedish personal identity number using the formatter yyMMddxxxx. + /// + public class NoSplitTenDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter + { + /// + public override string Format(SwedishPersonalIdentityNumber pn) + { + _ = pn ?? throw new ArgumentNullException(nameof(pn)); + + return $"{pn.DateOfBirth:yyMMdd}{pn.OrdinalNumber:0000}"; + } + } + + /// + /// Formats a Swedish personal identity number using the formatter yyyyMMdd-xxxx. + /// + public class TwelveDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter + { + /// + public override string Format(SwedishPersonalIdentityNumber pn) + { + _ = pn ?? throw new ArgumentNullException(nameof(pn)); + + return $"{pn.DateOfBirth:yyyyMMdd}-{pn.OrdinalNumber:0000}"; + } + } + + /// + /// Formats a Swedish personal identity number using the formatter yyMMdd-xxxx. + /// + public class TenDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter + { + /// + public override string Format(SwedishPersonalIdentityNumber pn) + { + _ = pn ?? throw new ArgumentNullException(nameof(pn)); + + return $"{pn.DateOfBirth:yyMMdd}-{pn.OrdinalNumber:0000}"; + } + } + + /// + /// Formats a Swedish personal identity number using the formatter yyyyMMdd-xxxx. + /// + public class SplitTwelveDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter + { + /// + public override string Format(SwedishPersonalIdentityNumber pn) + { + _ = pn ?? throw new ArgumentNullException(nameof(pn)); + + return $"{pn.DateOfBirth:yyyy-MM-dd}-{pn.OrdinalNumber:0000}"; + } + } + + /// + /// Formats a Swedish personal identity number using the formatter yyMMdd-xxxx. + /// + public class SplitTenDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter + { + /// + public override string Format(SwedishPersonalIdentityNumber pn) + { + _ = pn ?? throw new ArgumentNullException(nameof(pn)); + + return $"{pn.DateOfBirth:yy-MM-dd}-{pn.OrdinalNumber:0000}"; + } + } +} diff --git a/src/SwedishPersonalNumber/SwedishPersonalNumber.csproj b/src/SwedishPersonalNumber/SwedishPersonalNumber.csproj deleted file mode 100644 index 6a15b3b..0000000 --- a/src/SwedishPersonalNumber/SwedishPersonalNumber.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - net5.0;netstandard2.1 - - - - This package contains a representation of the Swedish Personal Number that can be reused across projects. It supports parsing a string and printing it as a string according to different formats. - - - - - SwedishPersonalNumber.cs - - - - diff --git a/src/SwedishPersonalNumber/SwedishPersonalNumberFormatter.cs b/src/SwedishPersonalNumber/SwedishPersonalNumberFormatter.cs deleted file mode 100644 index 26eb899..0000000 --- a/src/SwedishPersonalNumber/SwedishPersonalNumberFormatter.cs +++ /dev/null @@ -1,131 +0,0 @@ -using System; - -namespace InsightArchitectures.Types -{ - /// - /// An abstract class that represents the different ways to formatter a Swedish personal number as a string. - /// - public abstract class SwedishPersonalNumberFormatter - { - /// - /// Formats the according to the specified concrete implementation. - /// - /// The Swedish personal number to formatter as string. - /// The string formatted according to the concrete implementation. - public abstract string Format(SwedishPersonalNumber personalNumber); - - /// - /// A formatter that formats a using the formatter yyyyMMdd-xxxx. - /// - public static readonly SwedishPersonalNumberFormatter TwelveDigits = new TwelveDigitsSwedishPersonalNumberFormatter(); - - /// - /// A formatter that formats a using the formatter yyMMdd-xxxx. - /// - public static readonly SwedishPersonalNumberFormatter TenDigits = new TenDigitsSwedishPersonalNumberFormatter(); - - /// - /// A formatter that formats a using the formatter yyyyMMddxxxx. - /// - public static readonly SwedishPersonalNumberFormatter NoSplitTwelveDigits = new NoSplitTwelveDigitsSwedishPersonalNumberFormatter(); - - /// - /// A formatter that formats a using the formatter yyMMddxxxx. - /// - public static readonly SwedishPersonalNumberFormatter NoSplitTenDigits = new NoSplitTenDigitsSwedishPersonalNumberFormatter(); - - /// - /// A formatter that formats a using the formatter yyyy-MM-dd-xxxx. - /// - public static readonly SwedishPersonalNumberFormatter SplitTwelveDigits = new SplitTwelveDigitsSwedishPersonalNumberFormatter(); - - /// - /// A formatter that formats a using the formatter yy-MM-dd-xxxx. - /// - public static readonly SwedishPersonalNumberFormatter SplitTenDigits = new SplitTenDigitsSwedishPersonalNumberFormatter(); - } - - /// - /// Formats a Swedish personal number using the formatter yyyyMMddxxxx. - /// - public class NoSplitTwelveDigitsSwedishPersonalNumberFormatter : SwedishPersonalNumberFormatter - { - /// - public override string Format(SwedishPersonalNumber pn) - { - _ = pn ?? throw new ArgumentNullException(nameof(pn)); - - return $"{pn.DateOfBirth:yyyyMMdd}{pn.OrdinalNumber:0000}"; - } - } - - /// - /// Formats a Swedish personal number using the formatter yyMMddxxxx. - /// - public class NoSplitTenDigitsSwedishPersonalNumberFormatter : SwedishPersonalNumberFormatter - { - /// - public override string Format(SwedishPersonalNumber pn) - { - _ = pn ?? throw new ArgumentNullException(nameof(pn)); - - return $"{pn.DateOfBirth:yyMMdd}{pn.OrdinalNumber:0000}"; - } - } - - /// - /// Formats a Swedish personal number using the formatter yyyyMMdd-xxxx. - /// - public class TwelveDigitsSwedishPersonalNumberFormatter : SwedishPersonalNumberFormatter - { - /// - public override string Format(SwedishPersonalNumber pn) - { - _ = pn ?? throw new ArgumentNullException(nameof(pn)); - - return $"{pn.DateOfBirth:yyyyMMdd}-{pn.OrdinalNumber:0000}"; - } - } - - /// - /// Formats a Swedish personal number using the formatter yyMMdd-xxxx. - /// - public class TenDigitsSwedishPersonalNumberFormatter : SwedishPersonalNumberFormatter - { - /// - public override string Format(SwedishPersonalNumber pn) - { - _ = pn ?? throw new ArgumentNullException(nameof(pn)); - - return $"{pn.DateOfBirth:yyMMdd}-{pn.OrdinalNumber:0000}"; - } - } - - /// - /// Formats a Swedish personal number using the formatter yyyyMMdd-xxxx. - /// - public class SplitTwelveDigitsSwedishPersonalNumberFormatter : SwedishPersonalNumberFormatter - { - /// - public override string Format(SwedishPersonalNumber pn) - { - _ = pn ?? throw new ArgumentNullException(nameof(pn)); - - return $"{pn.DateOfBirth:yyyy-MM-dd}-{pn.OrdinalNumber:0000}"; - } - } - - /// - /// Formats a Swedish personal number using the formatter yyMMdd-xxxx. - /// - public class SplitTenDigitsSwedishPersonalNumberFormatter : SwedishPersonalNumberFormatter - { - /// - public override string Format(SwedishPersonalNumber pn) - { - _ = pn ?? throw new ArgumentNullException(nameof(pn)); - - return $"{pn.DateOfBirth:yy-MM-dd}-{pn.OrdinalNumber:0000}"; - } - } -} diff --git a/tests/Tests.SwedishPersonalNumber/CustomAutoDataAttribute.cs b/tests/Tests.SwedishPersonalIdentityNumber/CustomAutoDataAttribute.cs similarity index 87% rename from tests/Tests.SwedishPersonalNumber/CustomAutoDataAttribute.cs rename to tests/Tests.SwedishPersonalIdentityNumber/CustomAutoDataAttribute.cs index ad31319..2b5cbe7 100644 --- a/tests/Tests.SwedishPersonalNumber/CustomAutoDataAttribute.cs +++ b/tests/Tests.SwedishPersonalIdentityNumber/CustomAutoDataAttribute.cs @@ -1,12 +1,7 @@ using AutoFixture; using AutoFixture.AutoMoq; -using AutoFixture.Kernel; using AutoFixture.NUnit3; using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq.Expressions; -using System.Reflection; namespace Tests { diff --git a/tests/Tests.SwedishPersonalNumber/IsExternalInit.cs b/tests/Tests.SwedishPersonalIdentityNumber/IsExternalInit.cs similarity index 100% rename from tests/Tests.SwedishPersonalNumber/IsExternalInit.cs rename to tests/Tests.SwedishPersonalIdentityNumber/IsExternalInit.cs diff --git a/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormatterTests.cs b/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormatterTests.cs new file mode 100644 index 0000000..8ddd337 --- /dev/null +++ b/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberFormatterTests.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using AutoFixture; +using InsightArchitectures.Types; +using NUnit.Framework; + +namespace Tests +{ + [TestFixture] + [TestOf(typeof(SwedishPersonalIdentityNumberFormatter))] + public class SwedishPersonalIdentityNumberFormatterTests + { + [Test, TestCaseSource(nameof(GetTestCases))] + public void Format_uses_expected_format(SwedishPersonalIdentityNumberFormatter sut, SwedishPersonalIdentityNumber pin, string format) + { + var result = sut.Format(pin); + + Assert.That(result, Does.Match(format)); + } + + public static IEnumerable GetTestCases() + { + var fixture = new Fixture(); + + yield return new object []{ new NoSplitTenDigitsSwedishPersonalIdentityNumberFormatter(), fixture.Create(), @"^\d{10}$"}; + + yield return new object[] { new SplitTenDigitsSwedishPersonalIdentityNumberFormatter(), fixture.Create(), @"^\d{2}-\d{2}-\d{2}-\d{4}$" }; + + yield return new object[] { new TenDigitsSwedishPersonalIdentityNumberFormatter(), fixture.Create(), @"^\d{6}-\d{4}$" }; + + yield return new object[] { new NoSplitTwelveDigitsSwedishPersonalIdentityNumberFormatter(), fixture.Create(), @"^\d{12}$" }; + + yield return new object[] { new SplitTwelveDigitsSwedishPersonalIdentityNumberFormatter(), fixture.Create(), @"^\d{4}-\d{2}-\d{2}-\d{4}$" }; + + yield return new object[] { new TwelveDigitsSwedishPersonalIdentityNumberFormatter(), fixture.Create(), @"^\d{8}-\d{4}$" }; + } + } +} diff --git a/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberParsingTests.cs b/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberParsingTests.cs new file mode 100644 index 0000000..35b65cf --- /dev/null +++ b/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberParsingTests.cs @@ -0,0 +1,261 @@ +using System; +using InsightArchitectures.Types; +using NUnit.Framework; + +namespace Tests +{ + [TestFixture] + [TestOf(typeof(SwedishPersonalIdentityNumber))] + public class SwedishPersonalIdentityNumberParsingTests + { + [Test, CustomAutoData] + public void TryParse_returns_false_from_Random_string(string input) + { + var isSuccess = SwedishPersonalIdentityNumber.TryParse(input, out var result); + + Assert.Multiple(() => + { + Assert.That(isSuccess, Is.False); + Assert.That(result, Is.Null); + }); + } + + [Test, CustomAutoData] + public void TryParse_can_parse_Twelve_digits_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + var input = $"{date:yyyyMMdd}-{ordinal:0000}"; + + var isSuccess = SwedishPersonalIdentityNumber.TryParse(input, out var result); + + Assert.Multiple(() => + { + Assert.That(isSuccess, Is.True); + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void TryParse_can_parse_Ten_digits_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1900, 1999)] int year, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + date = new DateTime(year, date.Month, date.Day); + + var input = $"{date:yyMMdd}-{ordinal:0000}"; + + var isSuccess = SwedishPersonalIdentityNumber.TryParse(input, out var result); + + Assert.Multiple(() => + { + Assert.That(isSuccess, Is.True); + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void TryParse_can_parse_Twelve_digits_no_split_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + var input = $"{date:yyyyMMdd}{ordinal:0000}"; + + var isSuccess = SwedishPersonalIdentityNumber.TryParse(input, out var result); + + Assert.Multiple(() => + { + Assert.That(isSuccess, Is.True); + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void TryParse_can_parse_Ten_digits_format_no_split(DateTime date, [System.ComponentModel.DataAnnotations.Range(1900, 1999)] int year, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + date = new DateTime(year, date.Month, date.Day); + + var input = $"{date:yyMMdd}{ordinal:0000}"; + + var isSuccess = SwedishPersonalIdentityNumber.TryParse(input, out var result); + + Assert.Multiple(() => + { + Assert.That(isSuccess, Is.True); + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void TryParse_can_parse_Twelve_digits_split_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + var input = $"{date:yyyy-MM-dd}-{ordinal:0000}"; + + var isSuccess = SwedishPersonalIdentityNumber.TryParse(input, out var result); + + Assert.Multiple(() => + { + Assert.That(isSuccess, Is.True); + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void TryParse_can_parse_Ten_digits_split_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1900, 1999)] int year, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + date = new DateTime(year, date.Month, date.Day); + + var input = $"{date:yy-MM-dd}-{ordinal:0000}"; + + var isSuccess = SwedishPersonalIdentityNumber.TryParse(input, out var result); + + Assert.Multiple(() => + { + Assert.That(isSuccess, Is.True); + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test] + [Property("Issue", 3)] + [Property("IssueLink", "https://github.com/insight-architectures/dotnet-types/issues/3")] + [CustomInlineAutoData("1234567890")] + [CustomInlineAutoData("12345678-9012")] + [CustomInlineAutoData("12345678")] + [CustomInlineAutoData("123456-7890")] + [CustomInlineAutoData("12-34-56-7890")] + [CustomInlineAutoData("1234-56-78-9012")] + public void TryParse_should_not_throw_when_input_date_is_not_valid(string input) + { + var result = SwedishPersonalIdentityNumber.TryParse(input, out _); + + Assert.That(result, Is.False); + } + + [Test, CustomAutoData] + public void Parse_throws_from_Random_string(string input) + { + Assume.That(SwedishPersonalIdentityNumber.TryParse(input, out _), Is.False); + + Assert.That(() => SwedishPersonalIdentityNumber.Parse(input), Throws.TypeOf()); + } + + [Test, CustomAutoData] + public void Parse_can_parse_Twelve_digits_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + var input = $"{date:yyyyMMdd}-{ordinal:0000}"; + + var result = SwedishPersonalIdentityNumber.Parse(input); + + Assert.Multiple(() => + { + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void Parse_can_parse_Ten_digits_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1900, 1999)] int year, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + date = new DateTime(year, date.Month, date.Day); + + var input = $"{date:yyMMdd}-{ordinal:0000}"; + + var result = SwedishPersonalIdentityNumber.Parse(input); + + Assert.Multiple(() => + { + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void Parse_can_parse_Twelve_digits_no_split_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + var input = $"{date:yyyyMMdd}{ordinal:0000}"; + + var result = SwedishPersonalIdentityNumber.Parse(input); + + Assert.Multiple(() => + { + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void Parse_can_parse_Ten_digits_format_no_split(DateTime date, [System.ComponentModel.DataAnnotations.Range(1900, 1999)] int year, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + date = new DateTime(year, date.Month, date.Day); + + var input = $"{date:yyMMdd}{ordinal:0000}"; + + var result = SwedishPersonalIdentityNumber.Parse(input); + + Assert.Multiple(() => + { + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void Parse_can_parse_Twelve_digits_split_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + var input = $"{date:yyyy-MM-dd}-{ordinal:0000}"; + + var result = SwedishPersonalIdentityNumber.Parse(input); + + Assert.Multiple(() => + { + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test, CustomAutoData] + public void Parse_can_parse_Ten_digits_split_format(DateTime date, [System.ComponentModel.DataAnnotations.Range(1900, 1999)] int year, [System.ComponentModel.DataAnnotations.Range(1, 9999)] int ordinal) + { + date = new DateTime(year, date.Month, date.Day); + + var input = $"{date:yy-MM-dd}-{ordinal:0000}"; + + var result = SwedishPersonalIdentityNumber.Parse(input); + + Assert.Multiple(() => + { + Assert.That(result, Is.Not.Null); + Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); + Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); + }); + } + + [Test] + [Property("Issue", 3)] + [Property("IssueLink", "https://github.com/insight-architectures/dotnet-types/issues/3")] + [CustomInlineAutoData("1234567890")] + [CustomInlineAutoData("12345678-9012")] + [CustomInlineAutoData("12345678")] + [CustomInlineAutoData("123456-7890")] + [CustomInlineAutoData("12-34-56-7890")] + [CustomInlineAutoData("1234-56-78-9012")] + public void Parse_should_throw_when_input_date_is_not_valid(string input) + { + Assume.That(SwedishPersonalIdentityNumber.TryParse(input, out _), Is.False); + + Assert.That(() => SwedishPersonalIdentityNumber.Parse(input), Throws.TypeOf()); + } + } +} diff --git a/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberTests.cs b/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberTests.cs new file mode 100644 index 0000000..0d2bd1a --- /dev/null +++ b/tests/Tests.SwedishPersonalIdentityNumber/SwedishPersonalIdentityNumberTests.cs @@ -0,0 +1,49 @@ +using System; +using AutoFixture.Idioms; +using InsightArchitectures.Types; +using Moq; +using NUnit.Framework; +using RangeAttribute = System.ComponentModel.DataAnnotations.RangeAttribute; + +namespace Tests +{ + [TestFixture] + [TestOf(typeof(SwedishPersonalIdentityNumber))] + public class SwedishPersonalIdentityNumberTests + { + [Test, CustomAutoData] + public void Constructor_throws_if_ordinal_is_less_than_zero(DateTime date, [Range(-10_000, -1)] int ordinal) + { + Assert.That(() => new SwedishPersonalIdentityNumber(date, ordinal), Throws.InstanceOf()); + } + + [Test, CustomAutoData] + public void Constructor_throws_if_ordinal_is_bigger_than_9_999(DateTime date, [Range(10_000, 100_000)] int ordinal) + { + Assert.That(() => new SwedishPersonalIdentityNumber(date, ordinal), Throws.InstanceOf()); + } + + [Test, CustomAutoData] + public void Constructor_sets_properties(ConstructorInitializedMemberAssertion assertion) => assertion.Verify(typeof(SwedishPersonalIdentityNumber)); + + [Test, CustomAutoData] + public void ToFormattedString_uses_formatter(SwedishPersonalIdentityNumber sut, SwedishPersonalIdentityNumberFormatter formatter, string result) + { + Mock.Get(formatter).Setup(p => p.Format(It.IsAny())).Returns(result); + + var actual = sut.ToFormattedString(formatter); + + Assert.That(actual, Is.EqualTo(result)); + } + + [Test, CustomAutoData] + public void ToString_returns_twelve_digits_format(SwedishPersonalIdentityNumber sut) + { + var expected = sut.ToFormattedString(SwedishPersonalIdentityNumberFormats.TwelveDigits); + + var actual = sut.ToString(); + + Assert.That(actual, Is.EqualTo(expected)); + } + } +} diff --git a/tests/Tests.SwedishPersonalNumber/Tests.SwedishPersonalNumber.csproj b/tests/Tests.SwedishPersonalIdentityNumber/Tests.SwedishPersonalIdentityNumber.csproj similarity index 73% rename from tests/Tests.SwedishPersonalNumber/Tests.SwedishPersonalNumber.csproj rename to tests/Tests.SwedishPersonalIdentityNumber/Tests.SwedishPersonalIdentityNumber.csproj index b96119b..af99b43 100644 --- a/tests/Tests.SwedishPersonalNumber/Tests.SwedishPersonalNumber.csproj +++ b/tests/Tests.SwedishPersonalIdentityNumber/Tests.SwedishPersonalIdentityNumber.csproj @@ -1,4 +1,4 @@ - + net5.0;netcoreapp3.1 @@ -13,7 +13,7 @@ - + diff --git a/tests/Tests.SwedishPersonalNumber/SwedishPersonalNumberFormatterTests.cs b/tests/Tests.SwedishPersonalNumber/SwedishPersonalNumberFormatterTests.cs deleted file mode 100644 index ae8aa92..0000000 --- a/tests/Tests.SwedishPersonalNumber/SwedishPersonalNumberFormatterTests.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using AutoFixture; -using InsightArchitectures.Types; -using NUnit.Framework; - -namespace Tests -{ - [TestFixture] - [TestOf(typeof(SwedishPersonalNumberFormatter))] - public class SwedishPersonalNumberFormatterTests - { - [Test, TestCaseSource(nameof(GetTestCases))] - public void Format_uses_expected_format(SwedishPersonalNumberFormatter sut, SwedishPersonalNumber personalNumber, string format) - { - var result = sut.Format(personalNumber); - - Assert.That(result, Does.Match(format)); - } - - public static IEnumerable GetTestCases() - { - var fixture = new Fixture(); - - yield return new object []{ new NoSplitTenDigitsSwedishPersonalNumberFormatter(), fixture.Create(), @"^\d{10}$"}; - - yield return new object[] { new SplitTenDigitsSwedishPersonalNumberFormatter(), fixture.Create(), @"^\d{2}-\d{2}-\d{2}-\d{4}$" }; - - yield return new object[] { new TenDigitsSwedishPersonalNumberFormatter(), fixture.Create(), @"^\d{6}-\d{4}$" }; - - yield return new object[] { new NoSplitTwelveDigitsSwedishPersonalNumberFormatter(), fixture.Create(), @"^\d{12}$" }; - - yield return new object[] { new SplitTwelveDigitsSwedishPersonalNumberFormatter(), fixture.Create(), @"^\d{4}-\d{2}-\d{2}-\d{4}$" }; - - yield return new object[] { new TwelveDigitsSwedishPersonalNumberFormatter(), fixture.Create(), @"^\d{8}-\d{4}$" }; - } - } -} diff --git a/tests/Tests.SwedishPersonalNumber/SwedishPersonalNumberTests.cs b/tests/Tests.SwedishPersonalNumber/SwedishPersonalNumberTests.cs deleted file mode 100644 index 1218b96..0000000 --- a/tests/Tests.SwedishPersonalNumber/SwedishPersonalNumberTests.cs +++ /dev/null @@ -1,184 +0,0 @@ -using System; -using AutoFixture.Idioms; -using InsightArchitectures.Types; -using Moq; -using NUnit.Framework; -using RangeAttribute = System.ComponentModel.DataAnnotations.RangeAttribute; - -namespace Tests -{ - [TestFixture] - [TestOf(typeof(SwedishPersonalNumber))] - public class SwedishPersonalNumberTests - { - [Test, CustomAutoData] - public void Constructor_throws_if_ordinal_is_less_than_zero(DateTime date, [Range(-10_000, -1)] int ordinal) - { - Assert.That(() => new SwedishPersonalNumber(date, ordinal), Throws.InstanceOf()); - } - - [Test, CustomAutoData] - public void Constructor_throws_if_ordinal_is_bigger_than_9_999(DateTime date, [Range(10_000, 100_000)] int ordinal) - { - Assert.That(() => new SwedishPersonalNumber(date, ordinal), Throws.InstanceOf()); - } - - [Test, CustomAutoData] - public void Constructor_sets_properties(ConstructorInitializedMemberAssertion assertion) => assertion.Verify(typeof(SwedishPersonalNumber)); - - [Test, CustomAutoData] - public void ToFormattedString_uses_formatter(SwedishPersonalNumber sut, SwedishPersonalNumberFormatter formatter, string result) - { - Mock.Get(formatter).Setup(p => p.Format(It.IsAny())).Returns(result); - - var actual = sut.ToFormattedString(formatter); - - Assert.That(actual, Is.EqualTo(result)); - } - - [Test, CustomAutoData] - public void ToString_returns_twelve_digits_format(SwedishPersonalNumber sut) - { - var expected = sut.ToFormattedString(SwedishPersonalNumberFormatter.TwelveDigits); - - var actual = sut.ToString(); - - Assert.That(actual, Is.EqualTo(expected)); - } - } - - [TestFixture] - [TestOf(typeof(SwedishPersonalNumber))] - public class SwedishPersonalNumberParsingTests - { - [Test, CustomAutoData] - public void Random_string_returns_false(string input) - { - var isSuccess = SwedishPersonalNumber.TryParse(input, out var result); - - Assert.Multiple(() => - { - Assert.That(isSuccess, Is.False); - Assert.That(result, Is.Null); - }); - } - - [Test, CustomAutoData] - public void Twelve_digits_format_is_parsed(DateTime date, [Range(1, 9999)] int ordinal) - { - var input = $"{date:yyyyMMdd}-{ordinal:0000}"; - - var isSuccess = SwedishPersonalNumber.TryParse(input, out var result); - - Assert.Multiple(() => - { - Assert.That(isSuccess, Is.True); - Assert.That(result, Is.Not.Null); - Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); - Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); - }); - } - - [Test, CustomAutoData] - public void Ten_digits_format_is_parsed(DateTime date, [Range(1900, 1999)] int year, [Range(1, 9999)] int ordinal) - { - date = new DateTime(year, date.Month, date.Day); - - var input = $"{date:yyMMdd}-{ordinal:0000}"; - - var isSuccess = SwedishPersonalNumber.TryParse(input, out var result); - - Assert.Multiple(() => - { - Assert.That(isSuccess, Is.True); - Assert.That(result, Is.Not.Null); - Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); - Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); - }); - } - - [Test, CustomAutoData] - public void Twelve_digits_no_split_format_is_parsed(DateTime date, [Range(1, 9999)] int ordinal) - { - var input = $"{date:yyyyMMdd}{ordinal:0000}"; - - var isSuccess = SwedishPersonalNumber.TryParse(input, out var result); - - Assert.Multiple(() => - { - Assert.That(isSuccess, Is.True); - Assert.That(result, Is.Not.Null); - Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); - Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); - }); - } - - [Test, CustomAutoData] - public void Ten_digits_format_no_split_is_parsed(DateTime date, [Range(1900, 1999)] int year, [Range(1, 9999)] int ordinal) - { - date = new DateTime(year, date.Month, date.Day); - - var input = $"{date:yyMMdd}{ordinal:0000}"; - - var isSuccess = SwedishPersonalNumber.TryParse(input, out var result); - - Assert.Multiple(() => - { - Assert.That(isSuccess, Is.True); - Assert.That(result, Is.Not.Null); - Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); - Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); - }); - } - - [Test, CustomAutoData] - public void Twelve_digits_split_format_is_parsed(DateTime date, [Range(1, 9999)] int ordinal) - { - var input = $"{date:yyyy-MM-dd}-{ordinal:0000}"; - - var isSuccess = SwedishPersonalNumber.TryParse(input, out var result); - - Assert.Multiple(() => - { - Assert.That(isSuccess, Is.True); - Assert.That(result, Is.Not.Null); - Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); - Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); - }); - } - - [Test, CustomAutoData] - public void Ten_digits_format_split_is_parsed(DateTime date, [Range(1900, 1999)] int year, [Range(1, 9999)] int ordinal) - { - date = new DateTime(year, date.Month, date.Day); - - var input = $"{date:yy-MM-dd}-{ordinal:0000}"; - - var isSuccess = SwedishPersonalNumber.TryParse(input, out var result); - - Assert.Multiple(() => - { - Assert.That(isSuccess, Is.True); - Assert.That(result, Is.Not.Null); - Assert.That(result.DateOfBirth, Is.EqualTo(date.Date)); - Assert.That(result.OrdinalNumber, Is.EqualTo(ordinal)); - }); - } - - [Test] - [Property("Issue", 3)] - [Property("IssueLink", "https://github.com/insight-architectures/dotnet-types/issues/3")] - [CustomInlineAutoData("1234567890")] - [CustomInlineAutoData("12345678-9012")] - [CustomInlineAutoData("12345678")] - [CustomInlineAutoData("123456-7890")] - [CustomInlineAutoData("12-34-56-7890")] - [CustomInlineAutoData("1234-56-78-9012")] - public void TryParse_should_not_throw_when_input_date_is_not_valid(string input) - { - var result = SwedishPersonalNumber.TryParse(input, out _); - - Assert.That(result, Is.False); - } - } -}