-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (66 loc) · 2.09 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
/*jslint node: true, vars: true */
'use strict';
var util = require('util');
var assert = require('assert');
var test = global.test || global.it;
if (!test) {
throw new Error("'mocha' must be required before 'mocha-testdata'");
}
function noCasesFailingTest(title) {
return [
test(title, function () {
assert.fail('mocha-testdata used without test data');
})
];
}
function testData(async) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length === 0) {
return {
it: noCasesFailingTest,
test: noCasesFailingTest
};
}
if (args.length === 1 && Array.isArray(args[0])) {
args = args[0];
}
function formatTitle(title, value) {
var valueFmt;
if (typeof value === 'function') {
valueFmt = value.name ? value.name + '()' : '[function]';
} else if (typeof value === 'object') {
if (value && typeof value.description === 'string') {
valueFmt = value.description;
} else {
valueFmt = util.inspect(value);
}
} else {
valueFmt = value;
}
return title + ' <' + valueFmt + '>';
}
function testWithData(title, fn) {
return args.map(function (arg) {
return test(formatTitle(title, arg), function () {
return fn.apply(this, Array.isArray(arg) ? arg : [arg]);
});
});
}
function testWithDataAsync(title, fn) {
return args.map(function (arg) {
return test(formatTitle(title, arg), function (done) {
return fn.apply(this, Array.isArray(arg) ? [done].concat(arg) : [done, arg]);
});
});
}
var defineTest = async ? testWithDataAsync : testWithData;
return {
it: defineTest,
test: defineTest
};
}
module.exports = testData.bind(null, false);
module.exports.async = testData.bind(null, true);
// named exports as per TypeScript typings
module.exports.given = module.exports;
module.exports.givenAsync = module.exports.async;