This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
292 lines (252 loc) · 9.1 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict';
/* globals WeakMap */
var isCallable = require('is-callable');
var isString = require('is-string');
var has = require('has');
var forEach = require('for-each');
var isArray = require('isarray');
var functionName = require('function.prototype.name');
var inspect = require('object-inspect');
var checkWithName = require('./helpers/checkWithName');
var withOverrides = require('./withOverrides');
var withOverride = require('./withOverride');
var withGlobal = require('./withGlobal');
var hasPrivacy = typeof WeakMap === 'function';
var wrapperMap = hasPrivacy ? new WeakMap() : /* istanbul ignore next */ null;
var descriptionMap = hasPrivacy ? new WeakMap() : /* istanbul ignore next */ null;
var modeMap = hasPrivacy ? new WeakMap() : /* istanbul ignore next */ null;
var MODE_ALL = 'all';
var MODE_SKIP = 'skip';
var MODE_ONLY = 'only';
var beforeMethods = ['before', 'beforeEach'];
var afterMethods = ['after', 'afterEach'];
var supportedMethods = [].concat(beforeMethods, afterMethods);
var MochaWrapper;
var checkThis = function requireMochaWrapper(instance) {
if (!instance || typeof instance !== 'object' || !(instance instanceof MochaWrapper)) {
throw new TypeError(inspect(instance) + ' must be a MochaWrapper');
}
return instance;
};
var setThisWrappers = function (instance, value) {
checkThis(instance);
/* istanbul ignore else */
if (hasPrivacy) {
wrapperMap.set(instance, value);
} else {
instance.wrappers = value;
}
return instance;
};
var getThisWrappers = function (instance) {
checkThis(instance);
return hasPrivacy ? wrapperMap.get(instance) : /* istanbul ignore next */ instance.wrappers;
};
var setThisDescription = function (instance, value) {
checkThis(instance);
/* istanbul ignore else */
if (hasPrivacy) {
descriptionMap.set(instance, value);
} else {
instance.description = value;
}
return instance;
};
var getThisDescription = function (instance) {
checkThis(instance);
return hasPrivacy ? descriptionMap.get(instance) : /* istanbul ignore next */ instance.description;
};
var setThisMode = function (instance, mode) {
checkThis(instance);
/* istanbul ignore else */
if (hasPrivacy) {
modeMap.set(instance, mode);
} else {
instance.mode = mode;
}
return instance;
};
var getThisMode = function (instance) {
checkThis(instance);
return hasPrivacy ? modeMap.get(instance) : /* istanbul ignore next */ instance.mode;
};
MochaWrapper = function MochaWrapper() {
setThisWrappers(this, []);
setThisMode(this, MODE_ALL);
};
var createWithWrappers = function (wrappers, description) {
return setThisDescription(setThisWrappers(new MochaWrapper(), wrappers), description);
};
var concatThis = function (instance, toConcat) {
var thisWrappers = getThisWrappers(instance);
var thisMode = getThisMode(instance);
return setThisMode(createWithWrappers(thisWrappers.concat(toConcat || [])), thisMode);
};
var flattenToDescriptors = function flattenToDescriptors(wrappers) {
if (wrappers.length === 0) { return []; }
var descriptors = [];
forEach(wrappers, function (wrapper) {
var subWrappers = wrapper instanceof MochaWrapper ? getThisWrappers(wrapper) : wrapper;
if (Array.isArray(subWrappers)) {
descriptors.push.apply(descriptors, flattenToDescriptors(subWrappers));
} else {
descriptors.push(subWrappers);
}
});
return descriptors;
};
var applyMethods = function applyMethods(methodsToApply, descriptors) {
forEach(descriptors, function (methods) {
forEach(methodsToApply, function (method) {
var functions = methods[method];
if (functions) {
forEach(functions, function (func) {
global[method](func);
});
}
});
});
};
var createAssertion = function createAssertion(type, message, wrappers, block, mode) {
var descriptors = flattenToDescriptors(wrappers);
if (descriptors.length === 0 && mode === MODE_ALL) {
throw new RangeError(inspect(type) + ' called with no wrappers defined');
}
var describeMsgs = [];
forEach(wrappers, function (wrapper) {
checkThis(wrapper);
describeMsgs.push(getThisDescription(wrapper));
});
var describeMsg = 'wrapped: ' + describeMsgs.join('; ') + ':';
var describeMethod = global.describe;
if (mode === MODE_SKIP) {
describeMethod = global.describe.skip;
} else if (mode === MODE_ONLY) {
describeMethod = global.describe.only;
}
describeMethod(describeMsg, function () {
applyMethods(beforeMethods, descriptors);
global[type](message, block);
applyMethods(afterMethods, descriptors.reverse());
});
};
MochaWrapper.prototype.skip = function skip() {
return setThisMode(concatThis(this), MODE_SKIP);
};
MochaWrapper.prototype.only = function only() {
return setThisMode(concatThis(this), MODE_ONLY);
};
MochaWrapper.prototype.it = function it(msg, fn) {
var wrappers = getThisWrappers(checkThis(this));
var mode = getThisMode(this);
createAssertion('it', msg, wrappers, fn, mode);
};
MochaWrapper.prototype.it.skip = function skip() {
throw new SyntaxError('mocha-wrap requires `.skip().it` rather than `it.skip`');
};
MochaWrapper.prototype.it.only = function only() {
throw new SyntaxError('mocha-wrap requires `.only().it` rather than `it.only`');
};
MochaWrapper.prototype.specify = function specify(msg, fn) {
var wrappers = getThisWrappers(checkThis(this));
var mode = getThisMode(this);
createAssertion('specify', msg, wrappers, fn, mode);
};
MochaWrapper.prototype.specify.skip = function skip() {
throw new SyntaxError('mocha-wrap requires `.skip().specify` rather than `specify.skip`');
};
MochaWrapper.prototype.specify.only = function only() {
throw new SyntaxError('mocha-wrap requires `.only().specify` rather than `specify.only`');
};
MochaWrapper.prototype.describe = function describe(msg, fn) {
var wrappers = getThisWrappers(checkThis(this));
var mode = getThisMode(this);
createAssertion('describe', msg, wrappers, fn, mode);
};
MochaWrapper.prototype.describe.skip = function skip() {
throw new SyntaxError('mocha-wrap requires `.skip().describe` rather than `describe.skip`');
};
MochaWrapper.prototype.describe.only = function only() {
throw new SyntaxError('mocha-wrap requires `.only().describe` rather than `describe.only`');
};
MochaWrapper.prototype.context = function context(msg, fn) {
var wrappers = getThisWrappers(checkThis(this));
var mode = getThisMode(this);
createAssertion('context', msg, wrappers, fn, mode);
};
MochaWrapper.prototype.context.skip = function skip() {
throw new SyntaxError('mocha-wrap requires `.skip().context` rather than `context.skip`');
};
MochaWrapper.prototype.context.only = function only() {
throw new SyntaxError('mocha-wrap requires `.only().context` rather than `context.only`');
};
var wrap = function wrap() { return new MochaWrapper(); };
var isWithNameAvailable = function (name) {
checkWithName(name);
return !has(MochaWrapper.prototype, name) || !isCallable(MochaWrapper.prototype[name]);
};
wrap.supportedMethods = isCallable(Object.freeze)
? Object.freeze(supportedMethods)
: /* istanbul ignore next */ supportedMethods.slice();
MochaWrapper.prototype.extend = function extend(description, descriptor) {
checkThis(this);
if (!isString(description) || description.length === 0) {
throw new TypeError('a non-empty description string is required');
}
var newWrappers = [];
if (descriptor) {
forEach(supportedMethods, function (methodName) {
if (methodName in descriptor) {
if (!isArray(descriptor[methodName])) {
descriptor[methodName] = [descriptor[methodName]];
}
forEach(descriptor[methodName], function (method) {
if (!isCallable(method)) {
throw new TypeError('wrapper method "' + method + '" must be a function, or array of functions, if present');
}
});
}
});
newWrappers = [createWithWrappers([descriptor])];
}
return setThisDescription(concatThis(this, newWrappers), description);
};
MochaWrapper.prototype.use = function use(plugin) {
checkThis(this);
if (!isCallable(plugin)) {
throw new TypeError('plugin must be a function');
}
var withName = functionName(plugin);
checkWithName(withName);
var extraArguments = Array.prototype.slice.call(arguments, 1);
var descriptorOrInstance = plugin.apply(this, extraArguments) || {};
var instance = descriptorOrInstance;
if (!(descriptorOrInstance instanceof MochaWrapper)) {
instance = wrap().extend(descriptorOrInstance.description, descriptorOrInstance);
}
var thisMode = getThisMode(instance);
return setThisMode(setThisWrappers(new MochaWrapper(), [instance]), thisMode);
};
wrap.register = function register(plugin) {
var withName = functionName(plugin);
checkWithName(withName);
if (!isWithNameAvailable(withName)) {
// already registered
return;
}
MochaWrapper.prototype[withName] = function wrapper() {
return this.use.apply(this, [plugin].concat(Array.prototype.slice.call(arguments)));
};
};
wrap.unregister = function unregister(pluginOrWithName) {
var withName = isCallable(pluginOrWithName) ? functionName(pluginOrWithName) : pluginOrWithName;
checkWithName(withName);
if (isWithNameAvailable(withName)) {
throw new RangeError('error: plugin "' + withName + '" is not registered.');
}
delete MochaWrapper.prototype[withName];
};
wrap.register(withOverrides);
wrap.register(withOverride);
wrap.register(withGlobal);
module.exports = wrap;