Skip to content

Commit 5f02358

Browse files
committed
refactor: creating BRDA records, particularly adding hits to existing records
1 parent a686968 commit 5f02358

File tree

1 file changed

+40
-50
lines changed

1 file changed

+40
-50
lines changed

index.js

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BRDA {
4545
* @param {number} lineNumber
4646
* @param {number} blockNumber
4747
* @param {number} branchNumber
48-
* @param {number} hits
48+
* @param {number|"-"} hits
4949
*/
5050
constructor(lineNumber, blockNumber, branchNumber, hits) {
5151
this.lineNumber = lineNumber;
@@ -54,6 +54,24 @@ class BRDA {
5454
this.hits = hits;
5555
}
5656

57+
/**
58+
* @param {number|"-"} hits
59+
*/
60+
addHits(hits) {
61+
// If we've never executed the branch code path in an existing coverage
62+
// record, and we've never executed it here either, then keep it as '-'
63+
// (eg, never executed). If either of them is a number, then
64+
// use the number value.
65+
if (this.hits === '-' && hits === '-') {
66+
return;
67+
}
68+
69+
const oldHits = this.hits === '-' ? 0 : this.hits;
70+
const newHits = hits === '-' ? 0 : hits;
71+
72+
this.hits = oldHits + newHits;
73+
}
74+
5775
toString() {
5876
const { lineNumber, blockNumber, branchNumber, hits } = this;
5977
return `BRDA:${lineNumber},${blockNumber},${branchNumber},${hits}\n`;
@@ -120,6 +138,25 @@ class CoverageFile {
120138
);
121139
}
122140

141+
/**
142+
* @param {number} lineNumber
143+
* @param {number} blockNumber
144+
* @param {number} branchNumber
145+
* @param {number|"-"} hits
146+
*/
147+
addBRDA(lineNumber, blockNumber, branchNumber, hits) {
148+
const existingRecord = this.findBRDA(lineNumber, blockNumber, branchNumber);
149+
150+
if (existingRecord) {
151+
existingRecord.addHits(hits);
152+
return;
153+
}
154+
155+
this.BRDARecords.push(
156+
new BRDA(lineNumber, blockNumber, branchNumber, hits)
157+
);
158+
}
159+
123160
toString() {
124161
return (
125162
`SF:${this.filename}\n` +
@@ -148,40 +185,6 @@ function findCoverageFile(source, filename) {
148185
return null;
149186
}
150187

151-
/**
152-
* Returns appropriate number of hits based on the string value of hits.
153-
*
154-
* @param {string} hits
155-
*
156-
* @returns {number}
157-
*/
158-
function numericHits(hits) {
159-
if (hits === '-') {
160-
return 0;
161-
}
162-
return parseInt(hits, 10);
163-
}
164-
165-
/**
166-
* Merges BRDA hits.
167-
*
168-
* @param {string} existingBRDAHits
169-
* @param {string} newBRDAHits
170-
*
171-
* @returns {number|string}
172-
*/
173-
function mergedBRDAHits(existingBRDAHits, newBRDAHits) {
174-
// If we've never executed the branch code path in an existing coverage
175-
// record, and we've never executed it here either, then keep it as '-'
176-
// (eg, never executed). If either of them is a number, then
177-
// use the number value.
178-
if (existingBRDAHits !== '-' || newBRDAHits !== '-') {
179-
return numericHits(existingBRDAHits) + numericHits(newBRDAHits);
180-
}
181-
182-
return '-';
183-
}
184-
185188
/**
186189
* Splits the second part of the prefix into an array. The array
187190
* is a list of strings which represent numbers.
@@ -242,26 +245,13 @@ function parseBRDA(currentCoverageFile, prefixSplit) {
242245
const blockNumber = parseInt(numberSplit[1], 10);
243246
const branchNumber = parseInt(numberSplit[2], 10);
244247

245-
const existingBRDA = currentCoverageFile.findBRDA(
246-
lineNumber,
247-
blockNumber,
248-
branchNumber
249-
);
250-
251248
// Special case, hits might be a '-'. This means that the code block
252249
// where the branch was contained was never executed at all (as opposed
253250
// to the code being executed, but the branch not being taken). Keep
254251
// it as a string and let mergedBRDAHits work it out.
255-
const hits = numberSplit[3];
252+
const hits = numberSplit[3] === '-' ? '-' : parseInt(numberSplit[3], 10);
256253

257-
if (existingBRDA) {
258-
existingBRDA.hits = mergedBRDAHits(existingBRDA.hits, hits);
259-
return;
260-
}
261-
262-
currentCoverageFile.BRDARecords.push(
263-
new BRDA(lineNumber, blockNumber, branchNumber, hits)
264-
);
254+
currentCoverageFile.addBRDA(lineNumber, blockNumber, branchNumber, hits);
265255
}
266256

267257
/**

0 commit comments

Comments
 (0)