forked from dart-archive/intl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_time_strict_test.dart
79 lines (71 loc) · 2.57 KB
/
date_time_strict_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Tests for the strict option when parsing dates and times, which are
/// relatively locale-independent, depending only on the being a valid date
/// and consuming all the input data.
library date_time_strict_test;
import 'package:intl/intl.dart';
import 'package:unittest/unittest.dart';
main() {
test("All input consumed", () {
var format = new DateFormat.yMMMd();
var date = new DateTime(2014, 9, 3);
var formatted = 'Sep 3, 2014';
expect(format.format(date), formatted);
var parsed = format.parseStrict(formatted);
expect(parsed, date);
check(String s) {
expect(() => format.parseStrict(s), throwsFormatException);
expect(format.parse(s), date);
}
check(formatted + ",");
check(formatted + "abc");
check(formatted + " ");
});
test("Invalid dates", () {
var format = new DateFormat.yMd();
check(s) => expect(() => format.parseStrict(s), throwsFormatException);
check("0/3/2014");
check("13/3/2014");
check("9/0/2014");
check("9/31/2014");
check("09/31/2014");
check("10/32/2014");
check("2/29/2014");
expect(format.parseStrict("2/29/2016"), new DateTime(2016, 2, 29));
});
test("Invalid times am/pm", () {
var format = new DateFormat.jms();
check(s) => expect(() => format.parseStrict(s), throwsFormatException);
check("-1:15:00 AM");
expect(format.parseStrict("0:15:00 AM"), new DateTime(1970, 1, 1, 0, 15));
check("24:00:00 PM");
check("24:00:00 AM");
check("25:00:00 PM");
check("0:-1:00 AM");
check("0:60:00 AM");
expect(format.parseStrict("0:59:00 AM"), new DateTime(1970, 1, 1, 0, 59));
check("0:0:-1 AM");
check("0:0:60 AM");
check("2:0:60 PM");
expect(
format.parseStrict("2:0:59 PM"), new DateTime(1970, 1, 1, 14, 0, 59));
});
test("Invalid times 24 hour", () {
var format = new DateFormat.Hms();
check(s) => expect(() => format.parseStrict(s), throwsFormatException);
check("-1:15:00");
expect(format.parseStrict("0:15:00"), new DateTime(1970, 1, 1, 0, 15));
check("24:00:00");
check("24:00:00");
check("25:00:00");
check("0:-1:00");
check("0:60:00");
expect(format.parseStrict("0:59:00"), new DateTime(1970, 1, 1, 0, 59));
check("0:0:-1");
check("0:0:60");
check("14:0:60");
expect(format.parseStrict("14:0:59"), new DateTime(1970, 1, 1, 14, 0, 59));
});
}