getCallbackName function
Extracts or derives a human-readable display name for a scenario step callback.
Inspects the given stepInput to determine if its callback is a statically
named tear-off function or an anonymous lambda closure:
- If it is a named method, parses its string reflection signature and cleans it
- If it is an anonymous lambda, it falls back to extracting the explicit text
value from
stepInput.description.
Throws an ArgumentError if an anonymous lambda closure is supplied without an accompanying descriptive string.
Implementation
String getCallbackName(StepInput stepInput) {
bool isNamedMethod = false;
if (stepInput.callback != null) {
isNamedMethod = stepInput.callback.toString().contains("from Function");
}
String retVal;
if (isNamedMethod) {
retVal = stepInput.callback.toString().split("'")[1].expandFunctionName();
} else {
if ((stepInput.description == null) || stepInput.description!.isEmpty) {
throw ArgumentError(
"Lambdas and Anonymous Methods cannot be used without a text description for this step.",
);
} else {
retVal = stepInput.description!;
}
}
return retVal;
}