Skip to content

Commit d009aad

Browse files
committed
test:模糊匹配测试全过
1 parent f939930 commit d009aad

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/cvbManager.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,26 @@ class ExactReplaceOperation extends TcvbOperation {
206206
}
207207
}
208208

209+
// 函数:规范化 GlobalReplaceOperation 实例的成员
210+
export function normalizeData(operation: GlobalReplaceOperation): GlobalReplaceOperation {
211+
operation.m_strOldContent = normalizeInput(operation.m_strOldContent);
212+
operation.m_strNewContent = normalizeInput(operation.m_strNewContent);
213+
return operation;
214+
}
215+
209216
// 2. 全局替换操作(GLOBAL-REPLACE)
210217
class GlobalReplaceOperation extends TcvbOperation {
211218
public m_strOldContent: string;
212219
public m_strNewContent: string;
213220

214221
constructor(
215-
m_strFilePath: string,
216-
m_strOldContent: string,
217-
m_strNewContent: string
222+
strFilePath: string,
223+
strOldContent: string,
224+
strNewContent: string
218225
) {
219-
super(m_strFilePath, "global-replace");
220-
this.m_strOldContent = m_strOldContent;
221-
this.m_strNewContent = m_strNewContent;
226+
super(strFilePath, "global-replace");
227+
this.m_strOldContent = normalizeInput(strOldContent);
228+
this.m_strNewContent = normalizeInput(strNewContent);
222229
}
223230
}
224231

@@ -830,11 +837,13 @@ export function applyGlobalReplace(
830837
});
831838
}
832839

840+
try {
833841
return FuzzyMatch.applyFuzzyGlobalReplace(strContent, op.m_strOldContent, op.m_strNewContent);
834-
842+
}catch (error : any) {
835843
const errorMsg = `GLOBAL-REPLACE 失败:FILE:"${op.m_strFilePath}" 中未找到OLD_CONTENT: "${op.m_strOldContent}" 可能是和原文有细微差异,或者文件路径和别的文件搞错了`;
836844
console.log(errorMsg + `\n表达式: ${regPattern}`);
837845
throw new Error(errorMsg);
846+
}
838847
}
839848

840849
// 根据前锚点、内容、后锚点构建正则表达式(dotall 模式)

src/test/fuzzyMatch.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22
import * as vscode from 'vscode';
3-
import { applyGlobalReplace, normalizeInput } from '../cvbManager';
3+
import { applyGlobalReplace, normalizeInput, normalizeData } from '../cvbManager';
44
import * as fuzzyMatch from '../fuzzyMatch';
55

66
// 定义 GlobalReplaceOperation 接口
@@ -29,13 +29,6 @@ class GlobalReplaceOperation extends TcvbOperation {
2929
}
3030
}
3131

32-
// 函数:规范化 GlobalReplaceOperation 实例的成员
33-
function normalizeData(operation: GlobalReplaceOperation): GlobalReplaceOperation {
34-
operation.m_strOldContent = normalizeInput(operation.m_strOldContent);
35-
operation.m_strNewContent = normalizeInput(operation.m_strNewContent);
36-
return operation;
37-
}
38-
3932
suite('Extension Test Suite', () => {
4033
vscode.window.showInformationMessage('Start all tests.');
4134

@@ -236,7 +229,7 @@ function anotherCompute(a, b) {
236229
m_strType: "global-replace",
237230
m_strFilePath: 'test.js',
238231
m_strOldContent: `
239-
return a + b;
232+
return aaa + bbb;
240233
`,
241234
m_strNewContent: `
242235
return a - b;
@@ -513,12 +506,12 @@ function logWarning(warning) {
513506
const result = fuzzyMatch.applyReplacements(originalContent, matches, newContent);
514507

515508
assert.strictEqual(result.trim(), expectedContent, 'Replacement should match expected output');
516-
assert.ok(!result.includes('g);'), 'Result should not contain extra characters like "g);"');
509+
assert.ok(!result.includes('warn);'), 'Result should not contain incorrectly replaced warn);');
517510
});
518511

519512
test('applyFuzzyGlobalReplace should perform the full replacement correctly', () => {
520513
const result = fuzzyMatch.applyFuzzyGlobalReplace(originalContent, oldContent, newContent);
521514
assert.strictEqual(result.trim(), expectedContent, 'Full fuzzy replace should produce the expected output');
522-
assert.ok(!result.includes('g);'), 'Result should not contain extra characters like "g);"');
515+
assert.ok(!result.includes('warn);'), 'Result should not contain incorrectly replaced warn);');
523516
});
524517
});

0 commit comments

Comments
 (0)