equal.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. var test = require('tape');
  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([Object(false)], [false]),
  50. 'boolean instances are not real booleans'
  51. );
  52. t.ok(
  53. !deepEqual([Object('x')], ['x']),
  54. 'string instances are not real strings'
  55. );
  56. t.ok(
  57. !deepEqual([Object(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. function f(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 arrays with similarly deleted elements'
  131. );
  132. t.ok(
  133. !deepEqual(xs, [1, 2, undefined, 4]),
  134. 'deleted array elements cannot be undefined'
  135. );
  136. t.ok(
  137. !deepEqual(xs, [1, 2, null, 4]),
  138. 'deleted array elements cannot be null'
  139. );
  140. t.end();
  141. });
  142. test('deletedObjectEqual', function (t) {
  143. var obj = { a: 1, b: 2, c: 3 };
  144. delete obj.c;
  145. t.ok(
  146. deepEqual(obj, { a: 1, b: 2 }),
  147. 'deleted object elements should not show up'
  148. );
  149. t.ok(
  150. !deepEqual(obj, { a: 1, b: 2, c: undefined }),
  151. 'deleted object elements are not undefined'
  152. );
  153. t.ok(
  154. !deepEqual(obj, { a: 1, b: 2, c: null }),
  155. 'deleted object elements are not null'
  156. );
  157. t.end();
  158. });
  159. test('emptyKeyEqual', function (t) {
  160. t.ok(!deepEqual({ a: 1 }, { a: 1, '': 55 }));
  161. t.end();
  162. });
  163. test('deepArguments', function (t) {
  164. t.ok(
  165. !deepEqual(
  166. [4, 5, 6],
  167. (function () { return arguments; }(4, 5, 6))
  168. ),
  169. 'arguments are not arrays'
  170. );
  171. t.ok(
  172. deepEqual(
  173. (function () { return arguments; }(4, 5, 6)),
  174. (function () { return arguments; }(4, 5, 6))
  175. ),
  176. 'arguments should equal'
  177. );
  178. t.end();
  179. });
  180. test('deepUn', function (t) {
  181. t.ok(!deepEqual({ a: 1, b: 2 }, undefined));
  182. t.ok(!deepEqual({ a: 1, b: 2 }, {}));
  183. t.ok(!deepEqual(undefined, { a: 1, b: 2 }));
  184. t.ok(!deepEqual({}, { a: 1, b: 2 }));
  185. t.ok(deepEqual(undefined, undefined));
  186. t.ok(deepEqual(null, null));
  187. t.ok(!deepEqual(undefined, null));
  188. t.end();
  189. });
  190. test('deepLevels', function (t) {
  191. var xs = [1, 2, [3, 4, [5, 6]]];
  192. t.ok(!deepEqual(xs, []));
  193. t.end();
  194. });