You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
6.7 KiB

4 years ago
  1. var test = require('tape');
  2. require('./_tape');
  3. var equal = require('../');
  4. test('equal', function (t) {
  5. t.deepEqualTest(
  6. { a: [2, 3], b: [4] },
  7. { a: [2, 3], b: [4] },
  8. 'two equal objects',
  9. true,
  10. true,
  11. false
  12. );
  13. t.end();
  14. });
  15. test('not equal', function (t) {
  16. t.deepEqualTest(
  17. { x: 5, y: [6] },
  18. { x: 5, y: 6 },
  19. 'two inequal objects are',
  20. false,
  21. false
  22. );
  23. t.end();
  24. });
  25. test('nested nulls', function (t) {
  26. t.deepEqualTest(
  27. [null, null, null],
  28. [null, null, null],
  29. 'same-length arrays of nulls',
  30. true,
  31. true,
  32. true
  33. );
  34. t.end();
  35. });
  36. test('objects with strings vs numbers', function (t) {
  37. t.deepEqualTest(
  38. [{ a: 3 }, { b: 4 }],
  39. [{ a: '3' }, { b: '4' }],
  40. 'objects with equivalent string/number values',
  41. true,
  42. false
  43. );
  44. t.end();
  45. });
  46. test('non-objects', function (t) {
  47. t.deepEqualTest(3, 3, 'same numbers', true, true, true);
  48. t.deepEqualTest('beep', 'beep', 'same strings', true, true, true);
  49. t.deepEqualTest('3', 3, 'numeric string and number', true, false);
  50. t.deepEqualTest('3', [3], 'numeric string and array containing number', false, false);
  51. t.deepEqualTest(3, [3], 'number and array containing number', false, false);
  52. t.end();
  53. });
  54. test('infinities', function (t) {
  55. t.deepEqualTest(Infinity, Infinity, '∞ and ∞', true, true, true);
  56. t.deepEqualTest(-Infinity, -Infinity, '-∞ and -∞', true, true, true);
  57. t.deepEqualTest(Infinity, -Infinity, '∞ and -∞', false, false);
  58. t.end();
  59. });
  60. test('arguments class', function (t) {
  61. function getArgs() {
  62. return arguments;
  63. }
  64. t.ok(
  65. equal(getArgs(1, 2, 3), getArgs(1, 2, 3)),
  66. 'equivalent arguments objects are equal'
  67. );
  68. t.deepEqualTest(
  69. getArgs(1, 2, 3),
  70. [1, 2, 3],
  71. 'array and arguments with same contents',
  72. false,
  73. false
  74. );
  75. t.end();
  76. });
  77. test('dates', function (t) {
  78. var d0 = new Date(1387585278000);
  79. var d1 = new Date('Fri Dec 20 2013 16:21:18 GMT-0800 (PST)');
  80. t.deepEqualTest(d0, d1, 'equivalent Dates', true, true);
  81. t.end();
  82. });
  83. test('buffers', function (t) {
  84. /* eslint no-buffer-constructor: 1, new-cap: 1 */
  85. t.ok(equal(Buffer('xyz'), Buffer('xyz')), 'buffers with same contents are equal');
  86. t.ok(equal(Buffer('xyz'), Buffer('xyz'), { strict: true }), 'strict: buffers with same contents are equal');
  87. t.deepEqualTest(
  88. Buffer('abc'),
  89. Buffer('xyz'),
  90. 'buffers with different contents',
  91. false,
  92. false
  93. );
  94. t.deepEqualTest(
  95. Buffer(''),
  96. [],
  97. 'empty buffer and empty array',
  98. false,
  99. false
  100. );
  101. t.end();
  102. });
  103. test('booleans and arrays', function (t) {
  104. t.deepEqualTest(
  105. true,
  106. [],
  107. 'true and an empty array',
  108. false,
  109. false
  110. );
  111. t.deepEqualTest(
  112. false,
  113. [],
  114. 'false and an empty array',
  115. true,
  116. false
  117. );
  118. t.end();
  119. });
  120. test('arrays initiated', function (t) {
  121. var a0 = [
  122. undefined,
  123. null,
  124. -1,
  125. 0,
  126. 1,
  127. false,
  128. true,
  129. undefined,
  130. '',
  131. 'abc',
  132. null,
  133. undefined
  134. ],
  135. a1 = [
  136. undefined,
  137. null,
  138. -1,
  139. 0,
  140. 1,
  141. false,
  142. true,
  143. undefined,
  144. '',
  145. 'abc',
  146. null,
  147. undefined
  148. ];
  149. t.ok(equal(a0, a1));
  150. t.end();
  151. });
  152. // eslint-disable-next-line max-statements
  153. test('arrays assigned', function (t) {
  154. var a0 = [
  155. undefined,
  156. null,
  157. -1,
  158. 0,
  159. 1,
  160. false,
  161. true,
  162. undefined,
  163. '',
  164. 'abc',
  165. null,
  166. undefined
  167. ];
  168. var a1 = [];
  169. a1[0] = undefined;
  170. a1[1] = null;
  171. a1[2] = -1;
  172. a1[3] = 0;
  173. a1[4] = 1;
  174. a1[5] = false;
  175. a1[6] = true;
  176. a1[7] = undefined;
  177. a1[8] = '';
  178. a1[9] = 'abc';
  179. a1[10] = null;
  180. a1[11] = undefined;
  181. a1.length = 12;
  182. t.deepEqualTest(a0, a1, 'a literal array and an assigned array', true, true);
  183. t.end();
  184. });
  185. // eslint-disable-next-line max-statements
  186. test('arrays push', function (t) {
  187. var a0 = [
  188. undefined,
  189. null,
  190. -1,
  191. 0,
  192. 1,
  193. false,
  194. true,
  195. undefined,
  196. '',
  197. 'abc',
  198. null,
  199. undefined
  200. ],
  201. a1 = [];
  202. a1.push(undefined);
  203. a1.push(null);
  204. a1.push(-1);
  205. a1.push(0);
  206. a1.push(1);
  207. a1.push(false);
  208. a1.push(true);
  209. a1.push(undefined);
  210. a1.push('');
  211. a1.push('abc');
  212. a1.push(null);
  213. a1.push(undefined);
  214. a1.length = 12;
  215. t.deepEqualTest(a0, a1, 'a literal array and a pushed array', true, true);
  216. t.end();
  217. });
  218. test('null == undefined', function (t) {
  219. t.deepEqualTest(null, undefined, 'null and undefined', true, false);
  220. t.end();
  221. });
  222. test('NaNs', function (t) {
  223. t.notOk(equal(NaN, NaN), 'NaN is not NaN');
  224. t.ok(equal(NaN, NaN, { strict: true }), 'strict: NaN is NaN');
  225. t.notOk(equal({ a: NaN }, { a: NaN }), 'two equiv objects with a NaN value are not equiv');
  226. t.ok(equal({ a: NaN }, { a: NaN }, { strict: true }), 'strict: two equiv objects with a NaN value are equiv');
  227. t.notOk(equal(NaN, 1), 'NaN !== 1');
  228. t.notOk(equal(NaN, 1, { strict: true }), 'strict: NaN !== 1');
  229. t.end();
  230. });
  231. test('zeroes', function (t) {
  232. t.deepEqualTest(0, -0, '0 and -0', true, false);
  233. t.deepEqualTest({ a: 0 }, { a: -0 }, 'two objects with a same-keyed 0/-0 value', true, false);
  234. t.end();
  235. });
  236. test('Object.create', { skip: !Object.create }, function (t) {
  237. var a = { a: 'A' };
  238. var b = Object.create(a);
  239. b.b = 'B';
  240. var c = Object.create(a);
  241. c.b = 'C';
  242. t.deepEqualTest(
  243. b,
  244. c,
  245. 'two objects with the same [[Prototype]] but a different own property',
  246. false,
  247. false
  248. );
  249. t.end();
  250. });
  251. test('Object.create(null)', { skip: !Object.create }, function (t) {
  252. t.deepEqualTest(
  253. Object.create(null),
  254. Object.create(null),
  255. 'two empty null objects',
  256. true,
  257. true,
  258. true
  259. );
  260. t.deepEqualTest(
  261. Object.create(null, { a: { value: 'b' } }),
  262. Object.create(null, { a: { value: 'b' } }),
  263. 'two null objects with the same property pair',
  264. true,
  265. true,
  266. true
  267. );
  268. t.end();
  269. });
  270. test('regexes vs dates', function (t) {
  271. var d = new Date(1387585278000);
  272. var r = /abc/;
  273. t.deepEqualTest(d, r, 'Date and RegExp', false, false);
  274. t.end();
  275. });
  276. test('regexen', function (t) {
  277. t.deepEqualTest(/abc/, /xyz/, 'two different regexes', false, false);
  278. t.deepEqualTest(/abc/, /abc/, 'two abc regexes', true, true, false);
  279. t.deepEqualTest(/xyz/, /xyz/, 'two xyz regexes', true, true, false);
  280. t.end();
  281. });
  282. test('arrays and objects', function (t) {
  283. t.deepEqualTest([], {}, 'empty array and empty object', true, true);
  284. t.deepEqualTest([], { length: 0 }, 'empty array and empty arraylike object', false, false);
  285. t.deepEqualTest([1], { 0: 1 }, 'array and similar object', true, true);
  286. t.end();
  287. });
  288. test('functions', function (t) {
  289. function f() {}
  290. t.deepEqualTest(f, f, 'a function and itself', true, true, true);
  291. t.deepEqualTest(function () {}, function () {}, 'two distinct functions', false, false, true);
  292. t.end();
  293. });