validateActionTestSteps function

void validateActionTestSteps(
  1. EnsembleTestCase test,
  2. TestMocks mocks,
  3. String id
)

Implementation

void validateActionTestSteps(
    EnsembleTestCase test, TestMocks mocks, String id) {
  final action = test.action;
  final script = test.script;
  if (script != null && script.trim().isEmpty) {
    _invalid(id, 'script must be a non-empty string');
  }
  if (action != null && mocks.actions.containsKey(action.name)) {
    _invalid(id, 'cannot mock the target global action "${action.name}"');
  }
  for (final step in flattenTestSteps(test.steps)) {
    final keys = switch (step.type) {
      'expectFunctionResult' => {'function', 'args', 'equals'},
      'expectActionCalled' => {'name', 'times', 'inputs'},
      'expectActionNotCalled' => {'name', 'inputs'},
      'expectActionEvent' => {'name', 'data'},
      _ => null,
    };
    final path = '$id.steps.${step.type}';
    if (keys == null) {
      if ((action != null || script != null) &&
          !actionAssertionSteps.contains(step.type))
        _invalid(path, 'is unavailable in a screen-independent test');
      continue;
    }
    if (step.type == 'expectFunctionResult') {
      if (script == null) _invalid(path, 'requires a script test');
      _keys(step.args, keys, path);
      _name(step.args['function'], '$path.function');
      if (step.args['args'] is! List) _invalid('$path.args', 'must be a list');
      if (flattenTestSteps(test.steps)
              .where((candidate) => candidate.type == 'expectFunctionResult')
              .length !=
          1) {
        _invalid(id, 'requires exactly one expectFunctionResult step');
      }
      continue;
    }
    if (action == null) _invalid(path, 'requires a global action test');
    _keys(step.args, keys, path);
    _name(step.args['name'], '$path.name');
    if (step.args.containsKey('times')) {
      final times = step.args['times'];
      if (times is! int || times < 0)
        _invalid('$path.times', 'must be a non-negative integer');
    }
    for (final key in ['inputs', 'payload']) {
      if (step.args.containsKey(key)) _map(step.args[key], '$path.$key');
    }
  }
}