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 String sdkRoot = path.join(path.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 'name': 'closures', | |
56 'program': ''' | |
57 main() { | |
58 foo(bar) { | |
59 bar(); | |
60 } | |
61 | |
62 foo(() { | |
63 print("in closure"); | |
64 }); | |
65 } | |
66 ''', | |
67 'expectedHits': [-1, -1, 1, -1, -1, 1, 1, -1, -1] | |
68 } | |
69 ]; | |
70 | |
71 | |
72 String prepareEnv() { | |
73 Directory testDir = Directory.systemTemp.createTempSync("coverage-"); | |
74 for (var coverageProg in coverageTests) { | |
75 var coverageProgDir = new Directory( | |
76 path.join(testDir.path, coverageProg["name"])) | |
77 ..createSync(); | |
78 var f = new File(path.join(coverageProgDir.path, | |
79 "${coverageProg['name']}.dart")); | |
80 f.writeAsStringSync(coverageProg["program"], mode: FileMode.WRITE); | |
81 } | |
82 return testDir.path; | |
83 } | |
84 | |
85 | |
86 destroyEnv(base) => new Directory(base).deleteSync(recursive: true); | |
87 | |
88 | |
89 generateCoverage(String workingDirectory) { | |
90 for (var coverageProg in coverageTests) { | |
91 var progPath = path.join(workingDirectory, coverageProg['name']); | |
92 var script = path.join(progPath, "${coverageProg['name']}.dart"); | |
93 var dartArgs = new List.from(dartBaseArgs) | |
94 ..addAll(['--coverage-dir=${progPath}', '${script}']); | |
95 var result = Process.runSync(Platform.executable, dartArgs); | |
96 expect(result.exitCode, 0); | |
97 } | |
98 } | |
99 | |
100 | |
101 Future<Process> convertCoverage(String programDir, String format) { | |
102 var dartArgs = new List.from(dartBaseArgs) | |
103 ..addAll([ | |
104 coverageScript, | |
105 '--package-root=${packageRoot}', | |
106 '--sdk-root=${sdkRoot}', | |
107 '--in=${programDir}', | |
108 format | |
109 ]); | |
110 return Process.start(Platform.executable, dartArgs); | |
111 } | |
112 | |
113 | |
114 class PrettyPrintDescriptor { | |
115 var _programPath; | |
116 var _validFormat = new RegExp(r"^\s*\d*\|.*$", multiLine: true); | |
117 var _pattern = new RegExp(r"^\s*(\d+)\|", multiLine: true); | |
118 | |
119 PrettyPrintDescriptor(this._programPath); | |
120 | |
121 get sectionStart => _programPath; | |
122 get sectionEnd => '/'; | |
123 get coverageParameter => '--pretty-print'; | |
124 | |
125 hitData(line) { | |
126 expect(_validFormat.hasMatch(line), isTrue); | |
127 var match = _pattern.firstMatch(line); | |
128 var result = -1; | |
129 if (match != null) { | |
130 result = (int.parse(match.group(1)) != 0) ? 1 : 0; | |
131 } | |
132 return [result]; | |
133 } | |
134 } | |
135 | |
136 | |
137 class LcovDescriptor { | |
138 var _pattern = new RegExp(r"^DA:(\d+),(\d+)$", multiLine: true); | |
139 var _programPath; | |
140 var _line_nr = 0; | |
141 | |
142 LcovDescriptor(this._programPath); | |
143 | |
144 get sectionStart => 'SF:${_programPath}'; | |
145 get sectionEnd => 'end_of_record'; | |
146 get coverageParameter => '--lcov'; | |
147 | |
148 hitData(line) { | |
149 expect(_pattern.hasMatch(line), isTrue); | |
150 var match = _pattern.firstMatch(line); | |
151 // Lcov data starts at line 1, we start at 0. | |
152 var out_line = int.parse(match[1]) - 1; | |
153 var hitCount = int.parse(match[2]); | |
154 var result = []; | |
155 for ( ; _line_nr < out_line; _line_nr++) { | |
156 result.add(-1); | |
157 } | |
158 result.add((hitCount != 0) ? 1 : 0); | |
159 _line_nr++; | |
160 return result; | |
161 } | |
162 } | |
163 | |
164 | |
165 Stream filterHitData(input, descriptor) { | |
166 bool in_program_section = false; | |
167 return input.where((line) { | |
168 if (in_program_section) { | |
169 if (line.startsWith(descriptor.sectionEnd)) { | |
170 in_program_section = false; | |
171 return false; | |
172 } | |
173 return true; | |
174 } | |
175 if (line.startsWith(descriptor.sectionStart)) { | |
176 in_program_section = true; | |
177 } | |
178 return false; | |
179 }).map((line) { | |
180 return descriptor.hitData(line); | |
181 }); | |
182 } | |
183 | |
184 | |
185 testCoverage(String programDir, String programPath, descriptor, | |
186 List expectedHitMap) { | |
187 var p = convertCoverage(programDir, descriptor.coverageParameter); | |
188 expect(p.then((process) { | |
189 var hitStream = filterHitData( | |
190 process.stdout.transform(UTF8.decoder) | |
191 .transform(const LineSplitter()), | |
192 descriptor); | |
193 var hitMap = []; | |
194 var subscription = hitStream.listen((data) { | |
195 // Flatten results. | |
196 data.forEach((e) => hitMap.add(e)); | |
197 }); | |
198 expect(subscription.asFuture().then((_) { | |
199 hitMap.forEach((e) { | |
200 expect(e, expectedHitMap.removeAt(0)); | |
201 }); | |
202 // Make sure that there are only lines left that do not contain coverage | |
203 // data. | |
204 expectedHitMap.forEach((e) => expect(e, -1)); | |
205 }), completes); | |
206 }), completes); | |
207 } | |
208 | |
209 | |
210 main() { | |
211 String testingDirectory; | |
212 | |
213 setUp(() { | |
214 testingDirectory = prepareEnv(); | |
215 }); | |
216 | |
217 tearDown(() => destroyEnv(testingDirectory)); | |
218 | |
219 test('CoverageTests', () { | |
220 generateCoverage(testingDirectory); | |
221 | |
222 coverageTests.forEach((cTest) { | |
223 String programDir = path.join(testingDirectory, cTest['name']); | |
224 String programPath = path.join(programDir, "${cTest['name']}.dart"); | |
225 testCoverage(programDir, programPath, | |
226 new LcovDescriptor(programPath), | |
227 new List.from(cTest['expectedHits'])); | |
228 testCoverage(programDir, programPath, | |
229 new PrettyPrintDescriptor(programPath), | |
230 new List.from(cTest['expectedHits'])); | |
231 }); | |
232 }); | |
233 } | |
OLD | NEW |