From b073c48b4963149ea07fb6c5fdfb174d74921eef Mon Sep 17 00:00:00 2001 From: Frank Yung-Fong Tang <41213225+FrankYFTang@users.noreply.github.com> Date: Tue, 8 Oct 2019 11:44:41 -0700 Subject: [PATCH] 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 --- .../formatRangeToParts/pattern-on-calendar.js | 53 +++++++++++++++++++ .../formatToParts/pattern-on-calendar.js | 52 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 test/intl402/DateTimeFormat/prototype/formatRangeToParts/pattern-on-calendar.js create mode 100644 test/intl402/DateTimeFormat/prototype/formatToParts/pattern-on-calendar.js diff --git a/test/intl402/DateTimeFormat/prototype/formatRangeToParts/pattern-on-calendar.js b/test/intl402/DateTimeFormat/prototype/formatRangeToParts/pattern-on-calendar.js new file mode 100644 index 0000000000..208f6f5e73 --- /dev/null +++ b/test/intl402/DateTimeFormat/prototype/formatRangeToParts/pattern-on-calendar.js @@ -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); diff --git a/test/intl402/DateTimeFormat/prototype/formatToParts/pattern-on-calendar.js b/test/intl402/DateTimeFormat/prototype/formatToParts/pattern-on-calendar.js new file mode 100644 index 0000000000..2de954ec08 --- /dev/null +++ b/test/intl402/DateTimeFormat/prototype/formatToParts/pattern-on-calendar.js @@ -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);