[gjs] coverage: Allocate nLines + 1.
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] coverage: Allocate nLines + 1.
- Date: Sat, 17 Jan 2015 00:59:48 +0000 (UTC)
commit f3d99a324822721d2f20da903c0ed3b4e5c495f8
Author: Sam Spilsbury <smspillaz gmail com>
Date: Thu Jan 15 14:36:47 2015 +0800
coverage: Allocate nLines + 1.
We 1-index things anyways and we need to deal with the
lack of a trailing newline case.
https://bugzilla.gnome.org/show_bug.cgi?id=743007
installed-tests/js/testCoverage.js | 39 ++++++++++++++++++++++++++++++-----
modules/coverage.js | 6 ++--
2 files changed, 36 insertions(+), 9 deletions(-)
---
diff --git a/installed-tests/js/testCoverage.js b/installed-tests/js/testCoverage.js
index 9aa0810..30b0da1 100644
--- a/installed-tests/js/testCoverage.js
+++ b/installed-tests/js/testCoverage.js
@@ -17,6 +17,12 @@ function assertArrayEquals(actual, expected, assertion) {
}
}
+function testExpressionLinesWithNoTrailingNewline() {
+ let foundLines = parseScriptForExpressionLines("let x;\n" +
+ "let y;");
+ assertArrayEquals(foundLines, [1, 2], JSUnit.assertEquals);
+}
+
function testExpressionLinesFoundForAssignmentExpressionSides() {
let foundLinesOnBothExpressionSides =
parseScriptForExpressionLines("var x;\n" +
@@ -265,6 +271,17 @@ function functionDeclarationsEqual(actual, expected) {
JSUnit.assertEquals(expected.n_params, actual.n_params);
}
+function testFunctionsFoundNoTrailingNewline() {
+ let foundFuncs = parseScriptForFunctionNames("function f1() {}\n" +
+ "function f2() {}\n");
+ assertArrayEquals(foundFuncs,
+ [
+ { name: "f1", line: 1, n_params: 0 },
+ { name: "f2", line: 2, n_params: 0 }
+ ],
+ functionDeclarationsEqual);
+}
+
function testFunctionsFoundForDeclarations() {
let foundFunctionDeclarations =
parseScriptForFunctionNames("function f1() {}\n" +
@@ -324,6 +341,15 @@ function branchInfoEqual(actual, expected) {
assertArrayEquals(expected.exits, actual.exits, JSUnit.assertEquals);
}
+function testFindBranchWhereNoTrailingNewline() {
+ let foundBranchExits = parseScriptForBranches("if (1) { let a = 1; }");
+ assertArrayEquals(foundBranchExits,
+ [
+ { point: 1, exits: [1] }
+ ],
+ branchInfoEqual);
+}
+
function testBothBranchExitsFoundForSimpleBranch() {
let foundBranchExitsForSimpleBranch =
parseScriptForBranches("if (1) {\n" +
@@ -513,7 +539,7 @@ function testZeroExpressionLinesToCounters() {
let nLines = 1;
let counters = Coverage._expressionLinesToCounters(expressionLines, nLines);
- assertArrayEquals([undefined], counters, JSUnit.assertEquals);
+ assertArrayEquals([undefined, undefined], counters, JSUnit.assertEquals);
}
function testSingleExpressionLineToCounters() {
@@ -521,7 +547,8 @@ function testSingleExpressionLineToCounters() {
let nLines = 4;
let counters = Coverage._expressionLinesToCounters(expressionLines, nLines);
- assertArrayEquals([undefined, 0, 0, undefined], counters, JSUnit.assertEquals);
+ assertArrayEquals([undefined, 0, 0, undefined, undefined],
+ counters, JSUnit.assertEquals);
}
const MockFoundBranches = [
@@ -537,14 +564,14 @@ const MockFoundBranches = [
const MockNLines = 9;
-function testGetsSameNumberOfCountersAsNLines() {
+function testGetsSameNumberOfCountersAsNLinesPlusOne() {
let counters = Coverage._branchesToBranchCounters(MockFoundBranches, MockNLines);
- JSUnit.assertEquals(MockNLines, counters.length);
+ JSUnit.assertEquals(MockNLines + 1, counters.length);
}
function testEmptyArrayReturnedForNoBranches() {
let counters = Coverage._branchesToBranchCounters([], 1);
- assertArrayEquals([undefined], counters, JSUnit.assertEquals);
+ assertArrayEquals([undefined, undefined], counters, JSUnit.assertEquals);
}
function testBranchesOnLinesForArrayIndicies() {
@@ -626,7 +653,7 @@ function testKnownFunctionsArrayPopulatedForFunctions() {
let knownFunctionsArray = Coverage._populateKnownFunctions(functions, 4);
assertArrayEquals(knownFunctionsArray,
- [undefined, true, true, undefined],
+ [undefined, true, true, undefined, undefined],
JSUnit.assertEquals);
}
diff --git a/modules/coverage.js b/modules/coverage.js
index cc5f72b..e2b98bc 100644
--- a/modules/coverage.js
+++ b/modules/coverage.js
@@ -371,7 +371,7 @@ function _expressionLinesToCounters(expressionLines, nLines) {
expressionLines.sort(function(left, right) { return left - right; });
let expressionLinesIndex = 0;
- let counters = new Array(nLines);
+ let counters = new Array(nLines + 1);
if (expressionLines.length === 0)
return counters;
@@ -399,7 +399,7 @@ function _branchesToBranchCounters(branches, nLines) {
});
let branchIndex = 0;
- let counters = new Array(nLines);
+ let counters = new Array(nLines + 1);
if (branches.length === 0)
return counters;
@@ -459,7 +459,7 @@ function _functionsToFunctionCounters(functions) {
}
function _populateKnownFunctions(functions, nLines) {
- let knownFunctions = new Array(nLines);
+ let knownFunctions = new Array(nLines + 1);
functions.forEach(function(func) {
knownFunctions[func.line] = true;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]