Skip to content

Commit 60e4752

Browse files
justingrantptomato
authored andcommitted
Tests for Temporal PR #2574
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.
1 parent 016e4bf commit 60e4752

File tree

321 files changed

+2059
-1745
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

321 files changed

+2059
-1745
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.calendar
6+
description: RangeError thrown when constructor invoked with the wrong type
7+
features: [Temporal]
8+
---*/
9+
10+
const tests = [
11+
[null, "null"],
12+
[true, "boolean"],
13+
["", "empty string"],
14+
[1, "number that doesn't convert to a valid ISO string"],
15+
[19761118, "number that would convert to a valid ISO string in other contexts"],
16+
[1n, "bigint"],
17+
[Symbol(), "symbol"],
18+
[{}, "object not implementing any protocol"],
19+
[new Temporal.Calendar("iso8601"), "calendar instance"],
20+
[new Temporal.TimeZone("UTC"), "time zone instance"],
21+
[Temporal.ZonedDateTime.from("2020-01-01T00:00Z[UTC]"), "ZonedDateTime instance"],
22+
];
23+
24+
for (const [arg, description] of tests) {
25+
assert.throws(
26+
typeof (arg) === "string" ? RangeError : TypeError,
27+
() => new Temporal.Calendar(arg),
28+
`${description} is not accepted by this constructor`
29+
);
30+
}

test/built-ins/Temporal/Calendar/from/calendar-number.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33

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

10-
const arg = 19761118;
11-
12-
const result = Temporal.Calendar.from(arg);
13-
assert.sameValue(result.id, "iso8601", "19761118 is a valid ISO string for Calendar");
14-
1510
const numbers = [
1611
1,
1712
-19761118,
13+
19761118,
1814
1234567890,
1915
];
2016

2117
for (const arg of numbers) {
2218
assert.throws(
23-
RangeError,
19+
TypeError,
2420
() => Temporal.Calendar.from(arg),
25-
`Number ${arg} does not convert to a valid ISO string for Calendar`
21+
"A number is not a valid ISO string for Calendar"
2622
);
2723
}

test/built-ins/Temporal/Calendar/from/calendar-wrong-type.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ description: >
99
features: [BigInt, Symbol, Temporal]
1010
---*/
1111

