OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --allow-natives-syntax | |
6 | |
7 // Test precise code coverage. | |
8 | |
9 function GetCoverage(source) { | |
10 var scripts = %DebugGetLoadedScripts(); | |
11 for (var script of scripts) { | |
12 if (script.source == source) { | |
13 var coverage = %DebugCollectCoverage(); | |
14 for (var data of coverage) { | |
15 if (data.script_id == script.id) return data.entries; | |
16 } | |
17 } | |
18 } | |
19 return undefined; | |
20 } | |
21 | |
22 function ApplyCoverageToSource(source, coverage) { | |
23 var result = ""; | |
24 var cursor = 0; | |
25 for (var entry of coverage) { | |
26 var chunk = source.substring(cursor, entry.end_position); | |
27 cursor = entry.end_position; | |
28 result += `[${chunk}[${entry.count}]]`; | |
29 } | |
30 return result; | |
31 } | |
32 | |
33 function TestCoverage(name, source, expectation) { | |
34 source = source.trim(); | |
35 expectation = expectation.trim(); | |
36 eval(source); | |
37 var coverage = GetCoverage(source); | |
38 var result = ApplyCoverageToSource(source, coverage); | |
39 print(result); | |
40 assertEquals(expectation, result, name + " failed"); | |
41 } | |
42 | |
43 %DebugTogglePreciseCoverage(true); | |
44 | |
45 TestCoverage( | |
46 "call an IIFE", | |
47 ` | |
48 (function f() {})(); | |
49 `, | |
50 ` | |
51 [(function f() {})();[1]] | |
52 ` | |
53 ); | |
54 | |
55 TestCoverage( | |
56 "call locally allocated function", | |
57 ` | |
58 for (var i = 0; i < 10; i++) { | |
59 let f = () => 1; | |
60 i += f(); | |
61 } | |
62 `, | |
63 ` | |
64 [for (var i = 0; i < 10; i++) { | |
65 let f = [1]][() => 1[5]][; | |
66 i += f(); | |
67 }[1]] | |
68 ` | |
69 ); | |
70 | |
71 %DebugTogglePreciseCoverage(false); | |
jgruber
2017/02/09 16:08:48
Could you add the same tests as above after disabl
Yang
2017/02/09 17:42:15
Done.
| |
OLD | NEW |