OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // These tests fork a second VM process that runs the script |
| 6 // ``tools/full-coverage.dart'' and verifies that the tool |
| 7 // produces the expeced output. |
| 8 |
| 9 import 'dart:async'; |
| 10 import 'dart:convert'; |
| 11 import 'dart:io'; |
| 12 |
| 13 import 'package:path/path.dart' as path; |
| 14 import 'package:unittest/unittest.dart'; |
| 15 |
| 16 final String coverageScript = "tools/full-coverage.dart"; |
| 17 final String packageRoot = Platform.packageRoot; |
| 18 final List dartBaseArgs = ['--package-root=${packageRoot}', '--checked',]; |
| 19 |
| 20 // With line numbers starting at 0, the list of hits can be understood as |
| 21 // follows: |
| 22 // * -1: No coverage data on this line. |
| 23 // * 0: No hits on this line. |
| 24 // * 1: ``Some'' hits on this line. |
| 25 final coverageTests = [ |
| 26 { |
| 27 'name': 'faculty', |
| 28 'program': ''' |
| 29 dummy () { |
| 30 for (int i = 0; i < 100; i++) { |
| 31 print(i); |
| 32 } |
| 33 } |
| 34 |
| 35 int fac(int n) { |
| 36 int f = 1; |
| 37 for (int i = 1; i <= n; i++) { |
| 38 f *= i; |
| 39 } |
| 40 return f; |
| 41 } |
| 42 |
| 43 main() { |
| 44 if (false) { |
| 45 dummy(11); |
| 46 } else { |
| 47 fac(10); |
| 48 } |
| 49 } |
| 50 ''', |
| 51 'expectedHits': [-1, 0, 0, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, |
| 52 0, -1, 1, -1, -1] |
| 53 },{ |
| 54 'name': 'closures', |
| 55 'program': ''' |
| 56 main() { |
| 57 foo(bar) { |
| 58 bar(); |
| 59 } |
| 60 |
| 61 foo(() { |
| 62 print("in closure"); |
| 63 }); |
| 64 } |
| 65 ''', |
| 66 'expectedHits': [-1, -1, 1, -1, -1, 1, 1, -1, -1] |
| 67 } |
| 68 ]; |
| 69 |
| 70 |
| 71 String prepareEnv() { |
| 72 Directory testDir = Directory.systemTemp.createTempSync("coverage-"); |
| 73 for (var coverageProg in coverageTests) { |
| 74 var coverageProgDir = new Directory( |
| 75 path.join(testDir.path, coverageProg["name"])) |
| 76 ..createSync(); |
| 77 var f = new File(path.join(coverageProgDir.path, |
| 78 "${coverageProg['name']}.dart")); |
| 79 f.writeAsStringSync(coverageProg["program"], mode: FileMode.WRITE); |
| 80 } |
| 81 return testDir.path; |
| 82 } |
| 83 |
| 84 |
| 85 destroyEnv(base) => new Directory(base).deleteSync(recursive: true); |
| 86 |
| 87 |
| 88 generateCoverage(String workingDirectory) { |
| 89 for (var coverageProg in coverageTests) { |
| 90 var progPath = path.join(workingDirectory, coverageProg['name']); |
| 91 var script = path.join(progPath, "${coverageProg['name']}.dart"); |
| 92 var dartArgs = new List.from(dartBaseArgs) |
| 93 ..addAll(['--coverage-dir=${progPath}', '${script}']); |
| 94 var result = Process.runSync(Platform.executable, dartArgs); |
| 95 expect(result.exitCode, 0); |
| 96 } |
| 97 } |
| 98 |
| 99 |
| 100 Future<Process> convertCoverage(String programDir, String format) { |
| 101 var dartArgs = new List.from(dartBaseArgs) |
| 102 ..addAll([ |
| 103 coverageScript, |
| 104 '--package-root=${packageRoot}', |
| 105 '--in=${programDir}', |
| 106 format |
| 107 ]); |
| 108 return Process.start(Platform.executable, dartArgs); |
| 109 } |
| 110 |
| 111 |
| 112 class PrettyPrintDescriptor { |
| 113 var _programPath; |
| 114 var _validFormat = new RegExp(r"^\s*\d*\|.*$", multiLine: true); |
| 115 var _pattern = new RegExp(r"^\s*(\d+)\|", multiLine: true); |
| 116 |
| 117 PrettyPrintDescriptor(this._programPath); |
| 118 |
| 119 get sectionStart => _programPath; |
| 120 get sectionEnd => '/'; |
| 121 get coverageParameter => '--pretty-print'; |
| 122 |
| 123 hitData(line) { |
| 124 expect(_validFormat.hasMatch(line), isTrue); |
| 125 var match = _pattern.firstMatch(line); |
| 126 var result = -1; |
| 127 if (match != null) { |
| 128 result = (int.parse(match.group(1)) != 0) ? 1 : 0; |
| 129 } |
| 130 return [result]; |
| 131 } |
| 132 } |
| 133 |
| 134 |
| 135 class LcovDescriptor { |
| 136 var _pattern = new RegExp(r"^DA:(\d+),(\d+)$", multiLine: true); |
| 137 var _programPath; |
| 138 var _line_nr = 0; |
| 139 |
| 140 LcovDescriptor(this._programPath); |
| 141 |
| 142 get sectionStart => 'SF:${_programPath}'; |
| 143 get sectionEnd => 'end_of_record'; |
| 144 get coverageParameter => '--lcov'; |
| 145 |
| 146 hitData(line) { |
| 147 expect(_pattern.hasMatch(line), isTrue); |
| 148 var match = _pattern.firstMatch(line); |
| 149 // Lcov data starts at line 1, we start at 0. |
| 150 var out_line = int.parse(match[1]) - 1; |
| 151 var hitCount = int.parse(match[2]); |
| 152 var result = []; |
| 153 for ( ; _line_nr < out_line; _line_nr++) { |
| 154 result.add(-1); |
| 155 } |
| 156 result.add((hitCount != 0) ? 1 : 0); |
| 157 _line_nr++; |
| 158 return result; |
| 159 } |
| 160 } |
| 161 |
| 162 |
| 163 Stream filterHitData(input, descriptor) { |
| 164 bool in_program_section = false; |
| 165 return input.where((line) { |
| 166 if (in_program_section) { |
| 167 if (line.startsWith(descriptor.sectionEnd)) { |
| 168 in_program_section = false; |
| 169 return false; |
| 170 } |
| 171 return true; |
| 172 } |
| 173 if (line.startsWith(descriptor.sectionStart)) { |
| 174 in_program_section = true; |
| 175 } |
| 176 return false; |
| 177 }).map((line) { |
| 178 return descriptor.hitData(line); |
| 179 }); |
| 180 } |
| 181 |
| 182 |
| 183 testCoverage(String programDir, String programPath, descriptor, |
| 184 List expectedHitMap) { |
| 185 var p = convertCoverage(programDir, descriptor.coverageParameter); |
| 186 expect(p.then((process) { |
| 187 var hitStream = filterHitData( |
| 188 process.stdout.transform(UTF8.decoder) |
| 189 .transform(const LineSplitter()), |
| 190 descriptor); |
| 191 var hitMap = []; |
| 192 var subscription = hitStream.listen((data) { |
| 193 // Flatten results. |
| 194 data.forEach((e) => hitMap.add(e)); |
| 195 }); |
| 196 expect(subscription.asFuture().then((_) { |
| 197 hitMap.forEach((e) { |
| 198 expect(e, expectedHitMap.removeAt(0)); |
| 199 }); |
| 200 // Make sure that there are only lines left that do not contain coverage |
| 201 // data. |
| 202 expectedHitMap.forEach((e) => expect(e, -1)); |
| 203 }), completes); |
| 204 }), completes); |
| 205 } |
| 206 |
| 207 |
| 208 main() { |
| 209 String testingDirectory; |
| 210 |
| 211 setUp(() { |
| 212 testingDirectory = prepareEnv(); |
| 213 }); |
| 214 |
| 215 tearDown(() => destroyEnv(testingDirectory)); |
| 216 |
| 217 test('CoverageTests', () { |
| 218 generateCoverage(testingDirectory); |
| 219 |
| 220 coverageTests.forEach((cTest) { |
| 221 String programDir = path.join(testingDirectory, cTest['name']); |
| 222 String programPath = path.join(programDir, "${cTest['name']}.dart"); |
| 223 testCoverage(programDir, programPath, |
| 224 new LcovDescriptor(programPath), |
| 225 new List.from(cTest['expectedHits'])); |
| 226 testCoverage(programDir, programPath, |
| 227 new PrettyPrintDescriptor(programPath), |
| 228 new List.from(cTest['expectedHits'])); |
| 229 }); |
| 230 }); |
| 231 } |
OLD | NEW |