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.

136 lines
5.0 KiB

4 years ago
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var SaferBuffer = require('safer-buffer').Buffer;
  5. var forEach = require('for-each');
  6. var utils = require('../lib/utils');
  7. test('merge()', function (t) {
  8. t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');
  9. t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');
  10. t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
  11. var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
  12. t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
  13. var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
  14. t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
  15. var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
  16. t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
  17. var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
  18. t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
  19. var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
  20. t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
  21. t.test(
  22. 'avoids invoking array setters unnecessarily',
  23. { skip: typeof Object.defineProperty !== 'function' },
  24. function (st) {
  25. var setCount = 0;
  26. var getCount = 0;
  27. var observed = [];
  28. Object.defineProperty(observed, 0, {
  29. get: function () {
  30. getCount += 1;
  31. return { bar: 'baz' };
  32. },
  33. set: function () { setCount += 1; }
  34. });
  35. utils.merge(observed, [null]);
  36. st.equal(setCount, 0);
  37. st.equal(getCount, 1);
  38. observed[0] = observed[0]; // eslint-disable-line no-self-assign
  39. st.equal(setCount, 1);
  40. st.equal(getCount, 2);
  41. st.end();
  42. }
  43. );
  44. t.end();
  45. });
  46. test('assign()', function (t) {
  47. var target = { a: 1, b: 2 };
  48. var source = { b: 3, c: 4 };
  49. var result = utils.assign(target, source);
  50. t.equal(result, target, 'returns the target');
  51. t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
  52. t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
  53. t.end();
  54. });
  55. test('combine()', function (t) {
  56. t.test('both arrays', function (st) {
  57. var a = [1];
  58. var b = [2];
  59. var combined = utils.combine(a, b);
  60. st.deepEqual(a, [1], 'a is not mutated');
  61. st.deepEqual(b, [2], 'b is not mutated');
  62. st.notEqual(a, combined, 'a !== combined');
  63. st.notEqual(b, combined, 'b !== combined');
  64. st.deepEqual(combined, [1, 2], 'combined is a + b');
  65. st.end();
  66. });
  67. t.test('one array, one non-array', function (st) {
  68. var aN = 1;
  69. var a = [aN];
  70. var bN = 2;
  71. var b = [bN];
  72. var combinedAnB = utils.combine(aN, b);
  73. st.deepEqual(b, [bN], 'b is not mutated');
  74. st.notEqual(aN, combinedAnB, 'aN + b !== aN');
  75. st.notEqual(a, combinedAnB, 'aN + b !== a');
  76. st.notEqual(bN, combinedAnB, 'aN + b !== bN');
  77. st.notEqual(b, combinedAnB, 'aN + b !== b');
  78. st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
  79. var combinedABn = utils.combine(a, bN);
  80. st.deepEqual(a, [aN], 'a is not mutated');
  81. st.notEqual(aN, combinedABn, 'a + bN !== aN');
  82. st.notEqual(a, combinedABn, 'a + bN !== a');
  83. st.notEqual(bN, combinedABn, 'a + bN !== bN');
  84. st.notEqual(b, combinedABn, 'a + bN !== b');
  85. st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
  86. st.end();
  87. });
  88. t.test('neither is an array', function (st) {
  89. var combined = utils.combine(1, 2);
  90. st.notEqual(1, combined, '1 + 2 !== 1');
  91. st.notEqual(2, combined, '1 + 2 !== 2');
  92. st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
  93. st.end();
  94. });
  95. t.end();
  96. });
  97. test('isBuffer()', function (t) {
  98. forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {
  99. t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
  100. });
  101. var fakeBuffer = { constructor: Buffer };
  102. t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
  103. var saferBuffer = SaferBuffer.from('abc');
  104. t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
  105. var buffer = Buffer.from ? Buffer.from('abc') : new Buffer('abc');
  106. t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');
  107. t.end();
  108. });