From 7f2668f807d183af5759ab6f2fb1c7c806522f27 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 26 Apr 2022 13:58:02 +0200 Subject: [PATCH] Temporal: Port a few tests for Duration#{add,subtract}. --- .../prototype/add/argument-object-invalid.js | 13 ++++ .../Temporal/Duration/prototype/add/basic.js | 28 +++++++++ .../prototype/add/relativeto-required.js | 33 ++++++++++ .../subtract/argument-object-invalid.js | 13 ++++ .../Duration/prototype/subtract/basic.js | 63 +++++++++++++++++++ .../prototype/subtract/relativeto-required.js | 33 ++++++++++ 6 files changed, 183 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/add/argument-object-invalid.js create mode 100644 test/built-ins/Temporal/Duration/prototype/add/basic.js create mode 100644 test/built-ins/Temporal/Duration/prototype/add/relativeto-required.js create mode 100644 test/built-ins/Temporal/Duration/prototype/subtract/argument-object-invalid.js create mode 100644 test/built-ins/Temporal/Duration/prototype/subtract/basic.js create mode 100644 test/built-ins/Temporal/Duration/prototype/subtract/relativeto-required.js diff --git a/test/built-ins/Temporal/Duration/prototype/add/argument-object-invalid.js b/test/built-ins/Temporal/Duration/prototype/add/argument-object-invalid.js new file mode 100644 index 0000000000..ed5b5873a3 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/add/argument-object-invalid.js @@ -0,0 +1,13 @@ +// Copyright (C) 2022 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.add +description: Mixed positive and negative values or missing properties always throw +features: [Temporal] +---*/ + +const duration = Temporal.Duration.from({ days: 1, minutes: 5 }); +assert.throws(RangeError, () => duration.add({ hours: 1, minutes: -30 }), "mixed signs"); +assert.throws(TypeError, () => duration.add({}), "no properties"); +assert.throws(TypeError, () => duration.add({ month: 12 }), "only singular 'month' property"); diff --git a/test/built-ins/Temporal/Duration/prototype/add/basic.js b/test/built-ins/Temporal/Duration/prototype/add/basic.js new file mode 100644 index 0000000000..46eb1fb6cf --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/add/basic.js @@ -0,0 +1,28 @@ +// Copyright (C) 2022 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.add +description: Basic behavior +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const duration1 = Temporal.Duration.from({ days: 1, minutes: 5 }); +TemporalHelpers.assertDuration(duration1.add({ days: 2, minutes: 5 }), + 0, 0, 0, 3, 0, 10, 0, 0, 0, 0, "positive same units"); +TemporalHelpers.assertDuration(duration1.add({ hours: 12, seconds: 30 }), + 0, 0, 0, 1, 12, 5, 30, 0, 0, 0, "positive different units"); +TemporalHelpers.assertDuration(Temporal.Duration.from("P3DT10M").add({ days: -2, minutes: -5 }), + 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, "negative same units"); +TemporalHelpers.assertDuration(Temporal.Duration.from("P1DT12H5M30S").add({ hours: -12, seconds: -30 }), + 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, "negative different units"); +const duration2 = Temporal.Duration.from("P50DT50H50M50.500500500S"); +TemporalHelpers.assertDuration(duration2.add(duration2), + 0, 0, 0, 104, 5, 41, 41, 1, 1, 0, "balancing positive"); +const duration3 = Temporal.Duration.from({ hours: -1, seconds: -60 }); +TemporalHelpers.assertDuration(duration3.add({ minutes: 122 }), + 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, "balancing flipped sign 1"); +const duration4 = Temporal.Duration.from({ hours: -1, seconds: -3721 }); +TemporalHelpers.assertDuration(duration4.add({ minutes: 61, nanoseconds: 3722000000001 }), + 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, "balancing flipped sign 2"); diff --git a/test/built-ins/Temporal/Duration/prototype/add/relativeto-required.js b/test/built-ins/Temporal/Duration/prototype/add/relativeto-required.js new file mode 100644 index 0000000000..ae171e0f14 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/add/relativeto-required.js @@ -0,0 +1,33 @@ +// Copyright (C) 2022 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.add +description: relativeTo is required if the largest unit is at least weeks. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = Temporal.Duration.from({ hours: 1 }); +const dy = Temporal.Duration.from({ years: 1 }); +const dm = Temporal.Duration.from({ months: 1 }); +const dw = Temporal.Duration.from({ weeks: 1 }); +assert.throws(RangeError, () => d.add(dy)); +assert.throws(RangeError, () => d.add(dm)); +assert.throws(RangeError, () => d.add(dw)); +assert.throws(RangeError, () => dy.add(d)); +assert.throws(RangeError, () => dm.add(d)); +assert.throws(RangeError, () => dw.add(d)); +const relativeTo = Temporal.PlainDate.from("2000-01-01"); +TemporalHelpers.assertDuration(d.add(dy, { relativeTo }), + 1, 0, 0, 0, 1, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d.add(dm, { relativeTo }), + 0, 1, 0, 0, 1, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d.add(dw, { relativeTo }), + 0, 0, 1, 0, 1, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(dy.add(d, { relativeTo }), + 1, 0, 0, 0, 1, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(dm.add(d, { relativeTo }), + 0, 1, 0, 0, 1, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(dw.add(d, { relativeTo }), + 0, 0, 1, 0, 1, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/argument-object-invalid.js b/test/built-ins/Temporal/Duration/prototype/subtract/argument-object-invalid.js new file mode 100644 index 0000000000..8d823237fa --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/subtract/argument-object-invalid.js @@ -0,0 +1,13 @@ +// Copyright (C) 2022 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.subtract +description: Mixed positive and negative values or missing properties always throw +features: [Temporal] +---*/ + +const duration = Temporal.Duration.from({ days: 1, minutes: 5 }); +assert.throws(RangeError, () => duration.subtract({ hours: 1, minutes: -30 }), "mixed signs"); +assert.throws(TypeError, () => duration.subtract({}), "no properties"); +assert.throws(TypeError, () => duration.subtract({ month: 12 }), "only singular 'month' property"); diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/basic.js b/test/built-ins/Temporal/Duration/prototype/subtract/basic.js new file mode 100644 index 0000000000..fd221c5402 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/subtract/basic.js @@ -0,0 +1,63 @@ +// Copyright (C) 2022 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.subtract +description: Basic behavior +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const duration = Temporal.Duration.from({ days: 3, hours: 1, minutes: 10 }); +TemporalHelpers.assertDuration(duration.subtract({ days: 1, minutes: 5 }), + 0, 0, 0, 2, 1, 5, 0, 0, 0, 0); +TemporalHelpers.assertDuration(duration.subtract(duration), + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(duration.subtract({ days: 3 }), + 0, 0, 0, 0, 1, 10, 0, 0, 0, 0); +TemporalHelpers.assertDuration(duration.subtract({ minutes: 10 }), + 0, 0, 0, 3, 1, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(duration.subtract({ minutes: 15 }), + 0, 0, 0, 3, 0, 55, 0, 0, 0, 0); +TemporalHelpers.assertDuration(duration.subtract({ seconds: 30 }), + 0, 0, 0, 3, 1, 9, 30, 0, 0, 0); +TemporalHelpers.assertDuration(Temporal.Duration.from('P2DT1H5M').subtract({ days: -1, minutes: -5 }), + 0, 0, 0, 3, 1, 10, 0, 0, 0, 0); +TemporalHelpers.assertDuration(new Temporal.Duration().subtract({ days: -3, hours: -1, minutes: -10 }), + 0, 0, 0, 3, 1, 10, 0, 0, 0, 0); +TemporalHelpers.assertDuration(Temporal.Duration.from('PT1H10M').subtract({ days: -3 }), + 0, 0, 0, 3, 1, 10, 0, 0, 0, 0); +TemporalHelpers.assertDuration(Temporal.Duration.from('P3DT1H').subtract({ minutes: -10 }), + 0, 0, 0, 3, 1, 10, 0, 0, 0, 0); +TemporalHelpers.assertDuration(Temporal.Duration.from('P3DT55M').subtract({ minutes: -15 }), + 0, 0, 0, 3, 1, 10, 0, 0, 0, 0); +TemporalHelpers.assertDuration(Temporal.Duration.from('P3DT1H9M30S').subtract({ seconds: -30 }), + 0, 0, 0, 3, 1, 10, 0, 0, 0, 0); +const d = Temporal.Duration.from({ + minutes: 100, + seconds: 100, + milliseconds: 2000, + microseconds: 2000, + nanoseconds: 2000 +}); +const less = Temporal.Duration.from({ + minutes: 10, + seconds: 10, + milliseconds: 500, + microseconds: 500, + nanoseconds: 500 +}); +TemporalHelpers.assertDuration(d.subtract(less), + 0, 0, 0, 0, 0, 91, 31, 501, 501, 500); +const tenDays = Temporal.Duration.from('P10D'); +const tenMinutes = Temporal.Duration.from('PT10M'); +TemporalHelpers.assertDuration(tenDays.subtract({ days: 15 }), + 0, 0, 0, -5, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(tenMinutes.subtract({ minutes: 15 }), + 0, 0, 0, 0, 0, -5, 0, 0, 0, 0); +const d1 = Temporal.Duration.from({ hours: 1, seconds: 60 }); +TemporalHelpers.assertDuration(d1.subtract({ minutes: 122 }), + 0, 0, 0, 0, -1, -1, 0, 0, 0, 0); +const d2 = Temporal.Duration.from({ hours: 1, seconds: 3721 }); +TemporalHelpers.assertDuration(d2.subtract({ minutes: 61, nanoseconds: 3722000000001 }), + 0, 0, 0, 0, 0, -1, -1, 0, 0, -1); diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/relativeto-required.js b/test/built-ins/Temporal/Duration/prototype/subtract/relativeto-required.js new file mode 100644 index 0000000000..893a9e11f5 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/subtract/relativeto-required.js @@ -0,0 +1,33 @@ +// Copyright (C) 2022 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.subtract +description: relativeTo is required if the largest unit is at least weeks. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = Temporal.Duration.from({ hours: 1 }); +const dy = Temporal.Duration.from({ years: 1, hours: 1 }); +const dm = Temporal.Duration.from({ months: 1, hours: 1 }); +const dw = Temporal.Duration.from({ weeks: 1, hours: 1 }); +assert.throws(RangeError, () => d.subtract(dy)); +assert.throws(RangeError, () => d.subtract(dm)); +assert.throws(RangeError, () => d.subtract(dw)); +assert.throws(RangeError, () => dy.subtract(d)); +assert.throws(RangeError, () => dm.subtract(d)); +assert.throws(RangeError, () => dw.subtract(d)); +const relativeTo = Temporal.PlainDate.from("2000-01-01"); +TemporalHelpers.assertDuration(d.subtract(dy, { relativeTo }), + -1, 0, 0, 0, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d.subtract(dm, { relativeTo }), + 0, -1, 0, 0, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(d.subtract(dw, { relativeTo }), + 0, 0, -1, 0, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(dy.subtract(d, { relativeTo }), + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(dm.subtract(d, { relativeTo }), + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); +TemporalHelpers.assertDuration(dw.subtract(d, { relativeTo }), + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);