clock 0.1.0

  • README.md
  • Installing
  • Versions
  • --

clock pub package

The clock package provides testable replacements for the system clock APIs (new DateTime.now() and new Stopwatch()).

##Usage

Access now and getStopwatch() instead of new DateTime.now() and new Stopwatch(). You may want to import with a prefix:

import 'package:clock/clock.dart' as clock;

int get currentYear => clock.now.year;

Duration timeAction(action()) {
  var stopwatch = clock.getStopwatch()..start();
  action();
  return stopwatch.elapsed;
}

Then use withClock(new Clock(...), () { /* test code */ }); to make your tests deterministic:

import 'package:clock/clock.dart';
import 'package:unittest/unittest.dart';

import 'time_utils.dart'; // What we're testing.

main() {
  group('time utils', () {
  
    test('currentYear should return the current year', () {
      withClock(new Clock(initialTime: new DateTime(2014, 3, 6)), () {
        expect(currentYear, 2014);
      });
    });

    test('timeAction should time the action', () {
      var elapsed = Duration.ZERO;
      
      withClock(new Clock(elapsed: () => elapsed), () {
        expect(timeAction(() {
          elapsed += const Duration(seconds: 5);
        }), const Duration(seconds: 5));
      });
    });
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  clock: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:clock/clock.dart';
  
Version Uploaded Documentation Archive
1.0.1 Jul 14, 2018 Go to the documentation of clock 1.0.1 Download clock 1.0.1 archive
1.0.0 May 1, 2018 Go to the documentation of clock 1.0.0 Download clock 1.0.0 archive
0.1.2 Oct 7, 2014 Go to the documentation of clock 0.1.2 Download clock 0.1.2 archive
0.1.1 Oct 7, 2014 Go to the documentation of clock 0.1.1 Download clock 0.1.1 archive
0.1.0 Oct 7, 2014 Go to the documentation of clock 0.1.0 Download clock 0.1.0 archive
0.0.1 Oct 6, 2014 Go to the documentation of clock 0.0.1 Download clock 0.0.1 archive
Awaiting analysis to complete.