OLD | NEW |
1 // Copyright 2017 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --allow-natives-syntax --no-always-opt | 5 // Flags: --allow-natives-syntax --no-always-opt |
6 | 6 |
7 // Test precise code coverage. | 7 // Test precise code coverage. |
8 | 8 |
9 function GetCoverage(source) { | 9 function GetCoverage(source) { |
10 var scripts = %DebugGetLoadedScripts(); | 10 for (var script of %DebugCollectCoverage()) { |
11 for (var script of scripts) { | 11 if (script.script.source == source) return script.toplevel; |
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 } | 12 } |
19 return undefined; | 13 return undefined; |
20 } | 14 } |
21 | 15 |
22 function ApplyCoverageToSource(source, coverage) { | 16 function ApplyCoverageToSource(source, range) { |
23 var result = ""; | 17 var content = ""; |
24 var cursor = 0; | 18 var cursor = range.start; |
25 for (var entry of coverage) { | 19 if (range.inner) for (var inner of range.inner) { |
26 var chunk = source.substring(cursor, entry.end_position); | 20 content += source.substring(cursor, inner.start); |
27 cursor = entry.end_position; | 21 content += ApplyCoverageToSource(source, inner); |
28 result += `[${chunk}[${entry.count}]]`; | 22 cursor = inner.end; |
29 } | 23 } |
30 return result; | 24 content += source.substring(cursor, range.end); |
| 25 return `[${content}](${range.name}:${range.count})`; |
31 } | 26 } |
32 | 27 |
33 function TestCoverage(name, source, expectation) { | 28 function TestCoverage(name, source, expectation) { |
34 source = source.trim(); | 29 source = source.trim(); |
35 eval(source); | 30 eval(source); |
| 31 %CollectGarbage("remove dead objects"); |
36 var coverage = GetCoverage(source); | 32 var coverage = GetCoverage(source); |
37 if (expectation === undefined) { | 33 if (expectation === undefined) { |
38 assertEquals(undefined, coverage); | 34 assertEquals(undefined, coverage); |
39 } else { | 35 } else { |
40 expectation = expectation.trim(); | 36 expectation = expectation.trim(); |
41 var result = ApplyCoverageToSource(source, coverage); | 37 var result = ApplyCoverageToSource(source, coverage); |
42 print(result); | 38 print(result); |
43 assertEquals(expectation, result, name + " failed"); | 39 assertEquals(expectation, result, name + " failed"); |
44 } | 40 } |
45 } | 41 } |
(...skipping 21 matching lines...) Expand all Loading... |
67 | 63 |
68 // This does not happen with precise coverage enabled. | 64 // This does not happen with precise coverage enabled. |
69 %DebugTogglePreciseCoverage(true); | 65 %DebugTogglePreciseCoverage(true); |
70 | 66 |
71 TestCoverage( | 67 TestCoverage( |
72 "call an IIFE", | 68 "call an IIFE", |
73 ` | 69 ` |
74 (function f() {})(); | 70 (function f() {})(); |
75 `, | 71 `, |
76 ` | 72 ` |
77 [(function f() {})();[1]] | 73 [([function f() {}](f:1))();](anonymous:1) |
78 ` | 74 ` |
79 ); | 75 ); |
80 | 76 |
81 TestCoverage( | 77 TestCoverage( |
82 "call locally allocated function", | 78 "call locally allocated function", |
83 ` | 79 ` |
84 for (var i = 0; i < 10; i++) { | 80 for (var i = 0; i < 10; i++) { |
85 let f = () => 1; | 81 let f = () => 1; |
86 i += f(); | 82 i += f(); |
87 } | 83 } |
88 `, | 84 `, |
89 ` | 85 ` |
90 [for (var i = 0; i < 10; i++) { | 86 [for (var i = 0; i < 10; i++) { |
91 let f = [1]][() => 1[5]][; | 87 let f = [() => 1](f:5); |
92 i += f(); | 88 i += f(); |
93 }[1]] | 89 }](anonymous:1) |
94 ` | 90 ` |
95 ); | 91 ); |
96 | 92 |
97 %DebugTogglePreciseCoverage(false); | 93 %DebugTogglePreciseCoverage(false); |
OLD | NEW |