-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
532 lines (495 loc) · 10.8 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
var assert = require("assert");
var equal = require(".");
function eq(a, b, opts) {
var r = equal(a, b, opts);
if (r.length !== 0) {
r = r[0];
var msg = r.reason + " at " + r.path + " " + r.a + " =/= " + r.b;
assert.equal(a, b, msg);
}
}
function ne(a, b, opts) {
var r = equal(a, b, opts);
assert.ok(r.length !== 0);
}
/* 1. simple tests */
/* 1.1. positive */
it("NaN eqs NaN", function() {
return eq(NaN, NaN);
});
it("finite integer n eqs n", function() {
return eq(1234, 1234);
});
it("empty list eqs empty list", function() {
return eq([], []);
});
it("empty obj eqs empty obj", function() {
return eq({}, {});
});
it("number eqs number of same value", function() {
return eq(123.45678, 123.45678);
});
it("regex lit's w same pattern, flags are eq", function() {
return eq(/^abc[a-zA-Z]/, /^abc[a-zA-Z]/);
});
it("pods w same properties are eq", function() {
return eq(
{
a: "b",
c: "d"
},
{
a: "b",
c: "d"
}
);
});
it("pods that only differ wrt prop ord are eq", function() {
return eq(
{
a: "b",
c: "d"
},
{
c: "d",
a: "b"
}
);
});
/* 1.2. negative */
it("obj doesn't eq list", function() {
return ne({}, []);
});
it("obj in a list doesn't eq list in list", function() {
return ne([{}], [[]]);
});
it("integer n doesn't eq rpr n", function() {
return ne(1234, "1234");
});
it("integer n doesn't eq n + 1", function() {
return ne(1234, 1235);
});
it("empty list doesn't eq false", function() {
return ne([], false);
});
it("list w an integer doesn't eq one w rpr n", function() {
return ne([3], ["3"]);
});
it("regex lit's w diff. patterns, same flags aren't eq", function() {
return ne(/^abc[a-zA-Z]/, /^abc[a-zA-Z]x/);
});
it("regex lit's w same patterns, diff. flags aren't eq", function() {
return ne(/^abc[a-zA-Z]/, /^abc[a-zA-Z]/i);
});
it("number obj not eqs primitive number of same value", function() {
return ne(5, new Number(5));
});
it("string obj not eqs primitive string of same value", function() {
return ne("helo", new String("helo"));
});
it("(1) bool obj not eqs primitive bool of same value", function() {
return ne(false, new Boolean(false));
});
it("(2) bool obj not eqs primitive bool of same value", function() {
return ne(true, new Boolean(true));
});
it("number obj eqs the same valued number object", function() {
return eq(new Number(5), new Number(5));
});
/* 2. complex tests */
it("obj w undef member not eqs other obj w/out same member", function() {
var d, e;
d = {
x: void 0
};
e = {};
return ne(d, e);
});
it("fn1: functions w same source are eq", function() {
var d, e;
d = function(a, b, c) {
return a * b * c;
};
e = function(a, b, c) {
return a * b * c;
};
return eq(d, e);
});
it("fn2: functions w diff source aren't eq", function() {
var d, e;
d = function(a, b, c) {
return a * b * c;
};
e = function(a, b, s) {
return a * b * s;
};
return ne(d, e);
});
it("fn3: equal functions w equal props are eq", function() {
var d, e;
d = function() {
return null;
};
d.foo = {
some: "meaningless",
properties: "here"
};
e = function() {
return null;
};
e.foo = {
some: "meaningless",
properties: "here"
};
return eq(d, e);
});
it("fn4: equal functions w unequal props aren't eq", function() {
var d, e;
d = function() {
return null;
};
d.foo = {
some: "meaningless",
properties: "here"
};
e = function() {
return null;
};
e.foo = {
some: "meaningless",
properties: "here!!!"
};
return ne(d, e);
});
it("list w named member eqs other list w same member", function() {
var d, e;
d = ["foo", null, 3];
d["extra"] = 42;
e = ["foo", null, 3];
e["extra"] = 42;
return eq(d, e);
});
it("list w named member doesn't eq list w same member, other value", function() {
var d, e;
d = ["foo", null, 3];
d["extra"] = 42;
e = ["foo", null, 3];
e["extra"] = 108;
return ne(d, e);
});
it("date eqs other date pointing to same time", function() {
var d, e;
d = new Date("1995-12-17T03:24:00");
e = new Date("1995-12-17T03:24:00");
return eq(d, e);
});
it("date does not eq other date pointing to other time", function() {
var d, e;
d = new Date("1995-12-17T03:24:00");
e = new Date("1995-12-17T03:24:01");
return ne(d, e);
});
it("str obj w props eq same str, same props", function() {
var d, e;
d = new String("helo test");
d["abc"] = 42;
e = new String("helo test");
e["abc"] = 42;
return eq(d, e);
});
it("str obj w props not eq same str, other props", function() {
var d, e;
d = new String("helo test");
d["abc"] = 42;
e = new String("helo test");
e["def"] = 42;
return ne(d, e);
});
it("str obj w props eq same str, same props (circ)", function() {
var c, d, e;
c = ["a list"];
c.push(c);
d = new String("helo test");
d["abc"] = c;
e = new String("helo test");
e["abc"] = c;
return eq(d, e);
});
it("str obj w props not eq same str, other props (circ)", function() {
var c, d, e;
c = ["a list"];
c.push(c);
d = new String("helo test");
d["abc"] = c;
e = new String("helo test");
e["def"] = c;
return ne(d, e);
});
/*it("(1) circ arrays w similar layout, same values aren't eq", function() {
var d, e;
d = [1, 2, 3];
d.push(d);
e = [1, 2, 3];
e.push(d);
return ne(d, e);
});*/
it("(2) circ arrays w same layout, same values are eq", function() {
var d, e;
d = [1, 2, 3];
d.push(d);
e = [1, 2, 3];
e.push(e);
return eq(d, e);
});
it("(fkling1) arrays w eq subarrays are eq", function() {
var a, b, bar, foo;
a = [1, 2, 3];
b = [1, 2, 3];
foo = [a, a];
bar = [b, b];
return eq(foo, bar);
});
/*it("(fkling2) arrays w eq subarrays but diff distribution aren't eq"] = function() {
var a, b, bar, foo;
a = [1, 2, 3];
b = [1, 2, 3];
foo = [a, a];
bar = [a, b];
return ne(foo, bar);
};*/
/* joshwilsdon's test (https://github.com/joyent/node/issues/7161) */
it("joshwilsdon", function() {
var count, d1, d2, errors, idx1, idx2, v1, v2, _i, _j, _len, _ref;
d1 = [
NaN,
void 0,
null,
true,
false,
Infinity,
0,
1,
"a",
"b",
{
a: 1
},
{
a: "a"
},
[
{
a: 1
}
],
[
{
a: true
}
],
{
a: 1,
b: 2
},
[1, 2],
[1, 2, 3],
{
a: "1"
},
{
a: "1",
b: "2"
}
];
d2 = [
NaN,
void 0,
null,
true,
false,
Infinity,
0,
1,
"a",
"b",
{
a: 1
},
{
a: "a"
},
[
{
a: 1
}
],
[
{
a: true
}
],
{
a: 1,
b: 2
},
[1, 2],
[1, 2, 3],
{
a: "1"
},
{
a: "1",
b: "2"
}
];
errors = [];
count = 0;
for (idx1 = _i = 0, _len = d1.length; _i < _len; idx1 = ++_i) {
v1 = d1[idx1];
for (idx2 = _j = idx1, _ref = d2.length; idx1 <= _ref ? _j < _ref : _j > _ref; idx2 = idx1 <= _ref ? ++_j : --_j) {
count += 1;
v2 = d2[idx2];
if (idx1 === idx2) {
if (!eq(v1, v2)) {
//errors.push("eq " + (rpr(v1)) + ", " + (rpr(v2)));
}
} else {
if (!ne(v1, v2)) {
//errors.push("ne " + (rpr(v1)) + ", " + (rpr(v2)));
}
}
}
}
return [count, errors];
});
it("node buffer", function() {
eq(new Buffer("abc"), new Buffer("abc"));
ne(new Buffer("abc"), new Buffer("abc1"));
ne(new Buffer("abd"), new Buffer("abc"));
});
it("RegExp with props", function() {
var re1 = /a/;
re1.lastIndex = 3;
ne(re1, /a/);
});
it("Date with props", function() {
var now = Date.now();
var d1 = new Date(now);
var d2 = new Date(now);
d1.a = 10;
ne(d1, d2);
});
it("Check object prototypes", function() {
var nbRoot = {
toString: function() {
return this.first + " " + this.last;
}
};
function nameBuilder(first, last) {
this.first = first;
this.last = last;
return this;
}
nameBuilder.prototype = nbRoot;
function nameBuilder2(first, last) {
this.first = first;
this.last = last;
return this;
}
nameBuilder2.prototype = nbRoot;
var nb1 = new nameBuilder("Ryan", "Dahl");
var nb2 = new nameBuilder2("Ryan", "Dahl");
eq(nb1, nb2);
nameBuilder2.prototype = Object;
nb2 = new nameBuilder2("Ryan", "Dahl");
ne(nb1, nb2);
});
if (typeof Uint8Array !== "undefined") {
it("typed arrays and array buffer", function() {
var arr1 = new Uint8Array([21, 31]);
var arr2 = new Uint8Array([21, 31]);
eq(arr1, arr2);
eq(arr1.buffer, arr2.buffer);
});
}
if (typeof Set !== "undefined") {
it("es6 sets", function() {
var s1 = new Set([1, 2, 3]);
var s2 = new Set([1, 2, 3]);
var s3 = new Set(["a", "b", "c"]);
var s4 = new Set([]);
eq(s1, s2);
ne(s1, s3);
ne(s1, s4);
var k1 = { a: 1 };
var s5 = new Set([k1, k1, k1]);
var s6 = new Set([k1, k1, k1]);
ne(s1, s5);
eq(s5, s6);
eq(s6, s5);
});
}
if (typeof Map !== "undefined") {
it("es6 maps", function() {
var m1 = new Map([[1, 1], [2, 2], [3, 3]]);
var m2 = new Map([[1, 1], [2, 2], [3, 3]]);
var m3 = new Map([[1, 2], [2, 3], [3, 4]]);
eq(m1, m1);
eq(m1, m2);
ne(m3, m2);
var key_a10 = { a: 10 };
var key_a11 = { a: 11 };
var key_a12 = { a: 12 };
var key_a13 = { a: 13 };
var m4 = new Map([[key_a10, 2], [key_a11, 2], [key_a12, 2]]);
var m5 = new Map([[key_a11, 2], [key_a12, 2], [key_a13, 2]]);
var m6 = new Map([[key_a11, 2], [key_a12, 2], [key_a13, 2]]);
ne(m4, m5);
eq(m5, m6);
var m7 = new Map([[{}, 1], [{}, 2]]);
var m8 = new Map([[{}, 2], [{}, 3]]);
var m9 = new Map([[{}, 2], [{}, 3]]);
ne(m7, m8);
ne(m9, m8);
});
}
if (typeof WeakSet !== "undefined") {
it("es WeakMap, WeakSet", function() {
var a = new WeakSet();
var b = new WeakSet();
eq(a, b);
});
}
it("should treat -0 and +0 as not equal when param not passed", function() {
eq(-0, +0);
ne(-0, +0, { plusZeroAndMinusZeroEqual: false });
});
if (typeof Symbol !== "undefined") {
it("should work with Symbols", function() {
ne([Symbol()], [Symbol()]);
eq([Symbol.for("a")], [Symbol.for("a")]);
ne(Symbol(), Symbol());
ne(Symbol("abc"), Symbol("abc"));
eq(Symbol.for("a"), Symbol.for("a"));
ne(Symbol.for("a"), Symbol.for("b"));
});
}
if (typeof Buffer !== "undefined") {
it("should support node Buffer", function() {
var b1 = new Buffer("abc", "utf8");
var b2 = new Buffer("abc", "utf8");
var b3 = new Buffer("acc", "utf8");
eq(b1, b2);
ne(b1, b3);
});
}
it("should not assume object with only the same properties are equal", function() {
var F = function(a, b) {
this.a = a;
this.b = b;
};
var f = new F(19, 11);
var f1 = { a: 19, b: 11 };
ne(f, f1);
eq(f, f1, { checkProtoEql: false });
});