11import * as assert from 'assert' ;
22import * as vscode from 'vscode' ;
33import { applyGlobalReplace , normalizeInput } from '../cvbManager' ;
4- import { normalizeContent , removeComments , normalizeWhitespace , normalizePattern , removeSymbolSpaces } from '../fuzzyMatch' ;
4+ import * as fuzzyMatch from '../fuzzyMatch' ;
55
66// 定义 GlobalReplaceOperation 接口
77
@@ -276,7 +276,7 @@ suite('Normalization Full Coverage Test Suite', () =>
276276} // 结束函数
277277` ;
278278
279- const stResult = removeComments ( strInput ) ;
279+ const stResult = fuzzyMatch . removeComments ( strInput ) ;
280280 const strContent : string = stResult . content ;
281281 const arrMapping : number [ ] = stResult . mapping ;
282282
@@ -297,7 +297,7 @@ suite('Normalization Full Coverage Test Suite', () =>
297297 const strInput : string = `a + b
298298( x - y )
299299{ c * d }` ;
300- const stResult = removeSymbolSpaces ( strInput ) ;
300+ const stResult = fuzzyMatch . removeSymbolSpaces ( strInput ) ;
301301 const strContent : string = stResult . content ;
302302 const arrMapping : number [ ] = stResult . mapping ;
303303
@@ -316,7 +316,7 @@ suite('Normalization Full Coverage Test Suite', () =>
316316 const strInput : string = `abc def
317317ghi\t\tjkl
318318mno pqr` ;
319- const stResult = normalizeWhitespace ( strInput ) ;
319+ const stResult = fuzzyMatch . normalizeWhitespace ( strInput ) ;
320320 const strContent : string = stResult . content ;
321321 const arrMapping : number [ ] = stResult . mapping ;
322322
@@ -334,7 +334,7 @@ mno pqr`;
334334// 还有注释
335335` ;
336336
337- const stResult = removeComments ( strInput ) ;
337+ const stResult = fuzzyMatch . removeComments ( strInput ) ;
338338 const strContent : string = stResult . content ;
339339 const arrMapping : number [ ] = stResult . mapping ;
340340
@@ -351,7 +351,7 @@ mno pqr`;
351351
352352
353353def` ;
354- const stResult = normalizeWhitespace ( strInput ) ;
354+ const stResult = fuzzyMatch . normalizeWhitespace ( strInput ) ;
355355 const strContent : string = stResult . content ;
356356 const arrMapping : number [ ] = stResult . mapping ;
357357
@@ -365,7 +365,7 @@ def`;
365365 test ( 'removeSymbolSpaces - 复杂符号空格情况' , ( ) =>
366366 {
367367 const strInput : string = `a + ( b * c ) / [ d - e ]` ;
368- const stResult = removeSymbolSpaces ( strInput ) ;
368+ const stResult = fuzzyMatch . removeSymbolSpaces ( strInput ) ;
369369 const strContent : string = stResult . content ;
370370 const arrMapping : number [ ] = stResult . mapping ;
371371
@@ -379,7 +379,7 @@ def`;
379379 test ( 'normalizeWhitespace - 只有空格和换行符' , ( ) =>
380380 {
381381 const strInput : string = " \n \n " ;
382- const stResult = normalizeWhitespace ( strInput ) ;
382+ const stResult = fuzzyMatch . normalizeWhitespace ( strInput ) ;
383383 const strContent : string = stResult . content ;
384384 const arrMapping : number [ ] = stResult . mapping ;
385385
@@ -397,7 +397,7 @@ def`;
397397 let b = a * 2; // 还有注释
398398 return b;
399399}` ;
400- const stResult = normalizeContent ( strInput ) ;
400+ const stResult = fuzzyMatch . normalizeContent ( strInput ) ;
401401 const strContent : string = stResult . content ;
402402 const arrMapping : number [ ] = stResult . mapping ;
403403
@@ -412,3 +412,113 @@ def`;
412412 assert . strictEqual ( arrMapping . length , strExpectedContent . length , "normalizeContent mapping 长度错误" ) ;
413413 } ) ;
414414} ) ;
415+
416+ // 测试套件
417+ suite ( 'Fuzzy Global Replace Test Suite' , ( ) => {
418+ vscode . window . showInformationMessage ( 'Start all fuzzy global replace tests.' ) ;
419+
420+ const originalContent = `
421+ function logMessage(message) {
422+ console.log(message);
423+ }
424+
425+ function logError(error) {
426+ console.log(error);
427+ }
428+
429+ function logWarning(warning) {
430+ console.log(warning);
431+ }
432+ ` . trim ( ) ;
433+ const oldContent = `
434+ console.log(warn);
435+ ` . trim ( ) ;
436+ const newContent = `
437+ console.warn(warning);
438+ ` . trim ( ) ;
439+ const expectedContent = `
440+ function logMessage(message) {
441+ console.log(message);
442+ }
443+
444+ function logError(error) {
445+ console.log(error);
446+ }
447+
448+ function logWarning(warning) {
449+ console.warn(warning);
450+ }
451+ ` . trim ( ) ;
452+
453+ test ( 'normalizeContent should correctly normalize content and provide accurate mapping' , ( ) => {
454+ const { content : normContent , mapping } = fuzzyMatch . normalizeContent ( originalContent ) ;
455+ const logWarningStart = originalContent . indexOf ( 'console.log(warning);' ) ;
456+ const logWarningEnd = logWarningStart + 'console.log(warning);' . length ;
457+ const normLogWarningStart = normContent . indexOf ( 'console.log(warning);' ) ;
458+
459+ assert . ok ( normContent . includes ( 'console.log(warning);' ) , 'Normalized content should contain the target string' ) ;
460+ assert . strictEqual (
461+ mapping [ normLogWarningStart ] ,
462+ logWarningStart ,
463+ 'Mapping should point to original start position'
464+ ) ;
465+ assert . strictEqual (
466+ mapping [ normLogWarningStart + 'console.log(warning);' . length ] || mapping [ mapping . length - 1 ] ,
467+ logWarningEnd ,
468+ 'Mapping should point to original end position'
469+ ) ;
470+ } ) ;
471+
472+ test ( 'normalizePattern should correctly normalize the old content' , ( ) => {
473+ const normPattern = fuzzyMatch . normalizePattern ( oldContent ) ;
474+ assert . strictEqual (
475+ normPattern . trim ( ) ,
476+ 'console.log(warn);' ,
477+ 'Pattern should be normalized correctly'
478+ ) ;
479+ } ) ;
480+
481+ test ( 'findCandidatePositions should find potential match positions' , ( ) => {
482+ const { content : normContent } = fuzzyMatch . normalizeContent ( originalContent ) ;
483+ const normPattern = fuzzyMatch . normalizePattern ( oldContent ) ;
484+ const candidates = fuzzyMatch . findCandidatePositions ( normContent , normPattern ) ;
485+
486+ assert . ok ( candidates . length > 0 , 'Should find at least one candidate position' ) ;
487+ const logWarningPos = normContent . indexOf ( 'console.log(warning);' ) ;
488+ assert . ok (
489+ candidates . some ( pos => Math . abs ( pos - logWarningPos ) < normPattern . length * 2 ) ,
490+ 'Should include a position near the target string'
491+ ) ;
492+ } ) ;
493+
494+ test ( 'verifyMatches should select the best match with correct positions' , ( ) => {
495+ const { content : normContent , mapping } = fuzzyMatch . normalizeContent ( originalContent ) ;
496+ const normPattern = fuzzyMatch . normalizePattern ( oldContent ) ;
497+ const candidates = fuzzyMatch . findCandidatePositions ( normContent , normPattern ) ;
498+ const matches = fuzzyMatch . verifyMatches ( normContent , normPattern , candidates , mapping ) ;
499+
500+ assert . strictEqual ( matches . length , 1 , 'Should find exactly one best match' ) ;
501+ assert . strictEqual (
502+ originalContent . slice ( matches [ 0 ] . start , matches [ 0 ] . end ) . trim ( ) ,
503+ 'console.log(warning);' ,
504+ 'Best match should correspond to the closest substring'
505+ ) ;
506+ } ) ;
507+
508+ test ( 'applyReplacements should replace content correctly without extra characters' , ( ) => {
509+ const matches = [ {
510+ start : originalContent . indexOf ( 'console.log(warning);' ) ,
511+ end : originalContent . indexOf ( 'console.log(warning);' ) + 'console.log(warning);' . length
512+ } ] ;
513+ const result = fuzzyMatch . applyReplacements ( originalContent , matches , newContent ) ;
514+
515+ assert . strictEqual ( result . trim ( ) , expectedContent , 'Replacement should match expected output' ) ;
516+ assert . ok ( ! result . includes ( 'g);' ) , 'Result should not contain extra characters like "g);"' ) ;
517+ } ) ;
518+
519+ test ( 'applyFuzzyGlobalReplace should perform the full replacement correctly' , ( ) => {
520+ const result = fuzzyMatch . applyFuzzyGlobalReplace ( originalContent , oldContent , newContent ) ;
521+ 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);"' ) ;
523+ } ) ;
524+ } ) ;
0 commit comments