equal.js 5.2 KB

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