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