-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionalCoverageanalyzer.js
More file actions
64 lines (54 loc) · 1.86 KB
/
conditionalCoverageanalyzer.js
File metadata and controls
64 lines (54 loc) · 1.86 KB
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
56
57
58
59
60
61
62
63
64
const fs = require("fs");
const acorn = require("acorn");
const coverageData = require("./coverage/coverage.json");
// Read the content of the test.js file
const testCode = fs.readFileSync("test.js", "utf-8");
// Parse the code to generate AST
const ast = acorn.parse(testCode, { locations: true, ecmaVersion: "latest" });
// Function to traverse AST and count conditions
function countConditions(node, conditions) {
if (
node.type === "IfStatement" ||
node.type === "ForStatement" ||
node.type === "WhileStatement"
) {
conditions.push(node.loc.start.line);
}
// Traverse child nodes
for (const key in node) {
if (node[key] && typeof node[key] === "object") {
countConditions(node[key], conditions);
}
}
}
// Initialize conditions array
const conditions = [];
// Traverse AST to count conditions
countConditions(ast, conditions);
function calculateAndDisplayResults(conditions, coverageData) {
const totalConditions = conditions.length;
let executedConditions = 0;
conditions.forEach((lineNumber) => {
const lineExecutionCount = coverageData[lineNumber];
if (lineExecutionCount !== undefined && lineExecutionCount > 0) {
executedConditions++;
}
});
const percentageExecuted = (executedConditions / totalConditions) * 100;
console.log(
"=============================== Conditional Coverage summary ==============================="
);
console.log("Total Conditions:", totalConditions);
console.log("Executed Conditions:", executedConditions);
console.log("Percentage Executed:", percentageExecuted.toFixed(2) + "%");
console.log(
"============================================================================================="
);
}
// Display results
calculateAndDisplayResults(
conditions,
coverageData[
"C:\\Users\\fatim\\Downloads\\JS-Code-Analyzer-main\\JS-Code-Analyzer-main\\test.js"
]
);