项目原始demo,不改动
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
Este repositorio está archivado. Puede ver los archivos y clonarlo, pero no puede subir cambios o reportar incidencias ni pedir Pull Requests.
 
 
 
 

90 líneas
3.3 KiB

  1. 'use strict';
  2. var test = require('tape');
  3. var utils = require('../lib/utils');
  4. test('merge()', function (t) {
  5. t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
  6. var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
  7. t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
  8. var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
  9. t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
  10. var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
  11. t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
  12. var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
  13. t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
  14. var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
  15. t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
  16. t.end();
  17. });
  18. test('assign()', function (t) {
  19. var target = { a: 1, b: 2 };
  20. var source = { b: 3, c: 4 };
  21. var result = utils.assign(target, source);
  22. t.equal(result, target, 'returns the target');
  23. t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
  24. t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
  25. t.end();
  26. });
  27. test('combine()', function (t) {
  28. t.test('both arrays', function (st) {
  29. var a = [1];
  30. var b = [2];
  31. var combined = utils.combine(a, b);
  32. st.deepEqual(a, [1], 'a is not mutated');
  33. st.deepEqual(b, [2], 'b is not mutated');
  34. st.notEqual(a, combined, 'a !== combined');
  35. st.notEqual(b, combined, 'b !== combined');
  36. st.deepEqual(combined, [1, 2], 'combined is a + b');
  37. st.end();
  38. });
  39. t.test('one array, one non-array', function (st) {
  40. var aN = 1;
  41. var a = [aN];
  42. var bN = 2;
  43. var b = [bN];
  44. var combinedAnB = utils.combine(aN, b);
  45. st.deepEqual(b, [bN], 'b is not mutated');
  46. st.notEqual(aN, combinedAnB, 'aN + b !== aN');
  47. st.notEqual(a, combinedAnB, 'aN + b !== a');
  48. st.notEqual(bN, combinedAnB, 'aN + b !== bN');
  49. st.notEqual(b, combinedAnB, 'aN + b !== b');
  50. st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
  51. var combinedABn = utils.combine(a, bN);
  52. st.deepEqual(a, [aN], 'a is not mutated');
  53. st.notEqual(aN, combinedABn, 'a + bN !== aN');
  54. st.notEqual(a, combinedABn, 'a + bN !== a');
  55. st.notEqual(bN, combinedABn, 'a + bN !== bN');
  56. st.notEqual(b, combinedABn, 'a + bN !== b');
  57. st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
  58. st.end();
  59. });
  60. t.test('neither is an array', function (st) {
  61. var combined = utils.combine(1, 2);
  62. st.notEqual(1, combined, '1 + 2 !== 1');
  63. st.notEqual(2, combined, '1 + 2 !== 2');
  64. st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
  65. st.end();
  66. });
  67. t.end();
  68. });