Skip to content

Commit 4537cf2

Browse files
committed
test: additional refactoring/cleanup of buffer tests
* Favor use of strictEqual where possible * Use const as appropriate * Other miscellaneous cleanups PR-URL: #8283 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent f10e1ed commit 4537cf2

9 files changed

+118
-115
lines changed

test/parallel/test-buffer-arraybuffer.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ assert.equal(buf.length, ab.byteLength);
2222

2323
buf.fill(0xC);
2424
for (let i = 0; i < LENGTH; i++) {
25-
assert.equal(ui[i], 0xC);
25+
assert.strictEqual(ui[i], 0xC);
2626
ui[i] = 0xF;
27-
assert.equal(buf[i], 0xF);
27+
assert.strictEqual(buf[i], 0xF);
2828
}
2929

3030
buf.writeUInt32LE(0xF00, 0);
3131
buf.writeUInt32BE(0xB47, 4);
3232
buf.writeDoubleLE(3.1415, 8);
3333

34-
assert.equal(dv.getUint32(0, true), 0xF00);
35-
assert.equal(dv.getUint32(4), 0xB47);
36-
assert.equal(dv.getFloat64(8, true), 3.1415);
34+
assert.strictEqual(dv.getUint32(0, true), 0xF00);
35+
assert.strictEqual(dv.getUint32(4), 0xB47);
36+
assert.strictEqual(dv.getFloat64(8, true), 3.1415);
3737

3838

3939
// Now test protecting users from doing stupid things
@@ -61,12 +61,12 @@ b.writeDoubleBE(11.11, 0, true);
6161
ab[3] = 4;
6262
ab[4] = 5;
6363
const buf = Buffer.from(ab.buffer, 1, 3);
64-
assert.equal(buf.length, 3);
65-
assert.equal(buf[0], 2);
66-
assert.equal(buf[1], 3);
67-
assert.equal(buf[2], 4);
64+
assert.strictEqual(buf.length, 3);
65+
assert.strictEqual(buf[0], 2);
66+
assert.strictEqual(buf[1], 3);
67+
assert.strictEqual(buf[2], 4);
6868
buf[0] = 9;
69-
assert.equal(ab[1], 9);
69+
assert.strictEqual(ab[1], 9);
7070

