test-buffer-concat.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var zero = [];
  5. var one = [ Buffer.from('asdf') ];
  6. var long = [];
  7. for (var i = 0; i < 10; i++) long.push(Buffer.from('asdf'));
  8. var flatZero = Buffer.concat(zero);
  9. var flatOne = Buffer.concat(one);
  10. var flatLong = Buffer.concat(long);
  11. var flatLongLen = Buffer.concat(long, 40);
  12. assert(flatZero.length === 0);
  13. assert(flatOne.toString() === 'asdf');
  14. // A special case where concat used to return the first item,
  15. // if the length is one. This check is to make sure that we don't do that.
  16. assert(flatOne !== one[0]);
  17. assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
  18. assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
  19. assertWrongList();
  20. assertWrongList(null);
  21. assertWrongList(Buffer.from('hello'));
  22. assertWrongList([42]);
  23. assertWrongList(['hello', 'world']);
  24. assertWrongList(['hello', Buffer.from('world')]);
  25. function assertWrongList(value) {
  26. assert.throws(function() {
  27. Buffer.concat(value);
  28. }, function(err) {
  29. return err instanceof TypeError &&
  30. err.message === '"list" argument must be an Array of Buffers';
  31. });
  32. }