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 code coverage without explicitly activating it upfront. | 7 // Test code coverage without explicitly activating it upfront. |
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 expectation = expectation.trim(); | 30 expectation = expectation.trim(); |
36 eval(source); | 31 eval(source); |
37 var coverage = GetCoverage(source); | 32 var coverage = GetCoverage(source); |
38 var result = ApplyCoverageToSource(source, coverage); | 33 var result = ApplyCoverageToSource(source, coverage); |
39 print(result); | 34 print(result); |
40 assertEquals(expectation, result, name + " failed"); | 35 assertEquals(expectation, result, name + " failed"); |
41 } | 36 } |
42 | 37 |
43 TestCoverage( | 38 TestCoverage( |
44 "call simple function twice", | 39 "call simple function twice", |
45 ` | 40 ` |
46 function f() {} | 41 function f() {} |
47 f(); | 42 f(); |
48 f(); | 43 f(); |
49 `, | 44 `, |
50 ` | 45 ` |
51 [function f() {}[2]][ | 46 [[function f() {}](f:2) |
52 f(); | 47 f(); |
53 f();[1]] | 48 f();](anonymous:1) |
54 ` | 49 ` |
55 ); | 50 ); |
56 | 51 |
57 TestCoverage( | 52 TestCoverage( |
58 "call arrow function twice", | 53 "call arrow function twice", |
59 ` | 54 ` |
60 var f = () => 1; | 55 var f = () => 1; |
61 f(); | 56 f(); |
62 f(); | 57 f(); |
63 `, | 58 `, |
64 ` | 59 ` |
65 [var f = [1]][() => 1[2]][; | 60 [var f = [() => 1](f:2); |
66 f(); | 61 f(); |
67 f();[1]] | 62 f();](anonymous:1) |
68 ` | 63 ` |
69 ); | 64 ); |
70 | 65 |
71 TestCoverage( | 66 TestCoverage( |
72 "call nested function", | 67 "call nested function", |
73 ` | 68 ` |
74 function f() { | 69 function f() { |
75 function g() {} | 70 function g() {} |
76 g(); | 71 g(); |
77 g(); | 72 g(); |
78 } | 73 } |
79 f(); | 74 f(); |
80 f(); | 75 f(); |
81 `, | 76 `, |
82 ` | 77 ` |
83 [function f() { | 78 [[function f() { |
84 [2]][function g() {}[4]][ | 79 [function g() {}](g:4) |
85 g(); | 80 g(); |
86 g(); | 81 g(); |
87 }[2]][ | 82 }](f:2) |
88 f(); | 83 f(); |
89 f();[1]] | 84 f();](anonymous:1) |
| 85 |
90 ` | 86 ` |
91 ); | 87 ); |
92 | 88 |
93 TestCoverage( | 89 TestCoverage( |
94 "call recursive function", | 90 "call recursive function", |
95 ` | 91 ` |
96 function fib(x) { | 92 function fib(x) { |
97 if (x < 2) return 1; | 93 if (x < 2) return 1; |
98 return fib(x-1) + fib(x-2); | 94 return fib(x-1) + fib(x-2); |
99 } | 95 } |
100 fib(5); | 96 fib(5); |
101 `, | 97 `, |
102 ` | 98 ` |
103 [function fib(x) { | 99 [[function fib(x) { |
104 if (x < 2) return 1; | 100 if (x < 2) return 1; |
105 return fib(x-1) + fib(x-2); | 101 return fib(x-1) + fib(x-2); |
106 }[15]][ | 102 }](fib:15) |
107 fib(5);[1]] | 103 fib(5);](anonymous:1) |
108 ` | 104 ` |
109 ); | 105 ); |
OLD | NEW |