OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Tests for the loose option when parsing dates and times, which accept | 5 /// Tests for the loose option when parsing dates and times, which accept |
6 /// mixed-case input and are able to skip missing delimiters. This is only | 6 /// mixed-case input and are able to skip missing delimiters. Such valid input |
7 /// tested in basic US locale, it's hard to define for others. | 7 /// is only tested in basic US locale, it's hard to define for others. |
| 8 /// Inputs which should fail because they're missing data (currently only the |
| 9 /// year) are tested in more locales. |
8 library date_time_loose_test; | 10 library date_time_loose_test; |
9 | 11 |
| 12 import 'package:intl/date_symbol_data_local.dart'; |
10 import 'package:intl/intl.dart'; | 13 import 'package:intl/intl.dart'; |
11 import 'package:unittest/unittest.dart'; | 14 import 'package:test/test.dart'; |
12 | 15 |
13 main() { | 16 main() { |
14 var format; | 17 var format; |
15 | 18 |
16 var date = new DateTime(2014, 9, 3); | 19 var date = new DateTime(2014, 9, 3); |
17 | 20 |
18 check(String s) { | 21 check(String s) { |
19 expect(() => format.parse(s), throwsFormatException); | 22 expect(() => format.parse(s), throwsFormatException); |
20 expect(format.parseLoose(s), date); | 23 expect(format.parseLoose(s), date); |
21 } | 24 } |
22 | 25 |
23 test("Loose parsing yMMMd", () { | 26 test("Loose parsing yMMMd", () { |
24 // Note: We can't handle e.g. Sept, we don't have those abbreviations | 27 // Note: We can't handle e.g. Sept, we don't have those abbreviations |
25 // in our data. | 28 // in our data. |
26 // Also doesn't handle "sep3,2014", or "sep 3.2014" | 29 // Also doesn't handle "sep3,2014", or "sep 3.2014" |
27 format = new DateFormat.yMMMd("en_US"); | 30 format = new DateFormat.yMMMd("en_US"); |
28 check("Sep 3 2014"); | 31 check("Sep 3 2014"); |
29 check("sep 3 2014"); | 32 check("sep 3 2014"); |
30 check("sep 3 2014"); | 33 check("sep 3 2014"); |
31 check("sep 3 2014"); | 34 check("sep 3 2014"); |
32 check("sep 3 2014"); | 35 check("sep 3 2014"); |
33 check("sep3 2014"); | 36 check("sep3 2014"); |
34 check("september 3, 2014"); | 37 check("september 3, 2014"); |
35 check("sEPTembER 3, 2014"); | 38 check("sEPTembER 3, 2014"); |
36 check("seP 3, 2014"); | 39 check("seP 3, 2014"); |
| 40 check("Sep 3,2014"); |
37 }); | 41 }); |
38 | 42 |
39 test("Loose parsing yMMMd that parses strict", () { | 43 test("Loose parsing yMMMd that parses strict", () { |
40 expect(format.parseLoose("Sep 3, 2014"), date); | 44 expect(format.parseLoose("Sep 3, 2014"), date); |
41 }); | 45 }); |
42 | 46 |
43 test("Loose parsing yMd", () { | 47 test("Loose parsing yMd", () { |
44 format = new DateFormat.yMd("en_US"); | 48 format = new DateFormat.yMd("en_US"); |
45 check("09 3 2014"); | 49 check("09 3 2014"); |
46 check("09 00003 2014"); | 50 check("09 00003 2014"); |
47 check("09/ 03/2014"); | 51 check("09/ 03/2014"); |
48 expect(() => format.parseLoose("09 / 03 / 2014"), | 52 check("09 / 03 / 2014"); |
49 throwsA(new isInstanceOf<FormatException>())); | |
50 }); | 53 }); |
51 | 54 |
52 test("Loose parsing yMd that parses strict", () { | 55 test("Loose parsing yMd that parses strict", () { |
53 expect(format.parseLoose("09/03/2014"), date); | 56 expect(format.parseLoose("09/03/2014"), date); |
54 expect(format.parseLoose("09/3/2014"), date); | 57 expect(format.parseLoose("09/3/2014"), date); |
55 }); | 58 }); |
| 59 |
| 60 test("Loose parsing should handle standalone month format", () { |
| 61 // This checks that LL actually sets the month. |
| 62 // The appended whitespace and extra d pattern are present to trigger the |
| 63 // loose parsing code path. |
| 64 expect(new DateFormat('LL/d', 'en_US').parseLoose("05/ 2").month, 5); |
| 65 }); |
| 66 |
| 67 group("Loose parsing with year formats", () { |
| 68 test("should fail when year is omitted (en_US)", () { |
| 69 expect(() => new DateFormat('yyyy-MM-dd').parseLoose("1/11"), |
| 70 throwsFormatException); |
| 71 }); |
| 72 |
| 73 test("should fail when year is omitted (ja)", () { |
| 74 initializeDateFormatting('ja', null); |
| 75 expect(() => new DateFormat.yMMMd("ja").parseLoose('12月12日'), |
| 76 throwsFormatException); |
| 77 expect(() => new DateFormat.yMd("ja").parseLoose('12月12日'), |
| 78 throwsFormatException); |
| 79 expect(() => new DateFormat.yMEd("ja").parseLoose('12月12日'), |
| 80 throwsFormatException); |
| 81 expect(() => new DateFormat.yMMMEd("ja").parseLoose('12月12日'), |
| 82 throwsFormatException); |
| 83 expect(() => new DateFormat.yMMMMd("ja").parseLoose('12月12日'), |
| 84 throwsFormatException); |
| 85 expect(() => new DateFormat.yMMMMEEEEd("ja").parseLoose('12月12日'), |
| 86 throwsFormatException); |
| 87 }); |
| 88 |
| 89 test("should fail when year is omitted (hu)", () { |
| 90 initializeDateFormatting('hu', null); |
| 91 expect(() => new DateFormat.yMMMd("hu").parseLoose('3. 17.'), |
| 92 throwsFormatException); |
| 93 expect(() => new DateFormat.yMd("hu").parseLoose('3. 17.'), |
| 94 throwsFormatException); |
| 95 expect(() => new DateFormat.yMEd("hu").parseLoose('3. 17.'), |
| 96 throwsFormatException); |
| 97 expect(() => new DateFormat.yMMMEd("hu").parseLoose('3. 17.'), |
| 98 throwsFormatException); |
| 99 expect(() => new DateFormat.yMMMMd("hu").parseLoose('3. 17.'), |
| 100 throwsFormatException); |
| 101 expect(() => new DateFormat.yMMMMEEEEd("hu").parseLoose('3. 17.'), |
| 102 throwsFormatException); |
| 103 }); |
| 104 }); |
56 } | 105 } |
OLD | NEW |