Skip to content

Commit acb976a

Browse files
cjihrigevanlucas
authored andcommitted
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 <[email protected]>
1 parent 11d6f1a commit acb976a

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
@@ -1029,28 +1029,28 @@ Buffer.from(Buffer.allocUnsafe(0), 0, 0);
10291029

10301030

10311031
// GH-5110
1032-
(function() {
1032+
{
10331033
const buffer = Buffer.from('test');
10341034
const string = JSON.stringify(buffer);
10351035

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

10381038
assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) {
10391039
return value && value.type === 'Buffer'
10401040
? Buffer.from(value.data)
10411041
: value;
10421042
}));
1043-
})();
1043+
}
10441044

10451045
// issue GH-7849
1046-
(function() {
1047-
var buf = Buffer.from('test');
1048-
var json = JSON.stringify(buf);
1049-
var obj = JSON.parse(json);
1050-
var copy = Buffer.from(obj);
1046+
{
1047+
const buf = Buffer.from('test');
1048+
const json = JSON.stringify(buf);
1049+
const obj = JSON.parse(json);
1050+
const copy = Buffer.from(obj);
10511051

10521052
assert(buf.equals(copy));
1053-
})();
1053+
}
10541054

10551055
// issue GH-4331
10561056
assert.throws(function() {
@@ -1156,30 +1156,30 @@ assert.throws(function() {
11561156
});
11571157

11581158
// test for common read(U)IntLE/BE
1159-
(function() {
1159+
{
11601160
var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
11611161

1162-
assert.equal(buf.readUIntLE(0, 1), 0x01);
1163-
assert.equal(buf.readUIntBE(0, 1), 0x01);
1164-
assert.equal(buf.readUIntLE(0, 3), 0x030201);
1165-
assert.equal(buf.readUIntBE(0, 3), 0x010203);
1166-
assert.equal(buf.readUIntLE(0, 5), 0x0504030201);
1167-
assert.equal(buf.readUIntBE(0, 5), 0x0102030405);
1168-
assert.equal(buf.readUIntLE(0, 6), 0x060504030201);
1169-
assert.equal(buf.readUIntBE(0, 6), 0x010203040506);
1170-
assert.equal(buf.readIntLE(0, 1), 0x01);
1171-
assert.equal(buf.readIntBE(0, 1), 0x01);
1172-
assert.equal(buf.readIntLE(0, 3), 0x030201);
1173-
assert.equal(buf.readIntBE(0, 3), 0x010203);
1174-
assert.equal(buf.readIntLE(0, 5), 0x0504030201);
1175-
assert.equal(buf.readIntBE(0, 5), 0x0102030405);
1176-
assert.equal(buf.readIntLE(0, 6), 0x060504030201);
1177-
assert.equal(buf.readIntBE(0, 6), 0x010203040506);
1178-
})();
1162+
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
1163+
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
1164+
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
1165+
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
1166+
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
1167+
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
1168+
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
1169+
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
1170+
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
1171+
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
1172+
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
1173+
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
1174+
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
1175+
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
1176+
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
1177+
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
1178+
}
11791179

11801180
// test for common write(U)IntLE/BE
1181-
(function() {
1182-
var buf = Buffer.allocUnsafe(3);
1181+
{
1182+
let buf = Buffer.allocUnsafe(3);
11831183
buf.writeUIntLE(0x123456, 0, 3);
11841184
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
11851185
assert.equal(buf.readUIntLE(0, 3), 0x123456);
@@ -1268,11 +1268,11 @@ assert.throws(function() {
12681268
buf.writeIntBE(-0x0012000000, 0, 5);
12691269
assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
12701270
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
1271-
})();
1271+
}
12721272

12731273
// test Buffer slice
1274-
(function() {
1275-
var buf = Buffer.from('0123456789');
1274+
{
1275+
const buf = Buffer.from('0123456789');
12761276
assert.equal(buf.slice(-10, 10), '0123456789');
12771277
assert.equal(buf.slice(-20, 10), '0123456789');
12781278
assert.equal(buf.slice(-20, -10), '');
@@ -1305,7 +1305,7 @@ assert.throws(function() {
13051305
assert.equal(buf.slice(0, -i), s.slice(0, -i));
13061306
}
13071307

1308-
var utf16Buf = Buffer.from('0123456789', 'utf16le');
1308+
const utf16Buf = Buffer.from('0123456789', 'utf16le');
13091309
assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le'));
13101310

13111311
assert.equal(buf.slice('0', '1'), '0');
@@ -1319,7 +1319,7 @@ assert.throws(function() {
13191319
// try to slice a zero length Buffer
13201320
// see https://github.com/joyent/node/issues/5881
13211321
Buffer.alloc(0).slice(0, 1);
1322-
})();
1322+
}
13231323

13241324
// Regression test for #5482: should throw but not assert in C++ land.
13251325
assert.throws(function() {
@@ -1328,20 +1328,20 @@ assert.throws(function() {
13281328

13291329
// Regression test for #6111. Constructing a buffer from another buffer
13301330
// should a) work, and b) not corrupt the source buffer.
1331-
(function() {
1332-
var a = [0];
1331+
{
1332+
let a = [0];
13331333
for (let i = 0; i < 7; ++i) a = a.concat(a);
13341334
a = a.map(function(_, i) { return i; });
13351335
const b = Buffer.from(a);
13361336
const c = Buffer.from(b);
1337-
assert.equal(b.length, a.length);
1338-
assert.equal(c.length, a.length);
1337+
assert.strictEqual(b.length, a.length);
1338+
assert.strictEqual(c.length, a.length);
13391339
for (let i = 0, k = a.length; i < k; ++i) {
1340-
assert.equal(a[i], i);
1341-
assert.equal(b[i], i);
1342-
assert.equal(c[i], i);
1340+
assert.strictEqual(a[i], i);
1341+
assert.strictEqual(b[i], i);
1342+
assert.strictEqual(c[i], i);
13431343
}
1344-
})();
1344+
}
13451345

13461346

13471347
assert.throws(function() {

test/parallel/test-buffer.js

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

10321032

10331033
// GH-5110
1034-
(function() {
1034+
{
10351035
const buffer = new Buffer('test');
10361036
const string = JSON.stringify(buffer);
10371037

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

10401040
assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) {
10411041
return value && value.type === 'Buffer'
10421042
? new Buffer(value.data)
10431043
: value;
10441044
}));
1045-
})();
1045+
}
10461046

10471047
// issue GH-7849
1048-
(function() {
1049-
var buf = new Buffer('test');
1050-
var json = JSON.stringify(buf);
1051-
var obj = JSON.parse(json);
1052-
var copy = new Buffer(obj);
1048+
{
1049+
const buf = new Buffer('test');
1050+
const json = JSON.stringify(buf);
1051+
const obj = JSON.parse(json);
1052+
const copy = new Buffer(obj);
10531053

10541054
assert(buf.equals(copy));
1055-
})();
1055+
}
10561056

10571057
// issue GH-4331
10581058
assert.throws(function() {
@@ -1168,30 +1168,30 @@ assert.throws(function() {
11681168
});
11691169

11701170
// test for common read(U)IntLE/BE
1171-
(function() {
1172-
var buf = new Buffer([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
1173-
1174-
assert.equal(buf.readUIntLE(0, 1), 0x01);
1175-
assert.equal(buf.readUIntBE(0, 1), 0x01);
1176-
assert.equal(buf.readUIntLE(0, 3), 0x030201);
1177-
assert.equal(buf.readUIntBE(0, 3), 0x010203);
1178-
assert.equal(buf.readUIntLE(0, 5), 0x0504030201);
1179-
assert.equal(buf.readUIntBE(0, 5), 0x0102030405);
1180-
assert.equal(buf.readUIntLE(0, 6), 0x060504030201);
1181-
assert.equal(buf.readUIntBE(0, 6), 0x010203040506);
1182-
assert.equal(buf.readIntLE(0, 1), 0x01);
1183-
assert.equal(buf.readIntBE(0, 1), 0x01);
1184-
assert.equal(buf.readIntLE(0, 3), 0x030201);
1185-
assert.equal(buf.readIntBE(0, 3), 0x010203);
1186-
assert.equal(buf.readIntLE(0, 5), 0x0504030201);
1187-
assert.equal(buf.readIntBE(0, 5), 0x0102030405);
1188-
assert.equal(buf.readIntLE(0, 6), 0x060504030201);
1189-
assert.equal(buf.readIntBE(0, 6), 0x010203040506);
1190-
})();
1171+
{
1172+
const buf = new Buffer([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
1173+
1174+
assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
1175+
assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
1176+
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
1177+
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
1178+
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
1179+
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
1180+
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
1181+
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
1182+
assert.strictEqual(buf.readIntLE(0, 1), 0x01);
1183+
assert.strictEqual(buf.readIntBE(0, 1), 0x01);
1184+
assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
1185+
assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
1186+
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
1187+
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
1188+
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
1189+
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
1190+
}
11911191

11921192
// test for common write(U)IntLE/BE
1193-
(function() {
1194-
var buf = Buffer(3);
1193+
{
1194+
let buf = Buffer(3);
11951195
buf.writeUIntLE(0x123456, 0, 3);
11961196
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
11971197
assert.equal(buf.readUIntLE(0, 3), 0x123456);
@@ -1280,11 +1280,11 @@ assert.throws(function() {
12801280
buf.writeIntBE(-0x0012000000, 0, 5);
12811281
assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
12821282
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
1283-
})();
1283+
}
12841284

12851285
// test Buffer slice
1286-
(function() {
1287-
var buf = new Buffer('0123456789');
1286+
{
1287+
const buf = new Buffer('0123456789');
12881288
assert.equal(buf.slice(-10, 10), '0123456789');
12891289
assert.equal(buf.slice(-20, 10), '0123456789');
12901290
assert.equal(buf.slice(-20, -10), '');
@@ -1317,7 +1317,7 @@ assert.throws(function() {
13171317
assert.equal(buf.slice(0, -i), s.slice(0, -i));
13181318
}
13191319

1320-
var utf16Buf = new Buffer('0123456789', 'utf16le');
1320+
const utf16Buf = new Buffer('0123456789', 'utf16le');
13211321
assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le'));
13221322

13231323
assert.equal(buf.slice('0', '1'), '0');
@@ -1331,7 +1331,7 @@ assert.throws(function() {
13311331
// try to slice a zero length Buffer
13321332
// see https://github.com/joyent/node/issues/5881
13331333
SlowBuffer(0).slice(0, 1);
1334-
})();
1334+
}
13351335

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

13411341
// Regression test for #6111. Constructing a buffer from another buffer
13421342
// should a) work, and b) not corrupt the source buffer.
1343-
(function() {
1344-
var a = [0];
1343+
{
1344+
let a = [0];
13451345
for (let i = 0; i < 7; ++i) a = a.concat(a);
13461346
a = a.map(function(_, i) { return i; });
13471347
const b = Buffer(a);
13481348
const c = Buffer(b);
1349-
assert.equal(b.length, a.length);
1350-
assert.equal(c.length, a.length);
1349+
assert.strictEqual(b.length, a.length);
1350+
assert.strictEqual(c.length, a.length);
13511351
for (let i = 0, k = a.length; i < k; ++i) {
1352-
assert.equal(a[i], i);
1353-
assert.equal(b[i], i);
1354-
assert.equal(c[i], i);
1352+
assert.strictEqual(a[i], i);
1353+
assert.strictEqual(b[i], i);
1354+
assert.strictEqual(c[i], i);
13551355
}
1356-
})();
1356+
}
13571357

13581358

13591359
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)