12-
const rangeErrorTests = [
12+
const primitiveTests = [
1313
[null, "null"],
1414
[true, "boolean"],
1515
["", "empty string"],
1616
[1, "number that doesn't convert to a valid ISO string"],
1717
[1n, "bigint"],
1818
];
1919

20-
for (const [arg, description] of rangeErrorTests) {
21-
assert.throws(RangeError, () => Temporal.Calendar.from(arg), `${description} does not convert to a valid ISO string`);
20+
for (const [arg, description] of primitiveTests) {
21+
assert.throws(
22+
typeof arg === 'string' ? RangeError : TypeError,
23+
() => Temporal.Calendar.from(arg),
24+
`${description} does not convert to a valid ISO string`
25+
);
2226
}
2327

2428
const typeErrorTests = [

test/built-ins/Temporal/Calendar/missing-arguments.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ description: RangeError thrown when constructor invoked with no argument
77
features: [Temporal]
88
---*/
99

10-
assert.throws(RangeError, () => new Temporal.Calendar());
11-
assert.throws(RangeError, () => new Temporal.Calendar(undefined));
10+
assert.throws(TypeError, () => new Temporal.Calendar());
11+
assert.throws(TypeError, () => new Temporal.Calendar(undefined));

test/built-ins/Temporal/Calendar/prototype/dateAdd/argument-number.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@
33

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

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

13-
const arg = 19761118;
14-
15-
const result = instance.dateAdd(arg, new Temporal.Duration());
16-
TemporalHelpers.assertPlainDate(result, 1976, 11, "M11", 18, "19761118 is a valid ISO string for PlainDate");
17-
1812
const numbers = [
1913
1,
14+
19761118,
2015
-19761118,
2116
1234567890,
2217
];
2318

2419
for (const arg of numbers) {
2520
assert.throws(
26-
RangeError,
21+
TypeError,
2722
() => instance.dateAdd(arg, new Temporal.Duration()),
28-
`Number ${arg} does not convert to a valid ISO string for PlainDate`
23+
'Numbers cannot be used in place of an ISO string for PlainDate'
2924
);
3025
}

test/built-ins/Temporal/Calendar/prototype/dateAdd/argument-propertybag-calendar-number.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,23 @@
33

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

11-
const instance = new Temporal.Calendar("iso8601");
12-
13-
const calendar = 19970327;
14-
15-
const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
16-
const result = instance.dateAdd(arg, new Temporal.Duration());
17-
TemporalHelpers.assertPlainDate(result, 1976, 11, "M11", 18, "19970327 is a valid ISO string for calendar");
10+
const instance = new Temporal.PlainDate(1976, 11, 18);
1811

1912
const numbers = [
2013
1,
14+
19970327,
2115
-19970327,
2216
1234567890,
2317
];
24-
2518
for (const calendar of numbers) {
2619
const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
2720
assert.throws(
28-
RangeError,
21+
TypeError,
2922
() => instance.dateAdd(arg, new Temporal.Duration()),
30-
`Number ${calendar} does not convert to a valid ISO string for calendar`
23+
"Numbers cannot be used as a calendar"
3124
);
3225
}

test/built-ins/Temporal/Calendar/prototype/dateAdd/argument-propertybag-calendar-wrong-type.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ features: [BigInt, Symbol, Temporal]
1212
const timeZone = new Temporal.TimeZone("UTC");
1313
const instance = new Temporal.Calendar("iso8601");
1414

15-
const rangeErrorTests = [
15+
const primitiveTests = [
1616
[null, "null"],
1717
[true, "boolean"],
1818
["", "empty string"],
1919
[1, "number that doesn't convert to a valid ISO string"],
2020
[1n, "bigint"],
2121
];
2222

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

2832
const typeErrorTests = [

test/built-ins/Temporal/Calendar/prototype/dateAdd/argument-wrong-type.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ features: [BigInt, Symbol, Temporal]
1111

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

14-
const rangeErrorTests = [
14+
const primitiveTests = [
1515
[undefined, "undefined"],
1616
[null, "null"],
1717
[true, "boolean"],
@@ -20,8 +20,12 @@ const rangeErrorTests = [
2020
[1n, "bigint"],
2121
];
2222

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

2731
const typeErrorTests = [

test/built-ins/Temporal/Calendar/prototype/dateUntil/argument-number.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,28 @@
33

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

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

13-
const arg = 19761118;
14-
15-
const result1 = instance.dateUntil(arg, new Temporal.PlainDate(1976, 11, 19));
16-
TemporalHelpers.assertDuration(result1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "19761118 is a valid ISO string for PlainDate (first argument)");
17-
const result2 = instance.dateUntil(new Temporal.PlainDate(1976, 11, 19), arg);
18-
TemporalHelpers.assertDuration(result2, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, "19761118 is a valid ISO string for PlainDate (second argument)");
19-
2012
const numbers = [
2113
1,
14+
19761118,
2215
-19761118,
2316
1234567890,
2417
];
2518

2619
for (const arg of numbers) {
2720
assert.throws(
28-
RangeError,
21+
TypeError,
2922
() => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 18)),
30-
`Number ${arg} does not convert to a valid ISO string for PlainDate (first argument)`
23+
"A number is not a valid ISO string for PlainDate (first argument)"
3124
);
3225
assert.throws(
33-
RangeError,
26+
TypeError,
3427
() => instance.dateUntil(new Temporal.PlainDate(1977, 11, 18), arg),
35-
`Number ${arg} does not convert to a valid ISO string for PlainDate (second argument)`
28+
"A number is not a valid ISO string for PlainDate (second argument)"
3629
);
3730
}

test/built-ins/Temporal/Calendar/prototype/dateUntil/argument-propertybag-calendar-number.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,29 @@
33

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

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

13-
const calendar = 19970327;
14-
15-
const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
16-
const result1 = instance.dateUntil(arg, new Temporal.PlainDate(1976, 11, 19));
17-
TemporalHelpers.assertDuration(result1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "19970327 is a valid ISO string for calendar (first argument)");
18-
const result2 = instance.dateUntil(new Temporal.PlainDate(1976, 11, 19), arg);
19-
TemporalHelpers.assertDuration(result2, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, "19970327 is a valid ISO string for calendar (second argument)");
20-
2112
const numbers = [
2213
1,
14+
19970327,
2315
-19970327,
2416
1234567890,
2517
];
2618

2719
for (const calendar of numbers) {
2820
const arg = { year: 1976, monthCode: "M11", day: 18, calendar };
2921
assert.throws(
30-
RangeError,
22+
TypeError,
3123
() => instance.dateUntil(arg, new Temporal.PlainDate(1977, 11, 19)),
32-
`Number ${calendar} does not convert to a valid ISO string for calendar (first argument)`
24+
"A number is not a valid ISO string for calendar (first argument)"
3325
);
3426
assert.throws(
35-
RangeError,
27+
TypeError,
3628
() => instance.dateUntil(new Temporal.PlainDate(1977, 11, 19), arg),
37-
`Number ${calendar} does not convert to a valid ISO string for calendar (second argument)`
29+
"A number is not a valid ISO string for calendar (second argument)"
3830
);
3931
}

0 commit comments

Comments
 (0)