-
Notifications
You must be signed in to change notification settings - Fork 34
/
testCollectorHelper.ts
55 lines (53 loc) · 1.86 KB
/
testCollectorHelper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/***
* Create a unique statement id from path and start/end location for a given statement
*/
export function createUniqueStmtId(
relpath: string,
startLine: number,
startColumn: number,
endLine: number,
endColumn: number
) {
return `${relpath}@${startLine}:${startColumn}-${endLine}:${endColumn}`;
}
/**
* Get a map from statement index to unique statement id for a given file in the coverage report
* @param recordedStmtMap: the statement map recorded in the coverage report
* @param fileRelPath: the relative path of the file in the coverage report
* @returns a map from statement index to unique statement id (in same format as createUniqueStmtId)
*/
export function getFileStmts(recordedStmtMap: any, fileRelPath: string) {
const statementMap = new Map<string, string>();
for (const key of Object.keys(recordedStmtMap)) {
const {
start: { line: startLine, column: startColumn },
end: { line: endLine, column: endColumn },
} = recordedStmtMap[key];
const statementId = createUniqueStmtId(
fileRelPath,
startLine,
startColumn,
endLine,
endColumn
);
statementMap.set(key, statementId);
}
return statementMap;
}
/**
* Get the list of statements covered from a given file in the coverage report
* @param fileCoverage: the coverage report for a given file
* @param relpath: the relative path of the file in the coverage report
* @returns a list of covered statements (in same format as createUniqueStmtId)
*/
export function getCoveredStmtsForFile(fileCoverage: any, relpath: string) {
const statementMap = getFileStmts(fileCoverage.statementMap, relpath);
const coveredStmtIds = [];
for (const stmtIndx of Object.keys(fileCoverage.s)) {
const isCovered = fileCoverage.s[stmtIndx];
if (isCovered) {
coveredStmtIds.push(statementMap.get(stmtIndx)!);
}
}
return coveredStmtIds;
}