Add test to ensure Intl.DateTimeFormat choose pattern by calendar (#2381)

* Add test for different pattern based on calendar

* Add test for formatRangeToParts

* remove  debug print

* fix typo

* fix typo

* address review feedback

* address review feedback

* change the map and use string template

* rewrite maps and use string template
This commit is contained in:
Frank Yung-Fong Tang 2019-10-08 11:44:41 -07:00 committed by Leo Balter
parent 8f6369d1c4
commit b073c48b49
2 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,53 @@
// Copyright 2019 Google Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: Checks the DateTimeFormat choose different patterns based
on calendar.
locale: [en]
---*/
let calendars = [
"buddhist",
"chinese",
"coptic",
"dangi",
"ethiopic",
"ethioaa",
"gregory",
"hebrew",
"indian",
"islamic",
"islamic-civil",
"islamic-rgsa",
"islamic-tbla",
"islamic-umalqura",
"japanese",
"persian",
"roc"
];
let date1 = new Date(2017, 3, 12);
let date2 = new Date();
// serialize parts to a string by considering only the type and literal.
function serializeTypesAndLiteral(parts) {
let types = parts.map(part => {
if (part.type == "literal") {
return `${part.type}(${part.value})`;
}
return part.type;
});
return types.join(":");
}
let df = new Intl.DateTimeFormat("en");
let base = serializeTypesAndLiteral(df.formatRangeToParts(date1, date2));
const foundDifferentPattern = calendars.some(function(calendar) {
let cdf = new Intl.DateTimeFormat("en-u-ca-" + calendar);
return base != serializeTypesAndLiteral(cdf.formatRangeToParts(date1, date2));
});
// Expect at least some calendar use different pattern.
assert.sameValue(foundDifferentPattern, true);

View File

@ -0,0 +1,52 @@
// Copyright 2019 Google Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: Checks the DateTimeFormat choose different patterns based
on calendar.
locale: [en]
---*/
let calendars = [
"buddhist",
"chinese",
"coptic",
"dangi",
"ethiopic",
"ethioaa",
"gregory",
"hebrew",
"indian",
"islamic",
"islamic-civil",
"islamic-rgsa",
"islamic-tbla",
"islamic-umalqura",
"japanese",
"persian",
"roc"
];
let date = new Date();
// serialize parts to a string by considering only the type and literal.
function serializeTypesAndLiteral(parts) {
let types = parts.map(part => {
if (part.type == "literal") {
return `${part.type}(${part.value})`;
}
return part.type;
});
return types.join(":");
}
let df = new Intl.DateTimeFormat("en");
let base = serializeTypesAndLiteral(df.formatToParts(date));
const foundDifferentPattern = calendars.some(function(calendar) {
let cdf = new Intl.DateTimeFormat("en-u-ca-" + calendar);
return base != serializeTypesAndLiteral(cdf.formatToParts(date));
});
// Expect at least some calendar use different pattern.
assert.sameValue(foundDifferentPattern, true);