checkGlobalActionExpectation function
void
checkGlobalActionExpectation(
- OperationInput step,
- GlobalActionRuntime runtime,
- String testId
Asserts on the calls and outward effects from one invocation of the target.
Implementation
void checkGlobalActionExpectation(
OperationInput step,
GlobalActionRuntime runtime,
String testId,
) {
final args = step.arguments;
final name = args['name'];
if (name is! String || name.isEmpty) {
throw EnsembleTestFailure(
'$testId: ${step.operationName} requires a name.');
}
switch (step.operationName) {
case 'expectActionCalled':
case 'expectActionNotCalled':
final inputs = args['inputs'];
final times =
step.operationName == 'expectActionNotCalled' ? 0 : args['times'];
if ((inputs != null && inputs is! Map) ||
(times != null && (times is! int || times < 0))) {
throw EnsembleTestFailure('$testId: invalid call expectation $args.');
}
final count = runtime.calls
.where((call) =>
call['name'] == name && _matchesFields(call['inputs'], inputs))
.length;
if (times == null ? count > 0 : count == times) return;
throw EnsembleTestFailure(
'$testId: expected ${times ?? "at least one"} matching call(s) to "$name"'
'${inputs == null ? "" : " with inputs $inputs"}, found $count. '
'Observed calls: ${runtime.calls}',
);
case 'expectActionEvent':
if (runtime.events.any((event) =>
event['name'] == name &&
(!args.containsKey('data') ||
AssertionValueComparison.deepEquals(
event['data'], args['data'])))) return;
throw EnsembleTestFailure(
'$testId: expected target event $args. Observed target events: ${runtime.events}',
);
default:
throw EnsembleTestFailure(
'$testId: unknown global action assertion ${step.operationName}');
}
}