execute method

  1. @override
Future<void> execute({
  1. required CancelToken cancelToken,
  2. required TestReport report,
  3. required TestController tester,
})

Executes the step. This will succeed if, and only if, there exists a sub-step and when executed, the sub-step throws an exception.

Implementation

@override
Future<void> execute({
  required CancelToken cancelToken,
  required TestReport report,
  required TestController tester,
}) async {
  var step = tester.resolveVariable(this.step);

  var name = '$id()';
  log(
    name,
    tester: tester,
  );

  if (step == null) {
    throw Exception('expect_failure: failing due to no sub-step');
  } else {
    // This purposefully does not use [TestController.executeStep] because
    // that provides no way to keep the sub-step from triggering an error.
    // So instead, the logic from the controller is copied to ensure the
    // report has the step but failures are treated as successes.
    var testStep = TestStep.fromDynamic(step);

    var runnerStep = tester.registry.getRunnerStep(
      id: testStep.id,
      values: testStep.values,
    )!;
    if (cancelToken.cancelled == true) {
      throw Exception('[CANCELLED]: the step has been cancelled.');
    }
    if (tester.delays.preStep.inMilliseconds > 0) {
      await runnerStep.preStepSleep(tester.delays.preStep);
    }

    if (cancelToken.cancelled == true) {
      throw Exception('[CANCELLED]: the step has been cancelled.');
    }
    report.startStep(
      testStep,
      subStep: true,
    );
    var failureEncountered = false;
    try {
      try {
        await runnerStep.execute(
          cancelToken: cancelToken,
          report: report,
          tester: tester,
        );
      } finally {
        report.endStep(testStep);
      }

      if (cancelToken.cancelled == true) {
        throw Exception('[CANCELLED]: the step has been cancelled.');
      }
      if (tester.delays.postStep.inMilliseconds > 0) {
        await runnerStep.postStepSleep(tester.delays.preStep);
      }
    } catch (e) {
      failureEncountered = true;
      log(
        'expect_failure: Expected exception encountered: $e',
        tester: tester,
      );
    }
    if (failureEncountered != true) {
      throw Exception('expect_failure: failing lack of exception');
    }
  }
}