Name: clock
Owner: Dart
Description: null
Created: 2018-03-06 04:05:56.0
Updated: 2018-05-02 20:01:55.0
Pushed: 2018-05-02 20:01:57.0
Homepage: https://pub.dartlang.org/packages/clock
Size: 55
Language: Dart
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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:
un_with_timing.dart
rt 'package:clock/clock.dart';
Runs [callback] and prints how long it took.
nWithTiming<T>(T callback()) {
r stopwatch = clock.stopwatch()..start();
r result = callback();
int("It took ${stopwatch.elapsed}!");
turn result;
…and then test your code using the fake_async
package, which
automatically overrides the current clock:
un_with_timing_test.dart
rt 'run_with_timing.dart';
rt 'package:fake_async/fake_async.dart';
rt 'package:test/test.dart';
main() {
st("runWithTiming() prints the elapsed time", () {
new FakeAsync().run((async) {
expect(() {
runWithTiming(() {
async.elapse(new Duration(seconds: 10));
});
}, prints("It took 0:00:10.000000!"));
});
;