execute method

StepResult execute()

Executes the internal callback logic to determine test step outcomes.

Dynamically handles input arguments, tests for expected exceptions, captures diagnostic stack traces on unexpected failures, and handles string-based assertion mismatches.

Returns a corresponding StepResult detailing the outcome.

Implementation

StepResult execute() {
  try {
    // Execute callback if present
    if (callback != null) {
      if (params != null) {
        callback!(params);
      } else {
        callback!();
      }

      // If we expected an exception but got none
      if (exceptionType != null) {
        return StepResult.failed(
          'Expected $exceptionType but no exception was thrown',
          null,
        );
      }

      return StepResult.passed();
    }

    // No callback, just description
    return StepResult.nothing();
  } catch (e, st) {
    // Exception was thrown
    if (exceptionType != null) {
      // Check if caught exception matches expected type
      if (_isInstanceOfExpectedException(e)) {
        // test passes if no error message was tested
        if (exceptionMessage == null) {
          return StepResult.passed();
        }
        // test also passes if the supplied text matches the error text
        if (e.toString() == exceptionMessage) {
          return StepResult.passed();
        }
        // if a exception message was tested and didn't match
        return StepResult.failed(
          'Expected $exceptionMessage\n  but got ${e.toString()}',
          st,
        );
      } else {
        // Expected different exception
        return StepResult.failed(
          'Expected $exceptionType\n  but got ${e.runtimeType}: $e',
          st,
        );
      }
    } else {
      // Exception not expected
      return StepResult.failed(
        'Unexpected exception: ${e.runtimeType}:\n$e',
        st,
      );
    }
  }
}