From: Feross Aboukhadijeh Date: Thu, 11 Sep 2014 21:42:31 +0000 (+0100) Subject: organize tests better X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=0e94a40370475cd364214b1c12c15570d4451b5c;p=buffer.git organize tests better --- diff --git a/test/base64-newline.js b/test/base64.js similarity index 60% rename from test/base64-newline.js rename to test/base64.js index 4722aec..e5ecf25 100644 --- a/test/base64-newline.js +++ b/test/base64.js @@ -1,28 +1,39 @@ var B = require('../').Buffer var test = require('tape') -var data = [ - '', - '' -] +test('base64: ignore whitespace', function (t) { + var text = '\n YW9ldQ== ' + var buf = new B(text, 'base64') + t.equal(buf.toString(), 'aoeu') + t.end() +}) + +test('base64: strings without padding', function (t) { + t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu') + t.end() +}) -test('base64 strings with newlines / invalid charaters', function (t) { - // newline in utf8 -- should not be an issue +test('base64: newline in utf8 -- should not be an issue', function (t) { t.equal( new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK', 'base64').toString('utf8'), '---\ntitle: Three dashes marks the spot\ntags:\n' ) + t.end() +}) - // newline in base64 -- should get stripped +test('base64: newline in base64 -- should get stripped', function (t) { t.equal( new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'), '---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-' ) + t.end() +}) - // tab characters in base64 - should get stripped +test('base64: tab characters in base64 - should get stripped', function (t) { t.equal( new B('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\t\t\t\tICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt', 'base64').toString('utf8'), '---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-' ) t.end() }) + diff --git a/test/basic.js b/test/basic.js index 2f79d19..5ebb306 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,194 +1,29 @@ var B = require('../').Buffer var test = require('tape') -test('new buffer from array', function (t) { - t.equal( - new B([1, 2, 3]).toString(), - '\u0001\u0002\u0003' - ) +test('indexes from a string', function(t) { + var buf = new B('abc') + t.equal(buf[0], 97) + t.equal(buf[1], 98) + t.equal(buf[2], 99) t.end() }) -test('new buffer from array w/ negatives', function (t) { - t.equal( - new B([-1, -2, -3]).toString('hex'), - 'fffefd' - ) +test('indexes from an array', function(t) { + var buf = new B([ 97, 98, 99 ]) + t.equal(buf[0], 97) + t.equal(buf[1], 98) + t.equal(buf[2], 99) t.end() }) -test('new buffer from array with mixed signed input', function (t) { - t.equal( - new B([-255, 255, -128, 128, 512, -512, 511, -511]).toString('hex'), - '01ff80800000ff01' - ) - t.end() -}) - -test('new buffer from string', function (t) { - t.equal( - new B('hey', 'utf8').toString(), - 'hey' - ) - t.end() -}) - -test('new buffer from buffer', function (t) { - var b1 = new B('asdf') - var b2 = new B(b1) - t.equal(b1.toString('hex'), b2.toString('hex')) - t.end() -}) - -test('new buffer from uint8array', function (t) { - if (typeof Uint8Array !== 'undefined') { - var b1 = new Uint8Array([0, 1, 2, 3]) - var b2 = new B(b1) - t.equal(b1.length, b2.length) - t.equal(b1[0], 0) - t.equal(b1[1], 1) - t.equal(b1[2], 2) - t.equal(b1[3], 3) - t.equal(b1[4], undefined) - } - t.end() -}) - -test('new buffer from uint16array', function (t) { - if (typeof Uint16Array !== 'undefined') { - var b1 = new Uint16Array([0, 1, 2, 3]) - var b2 = new B(b1) - t.equal(b1.length, b2.length) - t.equal(b1[0], 0) - t.equal(b1[1], 1) - t.equal(b1[2], 2) - t.equal(b1[3], 3) - t.equal(b1[4], undefined) - } - t.end() -}) - -test('new buffer from uint32array', function (t) { - if (typeof Uint32Array !== 'undefined') { - var b1 = new Uint32Array([0, 1, 2, 3]) - var b2 = new B(b1) - t.equal(b1.length, b2.length) - t.equal(b1[0], 0) - t.equal(b1[1], 1) - t.equal(b1[2], 2) - t.equal(b1[3], 3) - t.equal(b1[4], undefined) - } - t.end() -}) - -test('new buffer from int16array', function (t) { - if (typeof Int16Array !== 'undefined') { - var b1 = new Int16Array([0, 1, 2, 3]) - var b2 = new B(b1) - t.equal(b1.length, b2.length) - t.equal(b1[0], 0) - t.equal(b1[1], 1) - t.equal(b1[2], 2) - t.equal(b1[3], 3) - t.equal(b1[4], undefined) - } - t.end() -}) - -test('new buffer from int32array', function (t) { - if (typeof Int32Array !== 'undefined') { - var b1 = new Int32Array([0, 1, 2, 3]) - var b2 = new B(b1) - t.equal(b1.length, b2.length) - t.equal(b1[0], 0) - t.equal(b1[1], 1) - t.equal(b1[2], 2) - t.equal(b1[3], 3) - t.equal(b1[4], undefined) - } - t.end() -}) - -test('new buffer from float32array', function (t) { - if (typeof Float32Array !== 'undefined') { - var b1 = new Float32Array([0, 1, 2, 3]) - var b2 = new B(b1) - t.equal(b1.length, b2.length) - t.equal(b1[0], 0) - t.equal(b1[1], 1) - t.equal(b1[2], 2) - t.equal(b1[3], 3) - t.equal(b1[4], undefined) - } - t.end() -}) - -test('new buffer from float64array', function (t) { - if (typeof Float64Array !== 'undefined') { - var b1 = new Float64Array([0, 1, 2, 3]) - var b2 = new B(b1) - t.equal(b1.length, b2.length) - t.equal(b1[0], 0) - t.equal(b1[1], 1) - t.equal(b1[2], 2) - t.equal(b1[3], 3) - t.equal(b1[4], undefined) - } - t.end() -}) - -test('buffer toArrayBuffer()', function (t) { - var data = [1, 2, 3, 4, 5, 6, 7, 8] - if (typeof Uint8Array !== 'undefined') { - var result = new B(data).toArrayBuffer() - var expected = new Uint8Array(data).buffer - for (var i = 0; i < expected.byteLength; i++) { - t.equal(result[i], expected[i]) - } - } else { - t.pass('No toArrayBuffer() method provided in old browsers') - } - t.end() -}) - -test('buffer toJSON()', function (t) { - var data = [1, 2, 3, 4] - t.deepEqual( - new B(data).toJSON(), - { type: 'Buffer', data: [1,2,3,4] } - ) - t.end() -}) - -test('new buffer from buffer.toJSON() output', function (t) { - if (typeof JSON === 'undefined') { - // ie6, ie7 lack support - t.end() - return - } - var buf = new B('test') - var json = JSON.stringify(buf) - var obj = JSON.parse(json) - var copy = new B(obj) - t.ok(buf.equals(copy)) - t.end() -}) - -test('buffer copy example', function (t) { - var buf1 = new B(26) - var buf2 = new B(26) - - for (var i = 0 ; i < 26 ; i++) { - buf1[i] = i + 97; // 97 is ASCII a - buf2[i] = 33; // ASCII ! - } - - buf1.copy(buf2, 8, 16, 20) +test('setting index value should modify buffer contents', function(t) { + var buf = new B([ 97, 98, 99 ]) + t.equal(buf[2], 99) + t.equal(buf.toString(), 'abc') - t.equal( - buf2.toString('ascii', 0, 25), - '!!!!!!!!qrst!!!!!!!!!!!!!' - ) + buf[2] += 10 + t.equal(buf[2], 109) + t.equal(buf.toString(), 'abm') t.end() }) diff --git a/test/compare.js b/test/compare.js index 75ba3b9..5d4d76c 100644 --- a/test/compare.js +++ b/test/compare.js @@ -1,7 +1,7 @@ var B = require('../').Buffer var test = require('tape') -test('compare', function (t) { +test('buffer.compare', function (t) { var b = new B(1).fill('a') var c = new B(1).fill('c') var d = new B(2).fill('aa') @@ -19,11 +19,7 @@ test('compare', function (t) { t.end() }) -test('compare argument validation', function (t) { - var b = new B(1).fill('a') - var c = new B(1).fill('c') - var d = new B(2).fill('aa') - +test('buffer.compare argument validation', function (t) { t.throws(function () { var b = new B(1) B.compare(b, 'abc') @@ -41,7 +37,7 @@ test('compare argument validation', function (t) { t.end() }) -test('equals', function (t) { +test('buffer.equals', function (t) { var b = new B(5).fill('abcdf') var c = new B(5).fill('abcdf') var d = new B(5).fill('abcde') @@ -53,12 +49,7 @@ test('equals', function (t) { t.end() }) -test('equals argument validation', function (t) { - var b = new B(5).fill('abcdf') - var c = new B(5).fill('abcdf') - var d = new B(5).fill('abcde') - var e = new B(6).fill('abcdef') - +test('buffer.equals argument validation', function (t) { t.throws(function () { var b = new B(1) b.equals('abc') diff --git a/test/constructor.js b/test/constructor.js new file mode 100644 index 0000000..1508f1f --- /dev/null +++ b/test/constructor.js @@ -0,0 +1,154 @@ +var B = require('../').Buffer +var test = require('tape') + +test('new buffer from array', function (t) { + t.equal( + new B([1, 2, 3]).toString(), + '\u0001\u0002\u0003' + ) + t.end() +}) + +test('new buffer from array w/ negatives', function (t) { + t.equal( + new B([-1, -2, -3]).toString('hex'), + 'fffefd' + ) + t.end() +}) + +test('new buffer from array with mixed signed input', function (t) { + t.equal( + new B([-255, 255, -128, 128, 512, -512, 511, -511]).toString('hex'), + '01ff80800000ff01' + ) + t.end() +}) + +test('new buffer from string', function (t) { + t.equal( + new B('hey', 'utf8').toString(), + 'hey' + ) + t.end() +}) + +test('new buffer from buffer', function (t) { + var b1 = new B('asdf') + var b2 = new B(b1) + t.equal(b1.toString('hex'), b2.toString('hex')) + t.end() +}) + +test('new buffer from Uint8Array', function (t) { + if (typeof Uint8Array !== 'undefined') { + var b1 = new Uint8Array([0, 1, 2, 3]) + var b2 = new B(b1) + t.equal(b1.length, b2.length) + t.equal(b1[0], 0) + t.equal(b1[1], 1) + t.equal(b1[2], 2) + t.equal(b1[3], 3) + t.equal(b1[4], undefined) + } + t.end() +}) + +test('new buffer from Uint16Array', function (t) { + if (typeof Uint16Array !== 'undefined') { + var b1 = new Uint16Array([0, 1, 2, 3]) + var b2 = new B(b1) + t.equal(b1.length, b2.length) + t.equal(b1[0], 0) + t.equal(b1[1], 1) + t.equal(b1[2], 2) + t.equal(b1[3], 3) + t.equal(b1[4], undefined) + } + t.end() +}) + +test('new buffer from Uint32Array', function (t) { + if (typeof Uint32Array !== 'undefined') { + var b1 = new Uint32Array([0, 1, 2, 3]) + var b2 = new B(b1) + t.equal(b1.length, b2.length) + t.equal(b1[0], 0) + t.equal(b1[1], 1) + t.equal(b1[2], 2) + t.equal(b1[3], 3) + t.equal(b1[4], undefined) + } + t.end() +}) + +test('new buffer from Int16Array', function (t) { + if (typeof Int16Array !== 'undefined') { + var b1 = new Int16Array([0, 1, 2, 3]) + var b2 = new B(b1) + t.equal(b1.length, b2.length) + t.equal(b1[0], 0) + t.equal(b1[1], 1) + t.equal(b1[2], 2) + t.equal(b1[3], 3) + t.equal(b1[4], undefined) + } + t.end() +}) + +test('new buffer from Int32Array', function (t) { + if (typeof Int32Array !== 'undefined') { + var b1 = new Int32Array([0, 1, 2, 3]) + var b2 = new B(b1) + t.equal(b1.length, b2.length) + t.equal(b1[0], 0) + t.equal(b1[1], 1) + t.equal(b1[2], 2) + t.equal(b1[3], 3) + t.equal(b1[4], undefined) + } + t.end() +}) + +test('new buffer from Float32Array', function (t) { + if (typeof Float32Array !== 'undefined') { + var b1 = new Float32Array([0, 1, 2, 3]) + var b2 = new B(b1) + t.equal(b1.length, b2.length) + t.equal(b1[0], 0) + t.equal(b1[1], 1) + t.equal(b1[2], 2) + t.equal(b1[3], 3) + t.equal(b1[4], undefined) + } + t.end() +}) + +test('new buffer from Float64Array', function (t) { + if (typeof Float64Array !== 'undefined') { + var b1 = new Float64Array([0, 1, 2, 3]) + var b2 = new B(b1) + t.equal(b1.length, b2.length) + t.equal(b1[0], 0) + t.equal(b1[1], 1) + t.equal(b1[2], 2) + t.equal(b1[3], 3) + t.equal(b1[4], undefined) + } + t.end() +}) + +test('new buffer from buffer.toJSON() output', function (t) { + if (typeof JSON === 'undefined') { + // ie6, ie7 lack support + t.end() + return + } + var buf = new B('test') + var json = JSON.stringify(buf) + var obj = JSON.parse(json) + var copy = new B(obj) + t.ok(buf.equals(copy)) + t.end() +}) + diff --git a/test/indexes.js b/test/indexes.js deleted file mode 100644 index 783bfa5..0000000 --- a/test/indexes.js +++ /dev/null @@ -1,29 +0,0 @@ -var B = require('../').Buffer -var test = require('tape') - -test('indexes from a string', function(t) { - var buf = new B('abc') - t.equal(buf[0], 97) - t.equal(buf[1], 98) - t.equal(buf[2], 99) - t.end() -}) - -test('indexes from an array', function(t) { - var buf = new B([ 97, 98, 99 ]) - t.equal(buf[0], 97) - t.equal(buf[1], 98) - t.equal(buf[2], 99) - t.end() -}) - -test('set then modify indexes from an array', function(t) { - var buf = new B([ 97, 98, 99 ]) - t.equal(buf[2], 99) - t.equal(buf.toString(), 'abc') - - buf[2] += 10 - t.equal(buf[2], 109) - t.equal(buf.toString(), 'abm') - t.end() -}) diff --git a/test/is-buffer.js b/test/is-buffer.js deleted file mode 100644 index 8fba076..0000000 --- a/test/is-buffer.js +++ /dev/null @@ -1,9 +0,0 @@ -var B = require('../').Buffer -var test = require('tape') - -test('Buffer.isBuffer', function (t) { - t.equal(B.isBuffer(new B('hey', 'utf8')), true) - t.equal(B.isBuffer(new B([1, 2, 3], 'utf8')), true) - t.equal(B.isBuffer('hey'), false) - t.end() -}) diff --git a/test/is-encoding.js b/test/is-encoding.js deleted file mode 100644 index b408434..0000000 --- a/test/is-encoding.js +++ /dev/null @@ -1,9 +0,0 @@ -var B = require('../').Buffer -var test = require('tape') - -test('Buffer.isEncoding', function (t) { - t.equal(B.isEncoding('HEX'), true) - t.equal(B.isEncoding('hex'), true) - t.equal(B.isEncoding('bad'), false) - t.end() -}) diff --git a/test/buffer.js b/test/methods.js similarity index 64% rename from test/buffer.js rename to test/methods.js index 24b3f66..1cbc172 100644 --- a/test/buffer.js +++ b/test/methods.js @@ -1,114 +1,30 @@ var B = require('../').Buffer var test = require('tape') -test('utf8 buffer to base64', function (t) { - t.equal( - new B('Ձאab', 'utf8').toString('base64'), - '1YHXkGFi' - ) - t.end() -}) - -test('utf8 buffer to hex', function (t) { - t.equal( - new B('Ձאab', 'utf8').toString('hex'), - 'd581d7906162' - ) - t.end() -}) - -test('utf8 to utf8', function (t) { - t.equal( - new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'), - 'öäüõÖÄÜÕ' +test('buffer.toJSON', function (t) { + var data = [1, 2, 3, 4] + t.deepEqual( + new B(data).toJSON(), + { type: 'Buffer', data: [1,2,3,4] } ) t.end() }) -test('utf16le to utf16', function (t) { - t.equal( - new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'), - 'abcd' - ) - t.end() -}) - -test('utf16le to hex', function (t) { - t.equal( - new B('abcd', 'utf16le').toString('hex'), - '6100620063006400' - ) - t.end() -}) - -test('ascii buffer to base64', function (t) { - t.equal( - new B('123456!@#$%^', 'ascii').toString('base64'), - 'MTIzNDU2IUAjJCVe' - ) - t.end() -}) - -test('ascii buffer to hex', function (t) { - t.equal( - new B('123456!@#$%^', 'ascii').toString('hex'), - '31323334353621402324255e' - ) - t.end() -}) - -test('base64 buffer to utf8', function (t) { - t.equal( - new B('1YHXkGFi', 'base64').toString('utf8'), - 'Ձאab' - ) - t.end() -}) +test('buffer.copy', function (t) { + // copied from nodejs.org example + var buf1 = new B(26) + var buf2 = new B(26) -test('hex buffer to utf8', function (t) { - t.equal( - new B('d581d7906162', 'hex').toString('utf8'), - 'Ձאab' - ) - t.end() -}) - -test('base64 buffer to ascii', function (t) { - t.equal( - new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'), - '123456!@#$%^' - ) - t.end() -}) - -test('hex buffer to ascii', function (t) { - t.equal( - new B('31323334353621402324255e', 'hex').toString('ascii'), - '123456!@#$%^' - ) - t.end() -}) - -test('base64 buffer to binary', function (t) { - t.equal( - new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'), - '123456!@#$%^' - ) - t.end() -}) + for (var i = 0 ; i < 26 ; i++) { + buf1[i] = i + 97; // 97 is ASCII a + buf2[i] = 33; // ASCII ! + } -test('hex buffer to binary', function (t) { - t.equal( - new B('31323334353621402324255e', 'hex').toString('binary'), - '123456!@#$%^' - ) - t.end() -}) + buf1.copy(buf2, 8, 16, 20) -test('utf8 to binary', function (t) { t.equal( - new B('öäüõÖÄÜÕ', 'utf8').toString('binary'), - 'öäüõÖÄÜÕ' + buf2.toString('ascii', 0, 25), + '!!!!!!!!qrst!!!!!!!!!!!!!' ) t.end() }) @@ -264,13 +180,6 @@ test('copy() after slice()', function (t) { t.end() }) -test('base64 ignore whitespace', function (t) { - var text = '\n YW9ldQ== ' - var buf = new B(text, 'base64') - t.equal(buf.toString(), 'aoeu') - t.end() -}) - test('buffer.slice sets indexes', function (t) { t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo') t.end() @@ -282,8 +191,3 @@ test('buffer.slice out of range', function (t) { t.equal((new B('hallo')).slice(10, 2).toString(), '') t.end() }) - -test('base64 strings without padding', function (t) { - t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu') - t.end() -}) diff --git a/test/static.js b/test/static.js new file mode 100644 index 0000000..4dc90f2 --- /dev/null +++ b/test/static.js @@ -0,0 +1,30 @@ +var B = require('../').Buffer +var test = require('tape') + +test('Buffer.isEncoding', function (t) { + t.equal(B.isEncoding('HEX'), true) + t.equal(B.isEncoding('hex'), true) + t.equal(B.isEncoding('bad'), false) + t.end() +}) + +test('Buffer.isBuffer', function (t) { + t.equal(B.isBuffer(new B('hey', 'utf8')), true) + t.equal(B.isBuffer(new B([1, 2, 3], 'utf8')), true) + t.equal(B.isBuffer('hey'), false) + t.end() +}) + +test('Buffer.toArrayBuffer', function (t) { + var data = [1, 2, 3, 4, 5, 6, 7, 8] + if (typeof Uint8Array !== 'undefined') { + var result = new B(data).toArrayBuffer() + var expected = new Uint8Array(data).buffer + for (var i = 0; i < expected.byteLength; i++) { + t.equal(result[i], expected[i]) + } + } else { + t.pass('No toArrayBuffer() method provided in old browsers') + } + t.end() +}) diff --git a/test/to-string.js b/test/to-string.js new file mode 100644 index 0000000..714ffd7 --- /dev/null +++ b/test/to-string.js @@ -0,0 +1,114 @@ +var B = require('../').Buffer +var test = require('tape') + +test('utf8 buffer to base64', function (t) { + t.equal( + new B('Ձאab', 'utf8').toString('base64'), + '1YHXkGFi' + ) + t.end() +}) + +test('utf8 buffer to hex', function (t) { + t.equal( + new B('Ձאab', 'utf8').toString('hex'), + 'd581d7906162' + ) + t.end() +}) + +test('utf8 to utf8', function (t) { + t.equal( + new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'), + 'öäüõÖÄÜÕ' + ) + t.end() +}) + +test('utf16le to utf16', function (t) { + t.equal( + new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'), + 'abcd' + ) + t.end() +}) + +test('utf16le to hex', function (t) { + t.equal( + new B('abcd', 'utf16le').toString('hex'), + '6100620063006400' + ) + t.end() +}) + +test('ascii buffer to base64', function (t) { + t.equal( + new B('123456!@#$%^', 'ascii').toString('base64'), + 'MTIzNDU2IUAjJCVe' + ) + t.end() +}) + +test('ascii buffer to hex', function (t) { + t.equal( + new B('123456!@#$%^', 'ascii').toString('hex'), + '31323334353621402324255e' + ) + t.end() +}) + +test('base64 buffer to utf8', function (t) { + t.equal( + new B('1YHXkGFi', 'base64').toString('utf8'), + 'Ձאab' + ) + t.end() +}) + +test('hex buffer to utf8', function (t) { + t.equal( + new B('d581d7906162', 'hex').toString('utf8'), + 'Ձאab' + ) + t.end() +}) + +test('base64 buffer to ascii', function (t) { + t.equal( + new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'), + '123456!@#$%^' + ) + t.end() +}) + +test('hex buffer to ascii', function (t) { + t.equal( + new B('31323334353621402324255e', 'hex').toString('ascii'), + '123456!@#$%^' + ) + t.end() +}) + +test('base64 buffer to binary', function (t) { + t.equal( + new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'), + '123456!@#$%^' + ) + t.end() +}) + +test('hex buffer to binary', function (t) { + t.equal( + new B('31323334353621402324255e', 'hex').toString('binary'), + '123456!@#$%^' + ) + t.end() +}) + +test('utf8 to binary', function (t) { + t.equal( + new B('öäüõÖÄÜÕ', 'utf8').toString('binary'), + 'öäüõÖÄÜÕ' + ) + t.end() +})