date-time-options.js (5007B)
1 // Copyright 2012 Mozilla Corporation. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 es5id: 12.1.1_TDTO 6 description: > 7 Tests that the set of options for the date and time components is 8 processed correctly. 9 author: Norbert Lindenberg 10 includes: [testIntl.js] 11 ---*/ 12 13 var locales = [[], ["zh-Hans-CN"], ["hi-IN"], ["en-US"], ["id-ID"]]; 14 var dates = [new Date(), new Date(0), new Date(Date.parse("1989-11-09T17:57:00Z"))]; 15 16 function testWithDateTimeFormat(options, expected) { 17 locales.forEach(function (locales) { 18 var format = new Intl.DateTimeFormat(locales, options); 19 var resolvedOptions = format.resolvedOptions(); 20 getDateTimeComponents().forEach(function (component) { 21 if (resolvedOptions.hasOwnProperty(component)) { 22 assert(expected.hasOwnProperty(component), 23 "Unrequested component " + component + 24 " added to expected subset " + JSON.stringify(expected) + 25 "; locales " + locales + ", options " + 26 (options ? JSON.stringify(options) : options) + "."); 27 } else { 28 assert.sameValue(expected.hasOwnProperty(component), false, 29 "Missing component " + component + 30 " from expected subset " + JSON.stringify(expected) + 31 "; locales " + locales + ", options " + 32 (options ? JSON.stringify(options) : options) + "."); 33 } 34 }); 35 }); 36 } 37 38 function testWithToLocale(f, options, expected) { 39 // expected can be either one subset or an array of possible subsets 40 if (expected.length === undefined) { 41 expected = [expected]; 42 } 43 locales.forEach(function (locales) { 44 dates.forEach(function (date) { 45 var formatted = Date.prototype[f].call(date, locales, options); 46 var expectedStrings = []; 47 expected.forEach(function (expected) { 48 var referenceFormat = new Intl.DateTimeFormat(locales, expected); 49 expectedStrings.push(referenceFormat.format(date)); 50 }); 51 assert.notSameValue(expectedStrings.indexOf(formatted), -1, 52 "Function " + f + " did not return expected string for locales " + 53 locales + ", options " + (options? JSON.stringify(options) : options) + 54 "; expected " + 55 (expectedStrings.length === 1 ? expectedStrings[0] : "one of " + expectedStrings) + 56 ", got " + formatted + "."); 57 }); 58 }); 59 } 60 61 // any/date: steps 5a, 6a, 7a 62 testWithDateTimeFormat(undefined, {year: "numeric", month: "numeric", day: "numeric"}); 63 64 // any/date: steps 5a, 6a 65 testWithDateTimeFormat({year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); 66 67 // any/date: steps 5a, 6a 68 testWithDateTimeFormat({hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); 69 70 // any/all: steps 5a, 6a, 7a, 8a 71 testWithToLocale("toLocaleString", undefined, [ 72 // the first one is not guaranteed to be supported; the second one is 73 {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, 74 {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} 75 ]); 76 77 // any/all: steps 5a, 6a 78 testWithToLocale("toLocaleString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); 79 80 // any/all: steps 5a, 6a 81 testWithToLocale("toLocaleString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); 82 83 // date/date: steps 5a, 7a 84 testWithToLocale("toLocaleDateString", undefined, {year: "numeric", month: "numeric", day: "numeric"}); 85 86 // date/date: steps 5a 87 testWithToLocale("toLocaleDateString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); 88 89 // date/date: steps 5a, 7a 90 testWithToLocale("toLocaleDateString", {hour: "numeric", minute: "numeric", second: "numeric"}, [ 91 // the first one is not guaranteed to be supported; the second one is 92 {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, 93 {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} 94 ]); 95 96 // time/time: steps 6a, 8a 97 testWithToLocale("toLocaleTimeString", undefined, {hour: "numeric", minute: "numeric", second: "numeric"}); 98 99 // time/time: steps 6a, 8a 100 testWithToLocale("toLocaleTimeString", {weekday: "short", year: "numeric", month: "numeric", day: "numeric"}, 101 {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}); 102 103 // time/time: steps 6a 104 testWithToLocale("toLocaleTimeString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); 105 106 reportCompare(0, 0);