UNPKG

2.38 kBJavaScriptView Raw
1/*
2 * Copyright 2012 Amadeus s.a.s.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var processFile = function processFile(out, fileName, fileInfos) {
17 fileName = fileName.replace(/\\/g, '/'); // use only forward slashes
18 out.push('SF:' + fileName);
19 var statements = fileInfos.code.src;
20 var execCount;
21 var realLines = {};
22 var lineNumber = 0;
23 var statementDetails = fileInfos.statements.detail;
24 for (var i = 1, l = statements.length; i <= l; i++) {
25 var curStatement = statements[i - 1];
26 var statementKey = curStatement.l;
27 if (statementKey) {
28 execCount = statementDetails[statementKey];
29
30 // TODO: perhaps replace this with something smarter (by changing node-coverage):
31 var matchLineNumber = /^[^0-9]*([0-9]+)_[0-9]+$/.exec(statementKey);
32 lineNumber = 1 + parseInt(matchLineNumber[1], 10);
33
34 var currentNumber = realLines[lineNumber];
35
36 if (currentNumber == null || execCount < currentNumber) {
37 // if several instructions are on the same line, keep the lowest execution count
38 realLines[lineNumber] = execCount;
39 }
40 }
41 }
42 var linesInstrumented = 0;
43 var linesCovered = 0;
44 for (lineNumber in realLines) {
45 execCount = realLines[lineNumber];
46 if (execCount > 0) {
47 linesCovered++;
48 }
49 linesInstrumented++;
50 out.push('DA:' + lineNumber + ',' + execCount);
51 }
52 out.push('LH:' + linesCovered);
53 out.push('LF:' + linesInstrumented);
54 out.push('end_of_record');
55};
56
57module.exports = function (coverage) {
58 var out = [];
59 var files = coverage.files;
60 for (var curFile in files) {
61 if (files.hasOwnProperty(curFile)) {
62 processFile(out, curFile, files[curFile]);
63 }
64 }
65 return out.join('\n');
66};