UNPKG

5.37 kBJavaScriptView Raw
1var test = require('tap').test;
2var traverse = require('../');
3var deepEqual = require('./lib/deep_equal');
4
5test('deepDates', function (t) {
6 t.plan(2);
7
8 t.ok(
9 deepEqual(
10 { d : new Date, x : [ 1, 2, 3 ] },
11 { d : new Date, x : [ 1, 2, 3 ] }
12 ),
13 'dates should be equal'
14 );
15
16 var d0 = new Date;
17 setTimeout(function () {
18 t.ok(
19 !deepEqual(
20 { d : d0, x : [ 1, 2, 3 ], },
21 { d : new Date, x : [ 1, 2, 3 ] }
22 ),
23 'microseconds should count in date equality'
24 );
25 }, 5);
26});
27
28test('deepCircular', function (t) {
29 var a = [1];
30 a.push(a); // a = [ 1, *a ]
31
32 var b = [1];
33 b.push(a); // b = [ 1, [ 1, *a ] ]
34
35 t.ok(
36 !deepEqual(a, b),
37 'circular ref mount points count towards equality'
38 );
39
40 var c = [1];
41 c.push(c); // c = [ 1, *c ]
42 t.ok(
43 deepEqual(a, c),
44 'circular refs are structurally the same here'
45 );
46
47 var d = [1];
48 d.push(a); // c = [ 1, [ 1, *d ] ]
49 t.ok(
50 deepEqual(b, d),
51 'non-root circular ref structural comparison'
52 );
53
54 t.end();
55});
56
57test('deepInstances', function (t) {
58 t.ok(
59 !deepEqual([ new Boolean(false) ], [ false ]),
60 'boolean instances are not real booleans'
61 );
62
63 t.ok(
64 !deepEqual([ new String('x') ], [ 'x' ]),
65 'string instances are not real strings'
66 );
67
68 t.ok(
69 !deepEqual([ new Number(4) ], [ 4 ]),
70 'number instances are not real numbers'
71 );
72
73 t.ok(
74 deepEqual([ new RegExp('x') ], [ /x/ ]),
75 'regexp instances are real regexps'
76 );
77
78 t.ok(
79 !deepEqual([ new RegExp(/./) ], [ /../ ]),
80 'these regexps aren\'t the same'
81 );
82
83 t.ok(
84 !deepEqual(
85 [ function (x) { return x * 2 } ],
86 [ function (x) { return x * 2 } ]
87 ),
88 'functions with the same .toString() aren\'t necessarily the same'
89 );
90
91 var f = function (x) { return x * 2 };
92 t.ok(
93 deepEqual([ f ], [ f ]),
94 'these functions are actually equal'
95 );
96
97 t.end();
98});
99
100test('deepEqual', function (t) {
101 t.ok(
102 !deepEqual([ 1, 2, 3 ], { 0 : 1, 1 : 2, 2 : 3 }),
103 'arrays are not objects'
104 );
105 t.end();
106});
107
108test('falsy', function (t) {
109 t.ok(
110 !deepEqual([ undefined ], [ null ]),
111 'null is not undefined!'
112 );
113
114 t.ok(
115 !deepEqual([ null ], [ undefined ]),
116 'undefined is not null!'
117 );
118
119 t.ok(
120 !deepEqual(
121 { a : 1, b : 2, c : [ 3, undefined, 5 ] },
122 { a : 1, b : 2, c : [ 3, null, 5 ] }
123 ),
124 'undefined is not null, however deeply!'
125 );
126
127 t.ok(
128 !deepEqual(
129 { a : 1, b : 2, c : [ 3, undefined, 5 ] },
130 { a : 1, b : 2, c : [ 3, null, 5 ] }
131 ),
132 'null is not undefined, however deeply!'
133 );
134
135 t.ok(
136 !deepEqual(
137 { a : 1, b : 2, c : [ 3, undefined, 5 ] },
138 { a : 1, b : 2, c : [ 3, null, 5 ] }
139 ),
140 'null is not undefined, however deeply!'
141 );
142
143 t.end();
144});
145
146test('deletedArrayEqual', function (t) {
147 var xs = [ 1, 2, 3, 4 ];
148 delete xs[2];
149
150 var ys = Object.create(Array.prototype);
151 ys[0] = 1;
152 ys[1] = 2;
153 ys[3] = 4;
154
155 t.ok(
156 deepEqual(xs, ys),
157 'arrays with deleted elements are only equal to'
158 + ' arrays with similarly deleted elements'
159 );
160
161 t.ok(
162 !deepEqual(xs, [ 1, 2, undefined, 4 ]),
163 'deleted array elements cannot be undefined'
164 );
165
166 t.ok(
167 !deepEqual(xs, [ 1, 2, null, 4 ]),
168 'deleted array elements cannot be null'
169 );
170
171 t.end();
172});
173
174test('deletedObjectEqual', function (t) {
175 var obj = { a : 1, b : 2, c : 3 };
176 delete obj.c;
177
178 t.ok(
179 deepEqual(obj, { a : 1, b : 2 }),
180 'deleted object elements should not show up'
181 );
182
183 t.ok(
184 !deepEqual(obj, { a : 1, b : 2, c : undefined }),
185 'deleted object elements are not undefined'
186 );
187
188 t.ok(
189 !deepEqual(obj, { a : 1, b : 2, c : null }),
190 'deleted object elements are not null'
191 );
192
193 t.end();
194});
195
196test('emptyKeyEqual', function (t) {
197 t.ok(!deepEqual(
198 { a : 1 }, { a : 1, '' : 55 }
199 ));
200
201 t.end();
202});
203
204test('deepArguments', function (t) {
205 t.ok(
206 !deepEqual(
207 [ 4, 5, 6 ],
208 (function () { return arguments })(4, 5, 6)
209 ),
210 'arguments are not arrays'
211 );
212
213 t.ok(
214 deepEqual(
215 (function () { return arguments })(4, 5, 6),
216 (function () { return arguments })(4, 5, 6)
217 ),
218 'arguments should equal'
219 );
220
221 t.end();
222});
223
224test('deepUn', function (t) {
225 t.ok(!deepEqual({ a : 1, b : 2 }, undefined));
226 t.ok(!deepEqual({ a : 1, b : 2 }, {}));
227 t.ok(!deepEqual(undefined, { a : 1, b : 2 }));
228 t.ok(!deepEqual({}, { a : 1, b : 2 }));
229 t.ok(deepEqual(undefined, undefined));
230 t.ok(deepEqual(null, null));
231 t.ok(!deepEqual(undefined, null));
232
233 t.end();
234});
235
236test('deepLevels', function (t) {
237 var xs = [ 1, 2, [ 3, 4, [ 5, 6 ] ] ];
238 t.ok(!deepEqual(xs, []));
239 t.end();
240});