-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
195 lines (157 loc) · 5.41 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
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
/*jslint node: true, vars: true */
/*global describe: false, it: false */
'use strict';
var assert = require('assert');
var testData = require('./index');
var Runner = require('mocha').Runner;
var Suite = require('mocha').Suite;
function countFailures(tests, expectedFailures) {
var hook = {},
err = {},
suite = new Suite(),
runner;
tests.forEach(function (test) {
suite.addTest(test);
});
runner = new Runner(suite);
runner.failHook(hook, err);
assert.strictEqual(expectedFailures, runner.failures);
}
function nop() {
return;
}
function run(tests) {
tests.forEach(function (test) {
test.run(nop);
});
return tests;
}
describe('testData', function () {
it('is a function', function () {
assert.strictEqual(typeof testData, 'function');
});
it('fails the test if no arguments are provided', function () {
countFailures(testData().test('example test', function () { assert.pass(); }), 1);
});
it('defines "it" as a chain function', function () {
assert.strictEqual(typeof testData(1).it, 'function');
});
it('defines "test" as a chain function', function () {
assert.strictEqual(typeof testData(1).test, 'function');
});
it('passes arguments to chained test functions', function () {
var testValues = [], testRuns = 0;
run(testData(1, 2).test('example test', function (val) {
testValues.push(val);
testRuns += 1;
}));
assert.deepEqual(testValues, [1, 2]);
assert.strictEqual(testRuns, 2);
});
it('accepts a single array argument', function () {
var testValues = [], testRuns = 0;
run(testData([1, 2]).test('example test', function (val) {
testValues.push(val);
testRuns += 1;
}));
assert.deepEqual(testValues, [1, 2]);
assert.strictEqual(testRuns, 2);
});
it('applies array arguments as test function parameters', function () {
var testValues = [], testRuns = 0;
run(testData([1, 2], [3, 4]).test('example test', function (a, b) {
testValues.push(a);
testValues.push(b);
testRuns += 1;
}));
assert.deepEqual(testValues, [1, 2, 3, 4]);
assert.strictEqual(testRuns, 2);
});
it('relays the return value of the test function', function () {
var tests = testData(1).test('example test', function () {
return 1337;
});
var testReturnValue = tests[0].fn();
assert.strictEqual(testReturnValue, 1337);
});
it('formats titles with passed value', function () {
var tests = run(testData(
1,
'a',
null,
undefined,
true,
nop,
function () { return; },
[3, 4],
{a: 1},
{description: "fave", b: 2},
{description: 42}
).test('example test', function () {
assert.ok(true);
}));
var titles = tests.map(function (test) { return test.title; });
assert.deepEqual(titles, [
'example test <1>',
'example test <a>',
'example test <null>',
'example test <undefined>',
'example test <true>',
'example test <nop()>',
'example test <[function]>',
'example test <[ 3, 4 ]>',
'example test <{ a: 1 }>',
'example test <fave>',
'example test <{ description: 42 }>'
]);
});
});
describe('testData.async', function () {
it('is a function', function () {
assert.strictEqual(typeof testData.async, 'function');
});
it('defines "it" as a chain function', function () {
assert.strictEqual(typeof testData.async(1).it, 'function');
});
it('defines "test" as a chain function', function () {
assert.strictEqual(typeof testData.async(1).test, 'function');
});
it('relays the return value of the test function', function () {
var tests = testData.async(1).test('example test', function () {
return 47;
});
var testReturnValue = tests[0].fn();
assert.strictEqual(testReturnValue, 47);
});
it('passes arguments to chained test functions after "done" callback', function () {
var testValues = [], testRuns = 0;
run(testData.async(1, 2).test('example test', function (done, val) {
testValues.push(val);
testRuns += 1;
done();
}));
assert.deepEqual(testValues, [1, 2]);
assert.strictEqual(testRuns, 2);
});
it('accepts a single array argument', function () {
var testValues = [], testRuns = 0;
run(testData.async([1, 2]).test('example test', function (done, val) {
testValues.push(val);
testRuns += 1;
done();
}));
assert.deepEqual(testValues, [1, 2]);
assert.strictEqual(testRuns, 2);
});
it('applies array arguments as test function parameters', function () {
var testValues = [], testRuns = 0;
run(testData.async([1, 2], [3, 4]).test('example test', function (done, a, b) {
testValues.push(a);
testValues.push(b);
testRuns += 1;
done();
}));
assert.deepEqual(testValues, [1, 2, 3, 4]);
assert.strictEqual(testRuns, 2);
});
});