forked from pvorb/clone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
171 lines (122 loc) · 3.36 KB
/
test.js
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
if(module.parent === null) {
console.log('Run this test file with nodeunit:');
console.log('$ nodeunit test.js');
}
var clone = require('./'),
util = require('util'),
_ = require('underscore');
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 instanceof Date);
test.ok(c instanceof Date);
test.equal(c.getTime(), a.getTime());
test.done();
};
exports["clone object"] = function(test) {
test.expect(2); // how many tests?
var a = { foo: { bar: "baz" } };
var b = clone(a);
test.ok(_(a).isEqual(b), "underscore equal");
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(_(a).isEqual(b), "underscore equal");
test.deepEqual(b, a);
test.done();
};
exports["clone object containing array"] = function(test) {
test.expect(2); // how many tests?
var a = {
arr1: [ { a: '1234', b: '2345' } ],
arr2: [ { c: '345', d: '456' } ]
};
var b = clone(a);
test.ok(_(a).isEqual(b), "underscore equal");
test.deepEqual(b, a);
test.done();
};
exports["clone object with circular reference"] = function(test) {
test.expect(8); // how many tests?
var _ = test.ok;
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);
_(a != aCopy);
_(a.c != aCopy.c);
_(aCopy.c == aCopy.b[0]);
_(aCopy.c.loop.loop.aloop == aCopy);
_(aCopy.c[0] == a.c[0]);
//console.log(util.inspect(aCopy, true, null) );
//console.log("------------------------------------------------------------");
//console.log(util.inspect(a, true, null) );
_(eq(a, aCopy));
aCopy.c[0] = 2;
_(!eq(a, aCopy));
aCopy.c = "2";
_(!eq(a, aCopy));
//console.log("------------------------------------------------------------");
//console.log(util.inspect(aCopy, true, null) );
function eq(x, y) {
return util.inspect(x, true, null) === util.inspect(y, true, null);
}
test.done();
};
exports['clonePrototype'] = 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['cloneWithinNewVMContext'] = function(test) {
var vm = require('vm');
var ctx = vm.createContext({ clone: clone });
var script = "clone( {array: [1, 2, 3], date: new Date(), regex: /^foo$/ig} );";
var results = vm.runInContext(script, ctx);
test.ok(results.array instanceof Array);
test.ok(results.date instanceof Date);
test.ok(results.regex instanceof RegExp);
test.done();
}