test.js
2.32 KB
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
var arity0 = require('./0'),
arity1 = require('./1'),
arity2 = require('./2'),
arity3 = require('./3'),
arity4 = require('./4'),
arity5 = require('./5'),
arityN = require('./N'),
spy = require('sinon').spy(),
expect = require('expect.js');
function createArray(l) {
var arr = [];
for (var i = 0; i < l; i++) {
arr.push(i);
}
return arr;
}
function hasArity(wrapped, fn, l) {
var arr = createArray(l);
expect(wrapped).to.be.a('function');
expect(wrapped.length).to.be.eql(l);
wrapped.call(null, arr);
expect(fn.calledWith.call(fn, arr)).to.be.ok();
}
describe('arity-function', () => {
describe('#arity0', () => {
it('should return a function with length 0', () => {
var spy0 = arity0(spy);
hasArity(spy0, spy, 0);
});
});
describe('#arity1', () => {
it('should return a function with length 1', () => {
var spy1 = arity1(spy);
hasArity(spy1, spy, 1);
});
});
describe('#arity2', () => {
it('should return a function with length 2', () => {
var spy2 = arity2(spy);
hasArity(spy2, spy, 2);
});
});
describe('#arity3', () => {
it('should return a function with length 3', () => {
var spy3 = arity3(spy);
hasArity(spy3, spy, 3);
});
});
describe('#arity4', () => {
it('should return a function with length 4', () => {
var spy4 = arity4(spy);
hasArity(spy4, spy, 4);
});
});
describe('#arity5', () => {
it('should return a function with length 5', () => {
var spy5 = arity5(spy);
hasArity(spy5, spy, 5);
});
});
describe('#arityN', () => {
it('should return a function with length N', () => {
var spy0 = arityN(spy, 0);
hasArity(spy0, spy, 0);
var spy1 = arityN(spy, 1);
hasArity(spy1, spy, 1);
var spy2 = arityN(spy, 2);
hasArity(spy2, spy, 2);
var spy3 = arityN(spy, 3);
hasArity(spy3, spy, 3);
var spy4 = arityN(spy, 4);
hasArity(spy4, spy, 4);
var spy5 = arityN(spy, 5);
hasArity(spy5, spy, 5);
var spyX = arityN(spy, undefined);
hasArity(spyX, spy, 0);
spyX = arityN(spy, null);
hasArity(spyX, spy, 0);
var newFn = arityN(function(a,b,c,d,e,f,g,h,i,j,k){ return true; }, 9);
expect(newFn.length).to.be.eql(11);
expect(newFn()).to.be.eql(true);
});
});
});