7171
assert.throws(() => Buffer.from(ab.buffer, 6), (err) => {
7272
assert(err instanceof RangeError);
@@ -89,12 +89,12 @@ b.writeDoubleBE(11.11, 0, true);
8989
ab[3] = 4;
9090
ab[4] = 5;
9191
const buf = Buffer(ab.buffer, 1, 3);
92-
assert.equal(buf.length, 3);
93-
assert.equal(buf[0], 2);
94-
assert.equal(buf[1], 3);
95-
assert.equal(buf[2], 4);
92+
assert.strictEqual(buf.length, 3);
93+
assert.strictEqual(buf[0], 2);
94+
assert.strictEqual(buf[1], 3);
95+
assert.strictEqual(buf[2], 4);
9696
buf[0] = 9;
97-
assert.equal(ab[1], 9);
97+
assert.strictEqual(ab[1], 9);
9898

9999
assert.throws(() => Buffer(ab.buffer, 6), (err) => {
100100
assert(err instanceof RangeError);

test/parallel/test-buffer-ascii.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict';
22
require('../common');
3-
var assert = require('assert');
3+
const assert = require('assert');
44

55
// ASCII conversion in node.js simply masks off the high bits,
66
// it doesn't do transliteration.
7-
assert.equal(Buffer.from('hérité').toString('ascii'), 'hC)ritC)');
7+
assert.strictEqual(Buffer.from('hérité').toString('ascii'), 'hC)ritC)');
88

99
// 71 characters, 78 bytes. The ’ character is a triple-byte sequence.
10-
var input = 'C’est, graphiquement, la réunion d’un accent aigu ' +
11-
'et d’un accent grave.';
10+
const input = 'C’est, graphiquement, la réunion d’un accent aigu ' +
11+
'et d’un accent grave.';
1212

13-
var expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' +
14-
'db\u0000\u0019un accent aigu et db\u0000\u0019un ' +
15-
'accent grave.';
13+
const expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' +
14+
'db\u0000\u0019un accent aigu et db\u0000\u0019un ' +
15+
'accent grave.';
1616

17-
var buf = Buffer.from(input);
17+
const buf = Buffer.from(input);
1818

1919
for (var i = 0; i < expected.length; ++i) {
20-
assert.equal(buf.slice(i).toString('ascii'), expected.slice(i));
20+
assert.strictEqual(buf.slice(i).toString('ascii'), expected.slice(i));
2121

2222
// Skip remainder of multi-byte sequence.
2323
if (input.charCodeAt(i) > 65535) ++i;
+48-46
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,89 @@
11
'use strict';
22

33
require('../common');
4-
var assert = require('assert');
5-
var Buffer = require('buffer').Buffer;
6-
var SlowBuffer = require('buffer').SlowBuffer;
4+
const assert = require('assert');
5+
const Buffer = require('buffer').Buffer;
6+
const SlowBuffer = require('buffer').SlowBuffer;
77

88
// coerce values to string
9-
assert.equal(Buffer.byteLength(32, 'latin1'), 2);
10-
assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
11-
assert.equal(Buffer.byteLength({}, 'latin1'), 15);
12-
assert.equal(Buffer.byteLength(), 9);
9+
assert.strictEqual(Buffer.byteLength(32, 'latin1'), 2);
10+
assert.strictEqual(Buffer.byteLength(NaN, 'utf8'), 3);
11+
assert.strictEqual(Buffer.byteLength({}, 'latin1'), 15);
12+
assert.strictEqual(Buffer.byteLength(), 9);
1313

14-
var buff = new Buffer(10);
15-
assert(ArrayBuffer.isView(buff));
16-
var slowbuff = new SlowBuffer(10);
17-
assert(ArrayBuffer.isView(slowbuff));
14+
assert(ArrayBuffer.isView(new Buffer(10)));
15+
assert(ArrayBuffer.isView(new SlowBuffer(10)));
16+
assert(ArrayBuffer.isView(Buffer.alloc(10)));
17+
assert(ArrayBuffer.isView(Buffer.allocUnsafe(10)));
18+
assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10)));
19+
assert(ArrayBuffer.isView(Buffer.from('')));
1820

1921
// buffer
2022
var incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
21-
assert.equal(Buffer.byteLength(incomplete), 5);
23+
assert.strictEqual(Buffer.byteLength(incomplete), 5);
2224
var ascii = Buffer.from('abc');
23-
assert.equal(Buffer.byteLength(ascii), 3);
25+
assert.strictEqual(Buffer.byteLength(ascii), 3);
2426

2527
// ArrayBuffer
2628
var buffer = new ArrayBuffer(8);
2729
assert.equal(Buffer.byteLength(buffer), 8);
2830

2931
// TypedArray
3032
var int8 = new Int8Array(8);
31-
assert.equal(Buffer.byteLength(int8), 8);
33+
assert.strictEqual(Buffer.byteLength(int8), 8);
3234
var uint8 = new Uint8Array(8);
33-
assert.equal(Buffer.byteLength(uint8), 8);
35+
assert.strictEqual(Buffer.byteLength(uint8), 8);
3436
var uintc8 = new Uint8ClampedArray(2);
35-
assert.equal(Buffer.byteLength(uintc8), 2);
37+
assert.strictEqual(Buffer.byteLength(uintc8), 2);
3638
var int16 = new Int16Array(8);
37-
assert.equal(Buffer.byteLength(int16), 16);
39+
assert.strictEqual(Buffer.byteLength(int16), 16);
3840
var uint16 = new Uint16Array(8);
39-
assert.equal(Buffer.byteLength(uint16), 16);
41+
assert.strictEqual(Buffer.byteLength(uint16), 16);
4042
var int32 = new Int32Array(8);
41-
assert.equal(Buffer.byteLength(int32), 32);
43+
assert.strictEqual(Buffer.byteLength(int32), 32);
4244
var uint32 = new Uint32Array(8);
43-
assert.equal(Buffer.byteLength(uint32), 32);
45+
assert.strictEqual(Buffer.byteLength(uint32), 32);
4446
var float32 = new Float32Array(8);
45-
assert.equal(Buffer.byteLength(float32), 32);
47+
assert.strictEqual(Buffer.byteLength(float32), 32);
4648
var float64 = new Float64Array(8);
47-
assert.equal(Buffer.byteLength(float64), 64);
49+
assert.strictEqual(Buffer.byteLength(float64), 64);
4850

4951
// DataView
5052
var dv = new DataView(new ArrayBuffer(2));
51-
assert.equal(Buffer.byteLength(dv), 2);
53+
assert.strictEqual(Buffer.byteLength(dv), 2);
5254

5355
// special case: zero length string
54-
assert.equal(Buffer.byteLength('', 'ascii'), 0);
55-
assert.equal(Buffer.byteLength('', 'HeX'), 0);
56+
assert.strictEqual(Buffer.byteLength('', 'ascii'), 0);
57+
assert.strictEqual(Buffer.byteLength('', 'HeX'), 0);
5658

5759
// utf8
58-
assert.equal(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19);
59-
assert.equal(Buffer.byteLength('κλμνξο', 'utf8'), 12);
60-
assert.equal(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15);
61-
assert.equal(Buffer.byteLength('𠝹𠱓𠱸', 'UTF8'), 12);
60+
assert.strictEqual(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19);
61+
assert.strictEqual(Buffer.byteLength('κλμνξο', 'utf8'), 12);
62+
assert.strictEqual(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15);
63+
assert.strictEqual(Buffer.byteLength('𠝹𠱓𠱸', 'UTF8'), 12);
6264
// without an encoding, utf8 should be assumed
63-
assert.equal(Buffer.byteLength('hey there'), 9);
64-
assert.equal(Buffer.byteLength('𠱸挶νξ#xx :)'), 17);
65-
assert.equal(Buffer.byteLength('hello world', ''), 11);
65+
assert.strictEqual(Buffer.byteLength('hey there'), 9);
66+
assert.strictEqual(Buffer.byteLength('𠱸挶νξ#xx :)'), 17);
67+
assert.strictEqual(Buffer.byteLength('hello world', ''), 11);
6668
// it should also be assumed with unrecognized encoding
67-
assert.equal(Buffer.byteLength('hello world', 'abc'), 11);
68-
assert.equal(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10);
69+
assert.strictEqual(Buffer.byteLength('hello world', 'abc'), 11);
70+
assert.strictEqual(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10);
6971

7072
// base64
71-
assert.equal(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11);
72-
assert.equal(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14);
73-
assert.equal(Buffer.byteLength('aGkk', 'base64'), 3);
74-
assert.equal(Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==',
73+
assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11);
74+
assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14);
75+
assert.strictEqual(Buffer.byteLength('aGkk', 'base64'), 3);
76+
assert.strictEqual(Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==',
7577
'base64'), 25);
7678
// special padding
77-
assert.equal(Buffer.byteLength('aaa=', 'base64'), 2);
78-
assert.equal(Buffer.byteLength('aaaa==', 'base64'), 3);
79+
assert.strictEqual(Buffer.byteLength('aaa=', 'base64'), 2);
80+
assert.strictEqual(Buffer.byteLength('aaaa==', 'base64'), 3);
7981

