checkCommands<S, M, C> function

void checkCommands<S, M, C>({
  1. required FutureOr<S> setup(
    1. Simulation sim
    ),
  2. required M model(),
  3. required C generate(
    1. Random random,
    2. M model
    ),
  4. required FutureOr<M> apply(
    1. Simulation sim,
    2. S subject,
    3. M model,
    4. C command,
    ),
  5. int seeds = 100,
  6. int commands = 30,
  7. int firstSeed = 0,
  8. FaultPolicy? faults,
  9. int maxSteps = 100000,
})

A property test over sequences of commands.

For each seed, setup creates the thing under test in a fresh simulated system and model creates a plain-Dart description of how it should behave. Then, commands times, generate draws a command (it may look at the model to pick a sensible one) and apply runs it against both, checks that they agree, and returns the model's next value.

A failure is rethrown as a SimulationFailure with the seed and the commands that led to it. Sequences are not shrunk.

Implementation

void checkCommands<S, M, C>({
  required FutureOr<S> Function(Simulation sim) setup,
  required M Function() model,
  required C Function(Random random, M model) generate,
  required FutureOr<M> Function(Simulation sim, S subject, M model, C command)
      apply,
  int seeds = 100,
  int commands = 30,
  int firstSeed = 0,
  FaultPolicy? faults,
  int maxSteps = 100000,
}) {
  for (var seed = firstSeed; seed < firstSeed + seeds; seed++) {
    final sim = Simulation._(seed, faults, maxSteps, const Duration(hours: 1));
    final history = <Object?>[];
    try {
      sim._run((sim) async {
        final subject = await setup(sim);
        var current = model();
        for (var i = 0; i < commands; i++) {
          final command = generate(sim.random, current);
          history.add(command);
          current = await apply(sim, subject, current, command);
        }
      }, null);
    } catch (e, s) {
      throw SimulationFailure(seed, e, s, sim._trace, commands: history);
    }
  }
}