Search packages, repositories and app categories

GitHub repositorydart-archive/clock

clock

A fakeable wrapper for dart:core clock APIs.

clock project cover
Stars
41
Forks
16
Last push (UTC)
Oct 28, 2024
Project status
Archived

Original README

English project snapshot. Visit GitHub for the latest content.

[!IMPORTANT]
This repo has moved to https://github.com/dart-lang/tools/tree/main/pkgs/clock

Dart CI pub package package publisher

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!'));
    });
  });
}
Dart Archive GitHub avatar
GitHub author

Dart Archive ↗

Legacy projects kept around for posterity – See github.com/dart-lang for current work

LanguagesDart

Dependencies used

Dependency list 2 items

  • dart_flutter_team_lintsDevelopment^3.0.0
  • testDevelopment^1.16.6

How do I choose a repository?

Start with your use case, compare repository activity, and check the original project before adopting it.

  1. Search by repository name, owner or description, then narrow the results by topic.
  2. Compare stars, forks and recent pushes. Stars show community interest, not a guarantee of quality.
  3. Read the README, license and issues on GitHub before using a project.

Sources and maintenance

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