commit | 3b52a74f3c788adf61ae325f660510f07b585d33 | [log] [tgz] |
---|---|---|
author | Nate Bosch <nbosch1@gmail.com> | Thu Dec 12 16:19:31 2019 -0800 |
committer | GitHub <noreply@github.com> | Thu Dec 12 16:19:31 2019 -0800 |
tree | bbe72721a525395d14a0310bbe00f93fc303f3b8 | |
parent | a288ff9c2531747103d58c1879273a2dba470cab [diff] |
Update for repo best practices (#14) - Use `pub.dev` links over `dardocs.org`. - In example, follow `package:pedantic` style for function typed argument. - Drop unused author field from pubspec. - Use a simpler constraint on `package:meta` since the SDK constraint already prevents us from picking up older 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!')); }); }); }