| 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 var format = new DateFormat(skeleton, localeName); | |
| 162 var badPatterns = badSkeletons.map( | |
| 163 (x) => new DateFormat(x, format.locale).pattern); | |
| 164 | |
| 165 if (!badPatterns.some((x) => x == format.pattern)) { | |
| 166 var actualResult = format.format(date); | |
| 167 var parsed = format.parse(actualResult); | |
| 168 var thenPrintAgain = format.format(parsed); | |
| 169 expect(thenPrintAgain, equals(actualResult)); | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 main() { | |
| 175 test('Basic date format parsing', () { | |
| 176 var date_format = new DateFormat(); | |
| 177 expect( | |
| 178 date_format.parsePattern("hh:mm:ss").map((x) => x.pattern), | |
| 179 orderedEquals(["hh",":", "mm",":","ss"])); | |
| 180 expect( | |
| 181 date_format.parsePattern("hh:mm:ss").map((x) => x.pattern), | |
| 182 orderedEquals(["hh",":", "mm",":","ss"])); | |
| 183 }); | |
| 184 | |
| 185 test('Test ALL the supported formats on representative locales', () { | |
| 186 var aDate = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
| 187 testLocale("en_US", English, aDate); | |
| 188 testLocale("de_DE", German, aDate); | |
| 189 testLocale("fr_FR", French, aDate); | |
| 190 testLocale("ja_JP", Japanese, aDate); | |
| 191 testLocale("el_GR", Greek, aDate); | |
| 192 testLocale("de_AT", Austrian, aDate); | |
| 193 }); | |
| 194 | |
| 195 test('Test round-trip parsing of dates', () { | |
| 196 var hours = [0, 1, 11, 12, 13, 23]; | |
| 197 var months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | |
| 198 var locales = dateTimePatterns.getKeys(); | |
| 199 for (var locale in locales) { | |
| 200 for (var month in months) { | |
| 201 var aDate = new Date(2012, month, 27, 13, 58, 59, 012, false); | |
| 202 testRoundTripParsing(locale, aDate); | |
| 203 } | |
| 204 for (var hour in hours) { | |
| 205 var aDate = new Date(2012, 1, 27, hour, 58, 59, 123, false); | |
| 206 testRoundTripParsing(locale, aDate); | |
| 207 } | |
| 208 } | |
| 209 }); | |
| 210 | |
| 211 test('Patterns and symbols have the same coverage',() { | |
| 212 var patterns = ["en_ISO"]; | |
| 213 patterns.addAll(dateTimePatterns.getKeys()); | |
| 214 var compare = (a, b) => a.compareTo(b); | |
| 215 patterns.sort(compare); | |
| 216 var symbols = dateTimeSymbols.getKeys() as List; | |
| 217 // Workaround for a dartj2 issue that treats the keys as immutable | |
| 218 symbols = new List.from(symbols); | |
| 219 symbols.sort(compare); | |
| 220 expect(patterns.length, equals(symbols.length)); | |
| 221 for (var i = 0; i < patterns.length; i++) | |
| 222 expect(patterns[i], equals(symbols[i])); | |
| 223 }); | |
| 224 | |
| 225 test('Test malformed locales', () { | |
| 226 var aDate = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
| 227 // Austrian is a useful test locale here because it differs slightly | |
| 228 // from the generic "de" locale so we can tell the difference between | |
| 229 // correcting to "de_AT" and falling back to just "de". | |
| 230 testLocale('de-AT', Austrian, aDate); | |
| 231 testLocale('de_at', Austrian, aDate); | |
| 232 testLocale('de-at', Austrian, aDate); | |
| 233 }); | |
| 234 | |
| 235 test('Test format creation via Intl', () { | |
| 236 var intl = new Intl('ja_JP'); | |
| 237 var instanceJP = intl.date('jms'); | |
| 238 var instanceUS = intl.date('jms', 'en_US'); | |
| 239 var blank = intl.date('jms'); | |
| 240 var date = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
| 241 expect(instanceJP.format(date), equals("20:58:59")); | |
| 242 expect(instanceUS.format(date), equals("8:58:59 PM")); | |
| 243 expect(blank.format(date), equals("20:58:59")); | |
| 244 }); | |
| 245 | |
| 246 test('Test explicit format string', () { | |
| 247 var aDate = new Date(2012, 1, 27, 20, 58, 59, 0, false); | |
| 248 // An explicit format that doesn't conform to any skeleton | |
| 249 var us = new DateFormat(@'yy //// :D \\\\ dd:ss ^&@ M'); | |
| 250 expect(us.format(aDate), equals(@"12 //// :D \\\\ 27:59 ^&@ 1")); | |
| 251 // The result won't change with locale unless we use fields that are words. | |
| 252 var greek = new DateFormat(@'yy //// :D \\\\ dd:ss ^&@ M', 'el_GR'); | |
| 253 expect(greek.format(aDate), equals(@"12 //// :D \\\\ 27:59 ^&@ 1")); | |
| 254 var usWithWords = new DateFormat('yy / :D \\ dd:ss ^&@ MMM', 'en_US'); | |
| 255 var greekWithWords = new DateFormat('yy / :D \\ dd:ss ^&@ MMM', 'el_GR'); | |
| 256 expect( | |
| 257 usWithWords.format(aDate), | |
| 258 equals(@"12 / :D \ 27:59 ^&@ Jan")); | |
| 259 expect( | |
| 260 greekWithWords.format(aDate), | |
| 261 equals(@"12 / :D \ 27:59 ^&@ Ιαν")); | |
| 262 var escaped = new DateFormat(@"hh 'o''clock'"); | |
| 263 expect(escaped.format(aDate), equals(@"08 o'clock")); | |
| 264 var reParsed = escaped.parse(escaped.format(aDate)); | |
| 265 expect(escaped.format(reParsed), equals(escaped.format(aDate))); | |
| 266 var noSeparators = new DateFormat('HHmmss'); | |
| 267 expect(noSeparators.format(aDate), equals("205859")); | |
| 268 }); | |
| 269 | |
| 270 test('Test fractional seconds padding', () { | |
| 271 var one = new Date(2012, 1, 27, 20, 58, 59, 1, false); | |
| 272 var oneHundred = new Date(2012, 1, 27, 20, 58, 59, 100, false); | |
| 273 var fractional = new DateFormat('hh:mm:ss.SSS', 'en_US'); | |
| 274 expect(fractional.format(one), equals('08:58:59.001')); | |
| 275 expect(fractional.format(oneHundred), equals('08:58:59.100')); | |
| 276 var long = new DateFormat('ss.SSSSSSSS', 'en_US'); | |
| 277 expect(long.format(oneHundred), equals('59.10000000')); | |
| 278 expect(long.format(one), equals('59.00100000')); | |
| 279 }); | |
| 280 | |
| 281 test('Test parseUTC', () { | |
| 282 var local = new Date(2012, 1, 27, 20, 58, 59, 1, false); | |
| 283 var utc = new Date(2012, 1, 27, 20, 58, 59, 1, true); | |
| 284 // Getting the offset as a duration via difference() would be simpler, | |
| 285 // but doesn't work on dart2js in checked mode. See issue 4437. | |
| 286 var offset = utc.millisecondsSinceEpoch - local.millisecondsSinceEpoch; | |
| 287 var format = new DateFormat('yyyy-MM-dd HH:mm:ss'); | |
| 288 var localPrinted = format.format(local); | |
| 289 var parsed = format.parse(localPrinted); | |
| 290 var parsedUTC = format.parseUTC(format.format(utc)); | |
| 291 var parsedOffset = parsedUTC.millisecondsSinceEpoch | |
| 292 - parsed.millisecondsSinceEpoch; | |
| 293 expect(parsedOffset, equals(offset)); | |
| 294 expect(utc.hour, equals(parsedUTC.hour)); | |
| 295 expect(local.hour, equals(parsed.hour)); | |
| 296 }); | |
| 297 } | |
| OLD | NEW |