equal.js 5.5 KB

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