-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgetTestCase.js
101 lines (98 loc) · 2.87 KB
/
getTestCase.js
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
import { removeDomTags } from '../functions/removeDomTags.js'
import { getDataStructure } from './parseStructure.js'
import { DefaultLang } from '#common/constants/question.const.js'
import { setBlockComment } from '#common/utils/question-handler/questionLanguage.js'
/**
* 输出的日志
* @param question
* @param functionName
* @param cases
* @param expires
* @returns {`showLogs(
${string},
{
data: [${string}],
structure: ${string}
},
{
data: [${string}],
structure: ${string}
}
)`}
*/
function logsTemplate(question, functionName, cases, expires) {
return `showLogs(
${functionName},
{
data: [${cases}],
structure: ${JSON.stringify(getDataStructure(question.code))}
},
{
data: [${expires}],
structure: ${JSON.stringify(getDataStructure(question.code, 'return'))}
}
)`
}
/**
* test case 需要从两个地方拿到内容
* 1.详情:拿到默认的几个用例
* 2.函数名:拿到函数名创建用例函数
* @param question
* @returns {string}
*/
export function getTestCase(question) {
// 完整的一条语句的reg
const inputReg = /(<[a-zA-Z]+>)?输入[:|:](<\/[a-zA-Z]+>)?.+\n/g
const inputStartReg = /(<[a-zA-Z]+>)?输入[:|:]/gm
// 输出的reg
const outputReg = /(<[a-zA-Z]+>)?输出[:|:](<\/[a-zA-Z]+>)?.+\n/g
const outputStartReg = /(<[a-zA-Z]+>)?输出[:|:]/gm
// 结尾
const endReg = /(<\/[a-zA-Z]+>)?/gm
const detail = question.detail.replaceAll('`', '')
const cases = detail.match(inputReg)?.map(
(str) =>
`[${removeDomTags(
str
?.replace(inputStartReg, '')
?.replace(endReg, '')
?.replace('\n', '')
.replace(/[a-zA-Z]+ =/g, '')
)}]`
)
const expires = detail.match(outputReg)?.map((str) =>
removeDomTags(
str
?.replace(outputStartReg, '')
?.replace(endReg, '')
?.replace('\n', '')
.replace(/[a-zA-Z]+ =/g, '')
)
)
if (question.lang === DefaultLang) {
const functionName = question.code
?.match(/(var|let|const).+=/g)?.[0]
?.replace(/((var|let|const)|=)\s?/gm, '')
.trim()
if (!functionName) return ''
return (
setBlockComment(question.lang, 'Test Cases') +
logsTemplate(question, functionName, cases, expires)
)
} else {
// 其他语言无法支持测试 只能提供测试数据
// 生成注释语句
let showText = `暂无法支持除JS外的语言测试,提取的一些入参和返回值供自行测试,每一个case中的第一行为入参,第二行为返回值\n`
for (
let i = 0;
i < Math.max(cases?.length ?? 0, expires?.length ?? 0);
i++
) {
showText += `case ${i + 1}:\n`
showText += `${cases?.[i]}\n` ?? '[参数获取错误]\n'
showText += `${expires?.[i]}\n` ?? '[返回值获取错误]\n'
}
showText += `\n`
return setBlockComment(question.lang, showText)
}
}