commit | 49cd69511d14d041a2504f60d5775b8febcd8274 | [log] [tgz] |
---|---|---|
author | Devon Carew <devoncarew@google.com> | Wed Jun 15 13:34:09 2022 -0700 |
committer | GitHub <noreply@github.com> | Wed Jun 15 13:34:09 2022 -0700 |
tree | 43605a6a217632b01658220f76ebb4829dd58818 | |
parent | a75eb69c8e939e2e7eab70e4728da3bcf004e717 [diff] | |
parent | a73ef13c5be333c823a9a06d9faccac55ed4a8b9 [diff] |
Merge pull request #35 from dart-lang/prep_publish prep for publishing 1.1.1
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!')); }); }); }