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 --no-always-opt |
| 6 |
| 7 // Test code coverage without explicitly activating it upfront. |
| 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 TestCoverage( |
| 44 "call simple function twice", |
| 45 ` |
| 46 function f() {} |
| 47 f(); |
| 48 f(); |
| 49 `, |
| 50 ` |
| 51 [function f() {}[2]][ |
| 52 f(); |
| 53 f();[1]] |
| 54 ` |
| 55 ); |
| 56 |
| 57 TestCoverage( |
| 58 "call arrow function twice", |
| 59 ` |
| 60 var f = () => 1; |
| 61 f(); |
| 62 f(); |
| 63 `, |
| 64 ` |
| 65 [var f = [1]][() => 1[2]][; |
| 66 f(); |
| 67 f();[1]] |
| 68 ` |
| 69 ); |
| 70 |
| 71 TestCoverage( |
| 72 "call nested function", |
| 73 ` |
| 74 function f() { |
| 75 function g() {} |
| 76 g(); |
| 77 g(); |
| 78 } |
| 79 f(); |
| 80 f(); |
| 81 `, |
| 82 ` |
| 83 [function f() { |
| 84 [2]][function g() {}[4]][ |
| 85 g(); |
| 86 g(); |
| 87 }[2]][ |
| 88 f(); |
| 89 f();[1]] |
| 90 ` |
| 91 ); |
| 92 |
| 93 TestCoverage( |
| 94 "call recursive function", |
| 95 ` |
| 96 function fib(x) { |
| 97 if (x < 2) return 1; |
| 98 return fib(x-1) + fib(x-2); |
| 99 } |
| 100 fib(5); |
| 101 `, |
| 102 ` |
| 103 [function fib(x) { |
| 104 if (x < 2) return 1; |
| 105 return fib(x-1) + fib(x-2); |
| 106 }[15]][ |
| 107 fib(5);[1]] |
| 108 ` |
| 109 ); |
OLD | NEW |