wrapWithPatrolLog<T> method

Future<T> wrapWithPatrolLog<T>({
  1. required String action,
  2. String? value,
  3. required String color,
  4. required Future<T> function(),
  5. bool enablePatrolLog = true,
})

Wraps a function with a log entry for the start and end of the function.

Implementation

Future<T> wrapWithPatrolLog<T>({
  required String action,
  String? value,
  required String color,
  required Future<T> Function() function,
  bool enablePatrolLog = true,
}) async {
  if (!(tester.config.printLogs && enablePatrolLog)) {
    return function();
  }

  final finderText = finder
      .toString(describeSelf: true)
      .replaceAll('A finder that searches for', '')
      .replaceAll(' (considering only hit-testable ones)', '')
      .replaceAll(' (ignoring all but first)', '');

  final valueText = value != null ? ' "$value"' : '';
  final text = '$color$action${AnsiCodes.reset}$valueText$finderText';
  tester.patrolLog.log(
    StepEntry(action: text, status: StepEntryStatus.start),
  );
  try {
    final result = await function();
    tester.patrolLog.log(
      StepEntry(action: text, status: StepEntryStatus.success),
    );
    return result;
  } catch (err) {
    tester.patrolLog.log(
      StepEntry(action: text, status: StepEntryStatus.failure),
    );
    rethrow;
  }
}