Temporal: Port a few tests for Duration#{add,subtract}.

This commit is contained in:
Ms2ger 2022-04-26 13:58:02 +02:00
parent d156a5a63f
commit 7f2668f807
6 changed files with 183 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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