test-gently.js
8.93 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
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
require('../common');
var Gently = require('gently')
, gently;
function test(test) {
process.removeAllListeners('exit');
gently = new Gently();
test();
}
test(function constructor() {
assert.deepEqual(gently.expectations, []);
assert.deepEqual(gently.hijacked, {});
assert.equal(gently.constructor.name, 'Gently');
});
test(function expectBadArgs() {
var BAD_ARG = 'oh no';
try {
gently.expect(BAD_ARG);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Bad 1st argument for gently.expect(), object, function, or number expected, got: '+(typeof BAD_ARG));
}
});
test(function expectObjMethod() {
var OBJ = {}, NAME = 'foobar';
OBJ.foo = function(x) {
return x;
};
gently._name = function() {
return NAME;
};
var original = OBJ.foo
, stubFn = function() {};
(function testAddOne() {
assert.strictEqual(gently.expect(OBJ, 'foo', stubFn), original);
assert.equal(gently.expectations.length, 1);
var expectation = gently.expectations[0];
assert.strictEqual(expectation.obj, OBJ);
assert.strictEqual(expectation.method, 'foo');
assert.strictEqual(expectation.stubFn, stubFn);
assert.strictEqual(expectation.name, NAME);
assert.strictEqual(OBJ.foo._original, original);
})();
(function testAddTwo() {
gently.expect(OBJ, 'foo', 2, stubFn);
assert.equal(gently.expectations.length, 2);
assert.strictEqual(OBJ.foo._original, original);
})();
(function testAddOneWithoutMock() {
gently.expect(OBJ, 'foo');
assert.equal(gently.expectations.length, 3);
})();
var stubFnCalled = 0, SELF = {};
gently._stubFn = function(self, obj, method, name, args) {
stubFnCalled++;
assert.strictEqual(self, SELF);
assert.strictEqual(obj, OBJ);
assert.strictEqual(method, 'foo');
assert.strictEqual(name, NAME);
assert.deepEqual(args, [1, 2]);
return 23;
};
assert.equal(OBJ.foo.apply(SELF, [1, 2]), 23);
assert.equal(stubFnCalled, 1);
});
test(function expectClosure() {
var NAME = 'MY CLOSURE';
function closureFn() {};
gently._name = function() {
return NAME;
};
var fn = gently.expect(closureFn);
assert.equal(gently.expectations.length, 1);
var expectation = gently.expectations[0];
assert.strictEqual(expectation.obj, null);
assert.strictEqual(expectation.method, null);
assert.strictEqual(expectation.stubFn, closureFn);
assert.strictEqual(expectation.name, NAME);
var stubFnCalled = 0, SELF = {};
gently._stubFn = function(self, obj, method, name, args) {
stubFnCalled++;
assert.strictEqual(self, SELF);
assert.strictEqual(obj, null);
assert.strictEqual(method, null);
assert.strictEqual(name, NAME);
assert.deepEqual(args, [1, 2]);
return 23;
};
assert.equal(fn.apply(SELF, [1, 2]), 23);
assert.equal(stubFnCalled, 1);
});
test(function expectClosureCount() {
var stubFnCalled = 0;
function closureFn() {stubFnCalled++};
var fn = gently.expect(2, closureFn);
assert.equal(gently.expectations.length, 1);
fn();
assert.equal(gently.expectations.length, 1);
fn();
assert.equal(stubFnCalled, 2);
});
test(function restore() {
var OBJ = {}, NAME = '[my object].myFn()';
OBJ.foo = function(x) {
return x;
};
gently._name = function() {
return NAME;
};
var original = OBJ.foo;
gently.expect(OBJ, 'foo');
gently.restore(OBJ, 'foo');
assert.strictEqual(OBJ.foo, original);
(function testError() {
try {
gently.restore(OBJ, 'foo');
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, NAME+' is not gently stubbed');
}
})();
});
test(function _stubFn() {
var OBJ1 = {toString: function() {return '[OBJ 1]'}}
, OBJ2 = {toString: function() {return '[OBJ 2]'}, foo: function () {return 'bar';}}
, SELF = {};
gently.expect(OBJ1, 'foo', function(x) {
assert.strictEqual(this, SELF);
return x * 2;
});
assert.equal(gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]), 10);
(function testAutorestore() {
assert.equal(OBJ2.foo(), 'bar');
gently.expect(OBJ2, 'foo', function() {
return 'stubbed foo';
});
gently.expect(OBJ2, 'foo', function() {
return "didn't restore yet";
});
assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), 'stubbed foo');
assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), "didn't restore yet");
assert.equal(OBJ2.foo(), 'bar');
assert.deepEqual(gently.expectations, []);
})();
(function testNoMoreCallExpected() {
try {
gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Unexpected call to dummy_name, no call was expected');
}
})();
(function testDifferentCallExpected() {
gently.expect(OBJ2, 'bar');
try {
gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Unexpected call to dummy_name, expected call to '+gently._name(OBJ2, 'bar'));
}
assert.equal(gently.expectations.length, 1);
})();
(function testNoMockCallback() {
OBJ2.bar();
assert.equal(gently.expectations.length, 0);
})();
});
test(function stub() {
var LOCATION = './my_class';
(function testRegular() {
var Stub = gently.stub(LOCATION);
assert.ok(Stub instanceof Function);
assert.strictEqual(gently.hijacked[LOCATION], Stub);
assert.ok(Stub['new'] instanceof Function);
assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
(function testConstructor() {
var newCalled = 0
, STUB
, ARGS = ['foo', 'bar'];
Stub['new'] = function(a, b) {
assert.equal(a, ARGS[0]);
assert.equal(b, ARGS[1]);
newCalled++;
STUB = this;
};
var stub = new Stub(ARGS[0], ARGS[1]);
assert.strictEqual(stub, STUB);
assert.equal(newCalled, 1);
assert.equal(stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
})();
(function testUseReturnValueAsInstance() {
var R = {};
Stub['new'] = function() {
return R;
};
var stub = new Stub();
assert.strictEqual(stub, R);
})();
})();
var EXPORTS_NAME = 'MyClass';
test(function testExportsName() {
var Stub = gently.stub(LOCATION, EXPORTS_NAME);
assert.strictEqual(gently.hijacked[LOCATION][EXPORTS_NAME], Stub);
assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
(function testConstructor() {
var stub = new Stub();
assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
})();
});
});
test(function hijack() {
var LOCATION = './foo'
, REQUIRE_CALLS = 0
, EXPORTS = {}
, REQUIRE = function() {
REQUIRE_CALLS++;
return EXPORTS;
};
var hijackedRequire = gently.hijack(REQUIRE);
hijackedRequire(LOCATION);
assert.strictEqual(gently.hijacked[LOCATION], EXPORTS);
assert.equal(REQUIRE_CALLS, 1);
// make sure we are caching the hijacked module
hijackedRequire(LOCATION);
assert.equal(REQUIRE_CALLS, 1);
});
test(function verify() {
var OBJ = {toString: function() {return '[OBJ]'}};
gently.verify();
gently.expect(OBJ, 'foo');
try {
gently.verify();
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen');
}
try {
gently.verify('foo');
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen (foo)');
}
});
test(function processExit() {
var verifyCalled = 0;
gently.verify = function(msg) {
verifyCalled++;
assert.equal(msg, 'process exit');
};
process.emit('exit');
assert.equal(verifyCalled, 1);
});
test(function _name() {
(function testNamedClass() {
function Foo() {};
var foo = new Foo();
assert.equal(gently._name(foo, 'bar'), '[Foo].bar()');
})();
(function testToStringPreference() {
function Foo() {};
Foo.prototype.toString = function() {
return '[Superman 123]';
};
var foo = new Foo();
assert.equal(gently._name(foo, 'bar'), '[Superman 123].bar()');
})();
(function testUnamedClass() {
var Foo = function() {};
var foo = new Foo();
assert.equal(gently._name(foo, 'bar'), foo.toString()+'.bar()');
})();
(function testNamedClosure() {
function myClosure() {};
assert.equal(gently._name(null, null, myClosure), myClosure.name+'()');
})();
(function testUnamedClosure() {
var myClosure = function() {2+2 == 5};
assert.equal(gently._name(null, null, myClosure), '>> '+myClosure.toString()+' <<');
})();
});
test(function verifyExpectNone() {
var OBJ = {toString: function() {return '[OBJ]'}};
gently.verify();
gently.expect(OBJ, 'foo', 0);
try {
gently.verify();
} catch (e) {
assert.fail('Exception should not have been thrown');
}
});