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 Date.prototype.toLocaleString & Co. produces the same results as Intl.DateTimeFormat. michael@0: * @author Norbert Lindenberg michael@0: */ michael@0: michael@0: $INCLUDE("testIntl.js"); michael@0: michael@0: var functions = { michael@0: toLocaleString: [Date.prototype.toLocaleString, michael@0: {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}], michael@0: toLocaleDateString: [Date.prototype.toLocaleDateString, michael@0: {year: "numeric", month: "numeric", day: "numeric"}], michael@0: toLocaleTimeString: [Date.prototype.toLocaleTimeString, michael@0: {hour: "numeric", minute: "numeric", second: "numeric"}] michael@0: }; michael@0: var dates = [new Date(), new Date(0), new Date(Date.parse("1989-11-09T17:57:00Z"))]; michael@0: var locales = [undefined, ["de"], ["th-u-ca-gregory-nu-thai"], ["en"], ["ja-u-ca-japanese"], ["ar-u-ca-islamicc-nu-arab"]]; michael@0: var options = [ michael@0: undefined, michael@0: {hour12: false}, michael@0: {month: "long", day: "numeric", hour: "2-digit", minute: "2-digit"} michael@0: ]; michael@0: michael@0: Object.getOwnPropertyNames(functions).forEach(function (p) { michael@0: var f = functions[p][0]; michael@0: var defaults = functions[p][1]; michael@0: locales.forEach(function (locales) { michael@0: options.forEach(function (options) { michael@0: var constructorOptions = options; michael@0: if (options === undefined) { michael@0: constructorOptions = defaults; michael@0: } else if (options.day === undefined) { michael@0: // for simplicity, our options above have either both date and time or neither michael@0: constructorOptions = Object.create(defaults); michael@0: for (var prop in options) { michael@0: if (options.hasOwnProperty(prop)) { michael@0: constructorOptions[prop] = options[prop]; michael@0: } michael@0: } michael@0: } michael@0: var referenceDateTimeFormat = new Intl.DateTimeFormat(locales, constructorOptions); michael@0: var referenceFormatted = dates.map(referenceDateTimeFormat.format); michael@0: michael@0: var formatted = dates.map(function (a) { return f.call(a, locales, options); }); michael@0: try { michael@0: testArraysAreSame(referenceFormatted, formatted); michael@0: } catch (e) { michael@0: e.message += " (Testing with locales " + locales + "; options " + michael@0: (options ? JSON.stringify(options) : options) + ".)"; michael@0: throw e; michael@0: } michael@0: }); michael@0: }); michael@0: }); michael@0: