mirror of
https://github.com/tc39/test262.git
synced 2025-07-26 07:25:15 +02:00
Temporal: Port some Duration tests.
This commit is contained in:
parent
78c6ec7f1c
commit
efcac4c05a
27
test/built-ins/Temporal/Duration/prototype/abs/new-object.js
vendored
Normal file
27
test/built-ins/Temporal/Duration/prototype/abs/new-object.js
vendored
Normal 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);
|
@ -4,10 +4,29 @@
|
|||||||
/*---
|
/*---
|
||||||
esid: sec-temporal.duration.prototype.add
|
esid: sec-temporal.duration.prototype.add
|
||||||
description: Verify that undefined options are handled correctly.
|
description: Verify that undefined options are handled correctly.
|
||||||
|
includes: [temporalHelpers.js]
|
||||||
features: [Temporal]
|
features: [Temporal]
|
||||||
---*/
|
---*/
|
||||||
|
|
||||||
const duration1 = new Temporal.Duration(1);
|
const duration1 = new Temporal.Duration(1);
|
||||||
const duration2 = new Temporal.Duration(0, 12);
|
const duration2 = new Temporal.Duration(0, 12);
|
||||||
assert.throws(RangeError, () => duration1.add(duration2), "default relativeTo is undefined");
|
const duration3 = new Temporal.Duration(0, 0, 0, 1);
|
||||||
assert.throws(RangeError, () => duration1.add(duration2, undefined), "default relativeTo is undefined");
|
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`);
|
||||||
|
}
|
||||||
|
25
test/built-ins/Temporal/Duration/prototype/blank/basic.js
vendored
Normal file
25
test/built-ins/Temporal/Duration/prototype/blank/basic.js
vendored
Normal 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);
|
@ -4,10 +4,29 @@
|
|||||||
/*---
|
/*---
|
||||||
esid: sec-temporal.duration.prototype.subtract
|
esid: sec-temporal.duration.prototype.subtract
|
||||||
description: Verify that undefined options are handled correctly.
|
description: Verify that undefined options are handled correctly.
|
||||||
|
includes: [temporalHelpers.js]
|
||||||
features: [Temporal]
|
features: [Temporal]
|
||||||
---*/
|
---*/
|
||||||
|
|
||||||
const duration1 = new Temporal.Duration(1);
|
const duration1 = new Temporal.Duration(1);
|
||||||
const duration2 = new Temporal.Duration(0, 12);
|
const duration2 = new Temporal.Duration(0, 24);
|
||||||
assert.throws(RangeError, () => duration1.subtract(duration2), "default relativeTo is undefined");
|
const duration3 = new Temporal.Duration(0, 0, 0, 1);
|
||||||
assert.throws(RangeError, () => duration1.subtract(duration2, undefined), "default relativeTo is undefined");
|
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`);
|
||||||
|
}
|
||||||
|
18
test/built-ins/Temporal/Duration/prototype/toJSON/options.js
vendored
Normal file
18
test/built-ins/Temporal/Duration/prototype/toJSON/options.js
vendored
Normal 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);
|
21
test/built-ins/Temporal/Duration/prototype/valueOf/basic.js
vendored
Normal file
21
test/built-ins/Temporal/Duration/prototype/valueOf/basic.js
vendored
Normal 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, "!==");
|
13
test/built-ins/Temporal/Duration/prototype/with/argument-invalid-prop.js
vendored
Normal file
13
test/built-ins/Temporal/Duration/prototype/with/argument-invalid-prop.js
vendored
Normal 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);
|
11
test/built-ins/Temporal/Duration/prototype/with/argument-mixed-sign.js
vendored
Normal file
11
test/built-ins/Temporal/Duration/prototype/with/argument-mixed-sign.js
vendored
Normal 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 }));
|
14
test/built-ins/Temporal/Duration/prototype/with/argument-sign-prop.js
vendored
Normal file
14
test/built-ins/Temporal/Duration/prototype/with/argument-sign-prop.js
vendored
Normal 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);
|
15
test/built-ins/Temporal/Duration/prototype/with/sign-replace.js
vendored
Normal file
15
test/built-ins/Temporal/Duration/prototype/with/sign-replace.js
vendored
Normal 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);
|
Loading…
x
Reference in New Issue
Block a user