80-
assert.equal(Buffer.byteLength('Il était tué'), 14);
81-
assert.equal(Buffer.byteLength('Il était tué', 'utf8'), 14);
82-
assert.equal(Buffer.byteLength('Il était tué', 'ascii'), 12);
83-
assert.equal(Buffer.byteLength('Il était tué', 'latin1'), 12);
84-
assert.equal(Buffer.byteLength('Il était tué', 'binary'), 12);
82+
assert.strictEqual(Buffer.byteLength('Il était tué'), 14);
83+
assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14);
84+
assert.strictEqual(Buffer.byteLength('Il était tué', 'ascii'), 12);
85+
assert.strictEqual(Buffer.byteLength('Il était tué', 'latin1'), 12);
86+
assert.strictEqual(Buffer.byteLength('Il était tué', 'binary'), 12);
8587
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
86-
assert.equal(24, Buffer.byteLength('Il était tué', encoding));
88+
assert.strictEqual(24, Buffer.byteLength('Il était tué', encoding));
8789
});

test/parallel/test-buffer-compare-offset.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,51 @@ const assert = require('assert');
66
const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
77
const b = Buffer.from([5, 6, 7, 8, 9, 0, 1, 2, 3, 4]);
88

9-
assert.equal(-1, a.compare(b));
9+
assert.strictEqual(-1, a.compare(b));
1010

