Skip to content

Commit 4a40832

Browse files
committed
test: cleanup IIFE tests
A number of test files use IIFEs to separate distinct tests from each other in the same file. The project has been moving toward using block scopes and let/const in favor of IIFEs. This commit moves IIFE tests to block scopes. Some additional cleanup such as use of strictEqual() and common.mustCall() is also included. PR-URL: #7694 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
1 parent 4220c24 commit 4a40832

34 files changed

+667
-752
lines changed

test/gc/test-net-timeout.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,16 @@ function getall() {
3535
if (count >= todo)
3636
return;
3737

38-
(function() {
39-
var req = net.connect(server.address().port, server.address().address);
40-
req.resume();
41-
req.setTimeout(10, function() {
42-
//console.log('timeout (expected)')
43-
req.destroy();
44-
done++;
45-
global.gc();
46-
});
38+
const req = net.connect(server.address().port, server.address().address);
39+
req.resume();
40+
req.setTimeout(10, function() {
41+
req.destroy();
42+
done++;
43+
global.gc();
44+
});
4745

48-
count++;
49-
weak(req, afterGC);
50-
})();
46+
count++;
47+
weak(req, afterGC);
5148

5249
setImmediate(getall);
5350
}
@@ -76,4 +73,3 @@ function status() {
7673
}, 200);
7774
}
7875
}
79-

test/parallel/test-buffer-alloc.js

+42-42
Original file line numberDiff line numberDiff line change
@@ -1038,28 +1038,28 @@ Buffer.from(Buffer.allocUnsafe(0), 0, 0);
10381038

10391039

10401040
// GH-5110
1041-
(function() {
1041+
{
10421042
const buffer = Buffer.from('test');
10431043
const string = JSON.stringify(buffer);
10441044

1045-
assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');
1045+
assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
10461046

10471047
assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) {
10481048
return value && value.type === 'Buffer'
10491049
? Buffer.from(value.data)
10501050
: value;
10511051
}));
1052-
})();
1052+
}
10531053

10541054
// issue GH-7849
1055-
(function() {
1056-
var buf = Buffer.from('test');
1057-
var json = JSON.stringify(buf);
1058-
var obj = JSON.parse(json);
1059-
var copy = Buffer.from(obj);
1055+
{
1056+
const buf = Buffer.from('test');
1057+
const json = JSON.stringify(buf);
1058+
const obj = JSON.parse(json);
1059+
const copy = Buffer.from(obj);
10601060

10611061
assert(buf.equals(copy));
1062-
})();
1062+
}
10631063

