Skip to content

Commit

Permalink
Add tests for PlainMonthDay#toString().
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger authored and ptomato committed Sep 14, 2021
1 parent eaadc5d commit efb46b7
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plainmonthday.protoype.tostring
description: If calendarName is "always", the calendar ID should be included.
features: [Temporal]
---*/

const tests = [
[[], "05-02[u-ca=iso8601]"],
[[{ toString() { return "custom"; } }], "1972-05-02[u-ca=custom]"],
[[{ toString() { return "iso8601"; } }], "05-02[u-ca=iso8601]"],
[[{ toString() { return "ISO8601"; } }], "1972-05-02[u-ca=ISO8601]"],
[[{ toString() { return "\u0131so8601"; } }], "1972-05-02[u-ca=\u0131so8601]"], // dotless i
];

for (const [args, expected] of tests) {
const monthday = new Temporal.PlainMonthDay(5, 2, ...args);
const result = monthday.toString({ calendarName: "always" });
assert.sameValue(result, expected);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plainmonthday.protoype.tostring
description: If calendarName is "auto", "iso8601" should be omitted.
features: [Temporal]
---*/

const tests = [
[[], "05-02"],
[[{ toString() { return "custom"; } }], "1972-05-02[u-ca=custom]"],
[[{ toString() { return "iso8601"; } }], "05-02"],
[[{ toString() { return "ISO8601"; } }], "1972-05-02[u-ca=ISO8601]"],
[[{ toString() { return "\u0131so8601"; } }], "1972-05-02[u-ca=\u0131so8601]"], // dotless i
];

for (const [args, expected] of tests) {
const monthday = new Temporal.PlainMonthDay(5, 2, ...args);
const result = monthday.toString({ calendarName: "auto" });
assert.sameValue(result, expected);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plainmonthday.protoype.tostring
description: If calendarName is "never", the calendar ID should be omitted.
features: [Temporal]
---*/

const tests = [
[[], "05-02"],
[[{ toString() { return "custom"; } }], "1972-05-02"],
[[{ toString() { return "iso8601"; } }], "05-02"],
[[{ toString() { return "ISO8601"; } }], "1972-05-02"],
[[{ toString() { return "\u0131so8601"; } }], "1972-05-02"], // dotless i
];

for (const [args, expected] of tests) {
const monthday = new Temporal.PlainMonthDay(5, 2, ...args);
const result = monthday.toString({ calendarName: "never" });
assert.sameValue(result, expected);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ info: |
features: [Temporal]
---*/

const calendar = {
toString() { return "custom"; }
};
const monthday1 = new Temporal.PlainMonthDay(5, 2);
const monthday2 = new Temporal.PlainMonthDay(5, 2, calendar);
const tests = [
[[], "05-02"],
[[{ toString() { return "custom"; } }], "1972-05-02[u-ca=custom]"],
[[{ toString() { return "iso8601"; } }], "05-02"],
[[{ toString() { return "ISO8601"; } }], "1972-05-02[u-ca=ISO8601]"],
[[{ toString() { return "\u0131so8601"; } }], "1972-05-02[u-ca=\u0131so8601]"], // dotless i
];

[
[monthday1, "05-02"],
[monthday2, "1972-05-02[u-ca=custom]"],
].forEach(([monthday, expected]) => {
for (const [args, expected] of tests) {
const monthday = new Temporal.PlainMonthDay(5, 2, ...args);
const explicit = monthday.toString({ calendarName: undefined });
assert.sameValue(explicit, expected, "default calendarName option is auto");

const implicit = monthday.toString({});
assert.sameValue(implicit, expected, "default calendarName option is auto");
});
}
20 changes: 10 additions & 10 deletions polyfill/test/PlainMonthDay/prototype/toString/options-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ esid: sec-temporal.plainmonthday.prototype.tostring
features: [Temporal]
---*/

const calendar = {
toString() { return "custom"; }
};
const monthday1 = new Temporal.PlainMonthDay(5, 2);
const monthday2 = new Temporal.PlainMonthDay(5, 2, calendar);
const tests = [
[[], "05-02"],
[[{ toString() { return "custom"; } }], "1972-05-02[u-ca=custom]"],
[[{ toString() { return "iso8601"; } }], "05-02"],
[[{ toString() { return "ISO8601"; } }], "1972-05-02[u-ca=ISO8601]"],
[[{ toString() { return "\u0131so8601"; } }], "1972-05-02[u-ca=\u0131so8601]"], // dotless i
];

[
[monthday1, "05-02"],
[monthday2, "1972-05-02[u-ca=custom]"],
].forEach(([monthday, expected]) => {
for (const [args, expected] of tests) {
const monthday = new Temporal.PlainMonthDay(5, 2, ...args);
const explicit = monthday.toString(undefined);
assert.sameValue(explicit, expected, "default calendarName option is auto");

const implicit = monthday.toString();
assert.sameValue(implicit, expected, "default calendarName option is auto");
});
}
19 changes: 0 additions & 19 deletions polyfill/test/plainmonthday.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,25 +179,6 @@ describe('MonthDay', () => {
equal(`${leapDay.toPlainDate({ year: 2019 }, { overflow: 'constrain' })}`, '2019-02-28');
});
});
describe('MonthDay.toString()', () => {
const md1 = PlainMonthDay.from('11-18');
const md2 = PlainMonthDay.from({ monthCode: 'M11', day: 18, calendar: 'gregory' });
it('shows only non-ISO calendar if calendarName = auto', () => {
equal(md1.toString({ calendarName: 'auto' }), '11-18');
equal(md2.toString({ calendarName: 'auto' }), '1972-11-18[u-ca=gregory]');
});
it('shows ISO calendar if calendarName = always', () => {
equal(md1.toString({ calendarName: 'always' }), '11-18[u-ca=iso8601]');
});
it('omits non-ISO calendar, but not year, if calendarName = never', () => {
equal(md1.toString({ calendarName: 'never' }), '11-18');
equal(md2.toString({ calendarName: 'never' }), '1972-11-18');
});
it('default is calendar = auto', () => {
equal(md1.toString(), '11-18');
equal(md2.toString(), '1972-11-18[u-ca=gregory]');
});
});
});

import { normalize } from 'path';
Expand Down

0 comments on commit efb46b7

Please sign in to comment.