Temporal: Extend PlainDate#{since,until} coverage. (#3581)

This commit is contained in:
Ms2ger 2022-06-20 15:47:09 +02:00 committed by GitHub
parent d5b2c706cf
commit 65e75e4eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 0 deletions

View 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.since
description: Throw when the returned value from the calendar's dateUntil method is not a Duration.
features: [Temporal]
---*/
class CustomCalendar extends Temporal.Calendar {
constructor(value) {
super("iso8601");
this.value = value;
}
dateUntil() {
return this.value;
}
}
const tests = [
[undefined],
[null, "null"],
[true],
["2000-05"],
[Symbol()],
[200005],
[200005n],
[{}, "plain object"],
[() => {}, "lambda"],
[Temporal.Duration, "Temporal.Duration"],
[Temporal.Duration.prototype, "Temporal.Duration.prototype"],
];
for (const [test, description = typeof test] of tests) {
const plainDate = new Temporal.PlainDate(2000, 5, 2, new CustomCalendar(test));
assert.throws(TypeError, () => plainDate.since("2022-06-20"), `Expected error with ${description}`);
}

View File

@ -0,0 +1,32 @@
// 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.since
description: Basic tests with custom calendar
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const result = new Temporal.Duration(1, 3, 5, 7, 9);
const options = {};
let calls = 0;
class CustomCalendar extends Temporal.Calendar {
constructor() {
super("iso8601");
}
dateUntil(...args) {
++calls;
assert.sameValue(args.length, 3, "Three arguments");
assert.sameValue(args[0], plainDate, "First argument");
assert.sameValue(args[1], other, "Second argument");
assert.sameValue(args[2].largestUnit, "day", "Third argument: largestUnit");
return result;
}
}
const calendar = new CustomCalendar();
const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar);
const other = new Temporal.PlainDate(2022, 6, 20, calendar);
TemporalHelpers.assertDuration(plainDate.since(other, options),
-1, -3, -5, -7, 0, 0, 0, 0, 0, 0, "result");
assert.sameValue(calls, 1);

View 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.until
description: Throw when the returned value from the calendar's dateUntil method is not a Duration.
features: [Temporal]
---*/
class CustomCalendar extends Temporal.Calendar {
constructor(value) {
super("iso8601");
this.value = value;
}
dateUntil() {
return this.value;
}
}
const tests = [
[undefined],
[null, "null"],
[true],
["2000-05"],
[Symbol()],
[200005],
[200005n],
[{}, "plain object"],
[() => {}, "lambda"],
[Temporal.Duration, "Temporal.Duration"],
[Temporal.Duration.prototype, "Temporal.Duration.prototype"],
];
for (const [test, description = typeof test] of tests) {
const plainDate = new Temporal.PlainDate(2000, 5, 2, new CustomCalendar(test));
assert.throws(TypeError, () => plainDate.until("2022-06-20"), `Expected error with ${description}`);
}

View File

@ -0,0 +1,32 @@
// 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.until
description: Basic tests with custom calendar
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const result = new Temporal.Duration(1, 3, 5, 7, 9);
const options = {};
let calls = 0;
class CustomCalendar extends Temporal.Calendar {
constructor() {
super("iso8601");
}
dateUntil(...args) {
++calls;
assert.sameValue(args.length, 3, "Three arguments");
assert.sameValue(args[0], plainDate, "First argument");
assert.sameValue(args[1], other, "Second argument");
assert.sameValue(args[2].largestUnit, "day", "Third argument: largestUnit");
return result;
}
}
const calendar = new CustomCalendar();
const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar);
const other = new Temporal.PlainDate(2022, 6, 20, calendar);
TemporalHelpers.assertDuration(plainDate.until(other, options),
1, 3, 5, 7, 0, 0, 0, 0, 0, 0, "result");
assert.sameValue(calls, 1);