-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathcallbackify.js
191 lines (170 loc) · 5.14 KB
/
callbackify.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
'use strict';
var test = require('tape');
var callbackify = require('../../').callbackify;
var isFunctionLengthConfigurable = require('../../support/isFunctionLengthConfigurable');
if (typeof Promise === 'undefined') {
console.log('no global Promise found, skipping callbackify tests');
return;
}
function after (n, cb) {
var i = 0;
return function () {
if (++i === n) cb();
}
}
var values = [
'hello world',
null,
undefined,
false,
0,
{},
{ key: 'value' },
function ok() {},
['array', 'with', 4, 'values'],
new Error('boo')
];
if (typeof Symbol !== 'undefined') {
values.push(Symbol('I am a symbol'));
}
test('util.callbackify resolution value is passed as second argument to callback', function (t) {
var end = after(values.length * 2, t.end);
// Test that the resolution value is passed as second argument to callback
values.forEach(function(value) {
// Test Promise factory
function promiseFn() {
return Promise.resolve(value);
}
var cbPromiseFn = callbackify(promiseFn);
cbPromiseFn(function(err, ret) {
t.ifError(err);
t.strictEqual(ret, value, 'cb ' + typeof value);
end();
});
// Test Thenable
function thenableFn() {
return {
then: function(onRes, onRej) {
onRes(value);
}
};
}
var cbThenableFn = callbackify(thenableFn);
cbThenableFn(function(err, ret) {
t.ifError(err);
t.strictEqual(ret, value, 'thenable ' + typeof value);
end();
});
});
});
test('util.callbackify rejection reason is passed as first argument to callback', function (t) {
var end = after(values.length * 2, t.end);
// Test that rejection reason is passed as first argument to callback
values.forEach(function(value) {
// test a Promise factory
function promiseFn() {
return Promise.reject(value);
}
var cbPromiseFn = callbackify(promiseFn);
cbPromiseFn(function(err, ret) {
t.strictEqual(ret, undefined, 'cb ' + typeof value);
if (err instanceof Error) {
if ('reason' in err) {
t.ok(!value);
t.strictEqual(err.message, 'Promise was rejected with a falsy value');
t.strictEqual(err.reason, value);
} else {
t.strictEqual(String(value).slice(-err.message.length), err.message);
}
} else {
t.strictEqual(err, value);
}
end();
});
// Test Thenable
function thenableFn() {
return {
then: function (onRes, onRej) {
onRej(value);
}
};
}
var cbThenableFn = callbackify(thenableFn);
cbThenableFn(function(err, ret) {
t.strictEqual(ret, undefined, 'thenable ' + typeof value);
if (err instanceof Error) {
if ('reason' in err) {
t.ok(!value);
t.strictEqual(err.message, 'Promise was rejected with a falsy value');
t.strictEqual(err.reason, value);
} else {
t.strictEqual(String(value).slice(-err.message.length), err.message);
}
} else {
t.strictEqual(err, value);
}
end();
});
});
});
test('util.callbackify arguments passed to callbackified function are passed to original', function (t) {
var end = after(values.length, t.end);
// Test that arguments passed to callbackified function are passed to original
values.forEach(function(value) {
function promiseFn(arg) {
t.strictEqual(arg, value);
return Promise.resolve(arg);
}
var cbPromiseFn = callbackify(promiseFn);
cbPromiseFn(value, function(err, ret) {
t.ifError(err);
t.strictEqual(ret, value);
end();
});
});
});
test('util.callbackify `this` binding is the same for callbackified and original', function (t) {
var end = after(values.length, t.end);
// Test that `this` binding is the same for callbackified and original
values.forEach(function(value) {
var iAmThis = {
fn: function(arg) {
t.strictEqual(this, iAmThis);
return Promise.resolve(arg);
},
};
iAmThis.cbFn = callbackify(iAmThis.fn);
iAmThis.cbFn(value, function(err, ret) {
t.ifError(err);
t.strictEqual(ret, value);
t.strictEqual(this, iAmThis);
end();
});
});
});
test('util.callbackify non-function inputs throw', function (t) {
// Verify that non-function inputs throw.
['foo', null, undefined, false, 0, {}, typeof Symbol !== 'undefined' ? Symbol() : undefined, []].forEach(function(value) {
t.throws(
function() { callbackify(value); },
'The "original" argument must be of type Function'
);
});
t.end();
});
test('util.callbackify resulting function should have one more argument', function (t) {
if (!isFunctionLengthConfigurable()) {
console.log("skipping this test as function.length is not configurable in the current javascript engine");
return;
}
// Test that resulting function should have one more argument
[
function() { },
function(a) { },
function(a, b) { }
].forEach(function (fct) {
var callbackified = callbackify(fct);
t.strictEqual(callbackified.length, fct.length + 1, "callbackified function should have one more argument");
});
t.end();
});