-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-verification.js
More file actions
129 lines (109 loc) · 3.45 KB
/
Copy pathtest-verification.js
File metadata and controls
129 lines (109 loc) · 3.45 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* 测试验证脚本 - 验证测试断言的正确性
*/
function parseCommentLogic(text, pattern) {
const trimmed = text.trim();
if (!trimmed.startsWith(pattern.start) || !trimmed.endsWith(pattern.end)) {
return null;
}
let content = trimmed.slice(pattern.start.length, -pattern.end.length);
const lines = content.split("\n");
const processedLines = [];
for (let line of lines) {
line = line.trimStart();
// 只移除单独的 *,不破坏 markdown 粗体标记 **text**
if (line.startsWith("*") && !line.startsWith("**")) {
line = line.slice(1).trimStart();
}
line = line.trimEnd();
processedLines.push(line);
}
// 移除首尾空行
while (processedLines.length > 0 && processedLines[0].trim() === "") {
processedLines.shift();
}
while (processedLines.length > 0 && processedLines[processedLines.length - 1].trim() === "") {
processedLines.pop();
}
return processedLines.join("\n");
}
// 测试用例
const tests = [
{
name: "连续星号测试",
input: `/*
**** Many stars
* Normal line
*/`,
expected: "**** Many stars\nNormal line",
pattern: { start: "/*", end: "*/" }
},
{
name: "Markdown 粗体测试",
input: `/*
* **Bold text** should work
* ***Extra bold*** should also work
*/`,
expected: "**Bold text** should work\n***Extra bold*** should also work",
pattern: { start: "/*", end: "*/" }
},
{
name: "嵌套星号测试",
input: `/*
* *Item 1*
* **Item 2**
*/`,
expected: "*Item 1*\n**Item 2**",
pattern: { start: "/*", end: "*/" }
}
];
console.log("=".repeat(60));
console.log("测试断言验证");
console.log("=".repeat(60));
let passed = 0;
let failed = 0;
tests.forEach(test => {
const result = parseCommentLogic(test.input, test.pattern);
const isPassed = result === test.expected;
console.log(`\n测试: ${test.name}`);
console.log(`输入:`);
console.log(` "${test.input.replace(/\n/g, '\\n')}"`);
console.log(`预期: "${test.expected.replace(/\n/g, '\\n')}"`);
console.log(`实际: "${result?.replace(/\n/g, '\\n')}"`);
console.log(`结果: ${isPassed ? '✅ 通过' : '❌ 失败'}`);
if (isPassed) {
passed++;
} else {
failed++;
}
});
console.log("\n" + "=".repeat(60));
console.log(`总计: ${passed} 通过, ${failed} 失败`);
console.log("=".repeat(60));
// 手动追踪第一个测试
console.log("\n详细追踪:连续星号测试");
console.log("-".repeat(60));
const input = `/*
**** Many stars
* Normal line
*/`;
console.log(`原始输入: "${input.replace(/\n/g, '\\n')}"`);
const trimmed = input.trim();
console.log(`1. trim: "${trimmed.replace(/\n/g, '\\n')}"`);
const content = trimmed.slice(2, -2);
console.log(`2. 移除标记: "${content.replace(/\n/g, '\\n')}"`);
const lines = content.split("\n");
console.log(`3. 分割行: ${JSON.stringify(lines)}`);
console.log(`4. 处理每一行:`);
lines.forEach((line, i) => {
const trimmedLine = line.trimStart();
const hasStar = trimmedLine.startsWith("*");
const hasDoubleStar = trimmedLine.startsWith("**");
const shouldRemove = hasStar && !hasDoubleStar;
const finalLine = shouldRemove ? trimmedLine.slice(1).trimStart() : trimmedLine.trimEnd();
console.log(` 行 ${i}: "${line}"`);
console.log(` - trimStart: "${trimmedLine}"`);
console.log(` - 以"*"开头: ${hasStar}, 以"**"开头: ${hasDoubleStar}`);
console.log(` - 是否移除: ${shouldRemove}`);
console.log(` - 结果: "${finalLine}"`);
});