code-coverage-ad-hoc.js 1.37 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax --no-always-opt

// Test code coverage without explicitly activating it upfront.

function GetCoverage(source) {
10
  for (var script of %DebugCollectCoverage()) {
11
    if (script.script.source == source) return script;
12 13 14 15 16 17 18 19
  }
  return undefined;
}

function TestCoverage(name, source, expectation) {
  source = source.trim();
  eval(source);
  var coverage = GetCoverage(source);
20
  var result = JSON.stringify(coverage);
21
  print(result);
22
  assertEquals(JSON.stringify(expectation), result, name + " failed");
23 24 25 26 27 28 29 30 31
}

TestCoverage(
"call simple function twice",
`
function f() {}
f();
f();
`,
32
[{"start":0,"end":25,"count":1},
33
 {"start":0,"end":15,"count":1}]
34 35 36 37 38 39 40 41 42
);

TestCoverage(
"call arrow function twice",
`
var f = () => 1;
f();
f();
`,
43
[{"start":0,"end":26,"count":1},
44
 {"start":8,"end":15,"count":1}]
45 46 47 48 49 50 51 52 53 54 55 56 57
);

TestCoverage(
"call nested function",
`
function f() {
  function g() {}
  g();
  g();
}
f();
f();
`,
58
[{"start":0,"end":58,"count":1},
59 60
 {"start":0,"end":48,"count":1},
 {"start":17,"end":32,"count":1}]
61 62 63 64 65 66 67 68 69 70 71
);

TestCoverage(
"call recursive function",
`
function fib(x) {
  if (x < 2) return 1;
  return fib(x-1) + fib(x-2);
}
fib(5);
`,
72
[{"start":0,"end":80,"count":1},
73
 {"start":0,"end":72,"count":1}]
74
);