1111
// Equivalent to a.compare(b).
12-
assert.equal(-1, a.compare(b, 0));
13-
assert.equal(-1, a.compare(b, '0'));
12+
assert.strictEqual(-1, a.compare(b, 0));
13+
assert.strictEqual(-1, a.compare(b, '0'));
1414

1515
// Equivalent to a.compare(b).
16-
assert.equal(-1, a.compare(b, 0, undefined, 0));
16+
assert.strictEqual(-1, a.compare(b, 0, undefined, 0));
1717

1818
// Zero-length targer, return 1
19-
assert.equal(1, a.compare(b, 0, 0, 0));
20-
assert.equal(1, a.compare(b, '0', '0', '0'));
19+
assert.strictEqual(1, a.compare(b, 0, 0, 0));
20+
assert.strictEqual(1, a.compare(b, '0', '0', '0'));
2121

2222
// Equivalent to Buffer.compare(a, b.slice(6, 10))
23-
assert.equal(1, a.compare(b, 6, 10));
23+
assert.strictEqual(1, a.compare(b, 6, 10));
2424

2525
// Zero-length source, return -1
26-
assert.equal(-1, a.compare(b, 6, 10, 0, 0));
26+
assert.strictEqual(-1, a.compare(b, 6, 10, 0, 0));
2727

2828
// Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5))
29-
assert.equal(1, a.compare(b, 0, 5, 4));
29+
assert.strictEqual(1, a.compare(b, 0, 5, 4));
3030

3131
// Equivalent to Buffer.compare(a.slice(1), b.slice(5))
32-
assert.equal(1, a.compare(b, 5, undefined, 1));
32+
assert.strictEqual(1, a.compare(b, 5, undefined, 1));
3333

3434
// Equivalent to Buffer.compare(a.slice(2), b.slice(2, 4))
35-
assert.equal(-1, a.compare(b, 2, 4, 2));
35+
assert.strictEqual(-1, a.compare(b, 2, 4, 2));
3636

3737
// Equivalent to Buffer.compare(a.slice(4), b.slice(0, 7))
38-
assert.equal(-1, a.compare(b, 0, 7, 4));
38+
assert.strictEqual(-1, a.compare(b, 0, 7, 4));
3939

4040
// Equivalent to Buffer.compare(a.slice(4, 6), b.slice(0, 7));
41-
assert.equal(-1, a.compare(b, 0, 7, 4, 6));
41+
assert.strictEqual(-1, a.compare(b, 0, 7, 4, 6));
4242

4343
// zero length target
44-
assert.equal(1, a.compare(b, 0, null));
44+
assert.strictEqual(1, a.compare(b, 0, null));
4545

4646
// coerces to targetEnd == 5
47-
assert.equal(-1, a.compare(b, 0, {valueOf: () => 5}));
47+
assert.strictEqual(-1, a.compare(b, 0, {valueOf: () => 5}));
4848

