clock 0.0.1

  • README.md
  • Installing
  • Versions
  • --

clock #

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

##Usage

Write some code using the top-level now (instead of new DateTime.now()) and getStopwatch() (instead of new Stopwatch()). You may want to import with a prefix:

library time_utils;

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

DateTime startOfDay([DateTime time]) {
  if(time == null) time = clock.now;
  return new DateTime(time.year, time.month, time.day);
}

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

Then test it using withClock and Clock.relative:

library time_utils.test;

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

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

main() {
  group('time utils', () {
  
    clock.Clock fakeClock;
    Duration elapsed;
    setUp(() {
      fakeClock = new clock.Clock.relative(new DateTime(2014, 3, 6, 10, 10, 10), 
          () => elapsed);
      elapsed = Duration.ZERO;
    });
  
    test('startOfDay should default to using now', () {
      clock.withClock(fakeClock, () {
        expect(startOfDay(), new DateTime(2014, 3, 6));
      });
    });

    test('timeAction should time the action', () {
      clock.withClock(fakeClock, () {
        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.0.1

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.