1
+ import 'package:clock/clock.dart' ;
1
2
import 'package:test/test.dart' ;
2
3
import 'package:time/time.dart' ;
3
4
4
5
void main () {
6
+ final date = DateTime (2000 , 1 , 1 );
7
+
5
8
group ('TimeExtension' , () {
6
9
group ('Integers' , () {
7
10
test ('can be converted into weeks' , () {
@@ -103,30 +106,36 @@ void main() {
103
106
});
104
107
105
108
test ('can handle isToday' , () {
106
- final today = DateTime .now ();
107
- final yesterday = DateTime .now ().subtract (Duration (days: 1 ));
108
- final tomorrow = DateTime .now ().add (Duration (days: 1 ));
109
- expect (today.isToday, true );
110
- expect (yesterday.isToday, false );
111
- expect (tomorrow.isToday, false );
109
+ final today = date;
110
+ withClock (Clock .fixed (today), () {
111
+ final yesterday = today.subtract (Duration (days: 1 ));
112
+ final tomorrow = today.add (Duration (days: 1 ));
113
+ expect (today.isToday, true );
114
+ expect (yesterday.isToday, false );
115
+ expect (tomorrow.isToday, false );
116
+ });
112
117
});
113
118
114
119
test ('can handle isTomorrow' , () {
115
- final today = DateTime .now ();
116
- final yesterday = DateTime .now ().subtract (Duration (days: 1 ));
117
- final tomorrow = DateTime .now ().add (Duration (days: 1 ));
118
- expect (today.isTomorrow, false );
119
- expect (yesterday.isTomorrow, false );
120
- expect (tomorrow.isTomorrow, true );
120
+ final today = date;
121
+ withClock (Clock .fixed (today), () {
122
+ final yesterday = today.subtract (Duration (days: 1 ));
123
+ final tomorrow = today.add (Duration (days: 1 ));
124
+ expect (today.isTomorrow, false );
125
+ expect (yesterday.isTomorrow, false );
126
+ expect (tomorrow.isTomorrow, true );
127
+ });
121
128
});
122
129
123
130
test ('can handle wasYesterday' , () {
124
- final today = DateTime .now ();
125
- final yesterday = DateTime .now ().subtract (Duration (days: 1 ));
126
- final tomorrow = DateTime .now ().add (Duration (days: 1 ));
127
- expect (today.wasYesterday, false );
128
- expect (yesterday.wasYesterday, true );
129
- expect (tomorrow.wasYesterday, false );
131
+ final today = date;
132
+ withClock (Clock .fixed (today), () {
133
+ final yesterday = today.subtract (Duration (days: 1 ));
134
+ final tomorrow = today.add (Duration (days: 1 ));
135
+ expect (today.wasYesterday, false );
136
+ expect (yesterday.wasYesterday, true );
137
+ expect (tomorrow.wasYesterday, false );
138
+ });
130
139
});
131
140
132
141
test ('can handle isLeapYear' , () {
@@ -535,11 +544,15 @@ void main() {
535
544
});
536
545
537
546
test ('can be converted into a future DateTime' , () {
538
- expect (7. days.fromNow, _isAbout (DateTime .now () + 7. days));
547
+ withClock (Clock .fixed (date), () {
548
+ expect (7. days.fromNow, date + 7. days);
549
+ });
539
550
});
540
551
541
552
test ('can be converted into a previous DateTime' , () {
542
- expect (7. days.ago, _isAbout (DateTime .now () - 7. days));
553
+ withClock (Clock .fixed (date), () {
554
+ expect (7. days.ago, date - 7. days);
555
+ });
543
556
});
544
557
545
558
test ('Can be used to pause the program flow' , () async {
@@ -551,10 +564,4 @@ void main() {
551
564
expect (extraTime >= 0 , true );
552
565
});
553
566
});
554
- }
555
-
556
- // Checks if the two times returned a *just* about equal. Since `fromNow` and
557
- // `ago` use DateTime.now(), we can't create an expected condition that is
558
- // exactly equal.
559
- Matcher _isAbout (DateTime expected) =>
560
- predicate <DateTime >((dateTime) => dateTime.millisecondsSinceEpoch - expected.millisecondsSinceEpoch < 1 );
567
+ }
0 commit comments