prettyPrint method

Future<String> prettyPrint(
  1. Resolver resolver,
  2. Loader loader, {
  3. List<String>? reportOn,
  4. bool reportFuncs = false,
  5. bool reportBranches = false,
})

Converts the given hitmap to a pretty-print format.

If reportOn is provided, coverage report output is limited to files prefixed with one of the paths included. If reportFuncs is provided, only function coverage information will be shown.

Implementation

Future<String> prettyPrint(
  Resolver resolver,
  Loader loader, {
  List<String>? reportOn,
  bool reportFuncs = false,
  bool reportBranches = false,
}) async {
  final pathFilter = _getPathFilter(reportOn);
  final buf = StringBuffer();
  for (final entry in entries) {
    final v = entry.value;
    if (reportFuncs && v.funcHits == null) {
      throw StateError(
        'Function coverage formatting was requested, but the hit map is '
        'missing function coverage information. Did you run '
        'collect_coverage with the --function-coverage flag?',
      );
    }
    if (reportBranches && v.branchHits == null) {
      throw StateError(
          'Branch coverage formatting was requested, but the hit map is '
          'missing branch coverage information. Did you run '
          'collect_coverage with the --branch-coverage flag?');
    }
    final hits = reportFuncs
        ? v.funcHits!
        : reportBranches
            ? v.branchHits!
            : v.lineHits;
    final source = resolver.resolve(entry.key);
    if (source == null) {
      continue;
    }

    if (!pathFilter(source)) {
      continue;
    }

    final lines = await loader.load(source);
    if (lines == null) {
      continue;
    }
    buf.writeln(source);
    for (var line = 1; line <= lines.length; line++) {
      var prefix = _prefix;
      if (hits.containsKey(line)) {
        prefix = hits[line].toString().padLeft(_prefix.length);
      }
      buf.writeln('$prefix|${lines[line - 1]}');
    }
  }

  return buf.toString();
}