forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest.js
112 lines (88 loc) · 2.67 KB
/
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
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
102
103
104
105
106
107
108
109
110
111
112
var util = require('util');
var _ = require('underscore');
var log = require('loglevel');
var chalk = require('../chalk');
var core = require('../core');
var h = require('../helper');
var cmd = {
command: 'test <filename>',
desc: 'send solution to leetcode and run test',
builder: {
testcase: {
alias: 't',
type: 'string',
default: '',
describe: 'Provide test case in command line'
},
i: {
type: 'boolean',
default: false,
describe: 'Provide test case interactively'
}
}
};
function prettyLine(actual, expected, key) {
if (!actual.hasOwnProperty(key))
return;
// hack: leetcode will return status_code = 10 even
// if the answer is not right!
if (key === 'status_code' && actual[key] === 10)
return;
var ok = true;
if (!actual.run_success) {
ok = false;
} else if (expected && !_.isEqual(actual[key], expected[key])) {
ok = false;
}
var line = (key === 'status_code') ?
util.format(' %s', h.statusToName(actual[key])) :
util.format(' %s: %s', key.split('_').pop(), actual[key]);
log.info(' ' + h.prettyText(line, ok));
}
function runTest(argv) {
// use the 1st section in filename as keyword
// e.g. two-sum.cpp, or two-sum.78502271.ac.cpp
var keyword = h.getFilename(argv.filename).split('.')[0];
core.getProblem(keyword, function(e, problem) {
if (e) return log.fail(e);
if (!problem.testable)
return log.fail('not testable? please submit directly!');
if (argv.testcase)
problem.testcase = argv.testcase.replace(/\\n/g, '\n');
if (!problem.testcase)
return log.fail('missing testcase?');
problem.file = argv.filename;
log.info('\nInput data:');
log.info(problem.testcase);
core.testProblem(problem, function(e, results) {
if (e) return log.fail(e);
for (var i = 0; i < results.length; ++i) {
log.info();
log.info(chalk.yellow(results[i].name));
prettyLine(results[i], null, 'status_code');
prettyLine(results[i], null, 'status_runtime');
prettyLine(results[i], results[i + 1], 'code_answer');
prettyLine(results[i], results[i + 1], 'code_output');
// show "xxx_error" message
_.chain(results[i])
.pick(function(v, k, obj) {
return /_error$/.test(k) && v.length > 0;
})
.keys()
.each(function(k) {
prettyLine(results[i], null, k);
});
}
});
});
}
cmd.handler = function(argv) {
if (!argv.i)
return runTest(argv);
h.readStdin(function(e, data) {
if (e) return log.fail(e);
argv.testcase = data;
return runTest(argv);
});
};
module.exports = cmd;