clock
v1.1.2A fakeable wrapper for dart:core clock APIs.
Package archive: https://pubdev.letsnova.ru/api/archives/clock/1.1.2.tar.gz
dart pub add clockReadme
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:
// 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!'));
});
});
}
Changelog
1.1.2
- Require Dart 3.4
- Move to
dart-lang/toolsmonorepo.
1.1.1
- Switch to using
package:lints. - Populate the pubspec
repositoryfield.
1.1.0
- Update SDK constraints to
>=2.12.0 <3.0.0. - Update to null safety.
1.0.1
- Update to lowercase Dart core library constants.
1.0.0
This release contains the Clock class that was defined in quiver. It's
backwards-compatible with the quiver version, and mostly
backwards-compatible with the old version of the clock package.
New Features
-
A top-level
clockfield has been added that provides a defaultClockimplementation. It can be controlled by thewithClock()function. It should generally be used in preference to manual dependency-injection, since it will work with thefake_asyncpackage. -
A
Clock.stopwatch()method has been added that creates aStopwatchthat uses the clock as its source of time.
Changes Relative to clock 0.1
-
The top-level
newgetter andgetStopwatch()methods are deprecated.clock.new()andclock.stopwatch()should be used instead. -
Clock.getStopwatch()is deprecated.Clock.stopwatch()should be used instead. -
The
isFinalargument towithClock()is deprecated. -
new Clock()now takes an optional positional argument that returns the current time as aDateTimeinstead of its old arguments. -
Clock.now()is now a method rather than a getter.
