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

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

example/gg_fake_timer_example.dart

import 'package:gg_fake_timer/gg_fake_timer.dart';

void main() {
  // 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!');
}
1
likes
150
points
124
downloads

Publisher

verified publisherinlavigo.com

Weekly Downloads

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on gg_fake_timer