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