From 165f249c9b24facd2cf82247d921e15cb83fe7e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tegn=C3=A9r?= Date: Thu, 9 Jan 2025 17:42:43 +0100 Subject: [PATCH] Updated tests to use 'real' age calculation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated timeprovider to use 0101 as default date. Signed-off-by: Johannes Tegnér --- Personnummer.Tests/CoordinationNumberTests.cs | 7 +++- Personnummer.Tests/PersonnummerTests.cs | 32 ++++++++++++------- Personnummer.Tests/TestTimeProvider.cs | 2 +- Personnummer/Personnummer.cs | 2 +- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Personnummer.Tests/CoordinationNumberTests.cs b/Personnummer.Tests/CoordinationNumberTests.cs index 2b08254..eb67ce1 100644 --- a/Personnummer.Tests/CoordinationNumberTests.cs +++ b/Personnummer.Tests/CoordinationNumberTests.cs @@ -105,12 +105,17 @@ public void TestParseInvalidCn(PersonnummerData ssn) public void TestAgeCn(PersonnummerData ssn) { var timeProvider = new TestTimeProvider(); + var localNow = timeProvider.GetLocalNow(); int day = int.Parse(ssn.LongFormat.Substring(ssn.LongFormat.Length - 6, 2)) - 60; string strDay = day < 10 ? $"0{day}" : day.ToString(); DateTime dt = DateTime.ParseExact(ssn.LongFormat.Substring(0, ssn.LongFormat.Length - 6) + strDay, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None); - int years = timeProvider.GetLocalNow().Year - dt.Year; + var years = localNow.Year - dt.Year; + if (!(localNow.Month > dt.Month || (localNow.Month == dt.Month && localNow.Day >= dt.Day))) + { + years--; + } Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age); Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age); diff --git a/Personnummer.Tests/PersonnummerTests.cs b/Personnummer.Tests/PersonnummerTests.cs index 4428084..6a4de8e 100644 --- a/Personnummer.Tests/PersonnummerTests.cs +++ b/Personnummer.Tests/PersonnummerTests.cs @@ -82,8 +82,14 @@ public void TestParseInvalid(PersonnummerData ssn) public void TestAge(PersonnummerData ssn) { var timeProvider = new TestTimeProvider(); + var localNow = timeProvider.GetLocalNow(); DateTime dt = DateTime.ParseExact(ssn.LongFormat.Substring(0, ssn.LongFormat.Length - 4), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None); - int years = timeProvider.GetLocalNow().Year - dt.Year; + + var years = localNow.Year - dt.Year; + if (!(localNow.Month > dt.Month || (localNow.Month == dt.Month && localNow.Day >= dt.Day))) + { + years--; + } Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age); Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age); @@ -91,6 +97,20 @@ public void TestAge(PersonnummerData ssn) // Du to age not being possible to fetch from >100 year short format without separator, we aught to check this here. Assert.Equal(years > 99 ? years - 100 : years, Personnummer.Parse(ssn.ShortFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age); } + + [Fact] + public void TestEdgeCasesAroundBirthday() + { + var timeProvider = new TestTimeProvider + { + Now = DateTimeOffset.Parse("2090-01-09 12:00") + }; + + Assert.Equal(10, new Personnummer("20800108-6670", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday + Assert.Equal(10, new Personnummer("20800109-8287", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today + Assert.Equal(9, new Personnummer("20800110-8516", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow + } + #endif [Theory] @@ -236,15 +256,5 @@ public void TestParseTooShort() }).Message ); } - - [Fact] - public void TestEdgeCasesAroundBirthday() - { - var timeProvider = new TestTimeProvider(); //TestTime is 2025-10-05 - Assert.Equal(18, new Personnummer("20071004-3654", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday - Assert.Equal(18, new Personnummer("20071005-3653", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today - Assert.Equal(17, new Personnummer("20071006-3652", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow - } - } } diff --git a/Personnummer.Tests/TestTimeProvider.cs b/Personnummer.Tests/TestTimeProvider.cs index 9995295..e2b67e3 100644 --- a/Personnummer.Tests/TestTimeProvider.cs +++ b/Personnummer.Tests/TestTimeProvider.cs @@ -10,7 +10,7 @@ namespace Personnummer.Tests; public class TestTimeProvider : TimeProvider { internal DateTimeOffset Now { get; set; } = new( - new DateOnly(2025, 1, 5), + new DateOnly(2025, 1, 1), new TimeOnly(0, 0, 0, 1), TimeSpan.Zero ); diff --git a/Personnummer/Personnummer.cs b/Personnummer/Personnummer.cs index 637b5f1..8847f58 100644 --- a/Personnummer/Personnummer.cs +++ b/Personnummer/Personnummer.cs @@ -46,7 +46,7 @@ public int Age var now = _options.DateTimeNow; var age = now.Year - Date.Year; - var hadBirthdayThisYear = (now.Month > Date.Month) || + var hadBirthdayThisYear = now.Month > Date.Month || (now.Month == Date.Month && now.Day >= Date.Day); if (!hadBirthdayThisYear)