| 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 for (var script of %DebugCollectCoverage()) { | 10 for (var script of %DebugCollectCoverage()) { |
| 11 if (script.script.source == source) return script.toplevel; | 11 if (script.script.source == source) return script; |
| 12 } | 12 } |
| 13 return undefined; | 13 return undefined; |
| 14 } | 14 } |
| 15 | 15 |
| 16 function ApplyCoverageToSource(source, range) { | |
| 17 var content = ""; | |
| 18 var cursor = range.start; | |
| 19 if (range.inner) for (var inner of range.inner) { | |
| 20 content += source.substring(cursor, inner.start); | |
| 21 content += ApplyCoverageToSource(source, inner); | |
| 22 cursor = inner.end; | |
| 23 } | |
| 24 content += source.substring(cursor, range.end); | |
| 25 return `[${content}](${range.name}:${range.count})`; | |
| 26 } | |
| 27 | |
| 28 function TestCoverage(name, source, expectation) { | 16 function TestCoverage(name, source, expectation) { |
| 29 source = source.trim(); | 17 source = source.trim(); |
| 30 expectation = expectation.trim(); | |
| 31 eval(source); | 18 eval(source); |
| 32 var coverage = GetCoverage(source); | 19 var coverage = GetCoverage(source); |
| 33 var result = ApplyCoverageToSource(source, coverage); | 20 var result = JSON.stringify(coverage); |
| 34 print(result); | 21 print(result); |
| 35 assertEquals(expectation, result, name + " failed"); | 22 assertEquals(JSON.stringify(expectation), result, name + " failed"); |
| 36 } | 23 } |
| 37 | 24 |
| 38 TestCoverage( | 25 TestCoverage( |
| 39 "call simple function twice", | 26 "call simple function twice", |
| 40 ` | 27 ` |
| 41 function f() {} | 28 function f() {} |
| 42 f(); | 29 f(); |
| 43 f(); | 30 f(); |
| 44 `, | 31 `, |
| 45 ` | 32 [{"start":0,"end":25,"count":1}, |
| 46 [[function f() {}](f:2) | 33 {"start":0,"end":15,"count":2}] |
| 47 f(); | |
| 48 f();](:1) | |
| 49 ` | |
| 50 ); | 34 ); |
| 51 | 35 |
| 52 TestCoverage( | 36 TestCoverage( |
| 53 "call arrow function twice", | 37 "call arrow function twice", |
| 54 ` | 38 ` |
| 55 var f = () => 1; | 39 var f = () => 1; |
| 56 f(); | 40 f(); |
| 57 f(); | 41 f(); |
| 58 `, | 42 `, |
| 59 ` | 43 [{"start":0,"end":26,"count":1}, |
| 60 [var f = [() => 1](f:2); | 44 {"start":8,"end":15,"count":2}] |
| 61 f(); | |
| 62 f();](:1) | |
| 63 ` | |
| 64 ); | 45 ); |
| 65 | 46 |
| 66 TestCoverage( | 47 TestCoverage( |
| 67 "call nested function", | 48 "call nested function", |
| 68 ` | 49 ` |
| 69 function f() { | 50 function f() { |
| 70 function g() {} | 51 function g() {} |
| 71 g(); | 52 g(); |
| 72 g(); | 53 g(); |
| 73 } | 54 } |
| 74 f(); | 55 f(); |
| 75 f(); | 56 f(); |
| 76 `, | 57 `, |
| 77 ` | 58 [{"start":0,"end":58,"count":1}, |
| 78 [[function f() { | 59 {"start":0,"end":48,"count":2}, |
| 79 [function g() {}](g:4) | 60 {"start":17,"end":32,"count":4}] |
| 80 g(); | |
| 81 g(); | |
| 82 }](f:2) | |
| 83 f(); | |
| 84 f();](:1) | |
| 85 | |
| 86 ` | |
| 87 ); | 61 ); |
| 88 | 62 |
| 89 TestCoverage( | 63 TestCoverage( |
| 90 "call recursive function", | 64 "call recursive function", |
| 91 ` | 65 ` |
| 92 function fib(x) { | 66 function fib(x) { |
| 93 if (x < 2) return 1; | 67 if (x < 2) return 1; |
| 94 return fib(x-1) + fib(x-2); | 68 return fib(x-1) + fib(x-2); |
| 95 } | 69 } |
| 96 fib(5); | 70 fib(5); |
| 97 `, | 71 `, |
| 98 ` | 72 [{"start":0,"end":80,"count":1}, |
| 99 [[function fib(x) { | 73 {"start":0,"end":72,"count":15}] |
| 100 if (x < 2) return 1; | |
| 101 return fib(x-1) + fib(x-2); | |
| 102 }](fib:15) | |
| 103 fib(5);](:1) | |
| 104 ` | |
| 105 ); | 74 ); |
| OLD | NEW |