Temporal: Move tests for ZonedDateTime/p/{add, subtract} out of staging into test/built-ins (#4287)

This commit is contained in:
Tim Chevalier 2024-10-30 16:20:09 -07:00 committed by GitHub
parent febf246e05
commit 85934bf49c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 206 additions and 79 deletions

View File

@ -0,0 +1,17 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.add
description: Adding a duration object works.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// "1969-12-25T12:23:45.678901234+00:00[UTC]"
const zdt = new Temporal.ZonedDateTime(-560174321098766n, "UTC")
const d = new Temporal.Duration(0, 0, 0, 0, 240, 0, 0, 0, 0, 800);
// "1970-01-04T12:23:45.678902034+00:00[UTC]"
const expected = new Temporal.ZonedDateTime(303825678902034n, "UTC");
TemporalHelpers.assertZonedDateTimesEqual(zdt.add(d), expected);

View File

@ -0,0 +1,16 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.add
description: Casts argument to Duration.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// "1969-12-25T12:23:45.678901234+00:00[UTC]"
const zdt = new Temporal.ZonedDateTime(-560174321098766n, "UTC")
// "1970-01-04T12:23:45.678902034+00:00[UTC]"
const expected = new Temporal.ZonedDateTime(303825678902034n, "UTC");
TemporalHelpers.assertZonedDateTimesEqual(zdt.add("PT240H0.000000800S"), expected);

View File

@ -0,0 +1,18 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.add
description: Constrains result when ambiguous.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// "2020-01-31T15:00-08:00[-08:00]"
const jan31 = new Temporal.ZonedDateTime(1580511600000000000n, "-08:00");
// "2020-02-29T15:00:00-08:00[-08:00]"
const expected = new Temporal.ZonedDateTime(1583017200000000000n, "-08:00");
TemporalHelpers.assertZonedDateTimesEqual(jan31.add({ months: 1 }), expected);
TemporalHelpers.assertZonedDateTimesEqual(
jan31.add({ months: 1 }, { overflow: "constrain" }), expected);

View File

@ -0,0 +1,39 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.add
description: Cross-epoch add/subtract
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// "1969-12-25T12:23:45.678901234+00:00[UTC]"
const zdt = new Temporal.ZonedDateTime(-560174321098766n, "UTC")
// cross epoch in ms
var one = zdt.subtract({
hours: 240,
nanoseconds: 800
});
var two = zdt.add({
hours: 240,
nanoseconds: 800
});
var three = two.subtract({
hours: 480,
nanoseconds: 1600
});
var four = one.add({
hours: 480,
nanoseconds: 1600
});
TemporalHelpers.assertZonedDateTimesEqual(one,
// "1969-12-15T12:23:45.678900434+00:00[UTC]"
new Temporal.ZonedDateTime(-1424174321099566n, "UTC"));
TemporalHelpers.assertZonedDateTimesEqual(two,
// "1970-01-04T12:23:45.678902034+00:00[UTC]")
new Temporal.ZonedDateTime(303825678902034n, "UTC"));
TemporalHelpers.assertZonedDateTimesEqual(three, one);
TemporalHelpers.assertZonedDateTimesEqual(four, two);

View File

@ -0,0 +1,19 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.add
description: Results are symmetrical with regard to negative durations in the time part.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// "2020-01-31T15:00-08:00[-08:00]"
const jan31 = new Temporal.ZonedDateTime(1580511600000000000n, "-08:00");
// "2020-01-31T14:30:00-08:00[-08:00]"
const expected1 = new Temporal.ZonedDateTime(1580509800000000000n, "-08:00");
// "2020-01-31T14:59:30-08:00[-08:00]"
const expected2 = new Temporal.ZonedDateTime(1580511570000000000n, "-08:00");
TemporalHelpers.assertZonedDateTimesEqual(jan31.add({ minutes: -30 }), expected1);
TemporalHelpers.assertZonedDateTimesEqual(jan31.add({ seconds: -30 }), expected2);

View File

@ -0,0 +1,13 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.add
description: Throws when ambiguous result with reject
features: [Temporal]
---*/
// "2020-01-31T15:00-08:00[-08:00]"
const jan31 = new Temporal.ZonedDateTime(1580511600000000000n, "-08:00");
assert.throws(RangeError, () => jan31.add({ months: 1 }, { overflow: "reject" }));

View File

@ -0,0 +1,15 @@
// 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.zoneddatetime.prototype.subtract
description: Casts argument from string.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const zdt = Temporal.ZonedDateTime.from("1969-12-25T12:23:45.678901234+00:00[UTC]");
TemporalHelpers.assertZonedDateTimesEqual(
zdt.subtract("PT240H0.000000800S"),
Temporal.ZonedDateTime.from("1969-12-15T12:23:45.678900434+00:00[UTC]"));

View File

@ -0,0 +1,21 @@
// 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.zoneddatetime.prototype.subtract
description: Constrains when ambiguous result.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const mar31 = Temporal.ZonedDateTime.from("2020-03-31T15:00+00:00[UTC]");
const expected = Temporal.ZonedDateTime.from("2020-02-29T15:00:00+00:00[UTC]");
TemporalHelpers.assertZonedDateTimesEqual(
mar31.subtract({ months: 1 }),
expected);
TemporalHelpers.assertZonedDateTimesEqual(
mar31.subtract({ months: 1 }, { overflow: "constrain" }),
expected);

View File

@ -0,0 +1,17 @@
// 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.zoneddatetime.prototype.subtract
description: Can subtract a duration object.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const zdt = Temporal.ZonedDateTime.from("1969-12-25T12:23:45.678901234+00:00[UTC]");
const d = new Temporal.Duration(0, 0, 0, 0, 240, 0, 0, 0, 0, 800);
const earlier = zdt.subtract(d);
TemporalHelpers.assertZonedDateTimesEqual(
earlier,
Temporal.ZonedDateTime.from("1969-12-15T12:23:45.678900434+00:00[UTC]"));

View File

@ -0,0 +1,19 @@
// 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.zoneddatetime.prototype.subtract
description: Symmetrical with regard to negative durations in the time part.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const mar31 = Temporal.ZonedDateTime.from("2020-03-31T15:00+00:00[UTC]");
TemporalHelpers.assertZonedDateTimesEqual(
mar31.subtract({ minutes: -30 }),
Temporal.ZonedDateTime.from("2020-03-31T15:30:00+00:00[UTC]"));
TemporalHelpers.assertZonedDateTimesEqual(
mar31.subtract({ seconds: -30 }),
Temporal.ZonedDateTime.from("2020-03-31T15:00:30+00:00[UTC]"));

View File

@ -0,0 +1,12 @@
// 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.zoneddatetime.prototype.subtract
description: Throws on an ambiguous result with reject.
features: [Temporal]
---*/
const mar31 = Temporal.ZonedDateTime.from("2020-03-31T15:00+00:00[UTC]");
assert.throws(RangeError, () => mar31.subtract({ months: 1 }, { overflow: "reject" }));

View File

@ -1,50 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-zoneddatetime-objects
description: Temporal.ZonedDateTime.prototype.add()
features: [Temporal]
---*/
var zdt = Temporal.ZonedDateTime.from("1969-12-25T12:23:45.678901234+00:00[UTC]");
// cross epoch in ms
var one = zdt.subtract({
hours: 240,
nanoseconds: 800
});
var two = zdt.add({
hours: 240,
nanoseconds: 800
});
var three = two.subtract({
hours: 480,
nanoseconds: 1600
});
var four = one.add({
hours: 480,
nanoseconds: 1600
});
assert.sameValue(`${ one }`, "1969-12-15T12:23:45.678900434+00:00[UTC]");
assert.sameValue(`${ two }`, "1970-01-04T12:23:45.678902034+00:00[UTC]");
assert(three.equals(one));
assert(four.equals(two));
// zdt.add(durationObj)
var later = zdt.add(Temporal.Duration.from("PT240H0.000000800S"));
assert.sameValue(`${ later }`, "1970-01-04T12:23:45.678902034+00:00[UTC]");
// casts argument
assert.sameValue(`${ zdt.add("PT240H0.000000800S") }`, "1970-01-04T12:23:45.678902034+00:00[UTC]");
var jan31 = Temporal.ZonedDateTime.from("2020-01-31T15:00-08:00[-08:00]");
// constrain when ambiguous result
assert.sameValue(`${ jan31.add({ months: 1 }) }`, "2020-02-29T15:00:00-08:00[-08:00]");
assert.sameValue(`${ jan31.add({ months: 1 }, { overflow: "constrain" }) }`, "2020-02-29T15:00:00-08:00[-08:00]");
// symmetrical with regard to negative durations in the time part
assert.sameValue(`${ jan31.add({ minutes: -30 }) }`, "2020-01-31T14:30:00-08:00[-08:00]");
assert.sameValue(`${ jan31.add({ seconds: -30 }) }`, "2020-01-31T14:59:30-08:00[-08:00]");
// throw when ambiguous result with reject
assert.throws(RangeError, () => jan31.add({ months: 1 }, { overflow: "reject" }));

View File

@ -1,29 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-zoneddatetime-objects
description: Temporal.ZonedDateTime.prototype.subtract()
features: [Temporal]
---*/
var zdt = Temporal.ZonedDateTime.from("1969-12-25T12:23:45.678901234+00:00[UTC]");
// inst.subtract(durationObj)
var earlier = zdt.subtract(Temporal.Duration.from("PT240H0.000000800S"));
assert.sameValue(`${ earlier }`, "1969-12-15T12:23:45.678900434+00:00[UTC]");
// casts argument
assert.sameValue(`${ zdt.subtract("PT240H0.000000800S") }`, "1969-12-15T12:23:45.678900434+00:00[UTC]");
var mar31 = Temporal.ZonedDateTime.from("2020-03-31T15:00+00:00[UTC]");
// constrain when ambiguous result
assert.sameValue(`${ mar31.subtract({ months: 1 }) }`, "2020-02-29T15:00:00+00:00[UTC]");
assert.sameValue(`${ mar31.subtract({ months: 1 }, { overflow: "constrain" }) }`, "2020-02-29T15:00:00+00:00[UTC]");
// symmetrical with regard to negative durations in the time part
assert.sameValue(`${ mar31.subtract({ minutes: -30 }) }`, "2020-03-31T15:30:00+00:00[UTC]");
assert.sameValue(`${ mar31.subtract({ seconds: -30 }) }`, "2020-03-31T15:00:30+00:00[UTC]");
// throw when ambiguous result with reject
assert.throws(RangeError, () => mar31.subtract({ months: 1 }, { overflow: "reject" }));