Initial commit
[yaffs-website] / node_modules / clone / test.js
1 var clone = require('./');
2
3 function inspect(obj) {
4   seen = [];
5   return JSON.stringify(obj, function (key, val) {
6     if (val != null && typeof val == "object") {
7       if (seen.indexOf(val) >= 0) {
8         return '[cyclic]';
9       }
10
11       seen.push(val);
12     }
13
14     return val;
15   });
16 }
17
18 // Creates a new VM in node, or an iframe in a browser in order to run the
19 // script
20 function apartContext(context, script, callback) {
21   var vm = require('vm');
22
23   if (vm) {
24     var ctx = vm.createContext({ ctx: context });
25     callback(vm.runInContext(script, ctx));
26   } else if (document && document.createElement) {
27     var iframe = document.createElement('iframe');
28     iframe.style.display = 'none';
29     document.body.appendChild(iframe);
30
31     var myCtxId = 'tmpCtx' + Math.random();
32
33     window[myCtxId] = context;
34     iframe.src = 'test-apart-ctx.html?' + myCtxId + '&' + encodeURIComponent(script);
35     iframe.onload = function() {
36       try {
37         callback(iframe.contentWindow.results);
38       } catch (e) {
39         throw e;
40       }
41     };
42   } else {
43     console.log('WARNING: cannot create an apart context.');
44   }
45 }
46
47 exports["clone string"] = function (test) {
48   test.expect(2); // how many tests?
49
50   var a = "foo";
51   test.strictEqual(clone(a), a);
52   a = "";
53   test.strictEqual(clone(a), a);
54
55   test.done();
56 };
57
58 exports["clone number"] = function (test) {
59   test.expect(5); // how many tests?
60
61   var a = 0;
62   test.strictEqual(clone(a), a);
63   a = 1;
64   test.strictEqual(clone(a), a);
65   a = -1000;
66   test.strictEqual(clone(a), a);
67   a = 3.1415927;
68   test.strictEqual(clone(a), a);
69   a = -3.1415927;
70   test.strictEqual(clone(a), a);
71
72   test.done();
73 };
74
75 exports["clone date"] = function (test) {
76   test.expect(3); // how many tests?
77
78   var a = new Date;
79   var c = clone(a);
80   test.ok(!!a.getUTCDate && !!a.toUTCString);
81   test.ok(!!c.getUTCDate && !!c.toUTCString);
82   test.equal(a.getTime(), c.getTime());
83
84   test.done();
85 };
86
87 exports["clone object"] = function (test) {
88   test.expect(1); // how many tests?
89
90   var a = { foo: { bar: "baz" } };
91   var b = clone(a);
92
93   test.deepEqual(b, a);
94
95   test.done();
96 };
97
98 exports["clone array"] = function (test) {
99   test.expect(2); // how many tests?
100
101   var a = [
102     { foo: "bar" },
103     "baz"
104   ];
105   var b = clone(a);
106
107   test.ok(b instanceof Array);
108   test.deepEqual(b, a);
109
110   test.done();
111 };
112
113 exports["clone buffer"] = function (test) {
114   if (typeof Buffer == 'undefined') {
115     return test.done();
116   }
117
118   test.expect(1);
119
120   var a = new Buffer("this is a test buffer");
121   var b = clone(a);
122
123   // no underscore equal since it has no concept of Buffers
124   test.deepEqual(b, a);
125   test.done();
126 };
127
128 exports["clone regexp"] = function (test) {
129   test.expect(5);
130
131   var a = /abc123/gi;
132   var b = clone(a);
133   test.deepEqual(b, a);
134
135   var c = /a/g;
136   test.ok(c.lastIndex === 0);
137
138   c.exec('123a456a');
139   test.ok(c.lastIndex === 4);
140
141   var d = clone(c);
142   test.ok(d.global);
143   test.ok(d.lastIndex === 4);
144
145   test.done();
146 };
147
148 exports["clone object containing array"] = function (test) {
149   test.expect(1); // how many tests?
150
151   var a = {
152     arr1: [ { a: '1234', b: '2345' } ],
153     arr2: [ { c: '345', d: '456' } ]
154   };
155
156   var b = clone(a);
157
158   test.deepEqual(b, a);
159
160   test.done();
161 };
162
163 exports["clone object with circular reference"] = function (test) {
164   test.expect(8); // how many tests?
165
166   var c = [1, "foo", {'hello': 'bar'}, function () {}, false, [2]];
167   var b = [c, 2, 3, 4];
168
169   var a = {'b': b, 'c': c};
170   a.loop = a;
171   a.loop2 = a;
172   c.loop = c;
173   c.aloop = a;
174
175   var aCopy = clone(a);
176   test.ok(a != aCopy);
177   test.ok(a.c != aCopy.c);
178   test.ok(aCopy.c == aCopy.b[0]);
179   test.ok(aCopy.c.loop.loop.aloop == aCopy);
180   test.ok(aCopy.c[0] == a.c[0]);
181
182   test.ok(eq(a, aCopy));
183   aCopy.c[0] = 2;
184   test.ok(!eq(a, aCopy));
185   aCopy.c = "2";
186   test.ok(!eq(a, aCopy));
187
188   function eq(x, y) {
189     return inspect(x) === inspect(y);
190   }
191
192   test.done();
193 };
194
195 exports['clone prototype'] = function (test) {
196   test.expect(3); // how many tests?
197
198   var a = {
199     a: "aaa",
200     x: 123,
201     y: 45.65
202   };
203   var b = clone.clonePrototype(a);
204
205   test.strictEqual(b.a, a.a);
206   test.strictEqual(b.x, a.x);
207   test.strictEqual(b.y, a.y);
208
209   test.done();
210 };
211
212 exports['clone within an apart context'] = function (test) {
213   var results = apartContext({ clone: clone },
214       "results = ctx.clone({ a: [1, 2, 3], d: new Date(), r: /^foo$/ig })",
215       function (results) {
216     test.ok(results.a.constructor.toString() === Array.toString());
217     test.ok(results.d.constructor.toString() === Date.toString());
218     test.ok(results.r.constructor.toString() === RegExp.toString());
219     test.done();
220   });
221 };
222
223 exports['clone object with no constructor'] = function (test) {
224   test.expect(3);
225
226   var n = null;
227
228   var a = { foo: 'bar' };
229   a.__proto__ = n;
230   test.ok(typeof a === 'object');
231   test.ok(typeof a !== null);
232
233   var b = clone(a);
234   test.ok(a.foo, b.foo);
235
236   test.done();
237 };
238
239 exports['clone object with depth argument'] = function (test) {
240   test.expect(6);
241
242   var a = {
243     foo: {
244       bar : {
245         baz : 'qux'
246       }
247     }
248   };
249
250   var b = clone(a, false, 1);
251   test.deepEqual(b, a);
252   test.notEqual(b, a);
253   test.strictEqual(b.foo, a.foo);
254
255   b = clone(a, true, 2);
256   test.deepEqual(b, a);
257   test.notEqual(b.foo, a.foo);
258   test.strictEqual(b.foo.bar, a.foo.bar);
259
260   test.done();
261 };
262
263 exports['maintain prototype chain in clones'] = function (test) {
264   test.expect(1);
265
266   function T() {}
267
268   var a = new T();
269   var b = clone(a);
270   test.strictEqual(Object.getPrototypeOf(a), Object.getPrototypeOf(b));
271
272   test.done();
273 };
274
275 exports['parent prototype is overriden with prototype provided'] = function (test) {
276   test.expect(1);
277
278   function T() {}
279
280   var a = new T();
281   var b = clone(a, true, Infinity, null);
282   test.strictEqual(b.__defineSetter__, undefined);
283
284   test.done();
285 };
286
287 exports['clone object with null children'] = function (test) {
288   test.expect(1);
289   var a = {
290     foo: {
291       bar: null,
292       baz: {
293         qux: false
294       }
295     }
296   };
297
298   var b = clone(a);
299
300   test.deepEqual(b, a);
301   test.done();
302 };
303
304 exports['clone instance with getter'] = function (test) {
305   test.expect(1);
306   function Ctor() {};
307   Object.defineProperty(Ctor.prototype, 'prop', {
308     configurable: true,
309     enumerable: true,
310     get: function() {
311       return 'value';
312     }
313   });
314
315   var a = new Ctor();
316   var b = clone(a);
317
318   test.strictEqual(b.prop, 'value');
319   test.done();
320 };
321
322 exports['get RegExp flags'] = function (test) {
323   test.strictEqual(clone.__getRegExpFlags(/a/),   ''  );
324   test.strictEqual(clone.__getRegExpFlags(/a/i),  'i' );
325   test.strictEqual(clone.__getRegExpFlags(/a/g),  'g' );
326   test.strictEqual(clone.__getRegExpFlags(/a/gi), 'gi');
327   test.strictEqual(clone.__getRegExpFlags(/a/m),  'm' );
328
329   test.done();
330 };
331
332 exports["recognize Array object"] = function (test) {
333   var results = apartContext(null, "results = [1, 2, 3]", function(alien) {
334     var local = [4, 5, 6];
335     test.ok(clone.__isArray(alien)); // recognize in other context.
336     test.ok(clone.__isArray(local)); // recognize in local context.
337     test.ok(!clone.__isDate(alien));
338     test.ok(!clone.__isDate(local));
339     test.ok(!clone.__isRegExp(alien));
340     test.ok(!clone.__isRegExp(local));
341     test.done();
342   });
343 };
344
345 exports["recognize Date object"] = function (test) {
346   var results = apartContext(null, "results = new Date()", function(alien) {
347     var local = new Date();
348
349     test.ok(clone.__isDate(alien)); // recognize in other context.
350     test.ok(clone.__isDate(local)); // recognize in local context.
351     test.ok(!clone.__isArray(alien));
352     test.ok(!clone.__isArray(local));
353     test.ok(!clone.__isRegExp(alien));
354     test.ok(!clone.__isRegExp(local));
355
356     test.done();
357   });
358 };
359
360 exports["recognize RegExp object"] = function (test) {
361   var results = apartContext(null, "results = /foo/", function(alien) {
362     var local = /bar/;
363
364     test.ok(clone.__isRegExp(alien)); // recognize in other context.
365     test.ok(clone.__isRegExp(local)); // recognize in local context.
366     test.ok(!clone.__isArray(alien));
367     test.ok(!clone.__isArray(local));
368     test.ok(!clone.__isDate(alien));
369     test.ok(!clone.__isDate(local));
370     test.done();
371   });
372 };