Skip to content

Commit 1caa72e

Browse files
committed
bug fix
1 parent 0e808e4 commit 1caa72e

2 files changed

Lines changed: 119 additions & 15 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {describe, it, expect} from "vitest";
2+
import {extractComments} from "../src/lib/utils";
3+
4+
describe("extractComments", () => {
5+
it("extracts Python # line comments above a test", () => {
6+
const code = [
7+
" # Calls:",
8+
" # (415) POST:/v2/pet/{petId}/uploadImage",
9+
" # Found 1 potential fault of type-code 101",
10+
" @timeout_decorator.timeout(60)",
11+
" def test_23_post_on_uploadImage_returnsMismatchResponseWithSchema(self):",
12+
" pass",
13+
].join("\n");
14+
15+
expect(extractComments(code)).toBe(
16+
[
17+
"Calls:",
18+
"(415) POST:/v2/pet/{petId}/uploadImage",
19+
"Found 1 potential fault of type-code 101",
20+
].join("\n"),
21+
);
22+
});
23+
24+
it("extracts Java /** */ Javadoc blocks", () => {
25+
const code = [
26+
" /**",
27+
" * Calls:",
28+
" * (200) GET:/pets",
29+
" */",
30+
" @Test",
31+
" public void test_1() { }",
32+
].join("\n");
33+
34+
expect(extractComments(code)).toBe(["Calls:", "(200) GET:/pets"].join("\n"));
35+
});
36+
37+
it("extracts // line comments", () => {
38+
const code = [
39+
" // Calls:",
40+
" // (200) GET:/pets",
41+
" public void test() { }",
42+
].join("\n");
43+
44+
expect(extractComments(code)).toBe(["Calls:", "(200) GET:/pets"].join("\n"));
45+
});
46+
47+
it("separates distinct comment blocks with a blank line", () => {
48+
const code = [
49+
"# first block",
50+
"# more of first block",
51+
"",
52+
"code_line()",
53+
"# second block",
54+
].join("\n");
55+
56+
expect(extractComments(code)).toBe(
57+
["first block\nmore of first block", "second block"].join("\n\n"),
58+
);
59+
});
60+
61+
it("returns empty string when there are no comments", () => {
62+
const code = ["def test():", " pass"].join("\n");
63+
expect(extractComments(code)).toBe("");
64+
});
65+
66+
it("handles a single-line /* ... */ block", () => {
67+
const code = ["/* hello world */", "def f(): pass"].join("\n");
68+
expect(extractComments(code)).toBe("hello world");
69+
});
70+
});

web-report/src/lib/utils.tsx

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,57 @@ export const extractCodeLines = (
8585
};
8686

8787
export const extractComments = (code: string): string => {
88-
const blockRegex = /\/\*\*[\s\S]*?\*\//g;
89-
const blocks = code.match(blockRegex);
90-
if (!blocks || blocks.length === 0) {
91-
return "";
88+
const lines = code.split("\n");
89+
const groups: string[] = [];
90+
let current: string[] = [];
91+
let inBlock = false;
92+
93+
const flush = () => {
94+
if (current.length === 0) return;
95+
const text = current.join("\n").trim();
96+
if (text.length > 0) groups.push(text);
97+
current = [];
98+
};
99+
100+
for (const raw of lines) {
101+
const trimmed = raw.trim();
102+
103+
if (inBlock) {
104+
const endIdx = trimmed.indexOf("*/");
105+
const body = (endIdx >= 0 ? trimmed.slice(0, endIdx) : trimmed).replace(/^\*+\s?/, "");
106+
if (body.length > 0) current.push(body);
107+
if (endIdx >= 0) {
108+
inBlock = false;
109+
flush();
110+
}
111+
continue;
112+
}
113+
114+
if (trimmed.startsWith("/*")) {
115+
const afterOpen = trimmed.replace(/^\/\*+\s?/, "");
116+
const endIdx = afterOpen.indexOf("*/");
117+
if (endIdx >= 0) {
118+
const body = afterOpen.slice(0, endIdx).trim();
119+
if (body) current.push(body);
120+
flush();
121+
} else {
122+
inBlock = true;
123+
if (afterOpen.length > 0) current.push(afterOpen);
124+
}
125+
continue;
126+
}
127+
128+
const lineMatch = trimmed.match(/^(?:#|\/\/)\s?(.*)$/);
129+
if (lineMatch) {
130+
current.push(lineMatch[1]);
131+
continue;
132+
}
133+
134+
flush();
92135
}
136+
flush();
93137

94-
return blocks
95-
.map(block => {
96-
const inner = block.replace(/^\/\*\*/, "").replace(/\*\/$/, "");
97-
return inner
98-
.split("\n")
99-
.map(line => line.replace(/^\s*\*\s?/, ""))
100-
.join("\n")
101-
.trim();
102-
})
103-
.filter(text => text.length > 0)
104-
.join("\n\n");
138+
return groups.join("\n\n");
105139
};
106140

107141
export const calculateAllStatusCounts = (coveredHttpStatus: CoveredEndpoint[], endpointIds:string[]) => {

0 commit comments

Comments
 (0)