OLD | NEW |
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 /** | 5 /** |
6 * A simple mocking/spy library. | 6 * A simple mocking/spy library. |
7 * | 7 * |
8 * To create a mock objects for some class T, create a new class using: | 8 * To create a mock objects for some class T, create a new class using: |
9 * | 9 * |
10 * class MockT extends Mock implements T {}; | 10 * class MockT extends Mock implements T {}; |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 /** Returns the [matcher]'s representation. */ | 358 /** Returns the [matcher]'s representation. */ |
359 String toString() => matcher.toString(); | 359 String toString() => matcher.toString(); |
360 } | 360 } |
361 | 361 |
362 /** | 362 /** |
363 * Every call to a [Mock] object method is logged. The logs are | 363 * Every call to a [Mock] object method is logged. The logs are |
364 * kept in instances of [LogEntry]. | 364 * kept in instances of [LogEntry]. |
365 */ | 365 */ |
366 class LogEntry { | 366 class LogEntry { |
367 /** The time of the event. */ | 367 /** The time of the event. */ |
368 Date time; | 368 DateTime time; |
369 | 369 |
370 /** The mock object name, if any. */ | 370 /** The mock object name, if any. */ |
371 final String mockName; | 371 final String mockName; |
372 | 372 |
373 /** The method name. */ | 373 /** The method name. */ |
374 final String methodName; | 374 final String methodName; |
375 | 375 |
376 /** The parameters. */ | 376 /** The parameters. */ |
377 final List args; | 377 final List args; |
378 | 378 |
379 /** The behavior that resulted. */ | 379 /** The behavior that resulted. */ |
380 final Action action; | 380 final Action action; |
381 | 381 |
382 /** The value that was returned (if no throw). */ | 382 /** The value that was returned (if no throw). */ |
383 final value; | 383 final value; |
384 | 384 |
385 LogEntry(this.mockName, this.methodName, | 385 LogEntry(this.mockName, this.methodName, |
386 this.args, this.action, [this.value]) { | 386 this.args, this.action, [this.value]) { |
387 time = new Date.now(); | 387 time = new DateTime.now(); |
388 } | 388 } |
389 | 389 |
390 String _pad2(int val) => (val >= 10 ? '$val' : '0$val'); | 390 String _pad2(int val) => (val >= 10 ? '$val' : '0$val'); |
391 | 391 |
392 String toString([Date baseTime]) { | 392 String toString([DateTime baseTime]) { |
393 Description d = new StringDescription(); | 393 Description d = new StringDescription(); |
394 if (baseTime == null) { | 394 if (baseTime == null) { |
395 // Show absolute time. | 395 // Show absolute time. |
396 d.add('${time.hour}:${_pad2(time.minute)}:' | 396 d.add('${time.hour}:${_pad2(time.minute)}:' |
397 '${_pad2(time.second)}.${time.millisecond}> '); | 397 '${_pad2(time.second)}.${time.millisecond}> '); |
398 } else { | 398 } else { |
399 // Show relative time. | 399 // Show relative time. |
400 int delta = time.millisecondsSinceEpoch - baseTime.millisecondsSinceEpoch; | 400 int delta = time.millisecondsSinceEpoch - baseTime.millisecondsSinceEpoch; |
401 int secs = delta ~/ 1000; | 401 int secs = delta ~/ 1000; |
402 int msecs = delta % 1000; | 402 int msecs = delta % 1000; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 } | 558 } |
559 } | 559 } |
560 return true; | 560 return true; |
561 } | 561 } |
562 | 562 |
563 /** | 563 /** |
564 * Turn the logs into human-readable text. If [baseTime] is specified | 564 * Turn the logs into human-readable text. If [baseTime] is specified |
565 * then each entry is prefixed with the offset from that time in | 565 * then each entry is prefixed with the offset from that time in |
566 * milliseconds; otherwise the time of day is used. | 566 * milliseconds; otherwise the time of day is used. |
567 */ | 567 */ |
568 String toString([Date baseTime]) { | 568 String toString([DateTime baseTime]) { |
569 String s = ''; | 569 String s = ''; |
570 for (var e in logs) { | 570 for (var e in logs) { |
571 s = '$s${e.toString(baseTime)}\n'; | 571 s = '$s${e.toString(baseTime)}\n'; |
572 } | 572 } |
573 return s; | 573 return s; |
574 } | 574 } |
575 | 575 |
576 /** | 576 /** |
577 * Find the first log entry that satisfies [logFilter] and | 577 * Find the first log entry that satisfies [logFilter] and |
578 * return its position. A search [start] position can be provided | 578 * return its position. A search [start] position can be provided |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 return newList; | 656 return newList; |
657 } | 657 } |
658 } | 658 } |
659 | 659 |
660 /** | 660 /** |
661 * Returns log events that happened after [when]. If [inPlace] | 661 * Returns log events that happened after [when]. If [inPlace] |
662 * is true, then it returns this LogEntryList after removing | 662 * is true, then it returns this LogEntryList after removing |
663 * the entries that happened up to [when]; otherwise a new | 663 * the entries that happened up to [when]; otherwise a new |
664 * list is created. | 664 * list is created. |
665 */ | 665 */ |
666 LogEntryList after(Date when, [bool inPlace = false]) => | 666 LogEntryList after(DateTime when, [bool inPlace = false]) => |
667 _tail((e) => e.time > when, inPlace, 'after $when', logs.length); | 667 _tail((e) => e.time > when, inPlace, 'after $when', logs.length); |
668 | 668 |
669 /** | 669 /** |
670 * Returns log events that happened from [when] onwards. If | 670 * Returns log events that happened from [when] onwards. If |
671 * [inPlace] is true, then it returns this LogEntryList after | 671 * [inPlace] is true, then it returns this LogEntryList after |
672 * removing the entries that happened before [when]; otherwise | 672 * removing the entries that happened before [when]; otherwise |
673 * a new list is created. | 673 * a new list is created. |
674 */ | 674 */ |
675 LogEntryList from(Date when, [bool inPlace = false]) => | 675 LogEntryList from(DateTime when, [bool inPlace = false]) => |
676 _tail((e) => e.time >= when, inPlace, 'from $when', logs.length); | 676 _tail((e) => e.time >= when, inPlace, 'from $when', logs.length); |
677 | 677 |
678 /** | 678 /** |
679 * Returns log events that happened until [when]. If [inPlace] | 679 * Returns log events that happened until [when]. If [inPlace] |
680 * is true, then it returns this LogEntryList after removing | 680 * is true, then it returns this LogEntryList after removing |
681 * the entries that happened after [when]; otherwise a new | 681 * the entries that happened after [when]; otherwise a new |
682 * list is created. | 682 * list is created. |
683 */ | 683 */ |
684 LogEntryList until(Date when, [bool inPlace = false]) => | 684 LogEntryList until(DateTime when, [bool inPlace = false]) => |
685 _head((e) => e.time > when, inPlace, 'until $when', logs.length); | 685 _head((e) => e.time > when, inPlace, 'until $when', logs.length); |
686 | 686 |
687 /** | 687 /** |
688 * Returns log events that happened before [when]. If [inPlace] | 688 * Returns log events that happened before [when]. If [inPlace] |
689 * is true, then it returns this LogEntryList after removing | 689 * is true, then it returns this LogEntryList after removing |
690 * the entries that happened from [when] onwards; otherwise a new | 690 * the entries that happened from [when] onwards; otherwise a new |
691 * list is created. | 691 * list is created. |
692 */ | 692 */ |
693 LogEntryList before(Date when, [bool inPlace = false]) => | 693 LogEntryList before(DateTime when, [bool inPlace = false]) => |
694 _head((e) => e.time >= when, inPlace, 'before $when', logs.length); | 694 _head((e) => e.time >= when, inPlace, 'before $when', logs.length); |
695 | 695 |
696 /** | 696 /** |
697 * Returns log events that happened after [logEntry]'s time. | 697 * Returns log events that happened after [logEntry]'s time. |
698 * If [inPlace] is true, then it returns this LogEntryList after | 698 * If [inPlace] is true, then it returns this LogEntryList after |
699 * removing the entries that happened up to [when]; otherwise a new | 699 * removing the entries that happened up to [when]; otherwise a new |
700 * list is created. If [logEntry] is null the current time is used. | 700 * list is created. If [logEntry] is null the current time is used. |
701 */ | 701 */ |
702 LogEntryList afterEntry(LogEntry logEntry, [bool inPlace = false]) => | 702 LogEntryList afterEntry(LogEntry logEntry, [bool inPlace = false]) => |
703 after(logEntry == null ? new Date.now() : logEntry.time); | 703 after(logEntry == null ? new DateTime.now() : logEntry.time); |
704 | 704 |
705 /** | 705 /** |
706 * Returns log events that happened from [logEntry]'s time onwards. | 706 * Returns log events that happened from [logEntry]'s time onwards. |
707 * If [inPlace] is true, then it returns this LogEntryList after | 707 * If [inPlace] is true, then it returns this LogEntryList after |
708 * removing the entries that happened before [when]; otherwise | 708 * removing the entries that happened before [when]; otherwise |
709 * a new list is created. If [logEntry] is null the current time is used. | 709 * a new list is created. If [logEntry] is null the current time is used. |
710 */ | 710 */ |
711 LogEntryList fromEntry(LogEntry logEntry, [bool inPlace = false]) => | 711 LogEntryList fromEntry(LogEntry logEntry, [bool inPlace = false]) => |
712 from(logEntry == null ? new Date.now() : logEntry.time); | 712 from(logEntry == null ? new DateTime.now() : logEntry.time); |
713 | 713 |
714 /** | 714 /** |
715 * Returns log events that happened until [logEntry]'s time. If | 715 * Returns log events that happened until [logEntry]'s time. If |
716 * [inPlace] is true, then it returns this LogEntryList after removing | 716 * [inPlace] is true, then it returns this LogEntryList after removing |
717 * the entries that happened after [when]; otherwise a new | 717 * the entries that happened after [when]; otherwise a new |
718 * list is created. If [logEntry] is null the epoch time is used. | 718 * list is created. If [logEntry] is null the epoch time is used. |
719 */ | 719 */ |
720 LogEntryList untilEntry(LogEntry logEntry, [bool inPlace = false]) => | 720 LogEntryList untilEntry(LogEntry logEntry, [bool inPlace = false]) => |
721 until(logEntry == null ? | 721 until(logEntry == null ? |
722 new Date.fromMillisecondsSinceEpoch(0) : logEntry.time); | 722 new DateTime.fromMillisecondsSinceEpoch(0) : logEntry.time); |
723 | 723 |
724 /** | 724 /** |
725 * Returns log events that happened before [logEntry]'s time. If | 725 * Returns log events that happened before [logEntry]'s time. If |
726 * [inPlace] is true, then it returns this LogEntryList after removing | 726 * [inPlace] is true, then it returns this LogEntryList after removing |
727 * the entries that happened from [when] onwards; otherwise a new | 727 * the entries that happened from [when] onwards; otherwise a new |
728 * list is created. If [logEntry] is null the epoch time is used. | 728 * list is created. If [logEntry] is null the epoch time is used. |
729 */ | 729 */ |
730 LogEntryList beforeEntry(LogEntry logEntry, [bool inPlace = false]) => | 730 LogEntryList beforeEntry(LogEntry logEntry, [bool inPlace = false]) => |
731 before(logEntry == null ? | 731 before(logEntry == null ? |
732 new Date.fromMillisecondsSinceEpoch(0) : logEntry.time); | 732 new DateTime.fromMillisecondsSinceEpoch(0) : logEntry.time); |
733 | 733 |
734 /** | 734 /** |
735 * Returns log events that happened after the first event in [segment]. | 735 * Returns log events that happened after the first event in [segment]. |
736 * If [inPlace] is true, then it returns this LogEntryList after removing | 736 * If [inPlace] is true, then it returns this LogEntryList after removing |
737 * the entries that happened earlier; otherwise a new list is created. | 737 * the entries that happened earlier; otherwise a new list is created. |
738 */ | 738 */ |
739 LogEntryList afterFirst(LogEntryList segment, [bool inPlace = false]) => | 739 LogEntryList afterFirst(LogEntryList segment, [bool inPlace = false]) => |
740 afterEntry(segment.first, inPlace); | 740 afterEntry(segment.first, inPlace); |
741 | 741 |
742 /** | 742 /** |
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1465 } | 1465 } |
1466 } | 1466 } |
1467 } | 1467 } |
1468 | 1468 |
1469 /** Clear both logs and behavior. */ | 1469 /** Clear both logs and behavior. */ |
1470 void reset() { | 1470 void reset() { |
1471 resetBehavior(); | 1471 resetBehavior(); |
1472 clearLogs(); | 1472 clearLogs(); |
1473 } | 1473 } |
1474 } | 1474 } |
OLD | NEW |