10641064
// issue GH-4331
10651065
assert.throws(function() {
@@ -1165,30 +1165,30 @@ assert.throws(function() {
11651165
});
11661166

11671167
// test for common read(U)IntLE/BE
1168-
(function() {
1168+
{
11691169
var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
11701170

1171-
assert.equal(buf.readUIntLE(0, 1), 0x01);
1172-
assert.equal(buf.readUIntBE(0, 1), 0x01);
1173-
assert.equal(buf.readUIntLE(0, 3), 0x030201);
1174-
assert.equal(buf.readUIntBE(0, 3), 0x010203);
1175-
assert.equal(buf.readUIntLE(0, 5), 0x0504030201);
1176-
assert.equal(buf.readUIntBE(0, 5), 0x0102030405);
1177-
assert.equal(buf.readUIntLE(0, 6), 0x060504030201);
1178-
assert.equal(buf.readUIntBE(0, 6), 0x010203040506);
1179-
assert.equal(buf.readIntLE(0, 1), 0x01);
1180-
assert.equal(buf.readIntBE(0, 1), 0x01);
1181-
assert.equal(buf.readIntLE(0, 3), 0x030201);
1182-
assert.equal(buf.readIntBE(0, 3), 0x010203);
1183-
assert.equal(buf.readIntLE(0, 5), 0x0504030201);
1184-
assert.equal(buf.readIntBE(0, 5), 0x0102030405);
1185-
assert.equal(buf.readIntLE(0, 6), 0x060504030201);
1186-
assert.equal(buf.readIntBE(0, 6), 0x010203040506);
1187-
})();
1171+
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
1172+
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
1173+
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
1174+
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
1175+
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
1176+
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
1177+
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
1178+
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
1179+
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
1180+
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
1181+
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
1182+
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
1183+
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
1184+
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
1185+
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
1186+
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
1187+
}
11881188

11891189
// test for common write(U)IntLE/BE
1190-
(function() {
1191-
var buf = Buffer.allocUnsafe(3);
1190+
{
1191+
let buf = Buffer.allocUnsafe(3);
11921192
buf.writeUIntLE(0x123456, 0, 3);
11931193
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
11941194
assert.equal(buf.readUIntLE(0, 3), 0x123456);
@@ -1277,11 +1277,11 @@ assert.throws(function() {
12771277
buf.writeIntBE(-0x0012000000, 0, 5);
12781278
assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
12791279
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
1280-
})();
1280+
}
12811281

12821282
// test Buffer slice
1283-
(function() {
1284-
var buf = Buffer.from('0123456789');
1283+
{
1284+
const buf = Buffer.from('0123456789');
12851285
assert.equal(buf.slice(-10, 10), '0123456789');
12861286
assert.equal(buf.slice(-20, 10), '0123456789');
12871287
assert.equal(buf.slice(-20, -10), '');
@@ -1314,7 +1314,7 @@ assert.throws(function() {
13141314
assert.equal(buf.slice(0, -i), s.slice(0, -i));
13151315
}
13161316

1317-
var utf16Buf = Buffer.from('0123456789', 'utf16le');
1317+
const utf16Buf = Buffer.from('0123456789', 'utf16le');
13181318
assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le'));
13191319

13201320
assert.equal(buf.slice('0', '1'), '0');
@@ -1328,7 +1328,7 @@ assert.throws(function() {
13281328
// try to slice a zero length Buffer
13291329
// see https://github.com/joyent/node/issues/5881
13301330
Buffer.alloc(0).slice(0, 1);
1331-
})();
1331+
}
13321332

13331333
// Regression test for #5482: should throw but not assert in C++ land.
13341334
assert.throws(function() {
@@ -1337,20 +1337,20 @@ assert.throws(function() {
13371337

13381338
// Regression test for #6111. Constructing a buffer from another buffer
13391339
// should a) work, and b) not corrupt the source buffer.
1340-
(function() {
1341-
var a = [0];
1340+
{
1341+
let a = [0];
13421342
for (let i = 0; i < 7; ++i) a = a.concat(a);
13431343
a = a.map(function(_, i) { return i; });
13441344
const b = Buffer.from(a);
13451345
const c = Buffer.from(b);
1346-
assert.equal(b.length, a.length);
1347-
assert.equal(c.length, a.length);
1346+
assert.strictEqual(b.length, a.length);
1347+
assert.strictEqual(c.length, a.length);
13481348
for (let i = 0, k = a.length; i < k; ++i) {
1349-
assert.equal(a[i], i);
1350-
assert.equal(b[i], i);
1351-
assert.equal(c[i], i);
1349+
assert.strictEqual(a[i], i);
1350+
assert.strictEqual(b[i], i);
1351+
assert.strictEqual(c[i], i);
13521352
}
1353-
})();
1353+
}
13541354

13551355

13561356
assert.throws(function() {

test/parallel/test-buffer.js

+44-44
Original file line numberDiff line numberDiff line change
@@ -1054,28 +1054,28 @@ Buffer(Buffer(0), 0, 0);
10541054

10551055

10561056
// GH-5110
1057-
(function() {
1057+
{
10581058
const buffer = new Buffer('test');
10591059
const string = JSON.stringify(buffer);
10601060

1061-
assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');
1061+
assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
10621062

10631063
assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) {
10641064
return value && value.type === 'Buffer'
10651065
? new Buffer(value.data)
10661066
: value;
10671067
}));
1068-
})();
1068+
}
10691069

10701070
// issue GH-7849
1071-
(function() {
1072-
var buf = new Buffer('test');
1073-
var json = JSON.stringify(buf);
1074-
var obj = JSON.parse(json);
1075-
var copy = new Buffer(obj);
1071+
{
1072+
const buf = new Buffer('test');
1073+
const json = JSON.stringify(buf);
1074+
const obj = JSON.parse(json);
1075+
const copy = new Buffer(obj);
10761076

10771077
assert(buf.equals(copy));
1078-
})();
1078+
}
10791079

10801080
// issue GH-4331
10811081
assert.throws(function() {
@@ -1191,30 +1191,30 @@ assert.throws(function() {
11911191
});
11921192

11931193
// test for common read(U)IntLE/BE
1194-
(function() {
1195-
var buf = new Buffer([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
1196-
1197-
assert.equal(buf.readUIntLE(0, 1), 0x01);
1198-
assert.equal(buf.readUIntBE(0, 1), 0x01);
1199-
assert.equal(buf.readUIntLE(0, 3), 0x030201);
1200-
assert.equal(buf.readUIntBE(0, 3), 0x010203);
1201-
assert.equal(buf.readUIntLE(0, 5), 0x0504030201);
1202-
assert.equal(buf.readUIntBE(0, 5), 0x0102030405);
1203-
assert.equal(buf.readUIntLE(0, 6), 0x060504030201);
1204-
assert.equal(buf.readUIntBE(0, 6), 0x010203040506);
1205-
assert.equal(buf.readIntLE(0, 1), 0x01);
1206-
assert.equal(buf.readIntBE(0, 1), 0x01);
1207-
assert.equal(buf.readIntLE(0, 3), 0x030201);
1208-
assert.equal(buf.readIntBE(0, 3), 0x010203);
1209-
assert.equal(buf.readIntLE(0, 5), 0x0504030201);
1210-
assert.equal(buf.readIntBE(0, 5), 0x0102030405);
1211-
assert.equal(buf.readIntLE(0, 6), 0x060504030201);
1212-
assert.equal(buf.readIntBE(0, 6), 0x010203040506);
1213-
})();
1194+
{
1195+
const buf = new Buffer([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
1196+
1197+
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
1198+
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
1199+
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
1200+
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
1201+
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
1202+
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
1203+
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
1204+
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
1205+
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
1206+
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
1207+
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
1208+
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
1209+
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
1210+
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
1211+
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
1212+
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
1213+
}
12141214

12151215
// test for common write(U)IntLE/BE
1216-
(function() {
1217-
var buf = Buffer(3);
1216+
{
1217+
let buf = Buffer(3);
12181218
buf.writeUIntLE(0x123456, 0, 3);
12191219
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
12201220
assert.equal(buf.readUIntLE(0, 3), 0x123456);
@@ -1303,11 +1303,11 @@ assert.throws(function() {
13031303
buf.writeIntBE(-0x0012000000, 0, 5);
13041304
assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
13051305
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
1306-
})();
1306+
}
13071307

13081308
// test Buffer slice
1309-
(function() {
1310-
var buf = new Buffer('0123456789');
1309+
{
1310+
const buf = new Buffer('0123456789');
13111311
assert.equal(buf.slice(-10, 10), '0123456789');
13121312
assert.equal(buf.slice(-20, 10), '0123456789');
13131313
assert.equal(buf.slice(-20, -10), '');
@@ -1340,7 +1340,7 @@ assert.throws(function() {
13401340
assert.equal(buf.slice(0, -i), s.slice(0, -i));
13411341
}
13421342

1343-
var utf16Buf = new Buffer('0123456789', 'utf16le');
1343+
const utf16Buf = new Buffer('0123456789', 'utf16le');
13441344
assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le'));
13451345

13461346
assert.equal(buf.slice('0', '1'), '0');
@@ -1354,7 +1354,7 @@ assert.throws(function() {
13541354
// try to slice a zero length Buffer
13551355
// see https://github.com/joyent/node/issues/5881
13561356
SlowBuffer(0).slice(0, 1);
1357-
})();
1357+
}
13581358

13591359
// Regression test for #5482: should throw but not assert in C++ land.
13601360
assert.throws(function() {
@@ -1363,20 +1363,20 @@ assert.throws(function() {
13631363

13641364
// Regression test for #6111. Constructing a buffer from another buffer
13651365
// should a) work, and b) not corrupt the source buffer.
1366-
(function() {
1367-
var a = [0];
1366+
{
1367+
let a = [0];
13681368
for (let i = 0; i < 7; ++i) a = a.concat(a);
13691369
a = a.map(function(_, i) { return i; });
13701370
const b = Buffer(a);
13711371
const c = Buffer(b);
1372-
assert.equal(b.length, a.length);
1373-
assert.equal(c.length, a.length);
1372+
assert.strictEqual(b.length, a.length);
1373+
assert.strictEqual(c.length, a.length);
13741374
for (let i = 0, k = a.length; i < k; ++i) {
1375-
assert.equal(a[i], i);
1376-
assert.equal(b[i], i);
1377-
assert.equal(c[i], i);
1375+
assert.strictEqual(a[i], i);
1376+
assert.strictEqual(b[i], i);
1377+
assert.strictEqual(c[i], i);
13781378
}
1379-
})();
1379+
}
13801380

13811381

13821382
assert.throws(function() {

test/parallel/test-child-process-cwd.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,11 @@ if (common.isWindows) {
4444
}
4545

4646
// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
47-
(function() {
48-
var errors = 0;
49-
50-
testCwd({cwd: 'does-not-exist'}, -1).on('error', function(e) {
47+
{
48+
testCwd({cwd: 'does-not-exist'}, -1).on('error', common.mustCall(function(e) {
5149
assert.equal(e.code, 'ENOENT');
52-
errors++;
53-
});
54-
55-
process.on('exit', function() {
56-
assert.equal(errors, 1);
57-
});
58-
})();
50+
}));
51+
}
5952

6053
// Spawn() shouldn't try to chdir() so this should just work
6154
testCwd(undefined, 0);

0 commit comments

Comments
 (0)