From 5100ee82c2ed1c7657bd066b7c380554645554df Mon Sep 17 00:00:00 2001 From: James Halliday <mail@substack.net> Date: Wed, 20 Mar 2013 12:49:11 -0700 Subject: [PATCH] inline all the core Buffer results so that the tests can run in browsers --- test/buffer.js | 107 ++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/test/buffer.js b/test/buffer.js index 53d8708..93b08b4 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -1,11 +1,11 @@ -var buffer = require('../index.js'); +var B = require('../index.js').Buffer; var test = require('tape'); test('utf8 buffer to base64', function (t) { t.plan(1); t.equal( - new buffer.Buffer("Õ×ab", "utf8").toString("base64"), - new Buffer("Õ×ab", "utf8").toString("base64") + new B("Õ×ab", "utf8").toString("base64"), + '1YHXkGFi' ); t.end(); }); @@ -13,8 +13,8 @@ test('utf8 buffer to base64', function (t) { test('utf8 buffer to hex', function (t) { t.plan(1); t.equal( - new buffer.Buffer("Õ×ab", "utf8").toString("hex"), - new Buffer("Õ×ab", "utf8").toString("hex") + new B("Õ×ab", "utf8").toString("hex"), + 'd581d7906162' ); t.end(); }); @@ -22,8 +22,8 @@ test('utf8 buffer to hex', function (t) { test('utf8 to utf8', function (t) { t.plan(1); t.equal( - new buffer.Buffer("öäüõÃÃÃÃ", "utf8").toString("utf8"), - new Buffer("öäüõÃÃÃÃ", "utf8").toString("utf8") + new B("öäüõÃÃÃÃ", "utf8").toString("utf8"), + 'öäüõÃÃÃÃ' ); t.end(); }); @@ -31,8 +31,8 @@ test('utf8 to utf8', function (t) { test('ascii buffer to base64', function (t) { t.plan(1); t.equal( - new buffer.Buffer("123456!@#$%^", "ascii").toString("base64"), - new Buffer("123456!@#$%^", "ascii").toString("base64") + new B("123456!@#$%^", "ascii").toString("base64"), + 'MTIzNDU2IUAjJCVe' ); t.end(); }); @@ -40,8 +40,8 @@ test('ascii buffer to base64', function (t) { test('ascii buffer to hex', function (t) { t.plan(1); t.equal( - new buffer.Buffer("123456!@#$%^", "ascii").toString("hex"), - new Buffer("123456!@#$%^", "ascii").toString("hex") + new B("123456!@#$%^", "ascii").toString("hex"), + '31323334353621402324255e' ); t.end(); }); @@ -49,8 +49,8 @@ test('ascii buffer to hex', function (t) { test('base64 buffer to utf8', function (t) { t.plan(1); t.equal( - new buffer.Buffer("1YHXkGFi", "base64").toString("utf8"), - new Buffer("1YHXkGFi", "base64").toString("utf8") + new B("1YHXkGFi", "base64").toString("utf8"), + 'Õ×ab' ); t.end(); }); @@ -58,8 +58,8 @@ test('base64 buffer to utf8', function (t) { test('hex buffer to utf8', function (t) { t.plan(1); t.equal( - new buffer.Buffer("d581d7906162", "hex").toString("utf8"), - new Buffer("d581d7906162", "hex").toString("utf8") + new B("d581d7906162", "hex").toString("utf8"), + 'Õ×ab' ); t.end(); }); @@ -67,8 +67,8 @@ test('hex buffer to utf8', function (t) { test('base64 buffer to ascii', function (t) { t.plan(1); t.equal( - new buffer.Buffer("MTIzNDU2IUAjJCVe", "base64").toString("ascii"), - new Buffer("MTIzNDU2IUAjJCVe", "base64").toString("ascii") + new B("MTIzNDU2IUAjJCVe", "base64").toString("ascii"), + '123456!@#$%^' ); t.end(); }); @@ -76,8 +76,8 @@ test('base64 buffer to ascii', function (t) { test('hex buffer to ascii', function (t) { t.plan(1); t.equal( - new buffer.Buffer("31323334353621402324255e", "hex").toString("ascii"), - new Buffer("31323334353621402324255e", "hex").toString("ascii") + new B("31323334353621402324255e", "hex").toString("ascii"), + '123456!@#$%^' ); t.end(); }); @@ -85,7 +85,7 @@ test('hex buffer to ascii', function (t) { test('utf8 to ascii', function (t) { t.plan(1); t.equal( - new buffer.Buffer("öäüõÃÃÃÃ", "utf8").toString("ascii"), + new B("öäüõÃÃÃÃ", "utf8").toString("ascii"), new Buffer("öäüõÃÃÃÃ", "utf8").toString("ascii") ); t.end(); @@ -95,8 +95,8 @@ test('utf8 to ascii', function (t) { test('base64 buffer to binary', function (t) { t.plan(1); t.equal( - new buffer.Buffer("MTIzNDU2IUAjJCVe", "base64").toString("binary"), - new Buffer("MTIzNDU2IUAjJCVe", "base64").toString("binary") + new B("MTIzNDU2IUAjJCVe", "base64").toString("binary"), + '123456!@#$%^' ); t.end(); }); @@ -104,8 +104,8 @@ test('base64 buffer to binary', function (t) { test('hex buffer to binary', function (t) { t.plan(1); t.equal( - new buffer.Buffer("31323334353621402324255e", "hex").toString("binary"), - new Buffer("31323334353621402324255e", "hex").toString("binary") + new B("31323334353621402324255e", "hex").toString("binary"), + '123456!@#$%^' ); t.end(); }); @@ -113,32 +113,35 @@ test('hex buffer to binary', function (t) { test('utf8 to binary', function (t) { t.plan(1); t.equal( - new buffer.Buffer("öäüõÃÃÃÃ", "utf8").toString("binary"), - new Buffer("öäüõÃÃÃÃ", "utf8").toString("binary") + new B("öäüõÃÃÃÃ", "utf8").toString("binary"), + "öäüõÃÂÃÂÃÂÃÂ" ); t.end(); }); test("hex of write{Uint,Int}{8,16,32}{LE,BE}", function (t) { t.plan(2*(2*2*2+2)); + var hex = [ + "03", "0300", "0003", "03000000", "00000003", + "fd", "fdff", "fffd", "fdffffff", "fffffffd" + ]; + var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ]; ["UInt","Int"].forEach(function(x){ [8,16,32].forEach(function(y){ var endianesses = (y === 8) ? [""] : ["LE","BE"]; endianesses.forEach(function(z){ - var v1 = new buffer.Buffer(y / 8); - var v2 = new Buffer(y / 8); + var v1 = new B(y / 8); var writefn = "write" + x + y + z; var val = (x === "Int") ? -3 : 3; v1[writefn](val, 0); - v2[writefn](val, 0); t.equal( v1.toString("hex"), - v2.toString("hex") + hex.shift() ); var readfn = "read" + x + y + z; t.equal( v1[readfn](0), - v2[readfn](0) + reads.shift() ); }); }); @@ -148,21 +151,27 @@ test("hex of write{Uint,Int}{8,16,32}{LE,BE}", function (t) { test("hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow", function (t) { t.plan(3*(2*2*2+2)); + var hex = [ + "", "03", "00", "030000", "000000", + "", "fd", "ff", "fdffff", "ffffff" + ]; + var reads = [ + undefined, 3, 0, 3, 0, + undefined, 253, -256, 16777213, -256 + ]; ["UInt","Int"].forEach(function(x){ [8,16,32].forEach(function(y){ var endianesses = (y === 8) ? [""] : ["LE","BE"]; endianesses.forEach(function(z){ - var v1 = new buffer.Buffer(y / 8 - 1); - var v2 = new Buffer(y / 8 - 1); - var next = new buffer.Buffer(4); + var v1 = new B(y / 8 - 1); + var next = new B(4); next.writeUInt32BE(0, 0); var writefn = "write" + x + y + z; var val = (x === "Int") ? -3 : 3; v1[writefn](val, 0, true); - v2[writefn](val, 0, true); t.equal( v1.toString("hex"), - v2.toString("hex") + hex.shift() ); // check that nothing leaked to next buffer. t.equal(next.readUInt32BE(0), 0); @@ -171,7 +180,7 @@ test("hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow", function (t) { var readfn = "read" + x + y + z; t.equal( v1[readfn](0, true), - v2[readfn](0, true) + reads.shift() ); }); }); @@ -182,14 +191,14 @@ test("hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow", function (t) { test("concat() a varying number of buffers", function (t) { t.plan(5); var zero = []; - var one = [ new buffer.Buffer('asdf') ]; + var one = [ new B('asdf') ]; var long = []; - for (var i = 0; i < 10; i++) long.push(new buffer.Buffer('asdf')); + for (var i = 0; i < 10; i++) long.push(new B('asdf')); - var flatZero = buffer.Buffer.concat(zero); - var flatOne = buffer.Buffer.concat(one); - var flatLong = buffer.Buffer.concat(long); - var flatLongLen = buffer.Buffer.concat(long, 40); + var flatZero = B.concat(zero); + var flatOne = B.concat(one); + var flatLong = B.concat(long); + var flatLongLen = B.concat(long, 40); t.equal(flatZero.length, 0); t.equal(flatOne.toString(), 'asdf'); @@ -201,18 +210,16 @@ test("concat() a varying number of buffers", function (t) { test("buffer from buffer", function (t) { t.plan(1); - var b1 = new buffer.Buffer('asdf'); - var b2 = new buffer.Buffer(b1); + var b1 = new B('asdf'); + var b2 = new B(b1); t.equal(b1.toString('hex'), b2.toString('hex')); t.end(); }); test("fill", function(t) { t.plan(1); - var b1 = new Buffer(10); - var b2 = new buffer.Buffer(10); - b1.fill(2); - b2.fill(2); - t.equal(b1.toString('hex'), b2.toString('hex')); + var b = new B(10); + b.fill(2); + t.equal(b.toString('hex'), '02020202020202020202'); t.end(); }); -- 2.34.1