1 if(module.parent === null) {
2 console.log('Run this test file with nodeunit:');
3 console.log('$ nodeunit test.js');
7 var clone = require('./');
8 var util = require('util');
9 var _ = require('underscore');
13 exports["clone string"] = function(test) {
14 test.expect(2); // how many tests?
17 test.strictEqual(clone(a), a);
19 test.strictEqual(clone(a), a);
26 exports["clone number"] = function(test) {
27 test.expect(5); // how many tests?
30 test.strictEqual(clone(a), a);
32 test.strictEqual(clone(a), a);
34 test.strictEqual(clone(a), a);
36 test.strictEqual(clone(a), a);
38 test.strictEqual(clone(a), a);
45 exports["clone date"] = function(test) {
46 test.expect(3); // how many tests?
50 test.ok(a instanceof Date);
51 test.ok(c instanceof Date);
52 test.equal(c.getTime(), a.getTime());
59 exports["clone object"] = function(test) {
60 test.expect(2); // how many tests?
62 var a = { foo: { bar: "baz" } };
65 test.ok(_(a).isEqual(b), "underscore equal");
73 exports["clone array"] = function(test) {
74 test.expect(2); // how many tests?
82 test.ok(_(a).isEqual(b), "underscore equal");
88 exports["clone buffer"] = function(test) {
91 var a = new Buffer("this is a test buffer");
94 // no underscore equal since it has no concept of Buffers
101 exports["clone regexp"] = function(test) {
107 test.deepEqual(b, a);
110 test.ok(c.lastIndex === 0);
113 test.ok(c.lastIndex === 4);
117 test.ok(d.lastIndex === 4);
123 exports["clone object containing array"] = function(test) {
124 test.expect(2); // how many tests?
127 arr1: [ { a: '1234', b: '2345' } ],
128 arr2: [ { c: '345', d: '456' } ]
132 test.ok(_(a).isEqual(b), "underscore equal");
133 test.deepEqual(b, a);
140 exports["clone object with circular reference"] = function(test) {
141 test.expect(8); // how many tests?
144 var c = [1, "foo", {'hello': 'bar'}, function() {}, false, [2]];
145 var b = [c, 2, 3, 4];
146 var a = {'b': b, 'c': c};
151 var aCopy = clone(a);
154 _(aCopy.c == aCopy.b[0]);
155 _(aCopy.c.loop.loop.aloop == aCopy);
156 _(aCopy.c[0] == a.c[0]);
158 //console.log(util.inspect(aCopy, true, null) );
159 //console.log("------------------------------------------------------------");
160 //console.log(util.inspect(a, true, null) );
166 //console.log("------------------------------------------------------------");
167 //console.log(util.inspect(aCopy, true, null) );
170 return util.inspect(x, true, null) === util.inspect(y, true, null);
178 exports['clonePrototype'] = function(test) {
179 test.expect(3); // how many tests?
186 var b = clone.clonePrototype(a);
188 test.strictEqual(b.a, a.a);
189 test.strictEqual(b.x, a.x);
190 test.strictEqual(b.y, a.y);
195 exports['cloneWithinNewVMContext'] = function(test) {
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);
207 exports['cloneObjectWithNoConstructor'] = function(test) {
210 var a = { foo: 'bar' };
212 test.ok(typeof a === 'object');
213 test.ok(typeof a !== null);
215 test.ok(a.foo, b.foo);
219 exports['clone object with depth argument'] = function (test) {
228 var b = clone(a, false, 1);
229 test.deepEqual(b, a);
231 test.strictEqual(b.foo, a.foo);
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);
240 exports['maintain prototype chain in clones'] = function (test) {
242 function Constructor() {}
243 var a = new Constructor();
245 test.strictEqual(Object.getPrototypeOf(a), Object.getPrototypeOf(b));
249 exports['parent prototype is overriden with prototype provided'] = function (test) {
251 function Constructor() {}
252 var a = new Constructor();
253 var b = clone(a, true, Infinity, null);
254 test.strictEqual(b.__defineSetter__, undefined);
258 exports['clone object with null children'] = function(test) {
269 test.deepEqual(b, a);
273 exports['clone instance with getter'] = function(test) {
276 Object.defineProperty(Ctor.prototype, 'prop', {
287 test.strictEqual(b.prop, 'value');