From cab1015a03c3f7db042507e3d5ee7edde8c079b5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 31 Jul 2021 13:52:29 +0100 Subject: [PATCH] LibJS/Tests: Fix Temporal.Now.plainDate{,ISO}() at end of month/year Evidently, going one day forward on the last day of month increases the month number by one and resets the day to 1. Doing the same on the last day of the year resets the month to 1. --- .../builtins/Temporal/Now/Now.plainDate.js | 18 ++++++++++++++---- .../builtins/Temporal/Now/Now.plainDateISO.js | 18 ++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDate.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDate.js index 68cea3fb9a..a9e2f4501e 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDate.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDate.js @@ -19,9 +19,19 @@ describe("correct behavior", () => { }; const plainDate = Temporal.Now.plainDate(calendar); const plainDateWithOffset = Temporal.Now.plainDate(calendar, timeZone); - // Yes, this will fail if a day, month, or year change happens between the above two lines :^) - expect(plainDateWithOffset.year).toBe(plainDate.year); - expect(plainDateWithOffset.month).toBe(plainDate.month); - expect(plainDateWithOffset.day).toBe(plainDate.day + 1); + if (plainDate.dayOfYear === plainDate.daysInYear) { + expect(plainDateWithOffset.year).toBe(plainDate.year + 1); + expect(plainDateWithOffset.month).toBe(1); + expect(plainDateWithOffset.day).toBe(1); + } else { + expect(plainDateWithOffset.year).toBe(plainDate.year); + if (plainDate.day === plainDate.daysInMonth) { + expect(plainDateWithOffset.month).toBe(plainDate.month + 1); + expect(plainDateWithOffset.day).toBe(1); + } else { + expect(plainDateWithOffset.month).toBe(plainDate.month); + expect(plainDateWithOffset.day).toBe(plainDate.day + 1); + } + } }); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js index 1463866f2d..ad70aedf4d 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js @@ -17,9 +17,19 @@ describe("correct behavior", () => { }; const plainDate = Temporal.Now.plainDateISO(); const plainDateWithOffset = Temporal.Now.plainDateISO(timeZone); - // Yes, this will fail if a day, month, or year change happens between the above two lines :^) - expect(plainDateWithOffset.year).toBe(plainDate.year); - expect(plainDateWithOffset.month).toBe(plainDate.month); - expect(plainDateWithOffset.day).toBe(plainDate.day + 1); + if (plainDate.dayOfYear === plainDate.daysInYear) { + expect(plainDateWithOffset.year).toBe(plainDate.year + 1); + expect(plainDateWithOffset.month).toBe(1); + expect(plainDateWithOffset.day).toBe(1); + } else { + expect(plainDateWithOffset.year).toBe(plainDate.year); + if (plainDate.day === plainDate.daysInMonth) { + expect(plainDateWithOffset.month).toBe(plainDate.month + 1); + expect(plainDateWithOffset.day).toBe(1); + } else { + expect(plainDateWithOffset.month).toBe(plainDate.month); + expect(plainDateWithOffset.day).toBe(plainDate.day + 1); + } + } }); });