瀏覽代碼

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.
Linus Groh 4 年之前
父節點
當前提交
cab1015a03

+ 14 - 4
Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDate.js

@@ -19,9 +19,19 @@ describe("correct behavior", () => {
         };
         };
         const plainDate = Temporal.Now.plainDate(calendar);
         const plainDate = Temporal.Now.plainDate(calendar);
         const plainDateWithOffset = Temporal.Now.plainDate(calendar, timeZone);
         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);
+            }
+        }
     });
     });
 });
 });

+ 14 - 4
Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js

@@ -17,9 +17,19 @@ describe("correct behavior", () => {
         };
         };
         const plainDate = Temporal.Now.plainDateISO();
         const plainDate = Temporal.Now.plainDateISO();
         const plainDateWithOffset = Temporal.Now.plainDateISO(timeZone);
         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);
+            }
+        }
     });
     });
 });
 });