commit | 5631a0612f4ac7e1b32f7c9a00fc7c00a41615e1 | [log] [tgz] |
---|---|---|
author | Nate Bosch <nbosch@google.com> | Tue Feb 02 11:36:37 2021 -0800 |
committer | GitHub <noreply@github.com> | Tue Feb 02 11:36:37 2021 -0800 |
tree | 134c45342a1d95a379f72c033fd833311bee887f | |
parent | 7b79e697b868325091b4cfa48a2a39c7bce17d62 [diff] |
Prepare to publish stable null safety (#28)
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!')); }); }); }