|
9 | 9 | const through = require('through2'); |
10 | 10 | const fs = require('fs'); |
11 | 11 | const path = require('path'); |
12 | | - |
13 | | -/** |
14 | | - * Represents a "DA" entry, which records the hit count of |
15 | | - * a line of code. |
16 | | - */ |
17 | | -class DA { |
18 | | - /** |
19 | | - * @param {number} lineNumber |
20 | | - * @param {number} hits |
21 | | - */ |
22 | | - constructor(lineNumber, hits) { |
23 | | - this.lineNumber = lineNumber; |
24 | | - this.hits = hits; |
25 | | - } |
26 | | - |
27 | | - /** |
28 | | - * @param {number} hits |
29 | | - */ |
30 | | - addHits(hits) { |
31 | | - this.hits += hits; |
32 | | - } |
33 | | - |
34 | | - toString() { |
35 | | - return `DA:${this.lineNumber},${this.hits}\n`; |
36 | | - } |
37 | | -} |
38 | | - |
39 | | -/** |
40 | | - * Represents a BRDA entry, which records the hit count |
41 | | - * of a logical branch in code. |
42 | | - */ |
43 | | -class BRDA { |
44 | | - /** |
45 | | - * @param {number} lineNumber |
46 | | - * @param {number} blockNumber |
47 | | - * @param {number} branchNumber |
48 | | - * @param {number|"-"} hits |
49 | | - */ |
50 | | - constructor(lineNumber, blockNumber, branchNumber, hits) { |
51 | | - this.lineNumber = lineNumber; |
52 | | - this.blockNumber = blockNumber; |
53 | | - this.branchNumber = branchNumber; |
54 | | - this.hits = hits; |
55 | | - } |
56 | | - |
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 | | - |
75 | | - toString() { |
76 | | - const { lineNumber, blockNumber, branchNumber, hits } = this; |
77 | | - return `BRDA:${lineNumber},${blockNumber},${branchNumber},${hits}\n`; |
78 | | - } |
79 | | -} |
80 | | - |
81 | | -/** |
82 | | - * Represents a coverage file, and it's DA/BRDA records. |
83 | | - */ |
84 | | -class CoverageFile { |
85 | | - /** |
86 | | - * @param {string} filename |
87 | | - */ |
88 | | - constructor(filename) { |
89 | | - this.filename = filename; |
90 | | - this.DARecords = []; |
91 | | - this.BRDARecords = []; |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Finds and returns an existing DA entry via its line number. |
96 | | - * |
97 | | - * @param {number} lineNumber |
98 | | - * |
99 | | - * @returns {DA|undefined} |
100 | | - */ |
101 | | - findDA(lineNumber) { |
102 | | - return this.DARecords.find((record) => record.lineNumber === lineNumber); |
103 | | - } |
104 | | - |
105 | | - /** |
106 | | - * Creates a new DA record, or adds the hit count to an existing |
107 | | - * record if available. |
108 | | - * |
109 | | - * @param {number} lineNumber |
110 | | - * @param {number} hits |
111 | | - */ |
112 | | - addDA(lineNumber, hits) { |
113 | | - const existingRecord = this.findDA(lineNumber); |
114 | | - |
115 | | - if (existingRecord) { |
116 | | - existingRecord.addHits(hits); |
117 | | - return; |
118 | | - } |
119 | | - |
120 | | - this.DARecords.push(new DA(lineNumber, hits)); |
121 | | - } |
122 | | - |
123 | | - /** |
124 | | - * Finds and returns an existing BRDA entry. |
125 | | - * |
126 | | - * @param {number} lineNumber |
127 | | - * @param {number} blockNumber |
128 | | - * @param {number} branchNumber |
129 | | - * |
130 | | - * @returns {BRDA|undefined} |
131 | | - */ |
132 | | - findBRDA(lineNumber, blockNumber, branchNumber) { |
133 | | - return this.BRDARecords.find( |
134 | | - (record) => |
135 | | - record.lineNumber === lineNumber && |
136 | | - record.blockNumber === blockNumber && |
137 | | - record.branchNumber === branchNumber |
138 | | - ); |
139 | | - } |
140 | | - |
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 | | - |
160 | | - toString() { |
161 | | - return ( |
162 | | - `SF:${this.filename}\n` + |
163 | | - this.DARecords.map((record) => record.toString()).join('') + |
164 | | - this.BRDARecords.map((record) => record.toString()).join('') + |
165 | | - 'end_of_record\n' |
166 | | - ); |
167 | | - } |
168 | | -} |
| 12 | +const CoverageFile = require('./lib/CoverageFile'); |
169 | 13 |
|
170 | 14 | /** |
171 | 15 | * Find an existing coverage file |
|
0 commit comments