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