Initial commit
[yaffs-website] / node_modules / gulp / node_modules / clone / test.js
1 if(module.parent === null) {
2   console.log('Run this test file with nodeunit:');
3   console.log('$ nodeunit test.js');
4 }
5
6
7 var clone = require('./');
8 var util = require('util');
9 var _ = require('underscore');
10
11
12
13 exports["clone string"] = function(test) {
14   test.expect(2); // how many tests?
15
16   var a = "foo";
17   test.strictEqual(clone(a), a);
18   a = "";
19   test.strictEqual(clone(a), a);
20
21   test.done();
22 };
23
24
25
26 exports["clone number"] = function(test) {
27   test.expect(5); // how many tests?
28
29   var a = 0;
30   test.strictEqual(clone(a), a);
31   a = 1;
32   test.strictEqual(clone(a), a);
33   a = -1000;
34   test.strictEqual(clone(a), a);
35   a = 3.1415927;
36   test.strictEqual(clone(a), a);
37   a = -3.1415927;
38   test.strictEqual(clone(a), a);
39
40   test.done();
41 };
42
43
44
45 exports["clone date"] = function(test) {
46   test.expect(3); // how many tests?
47
48   var a = new Date;
49   var c = clone(a);
50   test.ok(a instanceof Date);
51   test.ok(c instanceof Date);
52   test.equal(c.getTime(), a.getTime());
53
54   test.done();
55 };
56
57
58
59 exports["clone object"] = function(test) {
60   test.expect(2); // how many tests?
61
62   var a = { foo: { bar: "baz" } };
63   var b = clone(a);
64
65   test.ok(_(a).isEqual(b), "underscore equal");
66   test.deepEqual(b, a);
67
68   test.done();
69 };
70
71
72
73 exports["clone array"] = function(test) {
74   test.expect(2); // how many tests?
75
76   var a = [
77     { foo: "bar" },
78     "baz"
79   ];
80   var b = clone(a);
81
82   test.ok(_(a).isEqual(b), "underscore equal");
83   test.deepEqual(b, a);
84
85   test.done();
86 };
87
88 exports["clone buffer"] = function(test) {
89   test.expect(1);
90
91   var a = new Buffer("this is a test buffer");
92   var b = clone(a);
93
94   // no underscore equal since it has no concept of Buffers
95   test.deepEqual(b, a);
96   test.done();
97 };
98
99
100
101 exports["clone regexp"] = function(test) {
102   test.expect(5);
103
104   var a = /abc123/gi;
105   var b = clone(a);
106
107   test.deepEqual(b, a);
108
109   var c = /a/g;
110   test.ok(c.lastIndex === 0);
111
112   c.exec('123a456a');
113   test.ok(c.lastIndex === 4);
114
115   var d = clone(c);
116   test.ok(d.global);
117   test.ok(d.lastIndex === 4);
118
119   test.done();
120 };
121
122
123 exports["clone object containing array"] = function(test) {
124   test.expect(2); // how many tests?
125
126   var a = {
127     arr1: [ { a: '1234', b: '2345' } ],
128     arr2: [ { c: '345', d: '456' } ]
129   };
130   var b = clone(a);
131
132   test.ok(_(a).isEqual(b), "underscore equal");
133   test.deepEqual(b, a);
134
135   test.done();
136 };
137
138
139
140 exports["clone object with circular reference"] = function(test) {
141   test.expect(8); // how many tests?
142
143   var _ = test.ok;
144   var c = [1, "foo", {'hello': 'bar'}, function() {}, false, [2]];
145   var b = [c, 2, 3, 4];
146   var a = {'b': b, 'c': c};
147   a.loop = a;
148   a.loop2 = a;
149   c.loop = c;
150   c.aloop = a;
151   var aCopy = clone(a);
152   _(a != aCopy);
153   _(a.c != aCopy.c);
154   _(aCopy.c == aCopy.b[0]);
155   _(aCopy.c.loop.loop.aloop == aCopy);
156   _(aCopy.c[0] == a.c[0]);
157
158   //console.log(util.inspect(aCopy, true, null) );
159   //console.log("------------------------------------------------------------");
160   //console.log(util.inspect(a, true, null) );
161   _(eq(a, aCopy));
162   aCopy.c[0] = 2;
163   _(!eq(a, aCopy));
164   aCopy.c = "2";
165   _(!eq(a, aCopy));
166   //console.log("------------------------------------------------------------");
167   //console.log(util.inspect(aCopy, true, null) );
168
169   function eq(x, y) {
170     return util.inspect(x, true, null) === util.inspect(y, true, null);
171   }
172
173   test.done();
174 };
175
176
177
178 exports['clonePrototype'] = function(test) {
179   test.expect(3); // how many tests?
180
181   var a = {
182     a: "aaa",
183     x: 123,
184     y: 45.65
185   };
186   var b = clone.clonePrototype(a);
187
188   test.strictEqual(b.a, a.a);
189   test.strictEqual(b.x, a.x);
190   test.strictEqual(b.y, a.y);
191
192   test.done();
193 }
194
195 exports['cloneWithinNewVMContext'] = function(test) {
196   test.expect(3);
197   var vm = require('vm');
198   var ctx = vm.createContext({ clone: clone });
199   var script = "clone( {array: [1, 2, 3], date: new Date(), regex: /^foo$/ig} );";
200   var results = vm.runInContext(script, ctx);
201   test.ok(results.array instanceof Array);
202   test.ok(results.date instanceof Date);
203   test.ok(results.regex instanceof RegExp);
204   test.done();
205 }
206
207 exports['cloneObjectWithNoConstructor'] = function(test) {
208   test.expect(3);
209   var n = null;
210   var a = { foo: 'bar' };
211   a.__proto__ = n;
212   test.ok(typeof a === 'object');
213   test.ok(typeof a !== null);
214   var b = clone(a);
215   test.ok(a.foo, b.foo);
216   test.done();
217 }
218
219 exports['clone object with depth argument'] = function (test) {
220   test.expect(6);
221   var a = {
222     foo: {
223       bar : {
224         baz : 'qux'
225       }
226     }
227   };
228   var b = clone(a, false, 1);
229   test.deepEqual(b, a);
230   test.notEqual(b, a);
231   test.strictEqual(b.foo, a.foo);
232
233   b = clone(a, true, 2);
234   test.deepEqual(b, a);
235   test.notEqual(b.foo, a.foo);
236   test.strictEqual(b.foo.bar, a.foo.bar);
237   test.done();
238 }
239
240 exports['maintain prototype chain in clones'] = function (test) {
241   test.expect(1);
242   function Constructor() {}
243   var a = new Constructor();
244   var b = clone(a);
245   test.strictEqual(Object.getPrototypeOf(a), Object.getPrototypeOf(b));
246   test.done();
247 }
248
249 exports['parent prototype is overriden with prototype provided'] = function (test) {
250   test.expect(1);
251   function Constructor() {}
252   var a = new Constructor();
253   var b = clone(a, true, Infinity, null);
254   test.strictEqual(b.__defineSetter__, undefined);
255   test.done();
256 }
257
258 exports['clone object with null children'] = function(test) {
259   test.expect(1);
260   var a = {
261     foo: {
262       bar: null,
263       baz: {
264         qux: false
265       }
266     }
267   };
268   var b = clone(a);
269   test.deepEqual(b, a);
270   test.done();
271 }
272
273 exports['clone instance with getter'] = function(test) {
274   test.expect(1);
275   function Ctor() {};
276   Object.defineProperty(Ctor.prototype, 'prop', {
277     configurable: true,
278     enumerable: true,
279     get: function() {
280       return 'value';
281     }
282   });
283
284   var a = new Ctor();
285   var b = clone(a);
286
287   test.strictEqual(b.prop, 'value');
288   test.done();
289 };