OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.time.clock_test; |
| 16 |
| 17 import 'package:test/test.dart'; |
| 18 import 'package:quiver/time.dart'; |
| 19 |
| 20 Clock from(int y, int m, int d) => new Clock.fixed(new DateTime(y, m, d)); |
| 21 |
| 22 expectDate(DateTime date, int y, [int m = 1, int d = 1]) { |
| 23 expect(date, new DateTime(y, m, d)); |
| 24 } |
| 25 |
| 26 main() { |
| 27 group('clock', () { |
| 28 Clock subject; |
| 29 |
| 30 setUp(() { |
| 31 subject = new Clock.fixed(new DateTime(2013)); |
| 32 }); |
| 33 |
| 34 test('should return a non-null value from system clock', () { |
| 35 expect(const Clock().now(), isNotNull); |
| 36 }); |
| 37 |
| 38 // This test may be flaky on certain systems. I ran it over 10 million |
| 39 // cycles on my machine without any failures, but that's no guarantee. |
| 40 test('should be close enough to system clock', () { |
| 41 // At 10ms the test doesn't seem to be flaky. |
| 42 var epsilon = 10; |
| 43 expect( |
| 44 new DateTime.now().difference(new Clock().now()).inMilliseconds.abs(), |
| 45 lessThan(epsilon)); |
| 46 expect(new DateTime.now().difference(const Clock().now()).inMilliseconds |
| 47 .abs(), lessThan(epsilon)); |
| 48 }); |
| 49 |
| 50 test('should return time provided by custom TimeFunction', () { |
| 51 var time = new DateTime(2013); |
| 52 var fixedClock = new Clock(() => time); |
| 53 expect(fixedClock.now(), new DateTime(2013)); |
| 54 |
| 55 time = new DateTime(2014); |
| 56 expect(fixedClock.now(), new DateTime(2014)); |
| 57 }); |
| 58 |
| 59 test('should return fixed time', () { |
| 60 expect(new Clock.fixed(new DateTime(2013)).now(), new DateTime(2013)); |
| 61 }); |
| 62 |
| 63 test('should return time Duration ago', () { |
| 64 expect(subject.agoBy(new Duration(days: 366)), new DateTime(2012)); |
| 65 }); |
| 66 |
| 67 test('should return time Duration from now', () { |
| 68 expect(subject.fromNowBy(new Duration(days: 365)), new DateTime(2014)); |
| 69 }); |
| 70 |
| 71 test('should return time parts ago', () { |
| 72 expect(subject.ago( |
| 73 days: 1, |
| 74 hours: 1, |
| 75 minutes: 1, |
| 76 seconds: 1, |
| 77 milliseconds: 1, |
| 78 microseconds: 1000), new DateTime(2012, 12, 30, 22, 58, 58, 998)); |
| 79 }); |
| 80 |
| 81 test('should return time parts from now', () { |
| 82 expect(subject.fromNow( |
| 83 days: 1, |
| 84 hours: 1, |
| 85 minutes: 1, |
| 86 seconds: 1, |
| 87 milliseconds: 1, |
| 88 microseconds: 1000), new DateTime(2013, 1, 2, 1, 1, 1, 2)); |
| 89 }); |
| 90 |
| 91 test('should return time micros ago', () { |
| 92 expect( |
| 93 subject.microsAgo(1000), new DateTime(2012, 12, 31, 23, 59, 59, 999)); |
| 94 }); |
| 95 |
| 96 test('should return time micros from now', () { |
| 97 expect(subject.microsFromNow(1000), new DateTime(2013, 1, 1, 0, 0, 0, 1)); |
| 98 }); |
| 99 |
| 100 test('should return time millis ago', () { |
| 101 expect( |
| 102 subject.millisAgo(1000), new DateTime(2012, 12, 31, 23, 59, 59, 000)); |
| 103 }); |
| 104 |
| 105 test('should return time millis from now', () { |
| 106 expect(subject.millisFromNow(3), new DateTime(2013, 1, 1, 0, 0, 0, 3)); |
| 107 }); |
| 108 |
| 109 test('should return time seconds ago', () { |
| 110 expect( |
| 111 subject.secondsAgo(10), new DateTime(2012, 12, 31, 23, 59, 50, 000)); |
| 112 }); |
| 113 |
| 114 test('should return time seconds from now', () { |
| 115 expect(subject.secondsFromNow(3), new DateTime(2013, 1, 1, 0, 0, 3, 0)); |
| 116 }); |
| 117 |
| 118 test('should return time minutes ago', () { |
| 119 expect( |
| 120 subject.minutesAgo(10), new DateTime(2012, 12, 31, 23, 50, 0, 000)); |
| 121 }); |
| 122 |
| 123 test('should return time minutes from now', () { |
| 124 expect(subject.minutesFromNow(3), new DateTime(2013, 1, 1, 0, 3, 0, 0)); |
| 125 }); |
| 126 |
| 127 test('should return time hours ago', () { |
| 128 expect(subject.hoursAgo(10), new DateTime(2012, 12, 31, 14, 0, 0, 000)); |
| 129 }); |
| 130 |
| 131 test('should return time hours from now', () { |
| 132 expect(subject.hoursFromNow(3), new DateTime(2013, 1, 1, 3, 0, 0, 0)); |
| 133 }); |
| 134 |
| 135 test('should return time days ago', () { |
| 136 expectDate(subject.daysAgo(10), 2012, 12, 22); |
| 137 }); |
| 138 |
| 139 test('should return time days from now', () { |
| 140 expectDate(subject.daysFromNow(3), 2013, 1, 4); |
| 141 }); |
| 142 |
| 143 test('should return time months ago on the same date', () { |
| 144 expectDate(subject.monthsAgo(1), 2012, 12, 1); |
| 145 expectDate(subject.monthsAgo(2), 2012, 11, 1); |
| 146 expectDate(subject.monthsAgo(3), 2012, 10, 1); |
| 147 expectDate(subject.monthsAgo(4), 2012, 9, 1); |
| 148 }); |
| 149 |
| 150 test('should return time months from now on the same date', () { |
| 151 expectDate(subject.monthsFromNow(1), 2013, 2, 1); |
| 152 expectDate(subject.monthsFromNow(2), 2013, 3, 1); |
| 153 expectDate(subject.monthsFromNow(3), 2013, 4, 1); |
| 154 expectDate(subject.monthsFromNow(4), 2013, 5, 1); |
| 155 }); |
| 156 |
| 157 test('should go from 2013-05-31 to 2012-11-30', () { |
| 158 expectDate(from(2013, 5, 31).monthsAgo(6), 2012, 11, 30); |
| 159 }); |
| 160 |
| 161 test('should go from 2013-03-31 to 2013-02-28 (common year)', () { |
| 162 expectDate(from(2013, 3, 31).monthsAgo(1), 2013, 2, 28); |
| 163 }); |
| 164 |
| 165 test('should go from 2013-05-31 to 2013-02-28 (common year)', () { |
| 166 expectDate(from(2013, 5, 31).monthsAgo(3), 2013, 2, 28); |
| 167 }); |
| 168 |
| 169 test('should go from 2004-03-31 to 2004-02-29 (leap year)', () { |
| 170 expectDate(from(2004, 3, 31).monthsAgo(1), 2004, 2, 29); |
| 171 }); |
| 172 |
| 173 test('should go from 2013-03-31 to 2013-06-30', () { |
| 174 expectDate(from(2013, 3, 31).monthsFromNow(3), 2013, 6, 30); |
| 175 }); |
| 176 |
| 177 test('should go from 2003-12-31 to 2004-02-29 (common to leap)', () { |
| 178 expectDate(from(2003, 12, 31).monthsFromNow(2), 2004, 2, 29); |
| 179 }); |
| 180 |
| 181 test('should go from 2004-02-29 to 2003-02-28 by year', () { |
| 182 expectDate(from(2004, 2, 29).yearsAgo(1), 2003, 2, 28); |
| 183 }); |
| 184 |
| 185 test('should go from 2004-02-29 to 2003-02-28 by month', () { |
| 186 expectDate(from(2004, 2, 29).monthsAgo(12), 2003, 2, 28); |
| 187 }); |
| 188 |
| 189 test('should go from 2004-02-29 to 2005-02-28 by year', () { |
| 190 expectDate(from(2004, 2, 29).yearsFromNow(1), 2005, 2, 28); |
| 191 }); |
| 192 |
| 193 test('should go from 2004-02-29 to 2005-02-28 by month', () { |
| 194 expectDate(from(2004, 2, 29).monthsFromNow(12), 2005, 2, 28); |
| 195 }); |
| 196 |
| 197 test('should return time years ago on the same date', () { |
| 198 expectDate(subject.yearsAgo(1), 2012, 1, 1); // leap year |
| 199 expectDate(subject.yearsAgo(2), 2011, 1, 1); |
| 200 expectDate(subject.yearsAgo(3), 2010, 1, 1); |
| 201 expectDate(subject.yearsAgo(4), 2009, 1, 1); |
| 202 expectDate(subject.yearsAgo(5), 2008, 1, 1); // leap year |
| 203 expectDate(subject.yearsAgo(6), 2007, 1, 1); |
| 204 expectDate(subject.yearsAgo(30), 1983, 1, 1); |
| 205 expectDate(subject.yearsAgo(2013), 0, 1, 1); |
| 206 }); |
| 207 |
| 208 test('should return time years from now on the same date', () { |
| 209 expectDate(subject.yearsFromNow(1), 2014, 1, 1); |
| 210 expectDate(subject.yearsFromNow(2), 2015, 1, 1); |
| 211 expectDate(subject.yearsFromNow(3), 2016, 1, 1); |
| 212 expectDate(subject.yearsFromNow(4), 2017, 1, 1); |
| 213 expectDate(subject.yearsFromNow(5), 2018, 1, 1); |
| 214 expectDate(subject.yearsFromNow(6), 2019, 1, 1); |
| 215 expectDate(subject.yearsFromNow(30), 2043, 1, 1); |
| 216 expectDate(subject.yearsFromNow(1000), 3013, 1, 1); |
| 217 }); |
| 218 }); |
| 219 } |
OLD | NEW |