From 2c404a7bf68637ee369d8a62de10c9a88312f374 Mon Sep 17 00:00:00 2001 From: Philip Chimento <pchimento@igalia.com> Date: Tue, 7 May 2024 17:39:31 -0700 Subject: [PATCH] Temporal: Tests for removal of relativeTo from Duration.p.add/subtract See tc39/proposal-temporal#2825. Various edits to existing tests so that they make more sense with the removal of relativeTo. New tests specifically testing that calendar units cannot be added or subtracted directly. --- .../prototype/add/balance-negative-result.js | 4 +-- .../prototype/add/no-calendar-units.js | 35 +++++++++++++++++++ .../prototype/add/order-of-operations.js | 6 ++-- .../subtract/balance-negative-result.js | 4 +-- .../prototype/subtract/no-calendar-units.js | 35 +++++++++++++++++++ .../prototype/subtract/order-of-operations.js | 6 ++-- 6 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/prototype/add/no-calendar-units.js create mode 100644 test/built-ins/Temporal/Duration/prototype/subtract/no-calendar-units.js diff --git a/test/built-ins/Temporal/Duration/prototype/add/balance-negative-result.js b/test/built-ins/Temporal/Duration/prototype/add/balance-negative-result.js index 02b8b83d23f..3efd545d5c8 100644 --- a/test/built-ins/Temporal/Duration/prototype/add/balance-negative-result.js +++ b/test/built-ins/Temporal/Duration/prototype/add/balance-negative-result.js @@ -11,5 +11,5 @@ features: [Temporal] const duration1 = new Temporal.Duration(0, 0, 0, 0, -60); const duration2 = new Temporal.Duration(0, 0, 0, -1); -const resultNotRelative = duration1.add(duration2); -TemporalHelpers.assertDuration(resultNotRelative, 0, 0, 0, -3, -12, 0, 0, 0, 0, 0); +const result = duration1.add(duration2); +TemporalHelpers.assertDuration(result, 0, 0, 0, -3, -12, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/add/no-calendar-units.js b/test/built-ins/Temporal/Duration/prototype/add/no-calendar-units.js new file mode 100644 index 00000000000..6d64a668ac0 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/add/no-calendar-units.js @@ -0,0 +1,35 @@ +// Copyright (C) 2024 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: > + Throws if either the receiver or the argument is a duration with nonzero + calendar units +features: [Temporal] +---*/ + +const blank = new Temporal.Duration(); + +const withYears = new Temporal.Duration(1); +assert.throws(RangeError, () => withYears.add(blank), "should not add to receiver with years"); + +const withMonths = new Temporal.Duration(0, 1); +assert.throws(RangeError, () => withMonths.add(blank), "should not add to receiver with months"); + +const withWeeks = new Temporal.Duration(0, 0, 1); +assert.throws(RangeError, () => withWeeks.add(blank), "should not add to receiver with weeks"); + +const ok = new Temporal.Duration(0, 0, 0, 1); + +assert.throws(RangeError, () => ok.add(withYears), "should not add duration with years"); +assert.throws(RangeError, () => ok.add(withMonths), "should not add duration with months"); +assert.throws(RangeError, () => ok.add(withWeeks), "should not add duration with weeks"); + +assert.throws(RangeError, () => ok.add({ years: 1 }), "should not add property bag with years"); +assert.throws(RangeError, () => ok.add({ months: 1 }), "should not add property bag with months"); +assert.throws(RangeError, () => ok.add({ weeks: 1 }), "should not add property bag with weeks"); + +assert.throws(RangeError, () => ok.add('P1Y'), "should not add string with years"); +assert.throws(RangeError, () => ok.add('P1M'), "should not add string with months"); +assert.throws(RangeError, () => ok.add('P1W'), "should not add string with weeks"); diff --git a/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js b/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js index d2914eebe43..4ac88d3f4ca 100644 --- a/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js +++ b/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js @@ -43,7 +43,7 @@ const expected = [ ]; const actual = []; -const simpleFields = TemporalHelpers.propertyBagObserver(actual, { +const fields = TemporalHelpers.propertyBagObserver(actual, { years: 0, months: 0, weeks: 0, @@ -57,7 +57,7 @@ const simpleFields = TemporalHelpers.propertyBagObserver(actual, { }, "fields"); // basic order of observable operations, without any calendar units: -const simpleInstance = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1); -simpleInstance.add(simpleFields); +const instance = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1); +instance.add(fields); assert.compareArray(actual, expected, "order of operations"); actual.splice(0); // clear diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/balance-negative-result.js b/test/built-ins/Temporal/Duration/prototype/subtract/balance-negative-result.js index 3913519c971..ed678ab1f65 100644 --- a/test/built-ins/Temporal/Duration/prototype/subtract/balance-negative-result.js +++ b/test/built-ins/Temporal/Duration/prototype/subtract/balance-negative-result.js @@ -11,5 +11,5 @@ features: [Temporal] const duration1 = new Temporal.Duration(0, 0, 0, 0, -60); const duration2 = new Temporal.Duration(0, 0, 0, -1); -const resultNotRelative = duration1.subtract(duration2); -TemporalHelpers.assertDuration(resultNotRelative, 0, 0, 0, -1, -12, 0, 0, 0, 0, 0); +const result = duration1.subtract(duration2); +TemporalHelpers.assertDuration(result, 0, 0, 0, -1, -12, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/no-calendar-units.js b/test/built-ins/Temporal/Duration/prototype/subtract/no-calendar-units.js new file mode 100644 index 00000000000..6bdfa40dc4a --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/subtract/no-calendar-units.js @@ -0,0 +1,35 @@ +// Copyright (C) 2024 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: > + Throws if either the receiver or the argument is a duration with nonzero + calendar units +features: [Temporal] +---*/ + +const blank = new Temporal.Duration(); + +const withYears = new Temporal.Duration(1); +assert.throws(RangeError, () => withYears.subtract(blank), "should not subtract from receiver with years"); + +const withMonths = new Temporal.Duration(0, 1); +assert.throws(RangeError, () => withMonths.subtract(blank), "should not subtract from receiver with months"); + +const withWeeks = new Temporal.Duration(0, 0, 1); +assert.throws(RangeError, () => withWeeks.subtract(blank), "should not subtract from receiver with weeks"); + +const ok = new Temporal.Duration(0, 0, 0, 1); + +assert.throws(RangeError, () => ok.subtract(withYears), "should not subtract duration with years"); +assert.throws(RangeError, () => ok.subtract(withMonths), "should not subtract duration with months"); +assert.throws(RangeError, () => ok.subtract(withWeeks), "should not subtract duration with weeks"); + +assert.throws(RangeError, () => ok.subtract({ years: 1 }), "should not subtract property bag with years"); +assert.throws(RangeError, () => ok.subtract({ months: 1 }), "should not subtract property bag with months"); +assert.throws(RangeError, () => ok.subtract({ weeks: 1 }), "should not subtract property bag with weeks"); + +assert.throws(RangeError, () => ok.subtract('P1Y'), "should not subtract string with years"); +assert.throws(RangeError, () => ok.subtract('P1M'), "should not subtract string with months"); +assert.throws(RangeError, () => ok.subtract('P1W'), "should not subtract string with weeks"); diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js b/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js index 51729329f7f..0d461f5b471 100644 --- a/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js +++ b/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js @@ -43,7 +43,7 @@ const expected = [ ]; const actual = []; -const simpleFields = TemporalHelpers.propertyBagObserver(actual, { +const fields = TemporalHelpers.propertyBagObserver(actual, { years: 0, months: 0, weeks: 0, @@ -57,7 +57,7 @@ const simpleFields = TemporalHelpers.propertyBagObserver(actual, { }, "fields"); // basic order of observable operations, without any calendar units: -const simpleInstance = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1); -simpleInstance.subtract(simpleFields); +const instance = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1); +instance.subtract(fields); assert.compareArray(actual, expected, "order of operations"); actual.splice(0); // clear