michael@0: // Copyright 2012 Mozilla Corporation. All rights reserved. michael@0: // This code is governed by the BSD license found in the LICENSE file. michael@0: michael@0: /** michael@0: * @description Tests that the set of options for the date and time components is processed correctly. michael@0: * @author Norbert Lindenberg michael@0: */ michael@0: michael@0: $INCLUDE("testIntl.js"); michael@0: michael@0: var locales = [[], ["zh-Hans-CN"], ["hi-IN"], ["en-US"], ["id-ID"]]; michael@0: var dates = [new Date(), new Date(0), new Date(Date.parse("1989-11-09T17:57:00Z"))]; michael@0: michael@0: function testWithDateTimeFormat(options, expected) { michael@0: locales.forEach(function (locales) { michael@0: var format = new Intl.DateTimeFormat(locales, options); michael@0: var resolvedOptions = format.resolvedOptions(); michael@0: getDateTimeComponents().forEach(function (component) { michael@0: if (resolvedOptions.hasOwnProperty(component)) { michael@0: if (!expected.hasOwnProperty(component)) { michael@0: $ERROR("Unrequested component " + component + michael@0: " added to expected subset " + JSON.stringify(expected) + michael@0: "; locales " + locales + ", options " + michael@0: (options ? JSON.stringify(options) : options) + "."); michael@0: } michael@0: } else { michael@0: if (expected.hasOwnProperty(component)) { michael@0: $ERROR("Missing component " + component + michael@0: " from expected subset " + JSON.stringify(expected) + michael@0: "; locales " + locales + ", options " + michael@0: (options ? JSON.stringify(options) : options) + "."); michael@0: } michael@0: } michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function testWithToLocale(f, options, expected) { michael@0: // expected can be either one subset or an array of possible subsets michael@0: if (expected.length === undefined) { michael@0: expected = [expected]; michael@0: } michael@0: locales.forEach(function (locales) { michael@0: dates.forEach(function (date) { michael@0: var formatted = Date.prototype[f].call(date, locales, options); michael@0: var expectedStrings = []; michael@0: expected.forEach(function (expected) { michael@0: var referenceFormat = new Intl.DateTimeFormat(locales, expected); michael@0: expectedStrings.push(referenceFormat.format(date)); michael@0: }); michael@0: if (expectedStrings.indexOf(formatted) === -1) { michael@0: $ERROR("Function " + f + " did not return expected string for locales " + michael@0: locales + ", options " + (options? JSON.stringify(options) : options) + michael@0: "; expected " + michael@0: (expectedStrings.length === 1 ? expectedStrings[0] : "one of " + expectedStrings) + michael@0: ", got " + formatted + "."); michael@0: } michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: // any/date: steps 5a, 6a, 7a michael@0: testWithDateTimeFormat(undefined, {year: "numeric", month: "numeric", day: "numeric"}); michael@0: michael@0: // any/date: steps 5a, 6a michael@0: testWithDateTimeFormat({year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); michael@0: michael@0: // any/date: steps 5a, 6a michael@0: testWithDateTimeFormat({hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); michael@0: michael@0: // any/all: steps 5a, 6a, 7a, 8a michael@0: testWithToLocale("toLocaleString", undefined, [ michael@0: // the first one is not guaranteed to be supported; the second one is michael@0: {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, michael@0: {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} michael@0: ]); michael@0: michael@0: // any/all: steps 5a, 6a michael@0: testWithToLocale("toLocaleString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); michael@0: michael@0: // any/all: steps 5a, 6a michael@0: testWithToLocale("toLocaleString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); michael@0: michael@0: // date/date: steps 5a, 7a michael@0: testWithToLocale("toLocaleDateString", undefined, {year: "numeric", month: "numeric", day: "numeric"}); michael@0: michael@0: // date/date: steps 5a michael@0: testWithToLocale("toLocaleDateString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); michael@0: michael@0: // date/date: steps 5a, 7a michael@0: testWithToLocale("toLocaleDateString", {hour: "numeric", minute: "numeric", second: "numeric"}, [ michael@0: // the first one is not guaranteed to be supported; the second one is michael@0: {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, michael@0: {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} michael@0: ]); michael@0: michael@0: // time/time: steps 6a, 8a michael@0: testWithToLocale("toLocaleTimeString", undefined, {hour: "numeric", minute: "numeric", second: "numeric"}); michael@0: michael@0: // time/time: steps 6a, 8a michael@0: testWithToLocale("toLocaleTimeString", {weekday: "short", year: "numeric", month: "numeric", day: "numeric"}, michael@0: {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}); michael@0: michael@0: // time/time: steps 6a michael@0: testWithToLocale("toLocaleTimeString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); michael@0: michael@0: