OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Tests for the strict option when parsing dates and times, which are |
| 6 /// relatively locale-independent, depending only on the being a valid date |
| 7 /// and consuming all the input data. |
| 8 library date_time_strict_test; |
| 9 |
| 10 import 'package:intl/intl.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 main() { |
| 14 test("All input consumed", () { |
| 15 var format = new DateFormat.yMMMd(); |
| 16 var date = new DateTime(2014, 9, 3); |
| 17 var formatted = 'Sep 3, 2014'; |
| 18 expect(format.format(date), formatted); |
| 19 var parsed = format.parseStrict(formatted); |
| 20 expect(parsed, date); |
| 21 |
| 22 check(String s) { |
| 23 expect(() => format.parseStrict(s), throwsFormatException); |
| 24 expect(format.parse(s), date); |
| 25 } |
| 26 |
| 27 check(formatted + ","); |
| 28 check(formatted + "abc"); |
| 29 check(formatted + " "); |
| 30 }); |
| 31 |
| 32 test("Invalid dates", () { |
| 33 var format = new DateFormat.yMd(); |
| 34 check(s) => expect(() => format.parseStrict(s), throwsFormatException); |
| 35 check("0/3/2014"); |
| 36 check("13/3/2014"); |
| 37 check("9/0/2014"); |
| 38 check("9/31/2014"); |
| 39 check("09/31/2014"); |
| 40 check("10/32/2014"); |
| 41 check("2/29/2014"); |
| 42 expect(format.parseStrict("2/29/2016"), new DateTime(2016, 2, 29)); |
| 43 }); |
| 44 |
| 45 test("Invalid times am/pm", () { |
| 46 var format = new DateFormat.jms(); |
| 47 check(s) => expect(() => format.parseStrict(s), throwsFormatException); |
| 48 check("-1:15:00 AM"); |
| 49 expect(format.parseStrict("0:15:00 AM"), new DateTime(1970, 1, 1, 0, 15)); |
| 50 check("24:00:00 PM"); |
| 51 check("24:00:00 AM"); |
| 52 check("25:00:00 PM"); |
| 53 check("0:-1:00 AM"); |
| 54 check("0:60:00 AM"); |
| 55 expect(format.parseStrict("0:59:00 AM"), new DateTime(1970, 1, 1, 0, 59)); |
| 56 check("0:0:-1 AM"); |
| 57 check("0:0:60 AM"); |
| 58 check("2:0:60 PM"); |
| 59 expect( |
| 60 format.parseStrict("2:0:59 PM"), new DateTime(1970, 1, 1, 14, 0, 59)); |
| 61 }); |
| 62 |
| 63 test("Invalid times 24 hour", () { |
| 64 var format = new DateFormat.Hms(); |
| 65 check(s) => expect(() => format.parseStrict(s), throwsFormatException); |
| 66 check("-1:15:00"); |
| 67 expect(format.parseStrict("0:15:00"), new DateTime(1970, 1, 1, 0, 15)); |
| 68 check("24:00:00"); |
| 69 check("24:00:00"); |
| 70 check("25:00:00"); |
| 71 check("0:-1:00"); |
| 72 check("0:60:00"); |
| 73 expect(format.parseStrict("0:59:00"), new DateTime(1970, 1, 1, 0, 59)); |
| 74 check("0:0:-1"); |
| 75 check("0:0:60"); |
| 76 check("14:0:60"); |
| 77 expect(format.parseStrict("14:0:59"), new DateTime(1970, 1, 1, 14, 0, 59)); |
| 78 }); |
| 79 } |
OLD | NEW |