All files / src/utils inject-prompt-diagnostic-with-callback.test.ts

0% Statements 0/37
100% Branches 1/1
100% Functions 1/1
0% Lines 0/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                         
/**
 * 测试文件路径解析功能
 */
 
// 由于这是一个简单的单元测试,我们可以直接复制函数进行测试
function parseFilePathWithLineInfo(pathWithLineInfo: string): {
  filePath: string
  line?: number
  column?: number
} {
  // 使用正则表达式匹配文件路径和行号、列号
  const match = pathWithLineInfo.match(/^(.+?):(\d+)(?::(\d+))?$/)
 
  if (match) {
    const [, filePath, lineStr, columnStr] = match
    return {
      filePath: filePath.trim(),
      line: Number.parseInt(lineStr, 10),
      column: columnStr ? Number.parseInt(columnStr, 10) : undefined,
    }
  }
 
  // 如果没有匹配到行号信息,返回原始路径
  return {
    filePath: pathWithLineInfo.trim(),
  }
}
 
// 测试用例
console.log('测试文件路径解析功能:')
 
// 测试1: 包含行号和列号的路径
const test1 = parseFilePathWithLineInfo('playground/src/components/UserInfo.vue:21:7')
console.log('Test 1 - 完整路径:', test1)
console.assert(test1.filePath === 'playground/src/components/UserInfo.vue', 'filePath should match')
console.assert(test1.line === 21, 'line should be 21')
console.assert(test1.column === 7, 'column should be 7')
 
// 测试2: 只包含行号的路径
const test2 = parseFilePathWithLineInfo('src/components/App.vue:15')
console.log('Test 2 - 只有行号:', test2)
console.assert(test2.filePath === 'src/components/App.vue', 'filePath should match')
console.assert(test2.line === 15, 'line should be 15')
console.assert(test2.column === undefined, 'column should be undefined')
 
// 测试3: 普通文件路径(无行号信息)
const test3 = parseFilePathWithLineInfo('src/utils/helper.ts')
console.log('Test 3 - 普通路径:', test3)
console.assert(test3.filePath === 'src/utils/helper.ts', 'filePath should match')
console.assert(test3.line === undefined, 'line should be undefined')
console.assert(test3.column === undefined, 'column should be undefined')
 
// 测试4: 带空格的路径
const test4 = parseFilePathWithLineInfo('  src/components/UserInfo.vue:21:7  ')
console.log('Test 4 - 带空格:', test4)
console.assert(test4.filePath === 'src/components/UserInfo.vue', 'filePath should be trimmed')
console.assert(test4.line === 21, 'line should be 21')
console.assert(test4.column === 7, 'column should be 7')
 
console.log('所有测试通过! ✅')