|
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 * @description Tests that the set of options for the date and time components is processed correctly. |
|
6 * @author Norbert Lindenberg |
|
7 */ |
|
8 |
|
9 $INCLUDE("testIntl.js"); |
|
10 |
|
11 var locales = [[], ["zh-Hans-CN"], ["hi-IN"], ["en-US"], ["id-ID"]]; |
|
12 var dates = [new Date(), new Date(0), new Date(Date.parse("1989-11-09T17:57:00Z"))]; |
|
13 |
|
14 function testWithDateTimeFormat(options, expected) { |
|
15 locales.forEach(function (locales) { |
|
16 var format = new Intl.DateTimeFormat(locales, options); |
|
17 var resolvedOptions = format.resolvedOptions(); |
|
18 getDateTimeComponents().forEach(function (component) { |
|
19 if (resolvedOptions.hasOwnProperty(component)) { |
|
20 if (!expected.hasOwnProperty(component)) { |
|
21 $ERROR("Unrequested component " + component + |
|
22 " added to expected subset " + JSON.stringify(expected) + |
|
23 "; locales " + locales + ", options " + |
|
24 (options ? JSON.stringify(options) : options) + "."); |
|
25 } |
|
26 } else { |
|
27 if (expected.hasOwnProperty(component)) { |
|
28 $ERROR("Missing component " + component + |
|
29 " from expected subset " + JSON.stringify(expected) + |
|
30 "; locales " + locales + ", options " + |
|
31 (options ? JSON.stringify(options) : options) + "."); |
|
32 } |
|
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 if (expectedStrings.indexOf(formatted) === -1) { |
|
52 $ERROR("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 |
|
62 // any/date: steps 5a, 6a, 7a |
|
63 testWithDateTimeFormat(undefined, {year: "numeric", month: "numeric", day: "numeric"}); |
|
64 |
|
65 // any/date: steps 5a, 6a |
|
66 testWithDateTimeFormat({year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); |
|
67 |
|
68 // any/date: steps 5a, 6a |
|
69 testWithDateTimeFormat({hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); |
|
70 |
|
71 // any/all: steps 5a, 6a, 7a, 8a |
|
72 testWithToLocale("toLocaleString", undefined, [ |
|
73 // the first one is not guaranteed to be supported; the second one is |
|
74 {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, |
|
75 {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} |
|
76 ]); |
|
77 |
|
78 // any/all: steps 5a, 6a |
|
79 testWithToLocale("toLocaleString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); |
|
80 |
|
81 // any/all: steps 5a, 6a |
|
82 testWithToLocale("toLocaleString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); |
|
83 |
|
84 // date/date: steps 5a, 7a |
|
85 testWithToLocale("toLocaleDateString", undefined, {year: "numeric", month: "numeric", day: "numeric"}); |
|
86 |
|
87 // date/date: steps 5a |
|
88 testWithToLocale("toLocaleDateString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); |
|
89 |
|
90 // date/date: steps 5a, 7a |
|
91 testWithToLocale("toLocaleDateString", {hour: "numeric", minute: "numeric", second: "numeric"}, [ |
|
92 // the first one is not guaranteed to be supported; the second one is |
|
93 {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, |
|
94 {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} |
|
95 ]); |
|
96 |
|
97 // time/time: steps 6a, 8a |
|
98 testWithToLocale("toLocaleTimeString", undefined, {hour: "numeric", minute: "numeric", second: "numeric"}); |
|
99 |
|
100 // time/time: steps 6a, 8a |
|
101 testWithToLocale("toLocaleTimeString", {weekday: "short", year: "numeric", month: "numeric", day: "numeric"}, |
|
102 {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}); |
|
103 |
|
104 // time/time: steps 6a |
|
105 testWithToLocale("toLocaleTimeString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); |
|
106 |
|
107 |