dart-lang/clock

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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 Clocks 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!"));
});
;


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.