Temporal: Port some Duration tests.

This commit is contained in:
Ms2ger 2022-04-19 16:48:15 +02:00
parent 78c6ec7f1c
commit efcac4c05a
10 changed files with 187 additions and 5 deletions

View File

@ -0,0 +1,27 @@
// 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.abs
description: Temporal.Duration.prototype.abs returns a new object.
features: [Temporal]
---*/
let d1 = new Temporal.Duration();
assert.notSameValue(d1.abs(), d1);
let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
assert.notSameValue(d2.abs(), d2);
let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5);
assert.notSameValue(d3.abs(), d3);
let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10);
assert.notSameValue(d4.abs(), d4);
// Test with some zeros
let d5 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0);
assert.notSameValue(d5.abs(), d5);
let d6 = new Temporal.Duration(0, 2, 0, 4, 0, 6, 0, 8, 0, 10);
assert.notSameValue(d6.abs(), d6);

View File

@ -4,10 +4,29 @@
/*---
esid: sec-temporal.duration.prototype.add
description: Verify that undefined options are handled correctly.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const duration1 = new Temporal.Duration(1);
const duration2 = new Temporal.Duration(0, 12);
assert.throws(RangeError, () => duration1.add(duration2), "default relativeTo is undefined");
assert.throws(RangeError, () => duration1.add(duration2, undefined), "default relativeTo is undefined");
const duration3 = new Temporal.Duration(0, 0, 0, 1);
const duration4 = new Temporal.Duration(0, 0, 0, 0, 24);
assert.throws(RangeError, () => duration1.add(duration2), "no options with years");
TemporalHelpers.assertDuration(duration3.add(duration4),
0, 0, 0, /* days = */ 2, 0, 0, 0, 0, 0, 0,
"no options with days");
const optionValues = [
[undefined, "undefined"],
[{}, "plain object"],
[() => {}, "lambda"],
];
for (const [options, description] of optionValues) {
assert.throws(RangeError, () => duration1.add(duration2, options),
`options ${description} with years`);
TemporalHelpers.assertDuration(duration3.add(duration4, options),
0, 0, 0, /* days = */ 2, 0, 0, 0, 0, 0, 0,
`options ${description} with days`);
}

View File

@ -0,0 +1,25 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-get-temporal.duration.prototype.blank
description: Basic tests for blank.
features: [Temporal]
---*/
assert.sameValue(Temporal.Duration.from("P3DT1H").blank, false);
assert.sameValue(Temporal.Duration.from("-PT2H20M30S").blank, false);
assert.sameValue(Temporal.Duration.from("PT0S").blank, true);
const zero = Temporal.Duration.from({
years: 0,
months: 0,
weeks: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
});
assert.sameValue(zero.blank, true);

View File

@ -4,10 +4,29 @@
/*---
esid: sec-temporal.duration.prototype.subtract
description: Verify that undefined options are handled correctly.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const duration1 = new Temporal.Duration(1);
const duration2 = new Temporal.Duration(0, 12);
assert.throws(RangeError, () => duration1.subtract(duration2), "default relativeTo is undefined");
assert.throws(RangeError, () => duration1.subtract(duration2, undefined), "default relativeTo is undefined");
const duration2 = new Temporal.Duration(0, 24);
const duration3 = new Temporal.Duration(0, 0, 0, 1);
const duration4 = new Temporal.Duration(0, 0, 0, 0, 48);
assert.throws(RangeError, () => duration1.subtract(duration2), "no options with years");
TemporalHelpers.assertDuration(duration3.subtract(duration4),
0, 0, 0, /* days = */ -1, 0, 0, 0, 0, 0, 0,
"no options with days");
const optionValues = [
[undefined, "undefined"],
[{}, "plain object"],
[() => {}, "lambda"],
];
for (const [options, description] of optionValues) {
assert.throws(RangeError, () => duration1.subtract(duration2, options),
`options ${description} with years`);
TemporalHelpers.assertDuration(duration3.subtract(duration4, options),
0, 0, 0, /* days = */ -1, 0, 0, 0, 0, 0, 0,
`options ${description} with days`);
}

View File

@ -0,0 +1,18 @@
// 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.tojson
description: Temporal.Duration.prototype.toJSON does not support options, unlike toString.
features: [Temporal]
---*/
let called = 0;
const options = new Proxy({}, {
get() {
++called;
}
});
const d = new Temporal.Duration(1, 2);
assert.sameValue(d.toJSON(options), "P1Y2M");
assert.sameValue(called, 0);

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.duration.prototype.valueof
description: Basic tests for valueOf().
features: [Temporal]
---*/
const d1 = Temporal.Duration.from("P3DT1H");
const d2 = Temporal.Duration.from("P3DT1H");
assert.throws(TypeError, () => d1.valueOf(), "valueOf");
assert.throws(TypeError, () => d1 < d1, "<");
assert.throws(TypeError, () => d1 <= d1, "<=");
assert.throws(TypeError, () => d1 > d1, ">");
assert.throws(TypeError, () => d1 >= d1, ">=");
assert.sameValue(d1 === d1, true, "===");
assert.sameValue(d1 === d2, false, "===");
assert.sameValue(d1 !== d1, false, "!==");
assert.sameValue(d1 !== d2, true, "!==");

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.with
description: Singular properties are ignored.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const d = Temporal.Duration.from({ years: 5, days: 1 });
const d2 = d.with({ year: 1, days: 0 });
TemporalHelpers.assertDuration(d2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0);

View File

@ -0,0 +1,11 @@
// 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.with
description: The durationLike argument must not contain different signs.
features: [Temporal]
---*/
const d = new Temporal.Duration(1, 2, 3, 4, 5);
assert.throws(RangeError, () => d.with({ hours: 1, minutes: -1 }));

View File

@ -0,0 +1,14 @@
// 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.with
description: Passing a sign property is not supported.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const d = Temporal.Duration.from({ years: 5, days: 1 });
assert.throws(TypeError, () => d.with({ sign: -1 }));
const d2 = d.with({ sign: -1, days: 0 });
TemporalHelpers.assertDuration(d2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0);

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.duration.prototype.with
description: Replacing the sign is supported.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const d = Temporal.Duration.from({ years: 5, days: 1 });
assert.sameValue(d.sign, 1, "original sign");
const d2 = d.with({ years: -1, days: 0, minutes: -1 });
assert.sameValue(d2.sign, -1, "new sign");
TemporalHelpers.assertDuration(d2, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0);