Tizen 2.1 base
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / connect / node_modules / formidable / node-gently / test / simple / test-gently.js
1 require('../common');
2 var Gently = require('gently')
3   , gently;
4
5 function test(test) {
6   process.removeAllListeners('exit');
7   gently = new Gently();
8   test();
9 }
10
11 test(function constructor() {
12   assert.deepEqual(gently.expectations, []);
13   assert.deepEqual(gently.hijacked, {});
14   assert.equal(gently.constructor.name, 'Gently');
15 });
16
17 test(function expectBadArgs() {
18   var BAD_ARG = 'oh no';
19   try {
20     gently.expect(BAD_ARG);
21     assert.ok(false, 'throw needs to happen');
22   } catch (e) {
23     assert.equal(e.message, 'Bad 1st argument for gently.expect(), object, function, or number expected, got: '+(typeof BAD_ARG));
24   }
25 });
26
27 test(function expectObjMethod() {
28   var OBJ = {}, NAME = 'foobar';
29   OBJ.foo = function(x) {
30     return x;
31   };
32
33   gently._name = function() {
34     return NAME;
35   };
36
37   var original = OBJ.foo
38     , stubFn = function() {};
39
40   (function testAddOne() {
41     assert.strictEqual(gently.expect(OBJ, 'foo', stubFn), original);
42
43     assert.equal(gently.expectations.length, 1);
44     var expectation = gently.expectations[0];
45     assert.strictEqual(expectation.obj, OBJ);
46     assert.strictEqual(expectation.method, 'foo');
47     assert.strictEqual(expectation.stubFn, stubFn);
48     assert.strictEqual(expectation.name, NAME);
49     assert.strictEqual(OBJ.foo._original, original);
50   })();
51
52   (function testAddTwo() {
53     gently.expect(OBJ, 'foo', 2, stubFn);
54     assert.equal(gently.expectations.length, 2);
55     assert.strictEqual(OBJ.foo._original, original);
56   })();
57
58   (function testAddOneWithoutMock() {
59     gently.expect(OBJ, 'foo');
60     assert.equal(gently.expectations.length, 3);
61   })();
62
63   var stubFnCalled = 0, SELF = {};
64   gently._stubFn = function(self, obj, method, name, args) {
65     stubFnCalled++;
66     assert.strictEqual(self, SELF);
67     assert.strictEqual(obj, OBJ);
68     assert.strictEqual(method, 'foo');
69     assert.strictEqual(name, NAME);
70     assert.deepEqual(args, [1, 2]);
71     return 23;
72   };
73   assert.equal(OBJ.foo.apply(SELF, [1, 2]), 23);
74   assert.equal(stubFnCalled, 1);
75 });
76
77 test(function expectClosure() {
78   var NAME = 'MY CLOSURE';
79   function closureFn() {};
80
81   gently._name = function() {
82     return NAME;
83   };
84
85   var fn = gently.expect(closureFn);
86   assert.equal(gently.expectations.length, 1);
87   var expectation = gently.expectations[0];
88   assert.strictEqual(expectation.obj, null);
89   assert.strictEqual(expectation.method, null);
90   assert.strictEqual(expectation.stubFn, closureFn);
91   assert.strictEqual(expectation.name, NAME);
92
93   var stubFnCalled = 0, SELF = {};
94   gently._stubFn = function(self, obj, method, name, args) {
95     stubFnCalled++;
96     assert.strictEqual(self, SELF);
97     assert.strictEqual(obj, null);
98     assert.strictEqual(method, null);
99     assert.strictEqual(name, NAME);
100     assert.deepEqual(args, [1, 2]);
101     return 23;
102   };
103   assert.equal(fn.apply(SELF, [1, 2]), 23);
104   assert.equal(stubFnCalled, 1);
105 });
106
107 test(function expectClosureCount() {
108   var stubFnCalled = 0;
109   function closureFn() {stubFnCalled++};
110
111   var fn = gently.expect(2, closureFn);
112   assert.equal(gently.expectations.length, 1);
113   fn();
114   assert.equal(gently.expectations.length, 1);
115   fn();
116   assert.equal(stubFnCalled, 2);
117 });
118
119 test(function restore() {
120   var OBJ = {}, NAME = '[my object].myFn()';
121   OBJ.foo = function(x) {
122     return x;
123   };
124
125   gently._name = function() {
126     return NAME;
127   };
128
129   var original = OBJ.foo;
130   gently.expect(OBJ, 'foo');
131   gently.restore(OBJ, 'foo');
132   assert.strictEqual(OBJ.foo, original);
133
134   (function testError() {
135     try {
136       gently.restore(OBJ, 'foo');
137       assert.ok(false, 'throw needs to happen');
138     } catch (e) {
139       assert.equal(e.message, NAME+' is not gently stubbed');
140     }
141   })();
142 });
143
144 test(function _stubFn() {
145   var OBJ1 = {toString: function() {return '[OBJ 1]'}}
146     , OBJ2 = {toString: function() {return '[OBJ 2]'}, foo: function () {return 'bar';}}
147     , SELF = {};
148
149   gently.expect(OBJ1, 'foo', function(x) {
150     assert.strictEqual(this, SELF);
151     return x * 2;
152   });
153
154   assert.equal(gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]), 10);
155
156   (function testAutorestore() {
157     assert.equal(OBJ2.foo(), 'bar');
158
159     gently.expect(OBJ2, 'foo', function() {
160       return 'stubbed foo';
161     });
162
163     gently.expect(OBJ2, 'foo', function() {
164       return "didn't restore yet";
165     });
166
167     assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), 'stubbed foo');
168     assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), "didn't restore yet");
169     assert.equal(OBJ2.foo(), 'bar');
170     assert.deepEqual(gently.expectations, []);
171   })();
172
173   (function testNoMoreCallExpected() {
174     try {
175       gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
176       assert.ok(false, 'throw needs to happen');
177     } catch (e) {
178       assert.equal(e.message, 'Unexpected call to dummy_name, no call was expected');
179     }
180   })();
181
182   (function testDifferentCallExpected() {
183     gently.expect(OBJ2, 'bar');
184     try {
185       gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
186       assert.ok(false, 'throw needs to happen');
187     } catch (e) {
188       assert.equal(e.message, 'Unexpected call to dummy_name, expected call to '+gently._name(OBJ2, 'bar'));
189     }
190
191     assert.equal(gently.expectations.length, 1);
192   })();
193
194   (function testNoMockCallback() {
195     OBJ2.bar();
196     assert.equal(gently.expectations.length, 0);
197   })();
198 });
199
200 test(function stub() {
201   var LOCATION = './my_class';
202
203   (function testRegular() {
204     var Stub = gently.stub(LOCATION);
205     assert.ok(Stub instanceof Function);
206     assert.strictEqual(gently.hijacked[LOCATION], Stub);
207     assert.ok(Stub['new'] instanceof Function);
208     assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
209
210     (function testConstructor() {
211       var newCalled = 0
212         , STUB
213         , ARGS = ['foo', 'bar'];
214
215       Stub['new'] = function(a, b) {
216         assert.equal(a, ARGS[0]);
217         assert.equal(b, ARGS[1]);
218         newCalled++;
219         STUB = this;
220       };
221
222       var stub = new Stub(ARGS[0], ARGS[1]);
223       assert.strictEqual(stub, STUB);
224       assert.equal(newCalled, 1);
225       assert.equal(stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
226     })();
227
228     (function testUseReturnValueAsInstance() {
229       var R = {};
230
231       Stub['new'] = function() {
232         return R;
233       };
234
235       var stub = new Stub();
236       assert.strictEqual(stub, R);
237
238     })();
239   })();
240
241   var EXPORTS_NAME = 'MyClass';
242   test(function testExportsName() {
243     var Stub = gently.stub(LOCATION, EXPORTS_NAME);
244     assert.strictEqual(gently.hijacked[LOCATION][EXPORTS_NAME], Stub);
245     assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
246
247     (function testConstructor() {
248       var stub = new Stub();
249       assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
250     })();
251   });
252 });
253
254 test(function hijack() {
255   var LOCATION = './foo'
256     , REQUIRE_CALLS = 0
257     , EXPORTS = {}
258     , REQUIRE = function() {
259         REQUIRE_CALLS++;
260         return EXPORTS;
261       };
262
263   var hijackedRequire = gently.hijack(REQUIRE);
264   hijackedRequire(LOCATION);
265   assert.strictEqual(gently.hijacked[LOCATION], EXPORTS);
266
267   assert.equal(REQUIRE_CALLS, 1);
268
269   // make sure we are caching the hijacked module
270   hijackedRequire(LOCATION);
271   assert.equal(REQUIRE_CALLS, 1);
272 });
273
274 test(function verify() {
275   var OBJ = {toString: function() {return '[OBJ]'}};
276   gently.verify();
277
278   gently.expect(OBJ, 'foo');
279   try {
280     gently.verify();
281     assert.ok(false, 'throw needs to happen');
282   } catch (e) {
283     assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen');
284   }
285
286   try {
287     gently.verify('foo');
288     assert.ok(false, 'throw needs to happen');
289   } catch (e) {
290     assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen (foo)');
291   }
292 });
293
294 test(function processExit() {
295   var verifyCalled = 0;
296   gently.verify = function(msg) {
297     verifyCalled++;
298     assert.equal(msg, 'process exit');
299   };
300
301   process.emit('exit');
302   assert.equal(verifyCalled, 1);
303 });
304
305 test(function _name() {
306   (function testNamedClass() {
307     function Foo() {};
308     var foo = new Foo();
309     assert.equal(gently._name(foo, 'bar'), '[Foo].bar()');
310   })();
311
312   (function testToStringPreference() {
313     function Foo() {};
314     Foo.prototype.toString = function() {
315       return '[Superman 123]';
316     };
317     var foo = new Foo();
318     assert.equal(gently._name(foo, 'bar'), '[Superman 123].bar()');
319   })();
320
321   (function testUnamedClass() {
322     var Foo = function() {};
323     var foo = new Foo();
324     assert.equal(gently._name(foo, 'bar'), foo.toString()+'.bar()');
325   })();
326
327   (function testNamedClosure() {
328     function myClosure() {};
329     assert.equal(gently._name(null, null, myClosure), myClosure.name+'()');
330   })();
331
332   (function testUnamedClosure() {
333     var myClosure = function() {2+2 == 5};
334     assert.equal(gently._name(null, null, myClosure), '>> '+myClosure.toString()+' <<');
335   })();
336 });
337
338 test(function verifyExpectNone() {
339   var OBJ = {toString: function() {return '[OBJ]'}};
340   gently.verify();
341
342   gently.expect(OBJ, 'foo', 0);
343   try {
344     gently.verify();
345   } catch (e) {
346     assert.fail('Exception should not have been thrown');
347   }
348 });