From ba88f5b8f895806e8fc619fb5f57ab48fc8bb522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Thu, 15 Sep 2016 21:11:44 +0200 Subject: [PATCH] test: improve coverage of the buffer module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add tests for untested branches and statements. Also convert some lines to const. PR-URL: https://github.com/nodejs/node/pull/8552 Reviewed-By: Rich Trott Reviewed-By: Сковорода Никита Андреевич --- test/parallel/test-buffer-alloc.js | 21 ++++++++++++++++++++ test/parallel/test-buffer-compare-offset.js | 6 +++++- test/parallel/test-buffer-indexof.js | 22 +++++++++++++-------- test/parallel/test-buffer-new.js | 7 +++++++ 4 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-buffer-new.js diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index e20db561c68a6f..dabee43321bcb9 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -26,6 +26,14 @@ assert.strictEqual(512, c.length); const d = Buffer.from([]); assert.strictEqual(0, d.length); +// Test offset properties +{ + const b = Buffer.alloc(128); + assert.strictEqual(128, b.length); + assert.strictEqual(0, b.byteOffset); + assert.strictEqual(0, b.offset); +} + // Test creating a Buffer from a Uint32Array { const ui32 = new Uint32Array(4).fill(42); @@ -49,6 +57,9 @@ assert.throws(() => b.toString('invalid'), // invalid encoding for Buffer.write assert.throws(() => b.write('test string', 0, 5, 'invalid'), /Unknown encoding: invalid/); +// unsupported arguments for Buffer.write +assert.throws(() => b.write('test', 'utf8', 0), + /is no longer supported/); // try to create 0-length buffers @@ -706,6 +717,16 @@ assert.strictEqual('', x.inspect()); assert.strictEqual(buf[4], 0); } +{ + // test alloc with fill option + const buf = Buffer.alloc(5, '800A', 'hex'); + assert.strictEqual(buf[0], 128); + assert.strictEqual(buf[1], 10); + assert.strictEqual(buf[2], 128); + assert.strictEqual(buf[3], 10); + assert.strictEqual(buf[4], 128); +} + // Check for fractional length args, junk length args, etc. // https://github.com/joyent/node/issues/1758 diff --git a/test/parallel/test-buffer-compare-offset.js b/test/parallel/test-buffer-compare-offset.js index 2185517e06d0fd..deed1a41cb664c 100644 --- a/test/parallel/test-buffer-compare-offset.js +++ b/test/parallel/test-buffer-compare-offset.js @@ -15,7 +15,7 @@ assert.strictEqual(-1, a.compare(b, '0')); // Equivalent to a.compare(b). assert.strictEqual(-1, a.compare(b, 0, undefined, 0)); -// Zero-length targer, return 1 +// Zero-length target, return 1 assert.strictEqual(1, a.compare(b, 0, 0, 0)); assert.strictEqual(1, a.compare(b, '0', '0', '0')); @@ -25,6 +25,10 @@ assert.strictEqual(1, a.compare(b, 6, 10)); // Zero-length source, return -1 assert.strictEqual(-1, a.compare(b, 6, 10, 0, 0)); +// Zero-length source and target, return 0 +assert.strictEqual(0, a.compare(b, 0, 0, 0, 0)); +assert.strictEqual(0, a.compare(b, 1, 1, 2, 2)); + // Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5)) assert.strictEqual(1, a.compare(b, 0, 5, 4)); diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index d7598d8b75cbf1..5757200c2b0470 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -1,15 +1,15 @@ 'use strict'; require('../common'); -var assert = require('assert'); +const assert = require('assert'); -var Buffer = require('buffer').Buffer; +const Buffer = require('buffer').Buffer; -var b = Buffer.from('abcdef'); -var buf_a = Buffer.from('a'); -var buf_bc = Buffer.from('bc'); -var buf_f = Buffer.from('f'); -var buf_z = Buffer.from('z'); -var buf_empty = Buffer.from(''); +const b = Buffer.from('abcdef'); +const buf_a = Buffer.from('a'); +const buf_bc = Buffer.from('bc'); +const buf_f = Buffer.from('f'); +const buf_z = Buffer.from('z'); +const buf_empty = Buffer.from(''); assert.equal(b.indexOf('a'), 0); assert.equal(b.indexOf('a', 1), -1); @@ -78,6 +78,12 @@ assert.equal(b.indexOf(Buffer.from('f'), 6), -1); assert.equal(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1); +// test invalid and uppercase encoding +assert.strictEqual(b.indexOf('b', 'utf8'), 1); +assert.strictEqual(b.indexOf('b', 'UTF8'), 1); +assert.strictEqual(b.indexOf('62', 'HEX'), 1); +assert.throws(() => b.indexOf('bad', 'enc'), /Unknown encoding: enc/); + // test hex encoding assert.strictEqual( Buffer.from(b.toString('hex'), 'hex') diff --git a/test/parallel/test-buffer-new.js b/test/parallel/test-buffer-new.js new file mode 100644 index 00000000000000..919f31238d2d18 --- /dev/null +++ b/test/parallel/test-buffer-new.js @@ -0,0 +1,7 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const Buffer = require('buffer').Buffer; + +assert.throws(() => new Buffer(42, 'utf8'), /first argument must be a string/);