commit | e2018d8eeecc26f6b014a2f73491e64da53703cc | [log] [tgz] |
---|---|---|
author | Nate Bosch <nbosch1@gmail.com> | Tue Sep 22 09:57:46 2020 -0700 |
committer | GitHub <noreply@github.com> | Tue Sep 22 09:57:46 2020 -0700 |
tree | af2c967f2a8b6d7df43596b0560730066f6f80f4 | |
parent | 737be9ac12b5f01c1f55f95c9fe6e136bfe43e13 [diff] |
Prepare for the 2.11 dev SDKs (#23) Bump the upper bound to allow 2.10 stable and 2.11.0 dev SDK versions.
This package provides a Clock
class which encapsulates the notion of the “current time” and provides easy access to points relative to the current time. Different Clock
s can have a different notion of the current time, and the default top-level clock
's notion can be swapped out to reliably test timing-dependent code.
For example, you can use clock
in your libraries like this:
// run_with_timing.dart import 'package:clock/clock.dart'; /// Runs [callback] and prints how long it took. T runWithTiming<T>(T Function() callback) { var stopwatch = clock.stopwatch()..start(); var result = callback(); print('It took ${stopwatch.elapsed}!'); return result; }
...and then test your code using the fake_async
package, which automatically overrides the current clock:
// run_with_timing_test.dart import 'run_with_timing.dart'; import 'package:fake_async/fake_async.dart'; import 'package:test/test.dart'; void main() { test('runWithTiming() prints the elapsed time', () { FakeAsync().run((async) { expect(() { runWithTiming(() { async.elapse(Duration(seconds: 10)); }); }, prints('It took 0:00:10.000000!')); }); }); }