/
spy.js
492 lines (431 loc) · 12.7 KB
/
spy.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*!
* chai-spies :: a chai plugin
* Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/
/*!
* We are going to export a function that can be used through chai
*/
module.exports = function (chai, _) {
// Easy access
var Assertion = chai.Assertion
, flag = _.flag
, i = _.inspect
/**
* # chai.spy (function)
*
* Wraps a function in a proxy function. All calls will
* pass through to the original function.
*
* function original() {}
* var spy = chai.spy(original)
* , e_spy = chai.spy();
*
* @param {Function} function to spy on
* @returns function to actually call
* @api public
*/
chai.spy = function (name, fn) {
if (typeof name === 'function') {
fn = name;
name = undefined;
}
fn = fn || function () {};
function makeProxy (length, fn) {
switch (length) {
case 0 : return function () { return fn.apply(this, arguments); };
case 1 : return function (a) { return fn.apply(this, arguments); };
case 2 : return function (a,b) { return fn.apply(this, arguments); };
case 3 : return function (a,b,c) { return fn.apply(this, arguments); };
case 4 : return function (a,b,c,d) { return fn.apply(this, arguments); };
case 5 : return function (a,b,c,d,e) { return fn.apply(this, arguments); };
case 6 : return function (a,b,c,d,e,f) { return fn.apply(this, arguments); };
case 7 : return function (a,b,c,d,e,f,g) { return fn.apply(this, arguments); };
case 8 : return function (a,b,c,d,e,f,g,h) { return fn.apply(this, arguments); };
case 9 : return function (a,b,c,d,e,f,g,h,i) { return fn.apply(this, arguments); };
default : return function (a,b,c,d,e,f,g,h,i,j) { return fn.apply(this, arguments); };
}
};
var proxy = makeProxy(fn.length, function () {
var args = Array.prototype.slice.call(arguments);
proxy.__spy.calls.push(args);
proxy.__spy.called = true;
return fn.apply(this, args);
});
proxy.prototype = fn.prototype;
proxy.toString = function toString() {
var l = this.__spy.calls.length;
var s = "{ Spy";
if (this.__spy.name)
s += " '" + this.__spy.name + "'";
if (l > 0)
s += ", " + l + " call" + (l > 1 ? 's' : '');
s += " }";
return s;
};
/**
* # proxy.reset (function)
*
* Resets __spy object parameters for instantiation and reuse
* @returns proxy spy object
*/
proxy.reset = function() {
this.__spy = {
calls: []
, called: false
, name: name
};
return this;
}
return proxy.reset();
}
/**
* # chai.spy.on (function)
*
* Wraps an object method into spy. All calls will
* pass through to the original function.
*
* var spy = chai.spy.on(Array, 'isArray');
*
* @param {Object} object
* @param {...String} method names to spy on
* @returns passed object
* @api public
*/
chai.spy.on = function (object) {
var methodNames = Array.prototype.slice.call(arguments, 1);
methodNames.forEach(function(methodName) {
object[methodName] = chai.spy(object[methodName]);
});
return object;
};
/**
* # chai.spy.object (function)
*
* Creates an object with spied methods.
*
* var object = chai.spy.object('Array', [ 'push', 'pop' ]);
*
* @param {String} [name] object name
* @param {String[]|Object} method names or method definitions
* @returns object with spied methods
* @api public
*/
chai.spy.object = function (name, methods) {
var defs = {};
if (name && typeof name === 'object') {
methods = name;
name = 'object';
}
if (methods && !Array.isArray(methods)) {
defs = methods;
methods = Object.keys(methods);
}
return methods.reduce(function (object, methodName) {
object[methodName] = chai.spy(name + '.' + methodName, defs[methodName]);
return object;
}, {});
};
/**
* # chai.spy.returns (function)
*
* Creates a spy which returns static value.
*
* var method = chai.spy.returns(true);
*
* @param {*} value static value which is returned by spy
* @returns new spy function which returns static value
* @api public
*/
chai.spy.returns = function (value) {
return chai.spy(function () {
return value;
});
};
/**
* # spy
*
* Assert the the object in question is an chai.spy
* wrapped function by looking for internals.
*
* expect(spy).to.be.spy;
* spy.should.be.spy;
*
* @api public
*/
Assertion.addProperty('spy', function () {
this.assert(
'undefined' !== typeof this._obj.__spy
, 'expected ' + this._obj + ' to be a spy'
, 'expected ' + this._obj + ' to not be a spy');
return this;
});
/**
* # .called
*
* Assert that a spy has been called. Does not negate to allow for
* pass through language.
*
* @api public
*/
function assertCalled (n) {
new Assertion(this._obj).to.be.spy;
var spy = this._obj.__spy;
if (n) {
this.assert(
spy.calls.length === n
, 'expected ' + this._obj + ' to have been called #{exp} but got #{act}'
, 'expected ' + this._obj + ' to have not been called #{exp}'
, n
, spy.calls.length
);
} else {
this.assert(
spy.called === true
, 'expected ' + this._obj + ' to have been called'
, 'expected ' + this._obj + ' to not have been called'
);
}
}
function assertCalledChain () {
new Assertion(this._obj).to.be.spy;
}
Assertion.addChainableMethod('called', assertCalled, assertCalledChain);
/**
* # once
*
* Assert that a spy has been called exactly once
*
* @api public
*/
Assertion.addProperty('once', function () {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length === 1
, 'expected ' + this._obj + ' to have been called once but got #{act}'
, 'expected ' + this._obj + ' to not have been called once'
, 1
, this._obj.__spy.calls.length );
});
/**
* # twice
*
* Assert that a spy has been called exactly twice.
*
* @api public
*/
Assertion.addProperty('twice', function () {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length === 2
, 'expected ' + this._obj + ' to have been called twice but got #{act}'
, 'expected ' + this._obj + ' to not have been called twice'
, 2
, this._obj.__spy.calls.length
);
});
/**
* ### .with
*
*/
function assertWith () {
new Assertion(this._obj).to.be.spy;
var expArgs = [].slice.call(arguments, 0)
, calls = this._obj.__spy.calls
, always = _.flag(this, 'spy always')
, passed = 0;
calls.forEach(function (call) {
var actArgs = call.slice()
, found = 0;
expArgs.forEach(function (expArg) {
for (var i = 0; i < actArgs.length; i++) {
if (_.eql(actArgs[i], expArg)) {
found++;
actArgs.splice(i, 1);
break;
}
}
});
if (found === expArgs.length) passed++;
});
if (always) {
this.assert(
passed === calls.length
, 'expected ' + this._obj + ' to have been always called with #{exp} but got ' + passed + ' out of ' + calls.length
, 'expected ' + this._obj + ' to have not always been called with #{exp}'
, expArgs
);
} else {
this.assert(
passed > 0
, 'expected ' + this._obj + ' to have been called with #{exp}'
, 'expected ' + this._obj + ' to have not been called with #{exp} but got ' + passed + ' times'
, expArgs
);
}
}
function assertWithChain () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy with', true);
}
}
Assertion.addChainableMethod('with', assertWith, assertWithChain);
Assertion.addProperty('always', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy always', true);
}
});
/**
* # exactly (n)
*
* Assert that a spy has been called exactly `n` times.
*
* @param {Number} n times
* @api public
*/
Assertion.addMethod('exactly', function () {
new Assertion(this._obj).to.be.spy;
var always = _.flag(this, 'spy always')
, _with = _.flag(this, 'spy with')
, args = [].slice.call(arguments, 0)
, calls = this._obj.__spy.calls
, passed;
if (always && _with) {
passed = 0
calls.forEach(function (call) {
if (call.length !== args.length) return;
if (_.eql(call, args)) passed++;
});
this.assert(
passed === calls.length
, 'expected ' + this._obj + ' to have been always called with exactly #{exp} but got ' + passed + ' out of ' + calls.length
, 'expected ' + this._obj + ' to have not always been called with exactly #{exp}'
, args
);
} else if (_with) {
passed = 0;
calls.forEach(function (call) {
if (call.length !== args.length) return;
if (_.eql(call, args)) passed++;
});
this.assert(
passed > 0
, 'expected ' + this._obj + ' to have been called with exactly #{exp}'
, 'expected ' + this._obj + ' to not have been called with exactly #{exp} but got ' + passed + ' times'
, args
);
} else {
this.assert(
this._obj.__spy.calls.length === args[0]
, 'expected ' + this._obj + ' to have been called #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to not have been called #{exp} times'
, args[0]
, this._obj.__spy.calls.length
);
}
});
/**
* # gt (n)
*
* Assert that a spy has been called more than `n` times.
*
* @param {Number} n times
* @api public
*/
function above (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length > n
, 'expected ' + this._obj + ' to have been called more than #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called at most #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('above', above);
Assertion.overwriteMethod('gt', above);
/**
* # lt (n)
*
* Assert that a spy has been called less than `n` times.
*
* @param {Number} n times
* @api public
*/
function below (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length < n
, 'expected ' + this._obj + ' to have been called fewer than #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called at least #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('below', below);
Assertion.overwriteMethod('lt', below);
/**
* # min (n)
*
* Assert that a spy has been called `n` or more times.
*
* @param {Number} n times
* @api public
*/
function min (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length >= n
, 'expected ' + this._obj + ' to have been called at least #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called fewer than #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('min', min);
Assertion.overwriteMethod('least', min);
/**
* # max (n)
*
* Assert that a spy has been called `n` or fewer times.
*
* @param {Number} n times
* @api public
*/
function max (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length <= n
, 'expected ' + this._obj + ' to have been called at most #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called more than #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('max', max);
Assertion.overwriteMethod('most', max);
};