Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Side by Side Diff: tests/corelib/date_time_test.dart

Issue 324213003: Change DateTime.parse FormatException messages; update tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« sdk/lib/core/date_time.dart ('K') | « sdk/lib/core/date_time.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; 5 import "package:expect/expect.dart";
6 6
7 // Dart test program for DateTime. 7 // Dart test program for DateTime.
8 8
9 // Identical to _MAX_MILLISECONDS_SINCE_EPOCH in date_time.dart
10 const int _MAX_MILLISECONDS = 8640000000000000;
11
9 // Tests if the time moves eventually forward. 12 // Tests if the time moves eventually forward.
10 void testNow() { 13 void testNow() {
11 var t1 = new DateTime.now(); 14 var t1 = new DateTime.now();
12 bool timeMovedForward = false; 15 bool timeMovedForward = false;
13 for (int i = 0; i < 1000000; i++) { 16 for (int i = 0; i < 1000000; i++) {
14 var t2 = new DateTime.now(); 17 var t2 = new DateTime.now();
15 if (t1.millisecondsSinceEpoch < t2.millisecondsSinceEpoch) { 18 if (t1.millisecondsSinceEpoch < t2.millisecondsSinceEpoch) {
16 timeMovedForward = true; 19 timeMovedForward = true;
17 break; 20 break;
18 } 21 }
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 Expect.equals(18, dt.day); 228 Expect.equals(18, dt.day);
226 Expect.equals(13, dt.hour); 229 Expect.equals(13, dt.hour);
227 Expect.equals(20, dt.minute); 230 Expect.equals(20, dt.minute);
228 Expect.equals(0, dt.second); 231 Expect.equals(0, dt.second);
229 Expect.equals(0, dt.millisecond); 232 Expect.equals(0, dt.millisecond);
230 Expect.equals("2036-07-18 13:20:00.000", dt.toString()); 233 Expect.equals("2036-07-18 13:20:00.000", dt.toString());
231 } 234 }
232 235
233 void testExtremes() { 236 void testExtremes() {
234 var dt = 237 var dt =
235 new DateTime.fromMillisecondsSinceEpoch(8640000000000000, isUtc: true); 238 new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
236 Expect.equals(275760, dt.year); 239 Expect.equals(275760, dt.year);
237 Expect.equals(9, dt.month); 240 Expect.equals(9, dt.month);
238 Expect.equals(13, dt.day); 241 Expect.equals(13, dt.day);
239 Expect.equals(0, dt.hour); 242 Expect.equals(0, dt.hour);
240 Expect.equals(0, dt.minute); 243 Expect.equals(0, dt.minute);
241 Expect.equals(0, dt.second); 244 Expect.equals(0, dt.second);
242 Expect.equals(0, dt.millisecond); 245 Expect.equals(0, dt.millisecond);
243 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000, isUtc: true); 246 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
244 Expect.equals(-271821, dt.year); 247 Expect.equals(-271821, dt.year);
245 Expect.equals(4, dt.month); 248 Expect.equals(4, dt.month);
246 Expect.equals(20, dt.day); 249 Expect.equals(20, dt.day);
247 Expect.equals(0, dt.hour); 250 Expect.equals(0, dt.hour);
248 Expect.equals(0, dt.minute); 251 Expect.equals(0, dt.minute);
249 Expect.equals(0, dt.second); 252 Expect.equals(0, dt.second);
250 Expect.equals(0, dt.millisecond); 253 Expect.equals(0, dt.millisecond);
251 // Make sure that we can build the extreme dates in local too. 254 // Make sure that we can build the extreme dates in local too.
252 dt = new DateTime.fromMillisecondsSinceEpoch(8640000000000000); 255 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
253 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); 256 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
254 Expect.equals(8640000000000000, dt.millisecondsSinceEpoch); 257 Expect.equals(_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
255 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000); 258 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
256 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute); 259 dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
257 Expect.equals(-8640000000000000, dt.millisecondsSinceEpoch); 260 Expect.equals(-_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
258 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(8640000000000001, 261 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
259 isUtc: true)); 262 _MAX_MILLISECONDS + 1, isUtc: true));
260 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(-8640000000000001, 263 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
261 isUtc: true)); 264 -_MAX_MILLISECONDS - 1, isUtc: true));
262 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(8640000000000001)) ; 265 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
263 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(-8640000000000001) ); 266 _MAX_MILLISECONDS + 1));
264 dt = new DateTime.fromMillisecondsSinceEpoch(8640000000000000); 267 Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
268 -_MAX_MILLISECONDS - 1));
269 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
265 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, 270 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day,
266 dt.hour, dt.minute, 0, 1)); 271 dt.hour, dt.minute, 0, 1));
267 dt = new DateTime.fromMillisecondsSinceEpoch(8640000000000000, isUtc: true); 272 dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
268 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, 273 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day,
269 dt.hour, dt.minute, 0, 1)); 274 dt.hour, dt.minute, 0, 1));
270 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000); 275 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
271 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day, 276 Expect.throws(() => new DateTime(dt.year, dt.month, dt.day,
272 dt.hour, dt.minute, 0, -1)); 277 dt.hour, dt.minute, 0, -1));
273 dt = new DateTime.fromMillisecondsSinceEpoch(-8640000000000000, isUtc: true); 278 dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
274 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day, 279 Expect.throws(() => new DateTime.utc(dt.year, dt.month, dt.day,
275 dt.hour, dt.minute, 0, -1)); 280 dt.hour, dt.minute, 0, -1));
276 } 281 }
277 282
278 void testUTCGetters() { 283 void testUTCGetters() {
279 var dt = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); 284 var dt = new DateTime.fromMillisecondsSinceEpoch(1305140315000, isUtc: true);
280 Expect.equals(2011, dt.year); 285 Expect.equals(2011, dt.year);
281 Expect.equals(5, dt.month); 286 Expect.equals(5, dt.month);
282 Expect.equals(11, dt.day); 287 Expect.equals(11, dt.day);
283 Expect.equals(18, dt.hour); 288 Expect.equals(18, dt.hour);
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 Expect.equals(true, dt1.isUtc); 846 Expect.equals(true, dt1.isUtc);
842 dt1 = DateTime.parse("0009-09-09 09:09:09.009-2134"); 847 dt1 = DateTime.parse("0009-09-09 09:09:09.009-2134");
843 Expect.equals(9, dt1.year); 848 Expect.equals(9, dt1.year);
844 Expect.equals(9, dt1.month); 849 Expect.equals(9, dt1.month);
845 Expect.equals(10, dt1.day); 850 Expect.equals(10, dt1.day);
846 Expect.equals(6, dt1.hour); 851 Expect.equals(6, dt1.hour);
847 Expect.equals(43, dt1.minute); 852 Expect.equals(43, dt1.minute);
848 Expect.equals(9, dt1.second); 853 Expect.equals(9, dt1.second);
849 Expect.equals(9, dt1.millisecond); 854 Expect.equals(9, dt1.millisecond);
850 Expect.equals(true, dt1.isUtc); 855 Expect.equals(true, dt1.isUtc);
856
857 Expect.throws(() => DateTime.parse("bad"), (e) => e is FormatException);
858 var bad_year = _MAX_MILLISECONDS ~/ 60 ~/ 60 ~/ 24 ~/ 365 + 10;
859 Expect.throws(() => DateTime.parse(bad_year.toString() + "-1-1"),
Lasse Reichstein Nielsen 2014/08/04 07:25:47 make it "-01-01", just so the reader (me) doesn't
srawlins 2014/08/04 19:58:30 Thanks so much for these tips, lrn@! I've added th
860 (e) => e is FormatException);
851 } 861 }
852 862
853 void testWeekday() { 863 void testWeekday() {
854 // 2011-10-06 is Summertime. 864 // 2011-10-06 is Summertime.
855 var d = new DateTime(2011, 10, 6, 0, 45, 37, 0); 865 var d = new DateTime(2011, 10, 6, 0, 45, 37, 0);
856 Expect.equals(DateTime.THURSDAY, d.weekday); 866 Expect.equals(DateTime.THURSDAY, d.weekday);
857 d = new DateTime.utc(2011, 10, 6, 0, 45, 37, 0); 867 d = new DateTime.utc(2011, 10, 6, 0, 45, 37, 0);
858 Expect.equals(DateTime.THURSDAY, d.weekday); 868 Expect.equals(DateTime.THURSDAY, d.weekday);
859 d = new DateTime(2011, 10, 5, 23, 45, 37, 0); 869 d = new DateTime(2011, 10, 5, 23, 45, 37, 0);
860 Expect.equals(DateTime.WEDNESDAY, d.weekday); 870 Expect.equals(DateTime.WEDNESDAY, d.weekday);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
920 testChangeTimeZone(); 930 testChangeTimeZone();
921 testSubAdd(); 931 testSubAdd();
922 testUnderflowAndOverflow(); 932 testUnderflowAndOverflow();
923 testDateStrings(); 933 testDateStrings();
924 testEquivalentYears(); 934 testEquivalentYears();
925 testExtremes(); 935 testExtremes();
926 testFarAwayDates(); 936 testFarAwayDates();
927 testWeekday(); 937 testWeekday();
928 testToStrings(); 938 testToStrings();
929 } 939 }
OLDNEW
« sdk/lib/core/date_time.dart ('K') | « sdk/lib/core/date_time.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698