-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
204 lines (172 loc) · 4.71 KB
/
index.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
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
'use strict';
var async = require('async-done');
var expect = require('chai').expect;
var _it = it;
var INTERPOLATE = /\\{|\\}|{\s*([.\w]+?)\s*}/gm;
/**
* Run test runner using given test cases.
* A tiny mocha test case runner. Suited for simple input to output validation tests.
*
* @param testCases {array | object} the test case(s) to verify
* @param runner {function} optional, the test runner
* @param options {any} optional, extra value passed to runner
*
*/
function test(testCases, runner, options) {
var it;
if (!Array.isArray(testCases)) {
return test([testCases], runner, options);
} else if (runner && typeof runner !== 'function') {
return test(testCases, null, runner);
} else if (!options) {
return test(testCases, runner, {});
}
it = options.it || _it;
(filter(testCases, only) || filter(testCases, skip) || testCases).forEach(runTest);
function runTest(testCase) {
var prefix = testCase.prefix || options.prefix || '';
var testRunner = getTestRunner();
if ('cases' in testCase) {
testPairValues();
} else if ('values' in testCase) {
testMultiValues();
} else {
testSingleValue(testCase);
}
function getTestRunner() {
var run = testCase.runner || options.runner || runner || passThrough;
return isErrback() ? testErrback : testAsyncDone;
function passThrough(value) {
return value;
}
function isErrback() {
if ('errback' in testCase) {
return testCase.errback;
}
return options.errback;
}
function testErrback(theCase, done) {
run(theCase.value, theCase.options, done);
}
function testAsyncDone(theCase, done) {
async(function (asyncDone) {
var actual;
actual = run(theCase.value, theCase.options);
if (actual && typeof (actual.on || actual.subscribe || actual.then) === 'function') {
return actual;
}
asyncDone(null, actual);
}, done);
}
}
function testPairValues() {
testCase.cases.forEach(function (theCase) {
var value, expected;
if (Array.isArray(theCase)) {
value = theCase[0];
expected = theCase[1];
} else {
value = theCase.value;
expected = theCase.expected;
}
testSingleValue({
name: testCase.name,
value: value,
expected: expected,
error: testCase.error,
options: testCase.options
});
});
}
function testMultiValues() {
var expected = which(testCase.expected);
testCase.values.forEach(function (value, i) {
testSingleValue({
name: testCase.name,
value: value,
expected: expected(i),
error: testCase.error,
options: testCase.options
});
});
function which(values) {
if (Array.isArray(values)) {
return function (index) {
return values[index];
};
}
return function () {
return values;
};
}
}
function testSingleValue(theCase) {
it(prefix + title(theCase), function (done) {
testRunner(theCase, sandbox(verify, done));
});
function verify(err, actual) {
if (theCase.error) {
if (typeof theCase.error === 'function') {
expect(err).to.be.instanceof(theCase.error);
} else {
expect(err).to.deep.equal(theCase.error);
}
} else {
expect(actual).to.deep.equal(theCase.expected);
}
}
}
}
}
function filter(testCases, fn) {
var filtered = testCases.filter(fn);
if (filtered.length) {
return filtered;
}
}
function only(testCase) {
return testCase.only;
}
function skip(testCase) {
return !testCase.skip;
}
function title(testCase) {
if (typeof testCase.name === 'function') {
return testCase.name(testCase.value, testCase.expected, testCase.options);
}
return testCase.name.replace(INTERPOLATE, function (match, paths) {
switch (match) {
case '\\{':
return '{';
case '\\}':
return '}';
default:
return get(testCase, paths) || '{' + paths + '}';
}
});
}
function get(values, name) {
var i, n, path, paths, node;
node = values;
paths = name.split('.');
for (i = 0, n = paths.length; i < n; ++i) {
path = paths[i];
node = node[path];
if (typeof node === 'undefined') {
return null;
}
}
return JSON.stringify(node);
}
function sandbox(expr, done) {
return function () {
var err, result;
try {
result = expr.apply(null, arguments);
} catch (ex) {
err = ex;
}
done(err, result);
};
}
module.exports = test;