Skip to content

Commit 2c06be2

Browse files
committed
test: 新增单元测试
1 parent 0945b08 commit 2c06be2

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

test/create.spec.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { test, expect, describe,vi, afterEach, it } from 'vitest';
2+
import A from './features/test';
3+
const { getQuestionToday} = require('../common/utils/getQuestionToday');
4+
const {sourceFilePath} = require('../common/utils/createQuestion');
5+
const {generateTemplateContent} = require('../common/utils/fulfillQuestion');
6+
const {getQuestionById} = require('../common/utils/getQuestionById');
7+
const {removeDomTags} = require('../common/utils/removeDomTags');
8+
const fs = require('fs');
9+
10+
11+
vi.mock('fs/promises', () => {
12+
return {
13+
writeFile: vi.fn(),
14+
}
15+
})
16+
const funRegex = /var\s+(\w+)\s*=\s*function\s*\(([^)]*)\)\s*{\s*([^}]*)\s*}/;
17+
const isContainJsCode = (input) => funRegex.test(input);
18+
const isContainTestCase = (input) => input.includes('showLogs(');
19+
20+
const handleText = (input) => input.replace(/\n+/g, '\n').replaceAll('\n', '\n * ');
21+
const mockKeys = [ 'enName', 'title', 'detail', 'id', 'jsCode', 'date' ];
22+
23+
function isValidQuestion(res) {
24+
const content = generateTemplateContent(fileContent,res);
25+
// 是否含有函数
26+
expect(isContainJsCode(content)).toBeTruthy();
27+
// 是否含有测试用例
28+
expect(isContainTestCase(content)).toBeTruthy();
29+
// 是否含有描述
30+
expect(content.includes('示例')).toBeTruthy();
31+
expect(content.includes('提示')).toBeTruthy();
32+
33+
}
34+
const fileContent = fs.readFileSync(sourceFilePath, 'utf-8');
35+
36+
describe('leet-create', ()=> {
37+
// 清楚mock历史记录
38+
afterEach(()=> {
39+
vi.clearAllMocks();
40+
})
41+
describe('with -t option', async() => {
42+
const res = await getQuestionToday();
43+
44+
it('是否正确获取了今天的题目', () => {
45+
expect(Object.keys(res)).toEqual(mockKeys);
46+
// 比较日期是否相等
47+
expect(new Date(res.date).getDate()).toEqual(new Date().getDate());
48+
// 比较月份是否相等
49+
expect(new Date(res.date).getMonth()).toEqual(new Date().getMonth());
50+
// 比较年份是否相等
51+
expect(new Date(res.date).getFullYear()).toEqual(new Date().getFullYear());
52+
53+
});
54+
it('是否正确的填充了今天的题目', async ()=> {
55+
isValidQuestion(res)
56+
57+
})
58+
59+
});
60+
describe('with -r option', async () => {
61+
const id_25 = '25';
62+
const res_25 = await getQuestionById(id_25);
63+
const id_LCS_03 = 'LCS 03';
64+
const res_LCS_03 = await getQuestionById(id_LCS_03);
65+
it('是否正确的获取了指定id的题目 25', async () => {
66+
expect(res_25.id).toEqual(id_25);
67+
})
68+
it('是否正确填充了指定id的题目 25', async ()=> {
69+
isValidQuestion(res_25)
70+
})
71+
it('是否正确的获取了指定id的题目 LCS 03', async () => {
72+
expect(res_LCS_03.id).toEqual(id_LCS_03);
73+
})
74+
it('是否正确填充了指定id的题目 9', async ()=> {
75+
isValidQuestion(res_LCS_03)
76+
})
77+
78+
it('是否正确的获取了指定内容的题目 主题空间', async () => {
79+
const content = '主题空间';
80+
const res = await getQuestionById(content);
81+
expect(res.id).toEqual(null);
82+
})
83+
84+
})
85+
})
86+

test/graph.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test, expect, describe } from 'vitest';
1+
import { test, expect } from 'vitest';
22
const Node = require('../common/structures/Node');
33

44
const graphArray1 = [[2, 4], [1, 3], [2, 4], [1, 3]];
@@ -21,4 +21,5 @@ test('测试无向连通图', () => {
2121

2222
}
2323

24-
})
24+
25+
})

test/listNode.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ test('toArray false', () => {
2323
test('toArray 1', () => {
2424
const arr = ListNode.toArray(1)
2525
expect(arr).toEqual([undefined])
26+
})
27+
test('parse [1,2,3]', () => {
28+
const listNode = ListNode.parse([1,2,3])
29+
expect(listNode.val).toEqual(1)
30+
expect(listNode.next?.val).toEqual(2)
31+
expect(listNode.next?.next?.val).toEqual(3)
32+
})
33+
test('parse []', () => {
34+
const listNode = ListNode.parse([])
35+
expect(listNode).toEqual(null)
2636
})

test/tree.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ test('测试树', ()=>{
1212
expect(result).toEqual(mockTree);
1313

1414
})
15+
test('测试树2 特殊值', ()=>{
16+
expect(TreeNode.toArray(null)).toEqual([]);
17+
})

0 commit comments

Comments
 (0)