Skip to content

Implement uncovered branch highlight #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions assembly/diytest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function mycmp<T>(a: T, b: T): bool {
if (a > b) {
return true;
} else {
return false;
}
}

export function instancecmp<T>(a: T, b: T): bool {
if (isInteger<T>()) {
if (a > b) {
return true;
} else {
return false;
}
} else if (isFloat<T>()) {
if (a > b) {
return true;
} else {
return false;
}
}
return false;
}
14 changes: 10 additions & 4 deletions src/generator/html-generator/genCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ function generateLineCoverage(codes: CodeCoverage[]): string {
return str.join("\n");
}

function generateSource(codes: CodeCoverage[]): string {
function generateSource(codes: CodeCoverage[], result: FileCoverageResult): string {
const str: string[] = [];
for (const code of codes) {
str.push(escape(code.source));
for (const [index, code] of codes.entries()) {
if (result.linesToHighlight.has(index + 1)) {
// IMPORTANT! to add "nocode" here to preventing prettify from adding unwanted pln class
str.push('<span class="missing-if-branch nocode" title="Branch not taken">!</span>' + escape(code.source));
}
else {
str.push(escape(code.source));
}
}
return str.join("\n");
}
Expand All @@ -36,7 +42,7 @@ export function generateCodeHtml(relativePathofRoot: string, result: FileCoverag

const lineCoutHtml = generateLineCount(codes.length);
const lineCov = generateLineCoverage(codes);
const lineSource = generateSource(codes);
const lineSource = generateSource(codes, result);

return `
<!DOCTYPE html>
Expand Down
3 changes: 3 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ export class FileCoverageResult {
functionCoverageRate: Rate = new Rate();
lineCoverageRate: Rate = new Rate();
sourceUsedCount: CodeCoverage[] = [];
linesToHighlight: Set<number> = new Set();
}

export class FunctionCoverageResult {
constructor(public functionName: string) {}
branchCoverageRate: Rate = new Rate();
linesToHighlight: Set<number> = new Set();
lineRange: [number, number] = [Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER];
/**
* first means lineIndex;
Expand All @@ -111,6 +113,7 @@ export class FunctionCoverageResult {
];
result.branchCoverageRate = Rate.summarize(infos.map((info) => info.branchCoverageRate));
for (const info of infos) {
info.linesToHighlight.forEach(line => result.linesToHighlight.add(line));
for (const [lineIndex, count] of info.sourceUsedCount.entries()) {
const srcLineUsedCount = result.sourceUsedCount.get(lineIndex);
result.sourceUsedCount.set(lineIndex, srcLineUsedCount === undefined ? count : srcLineUsedCount + count);
Expand Down
6 changes: 4 additions & 2 deletions src/parser/singleFileAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ export class SingleFileCoverageAnalysis {
for (let index = startLine - 1; index < endLine; index++) {
const codeCoverage = this.result.sourceUsedCount[index];
if (codeCoverage === undefined) {
throw new Error(`unknowm error: There is no ${index} Line in file ${this.result.filename}`);
throw new Error(`unknown error: There is no ${index} Line in file ${this.result.filename}`);
}
codeCoverage.usedCount = 0;
}
}
}

merge(results: FunctionCoverageResult[]) {
// SingleFileCoverageAnalysis contains FileCoverageResult
if (results.length === 0) return;
for (const functionCovResult of results) {
functionCovResult.linesToHighlight.forEach(line => this.result.linesToHighlight.add(line));
for (const [lineIndex, count] of functionCovResult.sourceUsedCount.entries()) {
const srcLineUsedCount = this.result.sourceUsedCount[lineIndex - 1];
if (srcLineUsedCount === undefined) {
throw new Error(
`unknowm error: There is not Line ${lineIndex} in ${JSON.stringify(this.result.sourceUsedCount)}`
`unknown error: There is not Line ${lineIndex} in ${JSON.stringify(this.result.sourceUsedCount)}`
);
}
if (srcLineUsedCount.usedCount === CodeCoverage.default) {
Expand Down
17 changes: 17 additions & 0 deletions src/parser/singleFunctionAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type BranchGraph = Map<number, Map<number, boolean>>;
export class SingleFunctionCoverageAnalysis {
result: FunctionCoverageResult;
branchGraph: BranchGraph = new Map();
notFullyCoveredBasicBlock: Set<number> = new Set();
constructor(
public covInfo: CovInfo,
name: string
Expand Down Expand Up @@ -72,12 +73,28 @@ export class SingleFunctionCoverageAnalysis {
toNodes.set(second, true);
}
}
for (const toNodes of this.branchGraph) {
let [currentBasicBlock, branchesForThatBasicBlock] = toNodes;
for (const isCovered of branchesForThatBasicBlock.values()) {
if (!isCovered) {
this.notFullyCoveredBasicBlock.add(currentBasicBlock);
}
}
}
for (const toNodes of this.branchGraph.values()) {
let used = 0;
for (const toNode of toNodes.values()) {
if (toNode) used++;
}
this.result.branchCoverageRate.used += used;
}
// console.log(this.notFullyCoveredBasicBlock);
for (const block of this.notFullyCoveredBasicBlock) {
const lineInfo = this.covInfo.lineInfo.get(block);
if (lineInfo !== undefined && lineInfo.size > 0) {
this.result.linesToHighlight.add(Math.max(...lineInfo));
}
}
// console.log(this.result.functionName, this.result.lineRange, this.result.linesToHighlight);
}
}
31 changes: 31 additions & 0 deletions tests-as/diytest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, endTest, expect, test } from "../assembly";
import { mycmp, instancecmp } from "../assembly/diytest"

describe("cmp test", () => {
// 要考虑到流图可能不同,导致总branch数可能不同,所以计算branch覆盖率只能进行加权平均
test("a>b", () => {
let a: i32 = 1;
let b: i32 = 0;
expect(mycmp(a, b)).equal(true);
});
test("a<b", () => {
let a: f32 = 0;
let b: f32 = 0.5;
expect(mycmp(a, b)).equal(false);
});
});

describe("instance test", () => {
test("a>b", () => {
let a: i32 = 1;
let b: i32 = 0;
expect(instancecmp(a, b)).equal(true);
});
test("a<b", () => {
let a: f32 = 0;
let b: f32 = 0.5;
expect(instancecmp(a, b)).equal(false);
});
});

endTest();
13 changes: 13 additions & 0 deletions tests-ts/test/parser/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports[`Parser generateFileCoverage 1`] = `
"total": 18,
"used": 9,
},
"linesToHighlight": Set {},
"sourceUsedCount": [
CodeCoverage {
"source": "",
Expand Down Expand Up @@ -241,6 +242,7 @@ exports[`Parser generateFileCoverage 1`] = `
"total": 1,
"used": 1,
},
"linesToHighlight": Set {},
"sourceUsedCount": [
CodeCoverage {
"source": "",
Expand Down Expand Up @@ -466,6 +468,9 @@ exports[`Parser generateFileCoverage 1`] = `
"total": 13,
"used": 9,
},
"linesToHighlight": Set {
25,
},
"sourceUsedCount": [
CodeCoverage {
"source": "",
Expand Down Expand Up @@ -691,6 +696,7 @@ exports[`Parser generateFileCoverage 1`] = `
"total": 2,
"used": 2,
},
"linesToHighlight": Set {},
"sourceUsedCount": [
CodeCoverage {
"source": "",
Expand Down Expand Up @@ -917,6 +923,7 @@ exports[`Parser generateFunctionCoverage 1`] = `
39,
39,
],
"linesToHighlight": Set {},
"sourceUsedCount": Map {
39 => 3,
},
Expand All @@ -931,6 +938,7 @@ exports[`Parser generateFunctionCoverage 1`] = `
16,
24,
],
"linesToHighlight": Set {},
"sourceUsedCount": Map {
16 => 3,
17 => 3,
Expand All @@ -952,6 +960,7 @@ exports[`Parser generateFunctionCoverage 1`] = `
45,
45,
],
"linesToHighlight": Set {},
"sourceUsedCount": Map {
45 => 4,
},
Expand All @@ -966,6 +975,9 @@ exports[`Parser generateFunctionCoverage 1`] = `
10,
29,
],
"linesToHighlight": Set {
25,
},
"sourceUsedCount": Map {
10 => 2,
11 => 2,
Expand All @@ -989,6 +1001,7 @@ exports[`Parser generateFunctionCoverage 1`] = `
10,
11,
],
"linesToHighlight": Set {},
"sourceUsedCount": Map {
10 => 2,
11 => 2,
Expand Down
9 changes: 6 additions & 3 deletions tests-ts/test/parser/singleFileAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("singleFileAnalysis", () => {
functionName: "A",
lineRange: [6, 9],
branchCoverageRate: rate_A,
linesToHighlight: new Set<number>([]),
sourceUsedCount: new Map([
[6, 3],
[7, 0],
Expand All @@ -49,6 +50,7 @@ describe("singleFileAnalysis", () => {
functionName: "B",
lineRange: [10, 14],
branchCoverageRate: rate_B,
linesToHighlight: new Set<number>([]),
sourceUsedCount: new Map([
[10, 2],
[11, 0],
Expand All @@ -73,8 +75,8 @@ describe("singleFileAnalysis", () => {

test("setUnTestedFunction error", () => {
const analyzer = new SingleFileCoverageAnalysis("main", source);
expect(() => analyzer.setUnTestedFunction([[30, 31]])).toThrowError(
"unknowm error: There is no 29 Line in file main"
expect(() => analyzer.setUnTestedFunction([[30, 31]])).toThrow(
"unknown error: There is no 29 Line in file main"
);
});

Expand All @@ -88,12 +90,13 @@ describe("singleFileAnalysis", () => {
functionName: "A",
lineRange: [6, 30],
branchCoverageRate: rate,
linesToHighlight: new Set<number>([]),
sourceUsedCount: new Map([
[6, 3],
[7, 0],
[30, 3],
]),
};
expect(() => analyzer.merge([funcResult])).toThrowError();
expect(() => analyzer.merge([funcResult])).toThrow();
});
});