From: Feross Aboukhadijeh Date: Tue, 30 Jun 2015 22:32:53 +0000 (-0700) Subject: update node tests X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=8a0e7a3cb83a3b377d8d52974bce95d6f4a31908;p=buffer.git update node tests --- diff --git a/bin/download-node-tests.js b/bin/download-node-tests.js index 9b7a4d6..b871724 100755 --- a/bin/download-node-tests.js +++ b/bin/download-node-tests.js @@ -38,7 +38,7 @@ function downloadBufferTests (dir, files) { if (file.name !== 'test-buffer-iterator.js') { path = __dirname + '/../test/node/' + file.name } else { - path = __dirname + '/../test/es6/' + file.name + path = __dirname + '/../test/node-es6/' + file.name } hyperquest(file.download_url, httpOpts) diff --git a/package.json b/package.json index 412bed3..368dd13 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "test": "standard && node ./bin/test.js", "test-browser": "zuul -- test/*.js test/node/*.js", "test-browser-local": "zuul --local -- test/*.js test/node/*.js", - "test-node": "tape test/*.js test/node/*.js test/es6/*.js && OBJECT_IMPL=true tape test/*.js test/node/*.js", + "test-node": "tape test/*.js test/node/*.js test/node-es6/*.js && OBJECT_IMPL=true tape test/*.js test/node/*.js", "perf": "browserify --debug perf/bracket-notation.js > perf/bundle.js && open perf/index.html", "perf-node": "node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js", "size": "browserify -r ./ | uglifyjs -c -m | gzip | wc -c" @@ -59,7 +59,7 @@ "standard": { "ignore": [ "test/node/*.js", - "test/es6/*.js", + "test/node-es6/*.js", "test/_polyfill.js", "perf/*.js" ] diff --git a/test/es6/README.txt b/test/es6/README.txt deleted file mode 100644 index 933dd6f..0000000 --- a/test/es6/README.txt +++ /dev/null @@ -1 +0,0 @@ -io.js/node.js core buffer tests that require ES6 (for..of construct) diff --git a/test/node-es6/README.txt b/test/node-es6/README.txt new file mode 100644 index 0000000..94199ff --- /dev/null +++ b/test/node-es6/README.txt @@ -0,0 +1 @@ +node buffer tests that require ES6 (e.g. "for..of" construct) diff --git a/test/es6/test-buffer-iterator.js b/test/node-es6/test-buffer-iterator.js similarity index 87% rename from test/es6/test-buffer-iterator.js rename to test/node-es6/test-buffer-iterator.js index 0dae63b..1c316d8 100644 --- a/test/es6/test-buffer-iterator.js +++ b/test/node-es6/test-buffer-iterator.js @@ -1,5 +1,6 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-ascii.js b/test/node/test-buffer-ascii.js index 9e22a1f..40e6ee5 100644 --- a/test/node/test-buffer-ascii.js +++ b/test/node/test-buffer-ascii.js @@ -1,5 +1,6 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-big.js b/test/node/test-buffer-big.js index 858ca3f..777233d 100644 --- a/test/node/test-buffer-big.js +++ b/test/node/test-buffer-big.js @@ -1,11 +1,12 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); // The tests below should throw an error, not abort the process... -assert.throws(function() { new Buffer(0x3fffffff + 1) }, RangeError); -// assert.throws(function() { new Int8Array(0x3fffffff + 1) }, RangeError); -// assert.throws(function() { new ArrayBuffer(0x3fffffff + 1) }, RangeError); -// assert.throws(function() { new Float64Array(0x7ffffff + 1) }, RangeError); +assert.throws(function() { new Buffer(0x3fffffff + 1); }, RangeError); +// assert.throws(function() { new Int8Array(0x3fffffff + 1); }, RangeError); +// assert.throws(function() { new ArrayBuffer(0x3fffffff + 1); }, RangeError); +// assert.throws(function() { new Float64Array(0x7ffffff + 1); }, RangeError); diff --git a/test/node/test-buffer-bytelength.js b/test/node/test-buffer-bytelength.js new file mode 100644 index 0000000..7879eff --- /dev/null +++ b/test/node/test-buffer-bytelength.js @@ -0,0 +1,49 @@ +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; + +var common = {}; +var assert = require('assert'); +var Buffer = require('../../').Buffer; + +// coerce values to string +assert.equal(Buffer.byteLength(32, 'raw'), 2); +assert.equal(Buffer.byteLength(NaN, 'utf8'), 3); +assert.equal(Buffer.byteLength({}, 'raws'), 15); +assert.equal(Buffer.byteLength(), 9); + +// special case: zero length string +assert.equal(Buffer.byteLength('', 'ascii'), 0); +assert.equal(Buffer.byteLength('', 'HeX'), 0); + +// utf8 +assert.equal(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19); +assert.equal(Buffer.byteLength('κλμνξο', 'utf8'), 12); +assert.equal(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15); +assert.equal(Buffer.byteLength('𠝹𠱓𠱸', 'UTF8'), 12); +// without an encoding, utf8 should be assumed +assert.equal(Buffer.byteLength('hey there'), 9); +assert.equal(Buffer.byteLength('𠱸挶νξ#xx :)'), 17); +assert.equal(Buffer.byteLength('hello world', ''), 11); +// it should also be assumed with unrecognized encoding +assert.equal(Buffer.byteLength('hello world', 'abc'), 11); +assert.equal(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10); + +// base64 +assert.equal(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11); +assert.equal(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14); +assert.equal(Buffer.byteLength('aGkk', 'base64'), 3); +assert.equal(Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==', + 'base64'), 25); +// special padding +assert.equal(Buffer.byteLength('aaa=', 'base64'), 2); +assert.equal(Buffer.byteLength('aaaa==', 'base64'), 3); + +assert.equal(Buffer.byteLength('Il était tué'), 14); +assert.equal(Buffer.byteLength('Il était tué', 'utf8'), 14); +assert.equal(Buffer.byteLength('Il était tué', 'ascii'), 12); +assert.equal(Buffer.byteLength('Il était tué', 'binary'), 12); +['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { + assert.equal(24, Buffer.byteLength('Il était tué', encoding)); +}); + diff --git a/test/node/test-buffer-concat.js b/test/node/test-buffer-concat.js index 5e6636f..ae04925 100644 --- a/test/node/test-buffer-concat.js +++ b/test/node/test-buffer-concat.js @@ -1,5 +1,6 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); @@ -16,8 +17,8 @@ var flatLongLen = Buffer.concat(long, 40); assert(flatZero.length === 0); assert(flatOne.toString() === 'asdf'); assert(flatOne === one[0]); -assert(flatLong.toString() === (new Array(10+1).join('asdf'))); -assert(flatLongLen.toString() === (new Array(10+1).join('asdf'))); +assert(flatLong.toString() === (new Array(10 + 1).join('asdf'))); +assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf'))); -// console.log("ok"); +// console.log('ok'); diff --git a/test/node/test-buffer-fakes.js b/test/node/test-buffer-fakes.js new file mode 100644 index 0000000..848026c --- /dev/null +++ b/test/node/test-buffer-fakes.js @@ -0,0 +1,59 @@ +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; + +var common = {}; +const assert = require('assert'); +var Buffer = require('../../').Buffer; +const Bp = Buffer.prototype; + +function FakeBuffer() { } +FakeBuffer.__proto__ = Buffer; +FakeBuffer.prototype.__proto__ = Buffer.prototype; + +const fb = new FakeBuffer(); + +assert.throws(function() { + new Buffer(fb); +}, TypeError); + +assert.throws(function() { + +Buffer.prototype; +}, TypeError); + +assert.throws(function() { + Buffer.compare(fb, new Buffer(0)); +}, TypeError); + +assert.throws(function() { + fb.write('foo'); +}, TypeError); + +assert.throws(function() { + Buffer.concat([fb, fb]); +}, TypeError); + +assert.throws(function() { + fb.toString(); +}, TypeError); + +assert.throws(function() { + fb.equals(new Buffer(0)); +}, TypeError); + +assert.throws(function() { + fb.indexOf(5); +}, TypeError); + +assert.throws(function() { + fb.readFloatLE(0); +}, TypeError); + +assert.throws(function() { + fb.writeFloatLE(0); +}, TypeError); + +assert.throws(function() { + fb.fill(0); +}, TypeError); + diff --git a/test/node/test-buffer-indexof.js b/test/node/test-buffer-indexof.js index 39d731d..8fae483 100644 --- a/test/node/test-buffer-indexof.js +++ b/test/node/test-buffer-indexof.js @@ -1,5 +1,6 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); diff --git a/test/node/test-buffer-inspect.js b/test/node/test-buffer-inspect.js index 08415a9..98ec0d1 100644 --- a/test/node/test-buffer-inspect.js +++ b/test/node/test-buffer-inspect.js @@ -1,5 +1,6 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); @@ -37,3 +38,4 @@ assert.doesNotThrow(function() { assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected); }); + diff --git a/test/node/test-buffer-slice.js b/test/node/test-buffer-slice.js index 36cb3a3..2967195 100644 --- a/test/node/test-buffer-slice.js +++ b/test/node/test-buffer-slice.js @@ -1,5 +1,6 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); @@ -9,6 +10,6 @@ var buff = new Buffer(Buffer.poolSize + 1); var slicedBuffer = buff.slice(); assert.equal(slicedBuffer.parent, buff, - "slicedBufffer should have its parent set to the original " + - " buffer"); + 'slicedBufffer should have its parent set to the original ' + + ' buffer'); diff --git a/test/node/test-buffer.js b/test/node/test-buffer.js index b9bf4b6..77932a6 100644 --- a/test/node/test-buffer.js +++ b/test/node/test-buffer.js @@ -1,5 +1,6 @@ -var Buffer = require('../../').Buffer -if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false +'use strict'; +var Buffer = require('../../').Buffer; +if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false; var common = {}; var assert = require('assert'); @@ -129,8 +130,8 @@ assert.strictEqual(Math.floor(c.length / 2), copied); for (var i = 0; i < Math.floor(c.length / 2); i++) { assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]); } -for (var i = Math.floor(c.length /2) + 1; i < c.length; i++) { - assert.strictEqual(c[c.length-1], c[i]); +for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) { + assert.strictEqual(c[c.length - 1], c[i]); } // try to copy 513 bytes, and check we don't overrun c @@ -270,37 +271,52 @@ writeTest.write('e', 3, 'ascii'); writeTest.write('j', 'ascii', 4); assert.equal(writeTest.toString(), 'nodejs'); +// ASCII slice test + var asciiString = 'hello world'; var offset = 100; -for (var j = 0; j < 500; j++) { - - for (var i = 0; i < asciiString.length; i++) { - b[i] = asciiString.charCodeAt(i); - } - var asciiSlice = b.toString('ascii', 0, asciiString.length); - assert.equal(asciiString, asciiSlice); - - var written = b.write(asciiString, offset, 'ascii'); - assert.equal(asciiString.length, written); - var asciiSlice = b.toString('ascii', offset, offset + asciiString.length); - assert.equal(asciiString, asciiSlice); - - var sliceA = b.slice(offset, offset + asciiString.length); - var sliceB = b.slice(offset, offset + asciiString.length); - for (var i = 0; i < asciiString.length; i++) { - assert.equal(sliceA[i], sliceB[i]); - } - // TODO utf8 slice tests +for (var i = 0; i < asciiString.length; i++) { + b[i] = asciiString.charCodeAt(i); +} +var asciiSlice = b.toString('ascii', 0, asciiString.length); +assert.equal(asciiString, asciiSlice); + +var written = b.write(asciiString, offset, 'ascii'); +assert.equal(asciiString.length, written); +var asciiSlice = b.toString('ascii', offset, offset + asciiString.length); +assert.equal(asciiString, asciiSlice); + +var sliceA = b.slice(offset, offset + asciiString.length); +var sliceB = b.slice(offset, offset + asciiString.length); +for (var i = 0; i < asciiString.length; i++) { + assert.equal(sliceA[i], sliceB[i]); } +// UTF-8 slice test -for (var j = 0; j < 100; j++) { - var slice = b.slice(100, 150); - assert.equal(50, slice.length); - for (var i = 0; i < 50; i++) { - assert.equal(b[100 + i], slice[i]); - } +var utf8String = '¡hέlló wôrld!'; +var offset = 100; + +b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8'); +var utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String)); +assert.equal(utf8String, utf8Slice); + +var written = b.write(utf8String, offset, 'utf8'); +assert.equal(Buffer.byteLength(utf8String), written); +utf8Slice = b.toString('utf8', offset, offset + Buffer.byteLength(utf8String)); +assert.equal(utf8String, utf8Slice); + +var sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String)); +var sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String)); +for (var i = 0; i < Buffer.byteLength(utf8String); i++) { + assert.equal(sliceA[i], sliceB[i]); +} + +var slice = b.slice(100, 150); +assert.equal(50, slice.length); +for (var i = 0; i < 50; i++) { + assert.equal(b[100 + i], slice[i]); } @@ -319,7 +335,6 @@ assert.equal(b, c.parent); assert.equal(b, d.parent); - // Bug regression test var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 var buffer = new Buffer(32); @@ -562,15 +577,6 @@ assert.equal(sb, s); b = new Buffer('abcde'); assert.equal('bcde', b.slice(1).toString()); -// byte length -assert.equal(14, Buffer.byteLength('Il était tué')); -assert.equal(14, Buffer.byteLength('Il était tué', 'utf8')); -['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { - assert.equal(24, Buffer.byteLength('Il était tué', encoding)); -}); -assert.equal(12, Buffer.byteLength('Il était tué', 'ascii')); -assert.equal(12, Buffer.byteLength('Il était tué', 'binary')); - // slice(0,0).length === 0 assert.equal(0, Buffer('hello').slice(0, 0).length); @@ -845,7 +851,7 @@ Buffer(Buffer(0), 0, 0); // GH-5110 -(function () { +(function() { var buffer = new Buffer('test'), string = JSON.stringify(buffer); @@ -1070,15 +1076,11 @@ assert.equal(buf.readInt8(0), -1); // see https://github.com/joyent/node/issues/5881 SlowBuffer(0).slice(0, 1); // make sure a zero length slice doesn't set the .parent attribute - assert.equal(Buffer(5).slice(0,0).parent, undefined); + assert.equal(Buffer(5).slice(0, 0).parent, undefined); // and make sure a proper slice does have a parent assert.ok(typeof Buffer(5).slice(0, 5).parent === 'object'); })(); -// Make sure byteLength properly checks for base64 padding -assert.equal(Buffer.byteLength('aaa=', 'base64'), 2); -assert.equal(Buffer.byteLength('aaaa==', 'base64'), 3); - // Regression test for #5482: should throw but not assert in C++ land. assert.throws(function() { Buffer('', 'buffer'); @@ -1089,7 +1091,7 @@ assert.throws(function() { (function() { var a = [0]; for (var i = 0; i < 7; ++i) a = a.concat(a); - a = a.map(function(_, i) { return i }); + a = a.map(function(_, i) { return i; }); var b = Buffer(a); var c = Buffer(b); assert.equal(b.length, a.length); @@ -1102,11 +1104,11 @@ assert.throws(function() { })(); -assert.throws(function () { +assert.throws(function() { new Buffer(smalloc.kMaxLength + 1); }, RangeError); -assert.throws(function () { +assert.throws(function() { new SlowBuffer(smalloc.kMaxLength + 1); }, RangeError); @@ -1174,7 +1176,7 @@ assert.throws(function() { b.equals('abc'); }); -// Regression test for https://github.com/iojs/io.js/issues/649. +// Regression test for https://github.com/nodejs/io.js/issues/649. assert.throws(function() { Buffer(1422561062959).toString('utf8'); }); var ps = Buffer.poolSize;