OLD | NEW |
| (Empty) |
1 /** | |
2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
3 * for details. All rights reserved. Use of this source code is governed by a | |
4 * BSD-style license that can be found in the LICENSE file. | |
5 */ | |
6 | |
7 #library('date_time_format_test'); | |
8 | |
9 #import('../intl.dart'); | |
10 #import('../date_format.dart'); | |
11 #import('../../../pkg/unittest/unittest.dart'); | |
12 #import('../date_time_patterns.dart'); | |
13 #import('../date_symbol_data.dart'); | |
14 | |
15 #source('date_time_format_test_data.dart'); | |
16 | |
17 /** | |
18 * Tests the DateFormat library in dart. | |
19 */ | |
20 | |
21 var formatsToTest = const [ | |
22 DateFormat.DAY, | |
23 DateFormat.ABBR_WEEKDAY, | |
24 DateFormat.WEEKDAY, | |
25 DateFormat.ABBR_STANDALONE_MONTH, | |
26 DateFormat.STANDALONE_MONTH, | |
27 DateFormat.NUM_MONTH, | |
28 DateFormat.NUM_MONTH_DAY, | |
29 DateFormat.NUM_MONTH_WEEKDAY_DAY, | |
30 DateFormat.ABBR_MONTH, | |
31 DateFormat.ABBR_MONTH_DAY, | |
32 DateFormat.ABBR_MONTH_WEEKDAY_DAY, | |
33 DateFormat.MONTH, | |
34 DateFormat.MONTH_DAY, | |
35 DateFormat.MONTH_WEEKDAY_DAY, | |
36 DateFormat.ABBR_QUARTER, | |
37 DateFormat.QUARTER, | |
38 DateFormat.YEAR, | |
39 DateFormat.YEAR_NUM_MONTH, | |
40 DateFormat.YEAR_NUM_MONTH_DAY, | |
41 DateFormat.YEAR_NUM_MONTH_WEEKDAY_DAY, | |
42 DateFormat.YEAR_ABBR_MONTH, | |
43 DateFormat.YEAR_ABBR_MONTH_DAY, | |
44 DateFormat.YEAR_ABBR_MONTH_WEEKDAY_DAY, | |
45 DateFormat.YEAR_MONTH, | |
46 DateFormat.YEAR_MONTH_DAY, | |
47 DateFormat.YEAR_MONTH_WEEKDAY_DAY, | |
48 // TODO(alanknight): CLDR and ICU appear to disagree on these for Japanese | |
49 // DateFormat.YEAR_ABBR_QUARTER, | |
50 // DateFormat.YEAR_QUARTER, | |
51 DateFormat.HOUR24, | |
52 DateFormat.HOUR24_MINUTE, | |
53 DateFormat.HOUR24_MINUTE_SECOND, | |
54 DateFormat.HOUR, | |
55 DateFormat.HOUR_MINUTE, | |
56 DateFormat.HOUR_MINUTE_SECOND, | |
57 // TODO(alanknight): Time zone support | |
58 // DateFormat.HOUR_MINUTE_GENERIC_TZ, | |
59 // DateFormat.HOUR_MINUTE_TZ, | |
60 // DateFormat.HOUR_GENERIC_TZ, | |
61 // DateFormat.HOUR_TZ, | |
62 DateFormat.MINUTE, | |
63 DateFormat.MINUTE_SECOND, | |
64 DateFormat.SECOND | |
65 // ABBR_GENERIC_TZ, | |
66 // GENERIC_TZ, | |
67 // ABBR_SPECIFIC_TZ, | |
68 // SPECIFIC_TZ, | |
69 // ABBR_UTC_TZ | |
70 ]; | |
71 | |
72 var icuFormatNamesToTest = const [ | |
73 // It would be really nice to not have to duplicate this and just be able | |
74 // to use the names to get reflective access. | |
75 "DAY", | |
76 "ABBR_WEEKDAY", | |
77 "WEEKDAY", | |
78 "ABBR_STANDALONE_MONTH", | |
79 "STANDALONE_MONTH", | |
80 "NUM_MONTH", | |
81 "NUM_MONTH_DAY", | |
82 "NUM_MONTH_WEEKDAY_DAY", | |
83 "ABBR_MONTH", | |
84 "ABBR_MONTH_DAY", | |
85 "ABBR_MONTH_WEEKDAY_DAY", | |
86 "MONTH", | |
87 "MONTH_DAY", | |
88 "MONTH_WEEKDAY_DAY", | |
89 "ABBR_QUARTER", | |
90 "QUARTER", | |
91 "YEAR", | |
92 "YEAR_NUM_MONTH", | |
93 "YEAR_NUM_MONTH_DAY", | |
94 "YEAR_NUM_MONTH_WEEKDAY_DAY", | |
95 "YEAR_ABBR_MONTH", | |
96 "YEAR_ABBR_MONTH_DAY", | |
97 "YEAR_ABBR_MONTH_WEEKDAY_DAY", | |
98 "YEAR_MONTH", | |
99 "YEAR_MONTH_DAY", | |
100 "YEAR_MONTH_WEEKDAY_DAY", | |
101 // TODO(alanknight): CLDR and ICU appear to disagree on these for Japanese. | |
102 // omit for the time being | |
103 // "YEAR_ABBR_QUARTER", | |
104 // "YEAR_QUARTER", | |
105 "HOUR24", | |
106 "HOUR24_MINUTE", | |
107 "HOUR24_MINUTE_SECOND", | |
108 "HOUR", | |
109 "HOUR_MINUTE", | |
110 "HOUR_MINUTE_SECOND", | |
111 // TODO(alanknight): Time zone support | |
112 // "HOUR_MINUTE_GENERIC_TZ", | |
113 // "HOUR_MINUTE_TZ", | |
114 // "HOUR_GENERIC_TZ", | |
115 // "HOUR_TZ", | |
116 "MINUTE", | |
117 "MINUTE_SECOND", | |
118 "SECOND" | |
119 // ABBR_GENERIC_TZ, | |
120 // GENERIC_TZ, | |
121 // ABBR_SPECIFIC_TZ, | |
122 // SPECIFIC_TZ, | |
123 // ABBR_UTC_TZ | |
124 ]; | |
125 | |
126 /** | |
127 * Exercise all of the formats we have explicitly defined on a particular | |
128 * locale. [expectedResults] is a map from ICU format names to the | |
129 * expected result of formatting [date] according to that format in | |
130 * [locale]. | |
131 */ | |
132 testLocale(String localeName, Map expectedResults, Date date) { | |
133 var intl = new Intl(localeName); | |
134 for(int i=0; i<formatsToTest.length; i++) { | |
135 var skeleton = formatsToTest[i]; | |
136 var format = intl.date(skeleton); | |
137 var icuName = icuFormatNamesToTest[i]; | |
138 var actualResult = format.format(date); | |
139 expect(expectedResults[icuName], equals(actualResult)); | |
140 } | |
141 } | |
142 | |
143 testRoundTripParsing(String localeName, Date date) { | |
144 // In order to test parsing, we can't just read back the date, because | |
145 // printing in most formats loses information. But we can test that | |
146 // what we parsed back prints the same as what we originally printed. | |
147 // At least in most cases. In some cases, we can't even do that. e.g. | |
148 // the skeleton WEEKDAY can't be reconstructed at all, and YEAR_MONTH | |
149 // formats don't give us enough information to construct a valid date. | |
150 var badSkeletons = [ | |
151 DateFormat.ABBR_WEEKDAY, | |
152 DateFormat.WEEKDAY, | |
153 DateFormat.QUARTER, | |
154 DateFormat.ABBR_QUARTER, | |
155 DateFormat.YEAR, | |
156 DateFormat.YEAR_NUM_MONTH, | |
157 DateFormat.YEAR_ABBR_MONTH, | |
158 DateFormat.YEAR_MONTH]; | |
159 for(int i = 0; i < formatsToTest.length; i++) { | |
160 var skeleton = formatsToTest[i]; | |
161 if (!badSkeletons.some((x) => x == skeleton)) { | |
162 var format = new DateFormat(skeleton, localeName); | |
163 var actualResult = format.format(date); | |
164 var parsed = format.parse(actualResult); | |
165 var thenPrintAgain = format.format(parsed); | |
166 expect(thenPrintAgain, equals(actualResult)); | |
167 } | |
168 } | |
169 } | |
170 | |
171 main() { | |
172 test('Basic date format parsing', () { | |
173 var date_format = new DateFormat("d"); | |
174 expect( | |
175 date_format.parsePattern("hh:mm:ss").map((x) => x.pattern), | |
176 orderedEquals(["hh",":", "mm",":","ss"])); | |
177 expect( | |
178 date_format.parsePattern("hh:mm:ss").map((x) => x.pattern), | |
179 orderedEquals(["hh",":", "mm",":","ss"])); | |
180 }); | |
181 | |
182 test('Test ALL the supported formats on representative locales', () { | |
183 var aDate = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
184 testLocale("en_US", English, aDate); | |
185 testLocale("de_DE", German, aDate); | |
186 testLocale("fr_FR", French, aDate); | |
187 testLocale("ja_JP", Japanese, aDate); | |
188 testLocale("el_GR", Greek, aDate); | |
189 testLocale("de_AT", Austrian, aDate); | |
190 }); | |
191 | |
192 test('Test round-trip parsing of dates', () { | |
193 var hours = [0, 1, 11, 12, 13, 23]; | |
194 var months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | |
195 var locales = dateTimePatterns.getKeys(); | |
196 for (var locale in locales) { | |
197 for (var month in months) { | |
198 var aDate = new Date(2012, month, 27, 13, 58, 59, 012, false); | |
199 testRoundTripParsing(locale, aDate); | |
200 } | |
201 for (var hour in hours) { | |
202 var aDate = new Date(2012, 1, 27, hour, 58, 59, 123, false); | |
203 testRoundTripParsing(locale, aDate); | |
204 } | |
205 } | |
206 }); | |
207 | |
208 test('Patterns and symbols have the same coverage',() { | |
209 var patterns = ["en_ISO"]; | |
210 patterns.addAll(dateTimePatterns.getKeys()); | |
211 var compare = (a, b) => a.compareTo(b); | |
212 patterns.sort(compare); | |
213 var symbols = dateTimeSymbols.getKeys() as List; | |
214 // Workaround for a dartj2 issue that treats the keys as immutable | |
215 symbols = new List.from(symbols); | |
216 symbols.sort(compare); | |
217 expect(patterns.length, equals(symbols.length)); | |
218 for (var i = 0; i < patterns.length; i++) | |
219 expect(patterns[i], equals(symbols[i])); | |
220 }); | |
221 | |
222 test('Test malformed locales', () { | |
223 var aDate = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
224 // Austrian is a useful test locale here because it differs slightly | |
225 // from the generic "de" locale so we can tell the difference between | |
226 // correcting to "de_AT" and falling back to just "de". | |
227 testLocale('de-AT', Austrian, aDate); | |
228 testLocale('de_at', Austrian, aDate); | |
229 testLocale('de-at', Austrian, aDate); | |
230 }); | |
231 | |
232 test('Test format creation via Intl', () { | |
233 var intl = new Intl('ja_JP'); | |
234 var instanceJP = intl.date('jms'); | |
235 var instanceUS = intl.date('jms', 'en_US'); | |
236 var blank = intl.date('jms'); | |
237 var date = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
238 expect(instanceJP.format(date), equals("20:58:59")); | |
239 expect(instanceUS.format(date), equals("8:58:59 PM")); | |
240 expect(blank.format(date), equals("20:58:59")); | |
241 }); | |
242 | |
243 test('Test explicit format string', () { | |
244 var aDate = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
245 // An explicit format that doesn't conform to any skeleton | |
246 var us = new DateFormat(@'yy //// :D \\\\ dd:ss ^&@ M'); | |
247 expect(us.format(aDate), equals(@"12 //// :D \\\\ 27:59 ^&@ 1")); | |
248 // The result won't change with locale unless we use fields that are words. | |
249 var greek = new DateFormat(@'yy //// :D \\\\ dd:ss ^&@ M', 'el_GR'); | |
250 expect(greek.format(aDate), equals(@"12 //// :D \\\\ 27:59 ^&@ 1")); | |
251 var usWithWords = new DateFormat('yy / :D \\ dd:ss ^&@ MMM', 'en_US'); | |
252 var greekWithWords = new DateFormat('yy / :D \\ dd:ss ^&@ MMM', 'el_GR'); | |
253 expect( | |
254 usWithWords.format(aDate), | |
255 equals(@"12 / :D \ 27:59 ^&@ Jan")); | |
256 expect( | |
257 greekWithWords.format(aDate), | |
258 equals(@"12 / :D \ 27:59 ^&@ Ιαν")); | |
259 var escaped = new DateFormat(@"hh 'o''clock'"); | |
260 expect(escaped.format(aDate), equals(@"08 o'clock")); | |
261 var reParsed = escaped.parse(escaped.format(aDate)); | |
262 expect(escaped.format(reParsed), equals(escaped.format(aDate))); | |
263 var noSeparators = new DateFormat('HHmmss'); | |
264 expect(noSeparators.format(aDate), equals("205859")); | |
265 }); | |
266 | |
267 test('Test fractional seconds padding', () { | |
268 var one = new Date(2012, 1, 27, 20, 58, 59, 1, false); | |
269 var oneHundred = new Date(2012, 1, 27, 20, 58, 59, 100, false); | |
270 var fractional = new DateFormat('hh:mm:ss.SSS', 'en_US'); | |
271 expect(fractional.format(one), equals('08:58:59.001')); | |
272 expect(fractional.format(oneHundred), equals('08:58:59.100')); | |
273 var long = new DateFormat('ss.SSSSSSSS', 'en_US'); | |
274 expect(long.format(oneHundred), equals('59.10000000')); | |
275 expect(long.format(one), equals('59.00100000')); | |
276 }); | |
277 | |
278 test('Test parseUTC', () { | |
279 var local = new Date(2012, 1, 27, 20, 58, 59, 1, false); | |
280 var utc = new Date(2012, 1, 27, 20, 58, 59, 1, true); | |
281 // Getting the offset as a duration via difference() would be simpler, | |
282 // but doesn't work on dart2js in checked mode. See issue 4437. | |
283 var offset = utc.millisecondsSinceEpoch - local.millisecondsSinceEpoch; | |
284 var format = new DateFormat('yyyy-MM-dd HH:mm:ss'); | |
285 var localPrinted = format.format(local); | |
286 var parsed = format.parse(localPrinted); | |
287 var parsedUTC = format.parseUTC(format.format(utc)); | |
288 var parsedOffset = parsedUTC.millisecondsSinceEpoch | |
289 - parsed.millisecondsSinceEpoch; | |
290 expect(parsedOffset, equals(offset)); | |
291 expect(utc.hour, equals(parsedUTC.hour)); | |
292 expect(local.hour, equals(parsed.hour)); | |
293 }); | |
294 } | |
OLD | NEW |