1 var clone = require('./');
3 function inspect(obj) {
5 return JSON.stringify(obj, function (key, val) {
6 if (val != null && typeof val == "object") {
7 if (seen.indexOf(val) >= 0) {
18 // Creates a new VM in node, or an iframe in a browser in order to run the
20 function apartContext(context, script, callback) {
21 var vm = require('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);
31 var myCtxId = 'tmpCtx' + Math.random();
33 window[myCtxId] = context;
34 iframe.src = 'test-apart-ctx.html?' + myCtxId + '&' + encodeURIComponent(script);
35 iframe.onload = function() {
37 callback(iframe.contentWindow.results);
43 console.log('WARNING: cannot create an apart context.');
47 exports["clone string"] = function (test) {
48 test.expect(2); // how many tests?
51 test.strictEqual(clone(a), a);
53 test.strictEqual(clone(a), a);
58 exports["clone number"] = function (test) {
59 test.expect(5); // how many tests?
62 test.strictEqual(clone(a), a);
64 test.strictEqual(clone(a), a);
66 test.strictEqual(clone(a), a);
68 test.strictEqual(clone(a), a);
70 test.strictEqual(clone(a), a);
75 exports["clone date"] = function (test) {
76 test.expect(3); // how many tests?
80 test.ok(!!a.getUTCDate && !!a.toUTCString);
81 test.ok(!!c.getUTCDate && !!c.toUTCString);
82 test.equal(a.getTime(), c.getTime());
87 exports["clone object"] = function (test) {
88 test.expect(1); // how many tests?
90 var a = { foo: { bar: "baz" } };
98 exports["clone array"] = function (test) {
99 test.expect(2); // how many tests?
107 test.ok(b instanceof Array);
108 test.deepEqual(b, a);
113 exports["clone buffer"] = function (test) {
114 if (typeof Buffer == 'undefined') {
120 var a = new Buffer("this is a test buffer");
123 // no underscore equal since it has no concept of Buffers
124 test.deepEqual(b, a);
128 exports["clone regexp"] = function (test) {
133 test.deepEqual(b, a);
136 test.ok(c.lastIndex === 0);
139 test.ok(c.lastIndex === 4);
143 test.ok(d.lastIndex === 4);
148 exports["clone object containing array"] = function (test) {
149 test.expect(1); // how many tests?
152 arr1: [ { a: '1234', b: '2345' } ],
153 arr2: [ { c: '345', d: '456' } ]
158 test.deepEqual(b, a);
163 exports["clone object with circular reference"] = function (test) {
164 test.expect(8); // how many tests?
166 var c = [1, "foo", {'hello': 'bar'}, function () {}, false, [2]];
167 var b = [c, 2, 3, 4];
169 var a = {'b': b, 'c': c};
175 var aCopy = clone(a);
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]);
182 test.ok(eq(a, aCopy));
184 test.ok(!eq(a, aCopy));
186 test.ok(!eq(a, aCopy));
189 return inspect(x) === inspect(y);
195 exports['clone prototype'] = function (test) {
196 test.expect(3); // how many tests?
203 var b = clone.clonePrototype(a);
205 test.strictEqual(b.a, a.a);
206 test.strictEqual(b.x, a.x);
207 test.strictEqual(b.y, a.y);
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 })",
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());
223 exports['clone object with no constructor'] = function (test) {
228 var a = { foo: 'bar' };
230 test.ok(typeof a === 'object');
231 test.ok(typeof a !== null);
234 test.ok(a.foo, b.foo);
239 exports['clone object with depth argument'] = function (test) {
250 var b = clone(a, false, 1);
251 test.deepEqual(b, a);
253 test.strictEqual(b.foo, a.foo);
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);
263 exports['maintain prototype chain in clones'] = function (test) {
270 test.strictEqual(Object.getPrototypeOf(a), Object.getPrototypeOf(b));
275 exports['parent prototype is overriden with prototype provided'] = function (test) {
281 var b = clone(a, true, Infinity, null);
282 test.strictEqual(b.__defineSetter__, undefined);
287 exports['clone object with null children'] = function (test) {
300 test.deepEqual(b, a);
304 exports['clone instance with getter'] = function (test) {
307 Object.defineProperty(Ctor.prototype, 'prop', {
318 test.strictEqual(b.prop, 'value');
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' );
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));
345 exports["recognize Date object"] = function (test) {
346 var results = apartContext(null, "results = new Date()", function(alien) {
347 var local = new Date();
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));
360 exports["recognize RegExp object"] = function (test) {
361 var results = apartContext(null, "results = /foo/", function(alien) {
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));