Skip to content

Commit

Permalink
Initial tests for Temporal PR tc39#2574
Browse files Browse the repository at this point in the history
Edits Temporal tests to account for changes in
tc39/proposal-temporal#2574.

This PR stops coercing non-string primitive inputs to strings
in Temporal methods, to avoid cases where numbers
are coerced to syntactically valid but often unexpected
string results.
  • Loading branch information
justingrant committed Jul 17, 2023
1 parent 016e4bf commit 3e95111
Show file tree
Hide file tree
Showing 321 changed files with 2,059 additions and 1,745 deletions.
30 changes: 30 additions & 0 deletions test/built-ins/Temporal/Calendar/argument-wrong-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar
description: RangeError thrown when constructor invoked with the wrong type
features: [Temporal]
---*/

const tests = [
[null, "null"],
[true, "boolean"],
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[Symbol(), "symbol"],
[{}, "object not implementing any protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
[new Temporal.TimeZone("UTC"), "time zone instance"],
[Temporal.ZonedDateTime.from("2020-01-01T00:00Z[UTC]"), "ZonedDateTime instance"],
];

for (const [arg, description] of tests) {
assert.throws(
typeof (arg) === "string" ? RangeError : TypeError,
() => new Temporal.Calendar(arg),
`${description} is not accepted by this constructor`
);
}
12 changes: 4 additions & 8 deletions test/built-ins/Temporal/Calendar/from/calendar-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@

/*---
esid: sec-temporal.calendar.from
description: A number is converted to a string, then to Temporal.Calendar
description: A number is not allowed to be a calendar
features: [Temporal]
---*/

const arg = 19761118;

const result = Temporal.Calendar.from(arg);
assert.sameValue(result.id, "iso8601", "19761118 is a valid ISO string for Calendar");

const numbers = [
1,
-19761118,
19761118,
1234567890,
];

for (const arg of numbers) {
assert.throws(
RangeError,
TypeError,
() => Temporal.Calendar.from(arg),
`Number ${arg} does not convert to a valid ISO string for Calendar`
"A number is not a valid ISO string for Calendar"
);
}
10 changes: 7 additions & 3 deletions test/built-ins/Temporal/Calendar/from/calendar-wrong-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ description: >
features: [BigInt, Symbol, Temporal]
---*/

const rangeErrorTests = [
const primitiveTests = [
[null, "null"],
[true, "boolean"],
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
];

for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => Temporal.Calendar.from(arg), `${description} does not convert to a valid ISO string`);
for (const [arg, description] of primitiveTests) {
assert.throws(
typeof arg === 'string' ? RangeError : TypeError,
() => Temporal.Calendar.from(arg),
`${description} does not convert to a valid ISO string`
);
}

const typeErrorTests = [
Expand Down
4 changes: 2 additions & 2 deletions test/built-ins/Temporal/Calendar/missing-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ description: RangeError thrown when constructor invoked with no argument
features: [Temporal]
---*/

assert.throws(RangeError, () => new Temporal.Calendar());
assert.throws(RangeError, () => new Temporal.Calendar(undefined));
assert.throws(TypeError, () => new Temporal.Calendar());
assert.throws(TypeError, () => new Temporal.Calendar(undefined));
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@

/*---
esid: sec-temporal.calendar.prototype.dateadd
description: A number is converted to a string, then to Temporal.PlainDate
includes: [temporalHelpers.js]
description: A number cannot be used in place of a Temporal.PlainDate
features: [Temporal]
---*/

const instance = new Temporal.Calendar("iso8601");

const arg = 19761118;

const result = instance.dateAdd(arg, new Temporal.Duration());
TemporalHelpers.assertPlainDate(result, 1976, 11, "M11", 18, "19761118 is a valid ISO string for PlainDate");

const numbers = [
1,
19761118,
-19761118,
1234567890,
];

for (const arg of numbers) {
assert.throws(
RangeError,
TypeError,
() => instance.dateAdd(arg, new Temporal.Duration()),
`Number ${arg} does not convert to a valid ISO string for PlainDate`
'Numbers cannot be used in place of an ISO string for PlainDate'
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,23 @@

/*---
esid: sec-temporal.calendar.prototype.dateadd
description: A number as calendar in a property bag is converted to a string, then to a calendar
includes: [temporalHelpers.js]
description: A number as calendar in a property bag is not accepted
features: [Temporal]
---*/

const instance = new Temporal.Calendar("iso8601");

const calendar = 19970327;

const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
const result = instance.dateAdd(arg, new Temporal.Duration());
TemporalHelpers.assertPlainDate(result, 1976, 11, "M11", 18, "19970327 is a valid ISO string for calendar");
const instance = new Temporal.PlainDate(1976, 11, 18);

const numbers = [
1,
19970327,
-19970327,
1234567890,
];

for (const calendar of numbers) {
const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
assert.throws(
RangeError,
TypeError,
() => instance.dateAdd(arg, new Temporal.Duration()),
`Number ${calendar} does not convert to a valid ISO string for calendar`
"Numbers cannot be used as a calendar"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ features: [BigInt, Symbol, Temporal]
const timeZone = new Temporal.TimeZone("UTC");
const instance = new Temporal.Calendar("iso8601");

const rangeErrorTests = [
const primitiveTests = [
[null, "null"],
[true, "boolean"],
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
];

for (const [calendar, description] of rangeErrorTests) {
for (const [calendar, description] of primitiveTests) {
const arg = { year: 2019, monthCode: "M11", day: 1, calendar };
assert.throws(RangeError, () => instance.dateAdd(arg, new Temporal.Duration()), `${description} does not convert to a valid ISO string`);
assert.throws(
typeof calendar === 'string' ? RangeError : TypeError,
() => instance.dateAdd(arg, new Temporal.Duration()),
`${description} does not convert to a valid ISO string`
);
}

const typeErrorTests = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ features: [BigInt, Symbol, Temporal]

const instance = new Temporal.Calendar("iso8601");

const rangeErrorTests = [
const primitiveTests = [
[undefined, "undefined"],
[null, "null"],
[true, "boolean"],
Expand All @@ -20,8 +20,12 @@ const rangeErrorTests = [
[1n, "bigint"],
];

for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => instance.dateAdd(arg, new Temporal.Duration()), `${description} does not convert to a valid ISO string`);
for (const [arg, description] of primitiveTests) {
assert.throws(
typeof arg === 'string' ? RangeError : TypeError,
() => instance.dateAdd(arg, new Temporal.Duration()),
`${description} does not convert to a valid ISO string`
);
}

const typeErrorTests = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,28 @@

/*---
esid: sec-temporal.calendar.prototype.dateuntil
description: A number is converted to a string, then to Temporal.PlainDate
includes: [temporalHelpers.js]
description: A number cannot be used in place of a Temporal.PlainDate
features: [Temporal]
---*/

const instance = new Temporal.Calendar("iso8601");

const arg = 19761118;

const result1 = instance.dateUntil(arg, new Temporal.PlainDate(1976, 11, 19));
TemporalHelpers.assertDuration(result1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "19761118 is a valid ISO string for PlainDate (first argument)");
const result2 = instance.dateUntil(new Temporal.PlainDate(1976, 11, 19), arg);
TemporalHelpers.assertDuration(result2, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, "19761118 is a valid ISO string for PlainDate (second argument)");

const numbers = [
1,
19761118,
-19761118,
1234567890,
];

for (const arg of numbers) {
assert.throws(
RangeError,
TypeError,
() => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 18)),
`Number ${arg} does not convert to a valid ISO string for PlainDate (first argument)`
"A number is not a valid ISO string for PlainDate (first argument)"
);
assert.throws(
RangeError,
TypeError,
() => instance.dateUntil(new Temporal.PlainDate(1977, 11, 18), arg),
`Number ${arg} does not convert to a valid ISO string for PlainDate (second argument)`
"A number is not a valid ISO string for PlainDate (second argument)"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,29 @@

/*---
esid: sec-temporal.calendar.prototype.dateuntil
description: A number as calendar in a property bag is converted to a string, then to a calendar
includes: [temporalHelpers.js]
description: A number as calendar in a property bag is not accepted
features: [Temporal]
---*/

const instance = new Temporal.Calendar("iso8601");

const calendar = 19970327;

const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
const result1 = instance.dateUntil(arg, new Temporal.PlainDate(1976, 11, 19));
TemporalHelpers.assertDuration(result1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "19970327 is a valid ISO string for calendar (first argument)");
const result2 = instance.dateUntil(new Temporal.PlainDate(1976, 11, 19), arg);
TemporalHelpers.assertDuration(result2, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, "19970327 is a valid ISO string for calendar (second argument)");

const numbers = [
1,
19970327,
-19970327,
1234567890,
];

for (const calendar of numbers) {
const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
assert.throws(
RangeError,
TypeError,
() => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)),
`Number ${calendar} does not convert to a valid ISO string for calendar (first argument)`
"A number is not a valid ISO string for calendar (first argument)"
);
assert.throws(
RangeError,
TypeError,
() => instance.dateUntil(new Temporal.PlainDate(1977, 11, 19), arg),
`Number ${calendar} does not convert to a valid ISO string for calendar (second argument)`
"A number is not a valid ISO string for calendar (second argument)"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ features: [BigInt, Symbol, Temporal]
const timeZone = new Temporal.TimeZone("UTC");
const instance = new Temporal.Calendar("iso8601");

const rangeErrorTests = [
const primitiveTests = [
[null, "null"],
[true, "boolean"],
["", "empty string"],
[1, "number that doesn't convert to a valid ISO string"],
[1n, "bigint"],
];

for (const [calendar, description] of rangeErrorTests) {
for (const [calendar, description] of primitiveTests) {
const arg = { year: 2019, monthCode: "M11", day: 1, calendar };
assert.throws(RangeError, () => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)), `${description} does not convert to a valid ISO string (first argument)`);
assert.throws(RangeError, () => instance.dateUntil(new Temporal.PlainDate(1977, 11, 19), arg), `${description} does not convert to a valid ISO string (second argument)`);
assert.throws(
typeof calendar === "string" ? RangeError : TypeError,
() => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)),
`${description} does not convert to a valid ISO string (first argument)`
);
assert.throws(
typeof calendar === "string" ? RangeError : TypeError,
() => instance.dateUntil(new Temporal.PlainDate(1977, 11, 19), arg),
`${description} does not convert to a valid ISO string (second argument)`
);
}

const typeErrorTests = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ features: [BigInt, Symbol, Temporal]
const timeZone = new Temporal.TimeZone("UTC");
const instance = new Temporal.Calendar("iso8601");

const rangeErrorTests = [
const primitiveTests = [
[undefined, "undefined"],
[null, "null"],
[true, "boolean"],
Expand All @@ -21,9 +21,17 @@ const rangeErrorTests = [
[1n, "bigint"],
];

for (const [arg, description] of rangeErrorTests) {
assert.throws(RangeError, () => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)), `${description} does not convert to a valid ISO string (first argument)`);
assert.throws(RangeError, () => instance.dateUntil(new Temporal.PlainDate(1977, 11, 19), arg), `${description} does not convert to a valid ISO string (first argument)`);
for (const [arg, description] of primitiveTests) {
assert.throws(
typeof arg === 'string' ? RangeError : TypeError,
() => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)),
`${description} does not convert to a valid ISO string (first argument)`
);
assert.throws(
typeof arg === 'string' ? RangeError : TypeError,
() => instance.dateUntil(new Temporal.PlainDate(1977, 11, 19), arg),
`${description} does not convert to a valid ISO string (second argument)`
);
}

const typeErrorTests = [
Expand All @@ -34,6 +42,6 @@ const typeErrorTests = [
];

for (const [arg, description] of typeErrorTests) {
assert.throws(TypeError, () => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)), `${description} is not a valid property bag and does not convert to a string (second argument)`);
assert.throws(TypeError, () => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)), `${description} is not a valid property bag and does not convert to a string (first argument)`);
assert.throws(TypeError, () => instance.dateUntil(new Temporal.PlainDate(1977, 11, 19), arg), `${description} is not a valid property bag and does not convert to a string (second argument)`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@

/*---
esid: sec-temporal.calendar.prototype.day
description: A number is converted to a string, then to Temporal.PlainDate
description: A number cannot be used in place of a Temporal.PlainDate
features: [Temporal]
---*/

const instance = new Temporal.Calendar("iso8601");

const arg = 19761118;

const result = instance.day(arg);
assert.sameValue(result, 18, "19761118 is a valid ISO string for PlainDate");

const numbers = [
1,
19761118,
-19761118,
1234567890,
];

for (const arg of numbers) {
assert.throws(
RangeError,
TypeError,
() => instance.day(arg),
`Number ${arg} does not convert to a valid ISO string for PlainDate`
'Numbers cannot be used in place of an ISO string for PlainDate'
);
}
Loading

0 comments on commit 3e95111

Please sign in to comment.