formatLcov method

String formatLcov(
  1. Resolver resolver, {
  2. String? basePath,
  3. List<String>? reportOn,
})

Converts the given hitmap to lcov format.

If reportOn is provided, coverage report output is limited to files prefixed with one of the paths included. If basePath is provided, paths are reported relative to that path.

Implementation

String formatLcov(
  Resolver resolver, {
  String? basePath,
  List<String>? reportOn,
}) {
  final pathFilter = _getPathFilter(reportOn);
  final buf = StringBuffer();
  for (final entry in entries) {
    final v = entry.value;
    final lineHits = v.lineHits;
    final funcHits = v.funcHits;
    final funcNames = v.funcNames;
    final branchHits = v.branchHits;
    var source = resolver.resolve(entry.key);
    if (source == null) {
      continue;
    }

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

    if (basePath != null) {
      source = p.relative(source, from: basePath);
    }

    buf.write('SF:$source\n');
    if (funcHits != null && funcNames != null) {
      for (final k in funcNames.keys.toList()..sort()) {
        buf.write('FN:$k,${funcNames[k]}\n');
      }
      for (final k in funcHits.keys.toList()..sort()) {
        if (funcHits[k]! != 0) {
          buf.write('FNDA:${funcHits[k]},${funcNames[k]}\n');
        }
      }
      buf.write('FNF:${funcNames.length}\n');
      buf.write('FNH:${funcHits.values.where((v) => v > 0).length}\n');
    }
    for (final k in lineHits.keys.toList()..sort()) {
      buf.write('DA:$k,${lineHits[k]}\n');
    }
    buf.write('LF:${lineHits.length}\n');
    buf.write('LH:${lineHits.values.where((v) => v > 0).length}\n');
    if (branchHits != null) {
      for (final k in branchHits.keys.toList()..sort()) {
        buf.write('BRDA:$k,0,0,${branchHits[k]}\n');
      }
    }
    buf.write('end_of_record\n');
  }

  return buf.toString();
}