Skip to content

Commit

Permalink
Temporal: Tests for removal of relativeTo from Duration.p.add
Browse files Browse the repository at this point in the history
See tc39/proposal-temporal#2825.

Various edits to existing tests so that they make more sense with the
removal of relativeTo.

New test specifically testing that calendar units cannot be added
directly.
  • Loading branch information
ptomato committed Jun 9, 2024
1 parent c2c327d commit a945eb6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const expected = [
];
const actual = [];

const simpleFields = TemporalHelpers.propertyBagObserver(actual, {
const fields = TemporalHelpers.propertyBagObserver(actual, {
years: 0,
months: 0,
weeks: 0,
Expand All @@ -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

0 comments on commit a945eb6

Please sign in to comment.