Skip to content

Commit

Permalink
Temporal: Add tests for calendar validation normative change
Browse files Browse the repository at this point in the history
This adds tests for the normative change in tc39/proposal-temporal#2265,
which adds validation steps to certain abstract operations that call
Temporal.Calendar methods.

These had some duplication with some existing tests named
calendar-returns-infinity.js. Remove these, because the new tests are more
complete in testing what values are accepted.
  • Loading branch information
ptomato committed Oct 4, 2022
1 parent db375a4 commit 274a2a0
Show file tree
Hide file tree
Showing 57 changed files with 2,206 additions and 324 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.plaindate.prototype.day
description: Validate result returned from calendar day() method
features: [Temporal]
---*/

const badResults = [
[undefined, RangeError],
[null, RangeError],
[false, RangeError],
[Infinity, RangeError],
[-Infinity, RangeError],
[NaN, RangeError],
[-7, RangeError],
[-0.1, RangeError],
["string", RangeError],
[Symbol("foo"), TypeError],
[7n, TypeError],
[{}, RangeError],
];

badResults.forEach(([result, error]) => {
const calendar = new class extends Temporal.Calendar {
day() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.throws(error, () => instance.day, `${typeof result} not converted to positive integer`);
});

const convertedResults = [
[true, 1],
[7.1, 7],
["7", 7],
["7.5", 7],
[{valueOf() { return 7; }}, 7],
];

convertedResults.forEach(([result, convertedResult]) => {
const calendar = new class extends Temporal.Calendar {
day() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.sameValue(instance.day, convertedResult, `${typeof result} converted to positive integer ${convertedResult}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.plaindate.prototype.dayofweek
description: Validate result returned from calendar dayOfWeek() method
features: [Temporal]
---*/

const badResults = [
[undefined, RangeError],
[null, RangeError],
[false, RangeError],
[Infinity, RangeError],
[-Infinity, RangeError],
[NaN, RangeError],
[-7, RangeError],
[-0.1, RangeError],
["string", RangeError],
[Symbol("foo"), TypeError],
[7n, TypeError],
[{}, RangeError],
];

badResults.forEach(([result, error]) => {
const calendar = new class extends Temporal.Calendar {
dayOfWeek() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.throws(error, () => instance.dayOfWeek, `${typeof result} not converted to positive integer`);
});

const convertedResults = [
[true, 1],
[7.1, 7],
["7", 7],
["7.5", 7],
[{valueOf() { return 7; }}, 7],
];

convertedResults.forEach(([result, convertedResult]) => {
const calendar = new class extends Temporal.Calendar {
dayOfWeek() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.sameValue(instance.dayOfWeek, convertedResult, `${typeof result} converted to positive integer ${convertedResult}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.plaindate.prototype.dayofyear
description: Validate result returned from calendar dayOfYear() method
features: [Temporal]
---*/

const badResults = [
[undefined, RangeError],
[null, RangeError],
[false, RangeError],
[Infinity, RangeError],
[-Infinity, RangeError],
[NaN, RangeError],
[-7, RangeError],
[-0.1, RangeError],
["string", RangeError],
[Symbol("foo"), TypeError],
[7n, TypeError],
[{}, RangeError],
];

badResults.forEach(([result, error]) => {
const calendar = new class extends Temporal.Calendar {
dayOfYear() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.throws(error, () => instance.dayOfYear, `${typeof result} not converted to positive integer`);
});

const convertedResults = [
[true, 1],
[7.1, 7],
["7", 7],
["7.5", 7],
[{valueOf() { return 7; }}, 7],
];

convertedResults.forEach(([result, convertedResult]) => {
const calendar = new class extends Temporal.Calendar {
dayOfYear() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.sameValue(instance.dayOfYear, convertedResult, `${typeof result} converted to positive integer ${convertedResult}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.plaindate.prototype.daysinmonth
description: Validate result returned from calendar daysInMonth() method
features: [Temporal]
---*/

const badResults = [
[undefined, RangeError],
[null, RangeError],
[false, RangeError],
[Infinity, RangeError],
[-Infinity, RangeError],
[NaN, RangeError],
[-7, RangeError],
[-0.1, RangeError],
["string", RangeError],
[Symbol("foo"), TypeError],
[7n, TypeError],
[{}, RangeError],
];

badResults.forEach(([result, error]) => {
const calendar = new class extends Temporal.Calendar {
daysInMonth() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.throws(error, () => instance.daysInMonth, `${typeof result} not converted to positive integer`);
});

const convertedResults = [
[true, 1],
[7.1, 7],
["7", 7],
["7.5", 7],
[{valueOf() { return 7; }}, 7],
];

convertedResults.forEach(([result, convertedResult]) => {
const calendar = new class extends Temporal.Calendar {
daysInMonth() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.sameValue(instance.daysInMonth, convertedResult, `${typeof result} converted to positive integer ${convertedResult}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.plaindate.prototype.daysinweek
description: Validate result returned from calendar daysInWeek() method
features: [Temporal]
---*/

const badResults = [
[undefined, RangeError],
[null, RangeError],
[false, RangeError],
[Infinity, RangeError],
[-Infinity, RangeError],
[NaN, RangeError],
[-7, RangeError],
[-0.1, RangeError],
["string", RangeError],
[Symbol("foo"), TypeError],
[7n, TypeError],
[{}, RangeError],
];

badResults.forEach(([result, error]) => {
const calendar = new class extends Temporal.Calendar {
daysInWeek() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.throws(error, () => instance.daysInWeek, `${typeof result} not converted to positive integer`);
});

const convertedResults = [
[true, 1],
[7.1, 7],
["7", 7],
["7.5", 7],
[{valueOf() { return 7; }}, 7],
];

convertedResults.forEach(([result, convertedResult]) => {
const calendar = new class extends Temporal.Calendar {
daysInWeek() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.sameValue(instance.daysInWeek, convertedResult, `${typeof result} converted to positive integer ${convertedResult}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.plaindate.prototype.daysinyear
description: Validate result returned from calendar daysInYear() method
features: [Temporal]
---*/

const badResults = [
[undefined, RangeError],
[null, RangeError],
[false, RangeError],
[Infinity, RangeError],
[-Infinity, RangeError],
[NaN, RangeError],
[-7, RangeError],
[-0.1, RangeError],
["string", RangeError],
[Symbol("foo"), TypeError],
[7n, TypeError],
[{}, RangeError],
];

badResults.forEach(([result, error]) => {
const calendar = new class extends Temporal.Calendar {
daysInYear() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.throws(error, () => instance.daysInYear, `${typeof result} not converted to positive integer`);
});

const convertedResults = [
[true, 1],
[7.1, 7],
["7", 7],
["7.5", 7],
[{valueOf() { return 7; }}, 7],
];

convertedResults.forEach(([result, convertedResult]) => {
const calendar = new class extends Temporal.Calendar {
daysInYear() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.sameValue(instance.daysInYear, convertedResult, `${typeof result} converted to positive integer ${convertedResult}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.plaindate.prototype.inleapyear
description: Validate result returned from calendar inLeapYear() method
features: [Temporal]
---*/

const convertedResults = [
[undefined, false],
[null, false],
[true, true],
[false, false],
[0, false],
[-0, false],
[42, true],
[7.1, true],
[NaN, false],
[Infinity, true],
[-Infinity, true],
["", false],
["a string", true],
["0", true],
[Symbol("foo"), true],
[0n, false],
[42n, true],
[{}, true],
[{valueOf() { return false; }}, true],
];

convertedResults.forEach(([result, convertedResult]) => {
const calendar = new class extends Temporal.Calendar {
inLeapYear() {
return result;
}
}("iso8601");
const instance = new Temporal.PlainDate(1981, 12, 15, calendar);
assert.sameValue(instance.inLeapYear, convertedResult, `${typeof result} converted to boolean ${convertedResult}`);
});
Loading

0 comments on commit 274a2a0

Please sign in to comment.