-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
161 lines (141 loc) · 3.74 KB
/
test.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
'use strict';
const pitesti = require('pitesti');
const assert = require('assert');
const pifall = require('./index');
const obj = {
x: 1,
y: 2,
foo: cb => setImmediate(() => { cb(null, 'hello'); }),
get bar () {
return cb => setImmediate(() => { cb(null, 'barbar'); });
},
get baz () {
return 5;
},
set bop (x) { // eslint-disable-line accessor-pairs
// When iterating over this, we should get nothing.
},
[Symbol('bonk')] () {
// noop
}
};
const objWithProto = {
__proto__: {
foo: cb => setImmediate(() => { cb(null, 'hello'); })
}
};
const returnedObj = pifall(obj);
pifall(objWithProto, { proto: true });
const test = pitesti();
test`returns the input object`(() => {
assert.strictEqual(returnedObj, obj);
});
test`non-funcs aren't processed`(() => {
assert(!('xAsync' in obj));
assert(!('yAsync' in obj));
});
test`normal func`(() => obj.fooAsync().then(result => {
assert.strictEqual(result, 'hello');
}));
test`setter`(() => {
assert(!Reflect.getOwnPropertyDescriptor(obj, 'bopAsync'));
});
test`getter func`(() => obj.barAsync().then(result => {
assert.strictEqual(result, 'barbar');
}));
test`non-func getter`(() => {
assert.throws(() => {
obj.bazAsync();
}, /^TypeError: called Async suffix on non-function getter$/);
});
test`object has correct number of properties`(() => {
// also tests that symbol was ignored
assert.strictEqual(Reflect.ownKeys(obj).length, 10);
});
test`prototype is promisified`(() => objWithProto.fooAsync().then(result => {
assert.strictEqual(result, 'hello');
}));
test`non-obj/func fails to be promisified`(() => {
[
null,
5,
true,
'hello',
Symbol('bonk')
].forEach(obj => {
assert.throws(() => pifall(obj),
/^TypeError: Cannot pifall non-object$/);
});
});
Reflect.ownKeys(global).filter(k =>
typeof k === 'string' &&
k !== 'Proxy' && // ugh special case
k !== 'GLOBAL' && // deprecation
k !== 'root' && // deprecation
k.toUpperCase()[0] === k[0] &&
typeof global[k] === 'function'
).forEach(k => {
test`don't promisify built-in ${k}.prototype`(() => {
assert.throws(() => pifall(global[k].prototype),
/^TypeError: Cannot pifall built-in prototype$/);
});
});
test`class is promisified`(() => {
class Foo {
bar (cb) {
setImmediate(() => { cb(null, 'barbar'); });
}
}
class Foo2 {
bar (cb) {
setImmediate(() => { cb(null, 'barbar'); });
}
}
const obj = { Foo };
pifall(obj);
assert.strictEqual(typeof obj.FooAsync, 'function');
assert.notStrictEqual(typeof obj.Foo.prototype.barAsync, 'function');
const anotherObj = { Foo: Foo2 };
pifall(anotherObj, { classes: true });
assert.notStrictEqual(typeof anotherObj.FooAsync, 'function');
assert.strictEqual(typeof anotherObj.Foo.prototype.barAsync, 'function');
const f = new anotherObj.Foo();
return f.barAsync().then(result => {
assert.strictEqual(result, 'barbar');
});
});
test`different suffix`(() => {
const aObj = {
foo: cb => setImmediate(() => { cb(null, 'hello'); })
};
pifall(aObj, { suffix: 'Asink' });
return aObj.fooAsink().then(result => {
assert.strictEqual(result, 'hello');
});
});
test`custom promisifier`(() => {
let called = false;
const promisifier = (func) => {
return (...args) => {
called = true;
return new Promise((resolve, reject) => {
func(...args, (err, out) => {
if (err) {
reject(err);
} else {
resolve(out);
}
});
});
};
};
const aObj = {
foo: cb => setImmediate(() => { cb(null, 'hello'); })
};
pifall(aObj, { promisifier });
return aObj.fooAsync().then(result => {
assert.strictEqual(result, 'hello');
assert.strictEqual(called, true);
});
});
test();