mirror of
https://github.com/tc39/test262.git
synced 2025-07-01 19:24:42 +02:00
Temporal: Test PlainDate#{add,subtract} with custom calendar.
This commit is contained in:
parent
79c4496559
commit
81836ba124
36
test/built-ins/Temporal/PlainDate/prototype/add/calendar-invalid-return.js
vendored
Normal file
36
test/built-ins/Temporal/PlainDate/prototype/add/calendar-invalid-return.js
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// 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.plaindate.prototype.add
|
||||||
|
description: Throw when the returned value from the calendar's dateAdd method is not a PlainDate.
|
||||||
|
features: [Temporal]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
class CustomCalendar extends Temporal.Calendar {
|
||||||
|
constructor(value) {
|
||||||
|
super("iso8601");
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
dateAdd() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const tests = [
|
||||||
|
[undefined],
|
||||||
|
[null, "null"],
|
||||||
|
[true],
|
||||||
|
["2000-05"],
|
||||||
|
[Symbol()],
|
||||||
|
[200005],
|
||||||
|
[200005n],
|
||||||
|
[{}, "plain object"],
|
||||||
|
[() => {}, "lambda"],
|
||||||
|
[Temporal.PlainDate, "Temporal.PlainDate"],
|
||||||
|
[Temporal.PlainDate.prototype, "Temporal.PlainDate.prototype"],
|
||||||
|
];
|
||||||
|
for (const [test, description = typeof test] of tests) {
|
||||||
|
const plainDate = new Temporal.PlainDate(2000, 5, 2, new CustomCalendar(test));
|
||||||
|
assert.throws(TypeError, () => plainDate.add({ years: 1 }), `Expected error with ${description}`);
|
||||||
|
}
|
31
test/built-ins/Temporal/PlainDate/prototype/add/custom.js
vendored
Normal file
31
test/built-ins/Temporal/PlainDate/prototype/add/custom.js
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// 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.plaindate.prototype.add
|
||||||
|
description: Basic tests with custom calendar
|
||||||
|
includes: [compareArray.js,temporalHelpers.js]
|
||||||
|
features: [Temporal]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const result = new Temporal.PlainDate(1920, 5, 3);
|
||||||
|
let calls = 0;
|
||||||
|
class CustomCalendar extends Temporal.Calendar {
|
||||||
|
constructor() {
|
||||||
|
super("iso8601");
|
||||||
|
}
|
||||||
|
dateAdd(...args) {
|
||||||
|
++calls;
|
||||||
|
assert.sameValue(args.length, 3, "Three arguments");
|
||||||
|
assert.sameValue(args[0], plainDate, "First argument");
|
||||||
|
TemporalHelpers.assertDuration(args[1], 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Second argument");
|
||||||
|
assert.sameValue(typeof args[2], "object", "Third argument: type");
|
||||||
|
assert.sameValue(Object.getPrototypeOf(args[2]), null, "Third argument: prototype");
|
||||||
|
assert.compareArray(Object.keys(args[2]), [], "Third argument: keys");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const calendar = new CustomCalendar();
|
||||||
|
const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar);
|
||||||
|
assert.sameValue(plainDate.add({ years: 43 }), result);
|
||||||
|
assert.sameValue(calls, 1);
|
36
test/built-ins/Temporal/PlainDate/prototype/subtract/calendar-invalid-return.js
vendored
Normal file
36
test/built-ins/Temporal/PlainDate/prototype/subtract/calendar-invalid-return.js
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// 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.plaindate.prototype.subtract
|
||||||
|
description: Throw when the returned value from the calendar's dateAdd method is not a PlainDate.
|
||||||
|
features: [Temporal]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
class CustomCalendar extends Temporal.Calendar {
|
||||||
|
constructor(value) {
|
||||||
|
super("iso8601");
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
dateAdd() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const tests = [
|
||||||
|
[undefined],
|
||||||
|
[null, "null"],
|
||||||
|
[true],
|
||||||
|
["2000-05"],
|
||||||
|
[Symbol()],
|
||||||
|
[200005],
|
||||||
|
[200005n],
|
||||||
|
[{}, "plain object"],
|
||||||
|
[() => {}, "lambda"],
|
||||||
|
[Temporal.PlainDate, "Temporal.PlainDate"],
|
||||||
|
[Temporal.PlainDate.prototype, "Temporal.PlainDate.prototype"],
|
||||||
|
];
|
||||||
|
for (const [test, description = typeof test] of tests) {
|
||||||
|
const plainDate = new Temporal.PlainDate(2000, 5, 2, new CustomCalendar(test));
|
||||||
|
assert.throws(TypeError, () => plainDate.subtract({ years: 1 }), `Expected error with ${description}`);
|
||||||
|
}
|
31
test/built-ins/Temporal/PlainDate/prototype/subtract/custom.js
vendored
Normal file
31
test/built-ins/Temporal/PlainDate/prototype/subtract/custom.js
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// 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.plaindate.prototype.subtract
|
||||||
|
description: Basic tests with custom calendar
|
||||||
|
includes: [compareArray.js,temporalHelpers.js]
|
||||||
|
features: [Temporal]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const result = new Temporal.PlainDate(1920, 5, 3);
|
||||||
|
let calls = 0;
|
||||||
|
class CustomCalendar extends Temporal.Calendar {
|
||||||
|
constructor() {
|
||||||
|
super("iso8601");
|
||||||
|
}
|
||||||
|
dateAdd(...args) {
|
||||||
|
++calls;
|
||||||
|
assert.sameValue(args.length, 3, "Three arguments");
|
||||||
|
assert.sameValue(args[0], plainDate, "First argument");
|
||||||
|
TemporalHelpers.assertDuration(args[1], -43, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Second argument");
|
||||||
|
assert.sameValue(typeof args[2], "object", "Third argument: type");
|
||||||
|
assert.sameValue(Object.getPrototypeOf(args[2]), null, "Third argument: prototype");
|
||||||
|
assert.compareArray(Object.keys(args[2]), [], "Third argument: keys");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const calendar = new CustomCalendar();
|
||||||
|
const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar);
|
||||||
|
assert.sameValue(plainDate.subtract({ years: 43 }), result);
|
||||||
|
assert.sameValue(calls, 1);
|
Loading…
x
Reference in New Issue
Block a user