From 0b8319355b6b0533f61f9b85c13f25fbe9fd0c34 Mon Sep 17 00:00:00 2001 From: Jesse Alama Date: Tue, 30 Nov 2021 11:34:02 +0100 Subject: [PATCH] Test `since` for PD(T) with `largestUnit` = `year`, `month` Nails down intended behavior of `PlainDate` and `PlainDateTime`'s `since` that is already true in the polyfill but which was specified in a buggy way (and hence potentially not true in an implementation of Temporal). Add similar tests for Instant, PlainTime, PlainYearMonth, and ZonedDateTime. Reference: https://github.com/tc39/proposal-temporal/pull/1881 --- .../Instant/prototype/since/largestunit.js | 18 +++++++++++++++ .../PlainDate/prototype/since/largestunit.js | 14 ++++++++++++ .../prototype/since/largestunit.js | 14 ++++++++++++ .../PlainTime/prototype/since/largestunit.js | 15 +++++++++++++ .../prototype/since/largestunit.js | 14 ++++++++++++ .../prototype/since/largestunit.js | 22 +++++++++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 test/built-ins/Temporal/Instant/prototype/since/largestunit.js create mode 100644 test/built-ins/Temporal/PlainDate/prototype/since/largestunit.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/since/largestunit.js create mode 100644 test/built-ins/Temporal/PlainTime/prototype/since/largestunit.js create mode 100644 test/built-ins/Temporal/PlainYearMonth/prototype/since/largestunit.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/since/largestunit.js diff --git a/test/built-ins/Temporal/Instant/prototype/since/largestunit.js b/test/built-ins/Temporal/Instant/prototype/since/largestunit.js new file mode 100644 index 0000000000..0f8674fa9a --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/since/largestunit.js @@ -0,0 +1,18 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.instant.prototype.since +description: Specify behavior of Instant.since when largest specified unit is specified +includes: [temporalHelpers.js] +features: [Temporal, BigInt] +---*/ +const thePast = new Temporal.Instant(1234567890123456789n); +const theFuture = new Temporal.Instant(2345678901234567890n); +TemporalHelpers.assertDuration(theFuture.since(thePast), 0, 0, 0, 0, 0, 0, 1111111011, 111, 111, 101, 'does not include higher units than necessary (largest unit unspecified)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'hours' }), 0, 0, 0, 0, 308641, 56, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is hours)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'minutes' }), 0, 0, 0, 0, 0, 18518516, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is minutes)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'seconds' }), 0, 0, 0, 0, 0, 0, 1111111011, 111, 111, 101, 'does not include higher units than necessary (largest unit is seconds)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'milliseconds' }), 0, 0, 0, 0, 0, 0, 0, 1111111011111, 111, 101, 'does not include higher units than necessary (largest unit is milliseconds)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'microseconds' }), 0, 0, 0, 0, 0, 0, 0, 0, 1111111011111111, 101, 'does not include higher units than necessary (largest unit is microseconds)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'nanoseconds' }), 0, 0, 0, 0, 0, 0, 0, 0, 0, 1111111011111111000, 'does not include higher units than necessary (largest unit is nanoseconds)'); diff --git a/test/built-ins/Temporal/PlainDate/prototype/since/largestunit.js b/test/built-ins/Temporal/PlainDate/prototype/since/largestunit.js new file mode 100644 index 0000000000..596fe0c088 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/since/largestunit.js @@ -0,0 +1,14 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindate.prototype.since +description: Specify behavior of PlainDate.since when largest specified unit is years or months. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ +const pd1 = new Temporal.PlainDate(2020, 11, 1); +const pd2 = new Temporal.PlainDate(2021, 11, 30); +TemporalHelpers.assertDuration(pd2.since(pd1), 0, 0, 0, 394, 0, 0, 0, 0, 0, 0, 'does not include higher units than necessary (largest unit unspecified)'); +TemporalHelpers.assertDuration(pd2.since(pd1, { largestUnit: 'months' }), 0, 12, 0, 29, 0, 0, 0, 0, 0, 0, 'does not include higher units than necessary (largest unit is months)'); +TemporalHelpers.assertDuration(pd2.since(pd1, { largestUnit: 'years' }), 1, 0, 0, 29, 0, 0, 0, 0, 0, 0, 'does not include higher units than necessary (largest unit is years)'); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/since/largestunit.js b/test/built-ins/Temporal/PlainDateTime/prototype/since/largestunit.js new file mode 100644 index 0000000000..87b6ba10aa --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/since/largestunit.js @@ -0,0 +1,14 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime.prototype.since +description: Specify behavior of PlainDateTime.since when largest specified unit is years or months. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ +const lastFeb20 = new Temporal.PlainDateTime(2020, 2, 20, 5, 45, 20); +const lastFeb21 = new Temporal.PlainDateTime(2021, 2, 21, 17, 18, 57); +TemporalHelpers.assertDuration(lastFeb21.since(lastFeb20), 0, 0, 0, 367, 11, 33, 37, 0, 0, 0, 'does not include higher units than necessary (largest unit unspecified)'); +TemporalHelpers.assertDuration(lastFeb21.since(lastFeb20, { largestUnit: 'months' }), 0, 12, 0, 1, 11, 33, 37, 0, 0, 0, 'does not include higher units than necessary (largest unit is months)'); +TemporalHelpers.assertDuration(lastFeb21.since(lastFeb20, { largestUnit: 'years' }), 1, 0, 0, 1, 11, 33, 37, 0, 0, 0, 'does not include higher units than necessary (largest unit is years)'); diff --git a/test/built-ins/Temporal/PlainTime/prototype/since/largestunit.js b/test/built-ins/Temporal/PlainTime/prototype/since/largestunit.js new file mode 100644 index 0000000000..1345c64361 --- /dev/null +++ b/test/built-ins/Temporal/PlainTime/prototype/since/largestunit.js @@ -0,0 +1,15 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaintime.prototype.since +description: Specify behavior of PlainTime.since when largest specified unit is years or months. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ +const fourFortyEight = new Temporal.PlainTime(4, 48, 55); +const elevenFiftyNine = new Temporal.PlainTime(11, 59, 58); +TemporalHelpers.assertDuration(elevenFiftyNine.since(fourFortyEight), 0, 0, 0, 0, 7, 11, 3, 0, 0, 0, 'does not include higher units than necessary (largest unit unspecified)'); +TemporalHelpers.assertDuration(elevenFiftyNine.since(fourFortyEight, { largestUnit: 'hours' }), 0, 0, 0, 0, 7, 11, 3, 0, 0, 0, 'does not include higher units than necessary (largest unit is hours)'); +TemporalHelpers.assertDuration(elevenFiftyNine.since(fourFortyEight, { largestUnit: 'minutes' }), 0, 0, 0, 0, 0, 431, 3, 0, 0, 0, 'does not include higher units than necessary (largest unit is minutes)'); +TemporalHelpers.assertDuration(elevenFiftyNine.since(fourFortyEight, { largestUnit: 'seconds' }), 0, 0, 0, 0, 0, 0, 25863, 0, 0, 0, 'does not include higher units than necessary (largest unit is seconds)'); diff --git a/test/built-ins/Temporal/PlainYearMonth/prototype/since/largestunit.js b/test/built-ins/Temporal/PlainYearMonth/prototype/since/largestunit.js new file mode 100644 index 0000000000..edff6655a7 --- /dev/null +++ b/test/built-ins/Temporal/PlainYearMonth/prototype/since/largestunit.js @@ -0,0 +1,14 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plainyearmonth.prototype.since +description: Specify behavior of PlainYearMonth.since when largest specified unit is years or months. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ +const nov2020 = new Temporal.PlainYearMonth(2020, 11); +const dec2021 = new Temporal.PlainYearMonth(2021, 12); +TemporalHelpers.assertDuration(dec2021.since(nov2020), 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 'does not include higher units than necessary (largest unit unspecified)'); +TemporalHelpers.assertDuration(dec2021.since(nov2020, { largestUnit: 'years' }), 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 'does not include higher units than necessary (largest unit is years)'); +TemporalHelpers.assertDuration(dec2021.since(nov2020, { largestUnit: 'months' }), 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 'does not include higher units than necessary (largest unit is months)'); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/since/largestunit.js b/test/built-ins/Temporal/ZonedDateTime/prototype/since/largestunit.js new file mode 100644 index 0000000000..86e22d03c2 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/since/largestunit.js @@ -0,0 +1,22 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.zoneddatetime.prototype.since +description: Specify behavior of ZonedDateTime.since when largest specified unit is specified +includes: [temporalHelpers.js] +features: [Temporal, BigInt] +---*/ +const thePast = new Temporal.ZonedDateTime(1234567890123456789n, '-08:00'); +const theFuture = new Temporal.ZonedDateTime(2345678901234567890n, '-08:00'); +TemporalHelpers.assertDuration(theFuture.since(thePast), 0, 0, 0, 0, 308641, 56, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit unspecified)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'years' }), 35, 2, 0, 15, 1, 56, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is years)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'months' }), 0, 422, 0, 15, 1, 56, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is months)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'weeks' }), 0, 0, 1837, 1, 1, 56, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is weeks)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'days' }), 0, 0, 0, 12860, 1, 56, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is days)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'hours' }), 0, 0, 0, 0, 308641, 56, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is hours)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'minutes' }), 0, 0, 0, 0, 0, 18518516, 51, 111, 111, 101, 'does not include higher units than necessary (largest unit is minutes)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'seconds' }), 0, 0, 0, 0, 0, 0, 1111111011, 111, 111, 101, 'does not include higher units than necessary (largest unit is seconds)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'milliseconds' }), 0, 0, 0, 0, 0, 0, 0, 1111111011111, 111, 101, 'does not include higher units than necessary (largest unit is milliseconds)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'microseconds' }), 0, 0, 0, 0, 0, 0, 0, 0, 1111111011111111, 101, 'does not include higher units than necessary (largest unit is microseconds)'); +TemporalHelpers.assertDuration(theFuture.since(thePast, { largestUnit: 'nanoseconds' }), 0, 0, 0, 0, 0, 0, 0, 0, 0, 1111111011111111000, 'does not include higher units than necessary (largest unit is nanoseconds)');