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.
This commit is contained in:
Philip Chimento 2024-05-07 17:39:31 -07:00 committed by Philip Chimento
parent ea4945c66f
commit 61e3f5ec4d
6 changed files with 80 additions and 10 deletions

View File

@ -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);

View File

@ -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");

View File

@ -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

View File

@ -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);

View File

@ -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");

View File

@ -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