diff options
author | Linus Groh <mail@linusgroh.de> | 2021-11-27 18:56:02 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-27 19:11:31 +0000 |
commit | 8f99c05f976d511962c4a6ba300c3048593fb898 (patch) | |
tree | 2f9ccee055bbfaa1af114f586c5bb875cab1a905 /Userland/Libraries/LibJS/Tests | |
parent | bdd2c357fd0fd8ff1ae234efb43850ca1f762066 (diff) | |
download | serenity-8f99c05f976d511962c4a6ba300c3048593fb898.zip |
LibJS: Implement Temporal.PlainYearMonth.prototype.since()
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.since.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.since.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.since.js new file mode 100644 index 0000000000..a012dca4dd --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.since.js @@ -0,0 +1,120 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainYearMonth.prototype.since).toHaveLength(1); + }); + + test("basic functionality", () => { + const values = [ + [[0, 1], [0, 1], "PT0S"], + [[2, 3], [1, 2], "P1Y1M"], + [[1, 2], [0, 1], "P1Y1M"], + [[0, 1], [1, 2], "-P1Y1M"], + [[0, 12], [0, 1], "P11M"], + [[0, 1], [0, 12], "-P11M"], + ]; + for (const [args, argsOther, expected] of values) { + const plainYearMonth = new Temporal.PlainYearMonth(...args); + const other = new Temporal.PlainYearMonth(...argsOther); + expect(plainYearMonth.since(other).toString()).toBe(expected); + } + }); + + test("smallestUnit option", () => { + const plainYearMonth = new Temporal.PlainYearMonth(1, 2); + const other = new Temporal.PlainYearMonth(0, 1); + const values = [ + ["year", "P1Y"], + ["month", "P1Y1M"], + ]; + for (const [smallestUnit, expected] of values) { + expect(plainYearMonth.since(other, { smallestUnit }).toString()).toBe(expected); + } + }); + + test("largestUnit option", () => { + const plainYearMonth = new Temporal.PlainYearMonth(1, 2); + const other = new Temporal.PlainYearMonth(0, 1); + const values = [ + ["year", "P1Y1M"], + ["month", "P13M"], + ]; + for (const [largestUnit, expected] of values) { + expect(plainYearMonth.since(other, { largestUnit }).toString()).toBe(expected); + } + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Temporal.PlainYearMonth.prototype.since.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth"); + }); + + test("disallowed smallestUnit option values", () => { + const values = [ + "week", + "day", + "hour", + "minute", + "second", + "millisecond", + "microsecond", + "nanosecond", + ]; + for (const smallestUnit of values) { + const plainYearMonth = new Temporal.PlainYearMonth(1970, 1); + const other = new Temporal.PlainYearMonth(1970, 1); + expect(() => { + plainYearMonth.since(other, { smallestUnit }); + }).toThrowWithMessage( + RangeError, + `${smallestUnit} is not a valid value for option smallestUnit` + ); + } + }); + + test("disallowed largestUnit option values", () => { + const values = [ + "week", + "day", + "hour", + "minute", + "second", + "millisecond", + "microsecond", + "nanosecond", + ]; + for (const largestUnit of values) { + const plainYearMonth = new Temporal.PlainYearMonth(1970, 1); + const other = new Temporal.PlainYearMonth(1970, 1); + expect(() => { + plainYearMonth.since(other, { largestUnit }); + }).toThrowWithMessage( + RangeError, + `${largestUnit} is not a valid value for option largestUnit` + ); + } + }); + + test("cannot compare dates from different calendars", () => { + const calendarOne = { + toString() { + return "calendarOne"; + }, + }; + + const calendarTwo = { + toString() { + return "calendarTwo"; + }, + }; + + const plainYearMonthOne = new Temporal.PlainYearMonth(1970, 1, calendarOne); + const plainYearMonthTwo = new Temporal.PlainYearMonth(1970, 1, calendarTwo); + + expect(() => { + plainYearMonthOne.since(plainYearMonthTwo); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars"); + }); +}); |