-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblems.test.js
49 lines (45 loc) · 1.5 KB
/
problems.test.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
const fs = require('fs');
const expect = require('chai').expect;
const Benchmark = require('benchmark');
const _ = require('daguo');
const num = process.env.n;
const path = './problems';
const problems = fs.readdirSync(path);
console.log('<--start fns AC test-->'.underline);
if (num) {
// 测试指定题目
const reg = new RegExp(num);
const problem = problems.find(val => reg.test(val));
runTest(problem);
} else {
// 测试所以题目
problems.forEach(problem => runTest(problem));
}
/**
* 启动测试用例
* @param {string} problem 题目目录
*/
function runTest(problem) {
let fns = require('./problems/' + problem + '/index.js');
const testCases = require('./problems/' + problem + '/test.js');
//将单个函数转为数组格式
if (Object.prototype.toString.call(fns) !== '[object Array]') {
fns = [fns];
}
// 开始测试
describe(`test-problem: ${problem}`, function() {
// 将每个题目中导出的函数逐个执行测试用例
fns.forEach(fn => {
// 将多个测试用例逐个执行
testCases.forEach((testCase, testIndex) => {
// 如果该题目不需要测试则在module.exports =null即可
fn &&
it(`function:${fn.name} testCase-${testIndex}`, function() {
// 不能直接传入原始数组,不然testCase中的input被污染,影响下一个执行函数
const arr = _.clone(testCase);
expect(fn.apply(null, arr.input)).to.deep.equal(arr.output);
});
});
});
});
}