gg_fake_timer 1.0.4 copy "gg_fake_timer: ^1.0.4" to clipboard
gg_fake_timer: ^1.0.4 copied to clipboard

A class faking dart's Timer class. Can be used for test scenarios.

With GgFakeTimer it is easy to control Timer during tests

Usage #

Create a single shot timer #

// Define some constants
  const interval = Duration(seconds: 1);
  const oneMicroSecond = Duration(microseconds: 1);

  // ..........................
  // Create a single shot timer
  print('Single shot timer with elapse(...)');

  int counter = 0;
  var timer = GgFakeTimer(interval, () => counter++, isPeriodic: false);

  // Forward timer less then interval
  // Timer did not fire
  timer.elapse(interval - oneMicroSecond);
  assert(counter == 0);

  // Forward timer to interval
  // Timer should fire
  timer.elapse(oneMicroSecond);
  assert(counter == 1);

  // After firing a single time,
  // timer should not be active anymore.
  assert(timer.isActive == false);
  assert(timer.isCancelled == true);

  // When time elapses further, timer does not fire
  timer.elapse(interval);
  assert(counter == 1);

  // ..........................
  // Create a single shot timer
  print('Single shot timer with fire(...)');

  counter = 0;
  timer = GgFakeTimer(interval, () => counter++, isPeriodic: false);

  // Forward timer to interval
  // Timer should fire
  timer.fire();
  assert(counter == 1);

  // After firing a single time,
  // timer should not be active anymore.
  assert(timer.isActive == false);
  assert(timer.isCancelled == true);

  // Calling fire() another time will have no effect
  timer.fire();
  assert(counter == 1);

  // ..........................
  // Cancel a shingle shot timer
  print('Cancelling a single shot timer');

  counter = 0;
  timer = GgFakeTimer(interval, () => counter++, isPeriodic: false);

  // Cancel timer
  timer.cancel();

  // Forward timer to interval
  // Timer did not fire because it was cancelled.
  timer.elapse(interval);
  assert(counter == 0);

  // .........................
  // Work with periodic timers
  counter = 0;
  timer = GgFakeTimer.periodic(interval, (_) => counter++);

  // Forward time less then interval
  // Timer did not fire
  timer.elapse(interval - oneMicroSecond);
  assert(counter == 0);

  // Forward time to interval
  // Timer should fire
  timer.elapse(oneMicroSecond);
  assert(counter == 1);

  // Forward timer short before second interval
  // Timer should not have fired
  timer.elapse(interval - oneMicroSecond);
  assert(counter == 1);

  // Forward timer over second interval
  // Timer should have fired
  timer.elapse(oneMicroSecond);
  assert(counter == 2);

  // Cancel the timer
  timer.cancel();
  assert(timer.isActive == false);
  assert(timer.isCancelled == true);

  // Forward another interval
  // Timer should not fire anymore.
  timer.elapse(interval);
  timer.elapse(interval);
  assert(counter == 2);

  print('Did work!');

Features and bugs #

Please file feature requests and bugs at GitHub.

0
likes
160
pub points
19%
popularity

Publisher

verified publisherinlavigo.com

A class faking dart's Timer class. Can be used for test scenarios.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on gg_fake_timer