4949
// zero length target
50-
assert.equal(1, a.compare(b, Infinity, -Infinity));
50+
assert.strictEqual(1, a.compare(b, Infinity, -Infinity));
5151

5252
// zero length target because default for targetEnd <= targetSource
53-
assert.equal(1, a.compare(b, '0xff'));
53+
assert.strictEqual(1, a.compare(b, '0xff'));
5454

5555
const oor = /out of range index/;
5656

test/parallel/test-buffer-concat.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
'use strict';
22
require('../common');
3-
var assert = require('assert');
3+
const assert = require('assert');
44

5-
var zero = [];
6-
var one = [ Buffer.from('asdf') ];
7-
var long = [];
5+
const zero = [];
6+
const one = [ Buffer.from('asdf') ];
7+
const long = [];
88
for (var i = 0; i < 10; i++) long.push(Buffer.from('asdf'));
99

10-
var flatZero = Buffer.concat(zero);
11-
var flatOne = Buffer.concat(one);
12-
var flatLong = Buffer.concat(long);
13-
var flatLongLen = Buffer.concat(long, 40);
10+
const flatZero = Buffer.concat(zero);
11+
const flatOne = Buffer.concat(one);
12+
const flatLong = Buffer.concat(long);
13+
const flatLongLen = Buffer.concat(long, 40);
14+
15+
assert.strictEqual(flatZero.length, 0);
16+
assert.strictEqual(flatOne.toString(), 'asdf');
17+
18+
const check = new Array(10 + 1).join('asdf');
1419

15-
assert(flatZero.length === 0);
16-
assert(flatOne.toString() === 'asdf');
1720
// A special case where concat used to return the first item,
1821
// if the length is one. This check is to make sure that we don't do that.
19-
assert(flatOne !== one[0]);
20-
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
21-
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
22+
assert.notStrictEqual(flatOne, one[0]);
23+
assert.strictEqual(flatLong.toString(), check);
24+
assert.strictEqual(flatLongLen.toString(), check);
2225

2326
assertWrongList();
2427
assertWrongList(null);
@@ -28,7 +31,7 @@ assertWrongList(['hello', 'world']);
2831
assertWrongList(['hello', Buffer.from('world')]);
2932

3033
function assertWrongList(value) {
31-
assert.throws(function() {
34+
assert.throws(() => {
3235
Buffer.concat(value);
3336
}, function(err) {
3437
return err instanceof TypeError &&

test/parallel/test-buffer-copy.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
require('../common');
44
const assert = require('assert');
55

6-
var b = Buffer.allocUnsafe(1024);
7-
var c = Buffer.allocUnsafe(512);
6+
const b = Buffer.allocUnsafe(1024);
7+
const c = Buffer.allocUnsafe(512);
88
var cntr = 0;
99

1010
{
@@ -88,7 +88,7 @@ var cntr = 0;
8888
}
8989

9090
// copy string longer than buffer length (failure will segfault)
91-
var bb = Buffer.allocUnsafe(10);
91+
const bb = Buffer.allocUnsafe(10);
9292
bb.fill('hello crazy world');
9393

9494

test/parallel/test-buffer-inheritance.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ T.prototype.sum = function sum() {
2323
const vals = [new T(4), T(4)];
2424

2525
vals.forEach(function(t) {
26-
assert.equal(t.constructor, T);
27-
assert.equal(Object.getPrototypeOf(t), T.prototype);
28-
assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(t)),
26+
assert.strictEqual(t.constructor, T);
27+
assert.strictEqual(Object.getPrototypeOf(t), T.prototype);
28+
assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(t)),
2929
Buffer.prototype);
3030

3131
t.fill(5);
3232
let cntr = 0;
3333
for (let i = 0; i < t.length; i++)
3434
cntr += t[i];
35-
assert.equal(t.length * 5, cntr);
35+
assert.strictEqual(t.length * 5, cntr);
3636

3737
// Check this does not throw
3838
t.toString();

0 commit comments

Comments
 (0)