-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest.js
More file actions
225 lines (200 loc) · 6.72 KB
/
Copy pathtest.js
File metadata and controls
225 lines (200 loc) · 6.72 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const cp = require('child_process');
const _ = require('lodash');
const Aigle = require('aigle');
const argv = require('minimist')(process.argv.slice(2));
const target = argv.target || argv.t || '.*';
const { java, ruby, debug } = argv;
const mainpath = path.resolve(__dirname, 'algorithms');
const re = new RegExp(`${target}`);
(function resolve(dirpath) {
_.forEach(fs.readdirSync(dirpath), filename => {
const filepath = path.resolve(dirpath, filename);
if (fs.statSync(filepath).isDirectory()) {
return resolve(filepath);
}
if (/^test(.*).js$/.test(filename) && re.test(filepath)) {
try {
java ? createJavaTest(dirpath) : ruby ? createRubyTest(dirpath) : require(filepath);
} catch (e) {
debug && console.error(e.message, filepath);
}
}
});
})(mainpath);
function createJavaTest(dirpath) {
// copy Solution.java
const testname = dirpath.match(/\d+/)[0];
const newsolutionpath = path.resolve(__dirname, 'Solution.java');
const solutionpath = path.resolve(dirpath, 'Solution.java');
const solutionfile = fs.readFileSync(solutionpath, 'utf8');
// get function name
const jspath = path.resolve(dirpath, 'index.js');
const funcName = _.findKey(require(jspath));
// get function name and args from Solution.java
const re = new RegExp(`public (.*) ${funcName}`);
const resultType = solutionfile.match(re)[1];
const argre = new RegExp(`${funcName}\\((.*)\\)`);
const args = solutionfile.match(argre)[1].split(',');
const argMap = _.transform(
args,
(result, arg) => {
const [type, key] = arg.trim().split(/\s/);
result[key] = type;
},
{},
);
// get tasks from test.js
const testjspath = path.resolve(dirpath, 'test.js');
const tasks = require('./lib/test').getTasks(testjspath);
// create Test.java
const templatepath = path.resolve(__dirname, 'template', 'Template.java');
const testpath = path.resolve(__dirname, 'Test.java');
const template = fs.readFileSync(templatepath, 'utf8');
const offset = '\n\t\t';
describe(`#${testname}`, () => {
before(() => {
fs.writeFileSync(newsolutionpath, solutionfile, 'utf8');
return exec('javac Solution.java');
});
after(() => exec('rm Solution.* Test.*'));
let i = 0;
beforeEach(() => {
const task = tasks[i++];
const args = [];
let str = _.chain(task)
.omitBy((value, key) => /result/.test(key))
.reduce((result, value, key) => {
let s = '';
const type = argMap[key];
if (_.isInteger(value)) {
s = `${key} = ${value};`;
} else if (_.isNumber(value)) {
s = `${key} = ${value};`;
} else if (_.isString(value)) {
s = `${key} = "${value}";`;
} else if (_.isArray(value)) {
s = `${key} = {${value}};`;
}
args.push(key);
return `${result}${offset}${type} ${s}`.trim();
}, '')
.value();
str += `${offset}${resultType} result = new Solution().${funcName}(${args});`;
switch (resultType) {
case 'int[]':
str += `${offset}System.out.println(Arrays.toString(result));`;
break;
default:
str += `${offset}System.out.println(result);`;
break;
}
const file = template.replace(/<% tasks %>/, str);
fs.writeFileSync(testpath, file, 'utf8');
});
_.forEach(tasks, task => {
const { result } = task;
const str = _.chain(task)
.omit('result')
.reduce((result, value, key) => {
return `${result} ${key}: ${value}`;
}, '')
.add(` -> ${result}`)
.value();
it(str, async () => check(result, await exec('javac Test.java && java Test')));
});
});
}
function createRubyTest(dirpath) {
const testname = dirpath.match(/\d+/)[0];
const filepath = path.resolve(dirpath, 'solution.rb');
const testpath = path.resolve(__dirname, `${testname}.rb`);
const file = fs.readFileSync(filepath, 'utf8');
const [, funcName, argsStr] = file.match(/def (.*)\((.*)\)/);
const args = argsStr.split(',').map(s => s.trim());
const optionals = args.map(arg => /.+=.+/.test(arg));
const Optional = Symbol('optional');
const templatepath = path.resolve(__dirname, 'template', 'template.rb');
const template = fs.readFileSync(templatepath, 'utf8');
// get tasks from test.js
const testjspath = path.resolve(dirpath, 'test.js');
const tasks = require('./lib/test').getTasks(testjspath);
describe(`#${testname}`, () => {
beforeEach(() => {
const task = tasks.shift();
const argsStr = _.chain(args)
.map((arg, index) => {
const value = task[arg];
switch (typeof value) {
case 'undefined':
return optionals[index] ? Optional : 'nil';
case 'string':
return `'${value}'`;
case 'object':
return `[${value}]`;
default:
return value;
}
})
.filter(v => v !== Optional)
.join(',')
.value();
const file = template
.replace(/<% filepath %>/, `'${filepath}'`)
.replace(/<% func %>/, funcName)
.replace(/<% args %>/, argsStr);
fs.writeFileSync(testpath, file, 'utf8');
});
after(() => exec(`rm ${testpath}`));
_.forEach(tasks, task => {
const { result } = task;
const str = _.chain(task)
.omit('result')
.reduce((result, value, key) => {
return `${result} ${key}: ${value}`;
}, '')
.add(` -> ${result}`)
.value();
it(str, async () => check(result, await exec(`ruby ${testpath}`)));
});
});
}
function check(result, res) {
const logs = res.split(/\n/g);
logs.pop();
res = logs.pop();
_.forEach(logs, log => console.log(log));
switch (typeof result) {
case 'boolean':
assert.strictEqual(/true/.test(res), result);
break;
case 'string':
assert.strictEqual(res, result);
break;
case 'number':
assert.strictEqual(+res, result);
break;
case 'object':
assert.deepEqual(JSON.parse(res), result);
break;
default:
assert.fail();
break;
}
}
function exec(command, args) {
command = !args ? command : _.reduce(args, (str, arg) => `${str} ${arg}`, command);
return execute(cp.exec, command);
}
function execute(func, command, args) {
return new Aigle((resolve, reject) => {
let result = '';
const task = func(command, args);
task.on('close', err => (err ? reject(result) : resolve(result)));
task.stdout.on('data', data => (result += `${data}`));
task.stderr.on('data', data => (result += `${data}`));
});
}