Skip to content

Commit

Permalink
Temporal: Adjust and expand tests for observable calls to ToString(ca…
Browse files Browse the repository at this point in the history
…lendar)

This implements the normative change in
tc39/proposal-temporal#2269 which reached
consensus at the July 2022 TC39 meeting.

There was already a test for PlainDate for this topic, which needs to be
adjusted to accommodate the normative change. Tests for PlainDateTime and
ZonedDateTime did not yet exist, so add new ones based on the PlainDate
test.
  • Loading branch information
ptomato authored and Ms2ger committed Aug 3, 2022
1 parent 1587af3 commit 10a5c4f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
Expand Up @@ -3,7 +3,7 @@

/*---
esid: sec-temporal.plaindate.protoype.tostring
description: Should always call 'toString' on the calendar once.
description: Should call 'toString' on the calendar once unless calendarName == 'never'.
features: [Temporal]
---*/

Expand All @@ -16,13 +16,13 @@ const customCalendar = {
};
const date = new Temporal.PlainDate(2000, 5, 2, customCalendar);
[
["always", "2000-05-02[u-ca=custom]"],
["auto", "2000-05-02[u-ca=custom]"],
["never", "2000-05-02"],
[undefined, "2000-05-02[u-ca=custom]"],
].forEach(([calendarName, expected]) => {
["always", "2000-05-02[u-ca=custom]", 1],
["auto", "2000-05-02[u-ca=custom]", 1],
["never", "2000-05-02", 0],
[undefined, "2000-05-02[u-ca=custom]", 1],
].forEach(([calendarName, expectedResult, expectedCalls]) => {
calls = 0;
const result = date.toString({ calendarName });
assert.sameValue(result, expected, `calendarName = ${calendarName}: expected ${expected}`);
assert.sameValue(calls, 1, `calendarName = ${calendarName}: expected one call to 'toString'`);
assert.sameValue(result, expectedResult, `calendarName = ${calendarName}: expected ${expectedResult}`);
assert.sameValue(calls, expectedCalls, `calendarName = ${calendarName}: expected ${expectedCalls} call(s) to 'toString'`);
});
@@ -0,0 +1,28 @@
// 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.plaindatetime.protoype.tostring
description: Should call 'toString' on the calendar once unless calendarName == 'never'.
features: [Temporal]
---*/

let calls;
const customCalendar = {
toString() {
++calls;
return "custom";
}
};
const date = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321, customCalendar);
[
["always", "2000-05-02T12:34:56.987654321[u-ca=custom]", 1],
["auto", "2000-05-02T12:34:56.987654321[u-ca=custom]", 1],
["never", "2000-05-02T12:34:56.987654321", 0],
[undefined, "2000-05-02T12:34:56.987654321[u-ca=custom]", 1],
].forEach(([calendarName, expectedResult, expectedCalls]) => {
calls = 0;
const result = date.toString({ calendarName });
assert.sameValue(result, expectedResult, `calendarName = ${calendarName}: expected ${expectedResult}`);
assert.sameValue(calls, expectedCalls, `calendarName = ${calendarName}: expected ${expectedCalls} call(s) to 'toString'`);
});
@@ -0,0 +1,28 @@
// 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.zoneddatetime.protoype.tostring
description: Should call 'toString' on the calendar once unless calendarName == 'never'.
features: [Temporal]
---*/

let calls;
const customCalendar = {
toString() {
++calls;
return "custom";
}
};
const date = new Temporal.ZonedDateTime(3661_987_654_321n, "UTC", customCalendar);
[
["always", "1970-01-01T01:01:01.987654321+00:00[UTC][u-ca=custom]", 1],
["auto", "1970-01-01T01:01:01.987654321+00:00[UTC][u-ca=custom]", 1],
["never", "1970-01-01T01:01:01.987654321+00:00[UTC]", 0],
[undefined, "1970-01-01T01:01:01.987654321+00:00[UTC][u-ca=custom]", 1],
].forEach(([calendarName, expectedResult, expectedCalls]) => {
calls = 0;
const result = date.toString({ calendarName });
assert.sameValue(result, expectedResult, `calendarName = ${calendarName}: expected ${expectedResult}`);
assert.sameValue(calls, expectedCalls, `calendarName = ${calendarName}: expected ${expectedCalls} call(s) to 'toString'`);
});

0 comments on commit 10a5c4f

Please sign in to comment.