Temporal: Port Demitasse tests for `PlainDateTime`'s `toString`

This commit is contained in:
Jesse Alama 2022-04-25 11:27:42 +02:00 committed by Ms2ger
parent 28b31c0bf1
commit dcd25e616d
7 changed files with 131 additions and 1 deletions

View File

@ -0,0 +1,16 @@
// 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.prototype.tostring
description: Show ISO calendar if calendar name is "always"
features: [Temporal]
---*/
const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
assert.sameValue(
dt.toString({ calendarName: "always" }),
"1976-11-18T15:23:00[u-ca=iso8601]",
"shows ISO calendar if calendarName = always"
);

View File

@ -0,0 +1,35 @@
// 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.prototype.tostring
description: Possibly display calendar when calendarName is "auto"
features: [Temporal]
---*/
const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
const customCal = {
toString() { return "bogus"; }
};
const fakeISO8601Cal = {
toString() { return "iso8601"; }
};
const expected = "1976-11-18T15:23:00";
assert.sameValue(dt.toString(), expected, "default is calendar = auto (zero arguments)");
assert.sameValue(dt.toString({ calendarName: "auto" }), expected, "shows only non-ISO calendar if calendarName = auto");
assert.sameValue(
dt.withCalendar(fakeISO8601Cal).toString({ calendarName: "auto" }),
expected,
"Don't show ISO calendar even if calendarName = auto"
);
const dt2 = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 0, 0, 0, 0, customCal);
assert.sameValue(
dt2.toString({ calendarName: "auto" }),
"1976-11-18T15:23:00[u-ca=bogus]",
"Don't show calendar if calendarName = auto & PlainDateTime has non-ISO calendar"
);

View File

@ -15,4 +15,11 @@ features: [Temporal]
---*/
const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321);
assert.throws(RangeError, () => datetime.toString({ calendarName: "other string" }));
const invalidCals = ["other string", "ALWAYS", "sometimes", "auto\0"];
invalidCals.forEach((cal) => {
assert.throws(
RangeError,
() => datetime.toString({ calendarName: cal }),
`invalid calendar (${cal})`);
});

View File

@ -0,0 +1,26 @@
// 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.prototype.tostring
description: Do not show calendar if calendar name option is "never"
features: [Temporal]
---*/
const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
const cal = {
toString() { return "bogus"; }
};
const expected = "1976-11-18T15:23:00";
assert.sameValue(
dt.toString({ calendarName: "never" }),
expected,
"Do not show calendar if calendarName = never"
);
assert.sameValue(
dt.withCalendar(cal).toString({ calendarName: "never" }),
expected,
"Do not show calendar when calendarName = never, even if non-ISO calendar is used"
);

View File

@ -0,0 +1,16 @@
// 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.prototype.tostring
description: Show calendar when calendarName is "always"
features: [Temporal]
---*/
const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 0, 0, 0, 0, "gregory");
assert.sameValue(
dt.toString({ calendarName: "always" }),
"1976-11-18T15:23:00[u-ca=gregory]",
"shows non-ISO calendar if calendarName = always"
);

View File

@ -0,0 +1,14 @@
// 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.prototype.tostring
description: Possibly display calendar when calendarName is "auto"
features: [Temporal]
---*/
const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 0, 0, 0, 0, "gregory");
const expected = "1976-11-18T15:23:00[u-ca=gregory]";
assert.sameValue(dt.toString(), expected, "shows non-ISO calendar by default (no arguments)");
assert.sameValue(dt.toString({ calendarName: "auto" }), expected, "shows only non-ISO calendar if calendarName = auto");

View File

@ -0,0 +1,16 @@
// 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.prototype.tostring
description: Do not show calendar (even non-ISO calendars) if calendarName = "never"
features: [Temporal]
---*/
const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
assert.sameValue(
dt.withCalendar("gregory").toString({ calendarName: "never" }),
"1976-11-18T15:23:00",
"omits non-ISO calendar if calendarName = never"
);