Skip to content

Commit 49e7866

Browse files
Nataly ShritsMylesBorins
Nataly Shrits
authored andcommitted
test: replace indexOf with includes and startsWith
PR-URL: #13852 Refs: #12586 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]>
1 parent 2eb926b commit 49e7866

20 files changed

+39
-43
lines changed

test/addons/repl-domain-abort/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dInput._read = function _read(size) {
3333
};
3434

3535
dOutput._write = function _write(chunk, encoding, cb) {
36-
if (chunk.toString().indexOf('cb_ran') === 0)
36+
if (chunk.toString().startsWith('cb_ran'))
3737
cb_ran = true;
3838
cb();
3939
};

test/disabled/test-sendfd.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pipeReadStream.on('data', function(data) {
7474
var rd = JSON.parse(d);
7575

7676
assert.strictEqual(rd.pid, cpp);
77-
assert.strictEqual(seenOrdinals.indexOf(rd.ord), -1);
77+
assert.strictEqual(seenOrdinals.includes(rd.ord), false)
7878

7979
seenOrdinals.unshift(rd.ord);
8080
});

test/doctool/test-doctool-html.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ testData.forEach((item) => {
106106
const actual = output.replace(spaces, '');
107107
// Assert that the input stripped of all whitespace contains the
108108
// expected list
109-
assert.notStrictEqual(actual.indexOf(expected), -1);
109+
assert(actual.includes(expected));
110110

111111
// Testing the insertion of Google Analytics script when
112112
// an analytics id is provided. Should not be present by default
113113
if (includeAnalytics) {
114-
assert.notStrictEqual(actual.indexOf('google-analytics.com'), -1,
115-
'Google Analytics script was not present');
114+
assert(actual.includes('google-analytics.com'),
115+
'Google Analytics script was not present');
116116
} else {
117-
assert.strictEqual(actual.indexOf('google-analytics.com'), -1,
117+
assert.strictEqual(actual.includes('google-analytics.com'), false,
118118
'Google Analytics script was present');
119119
}
120120
}));

test/internet/test-dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ TEST(function test_resolveTxt(done) {
360360
if (err) throw err;
361361
assert.strictEqual(records.length, 1);
362362
assert.ok(util.isArray(records[0]));
363-
assert.strictEqual(records[0][0].indexOf('v=spf1'), 0);
363+
assert(records[0][0].startsWith('v=spf1'));
364364
done();
365365
});
366366

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ if (common.isWindows) {
1515

1616
exec(pwdcommand, {cwd: dir}, common.mustCall(function(err, stdout, stderr) {
1717
assert.ifError(err);
18-
assert.strictEqual(stdout.indexOf(dir), 0);
18+
assert(stdout.startsWith(dir));
1919
}));

test/parallel/test-console.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ assert.strictEqual("{ foo: 'bar', inspect: [Function: inspect] }\n",
115115
strings.shift());
116116
assert.strictEqual("{ foo: 'bar', inspect: [Function: inspect] }\n",
117117
strings.shift());
118-
assert.notStrictEqual(-1, strings.shift().indexOf('foo: [Object]'));
119-
assert.strictEqual(-1, strings.shift().indexOf('baz'));
118+
assert.ok(strings.shift().includes('foo: [Object]'));
119+
assert.strictEqual(strings.shift().includes('baz'), false);
120120
assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim()));
121121
assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim()));
122122
assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim()));

test/parallel/test-dgram-error-message-address.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ socket_ipv6.on('listening', common.mustNotCall());
2626
socket_ipv6.on('error', common.mustCall(function(e) {
2727
// EAFNOSUPPORT or EPROTONOSUPPORT means IPv6 is disabled on this system.
2828
const allowed = ['EADDRNOTAVAIL', 'EAFNOSUPPORT', 'EPROTONOSUPPORT'];
29-
assert.notStrictEqual(allowed.indexOf(e.code), -1);
29+
assert(allowed.includes(e.code));
3030
assert.strictEqual(e.port, undefined);
3131
assert.strictEqual(e.message, `bind ${e.code} 111::1`);
3232
assert.strictEqual(e.address, '111::1');

test/parallel/test-domain-top-level-error-handler-throw.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ if (process.argv[2] === 'child') {
3737
});
3838

3939
child.on('close', function onChildClosed() {
40-
assert.notStrictEqual(
41-
stderrOutput.indexOf(domainErrHandlerExMessage),
42-
-1
43-
);
44-
assert.strictEqual(stderrOutput.indexOf(internalExMessage), -1);
40+
assert(stderrOutput.includes(domainErrHandlerExMessage));
41+
assert.strictEqual(stderrOutput.includes(internalExMessage), false);
4542
});
4643

4744
child.on('exit', function onChildExited(exitCode, signal) {

test/parallel/test-domain-uncaught-exception.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ if (process.argv[2] === 'child') {
190190

191191
if (test.messagesReceived) {
192192
test.messagesReceived.forEach(function(receivedMessage) {
193-
if (test.expectedMessages.indexOf(receivedMessage) === -1) {
193+
if (!test.expectedMessages.includes(receivedMessage)) {
194194
assert(false, `test ${test.fn.name} should not have sent message: ${
195195
receivedMessage} but did`);
196196
}

test/parallel/test-http-keepalive-maxsockets.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const http = require('http');
77

88
const serverSockets = [];
99
const server = http.createServer(function(req, res) {
10-
if (serverSockets.indexOf(req.socket) === -1) {
10+
if (!serverSockets.includes(req.socket)) {
1111
serverSockets.push(req.socket);
1212
}
1313
res.end(req.url);

test/parallel/test-http-methods.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const util = require('util');
66

77
assert(Array.isArray(http.METHODS));
88
assert(http.METHODS.length > 0);
9-
assert.notStrictEqual(http.METHODS.indexOf('GET'), -1);
10-
assert.notStrictEqual(http.METHODS.indexOf('HEAD'), -1);
11-
assert.notStrictEqual(http.METHODS.indexOf('POST'), -1);
9+
assert(http.METHODS.includes('GET'));
10+
assert(http.METHODS.includes('HEAD'));
11+
assert(http.METHODS.includes('POST'));
1212
assert.deepStrictEqual(util._extend([], http.METHODS), http.METHODS.sort());

test/parallel/test-http-write-head.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function runTest() {
4646
http.get({ port: this.address().port }, common.mustCall((response) => {
4747
response.on('end', common.mustCall(() => {
4848
assert.strictEqual(response.headers['test'], '2');
49-
assert.notStrictEqual(response.rawHeaders.indexOf('Test'), -1);
49+
assert(response.rawHeaders.includes('Test'));
5050
s.close();
5151
}));
5252
response.resume();

test/parallel/test-net-server-connections.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const net = require('net');
66

77
// test that server.connections property is no longer enumerable now that it
88
// has been marked as deprecated
9-
109
const server = new net.Server();
1110

12-
assert.strictEqual(Object.keys(server).indexOf('connections'), -1);
11+
assert.strictEqual(Object.keys(server).includes('connections'), false);

test/parallel/test-path-parse-format.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ trailingTests.forEach(function(test) {
154154
if (!failed) {
155155
for (let i = 0; i < actualKeys.length; ++i) {
156156
const key = actualKeys[i];
157-
if (expectedKeys.indexOf(key) === -1 || actual[key] !== expected[key]) {
157+
if (!expectedKeys.includes(key) || actual[key] !== expected[key]) {
158158
failed = true;
159159
break;
160160
}

test/parallel/test-process-getgroups.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (typeof process.getgroups === 'function') {
2424
}
2525

2626
function check(a, b) {
27-
for (let i = 0; i < a.length; ++i) assert.notStrictEqual(b.indexOf(a[i]), -1);
27+
for (let i = 0; i < a.length; ++i) assert(b.includes(a[i]));
2828
}
2929

3030
function unique(groups) {

test/parallel/test-repl-tab-complete.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ putIn.run(['.clear']);
186186
testMe.complete('require(\'', common.mustCall(function(error, data) {
187187
assert.strictEqual(error, null);
188188
repl._builtinLibs.forEach(function(lib) {
189-
assert.notStrictEqual(data[0].indexOf(lib), -1, `${lib} not found`);
189+
assert(data[0].includes(lib), `${lib} not found`);
190190
});
191191
}));
192192

193193
testMe.complete('require(\'n', common.mustCall(function(error, data) {
194194
assert.strictEqual(error, null);
195195
assert.strictEqual(data.length, 2);
196196
assert.strictEqual(data[1], 'n');
197-
assert.notStrictEqual(data[0].indexOf('net'), -1);
197+
assert(data[0].includes('net'));
198198
// It's possible to pick up non-core modules too
199199
data[0].forEach(function(completion) {
200200
if (completion)
@@ -239,19 +239,19 @@ putIn.run(['.clear']);
239239

240240
putIn.run(['var ary = [1,2,3];']);
241241
testMe.complete('ary.', common.mustCall(function(error, data) {
242-
assert.strictEqual(data[0].indexOf('ary.0'), -1);
243-
assert.strictEqual(data[0].indexOf('ary.1'), -1);
244-
assert.strictEqual(data[0].indexOf('ary.2'), -1);
242+
assert.strictEqual(data[0].includes('ary.0'), false);
243+
assert.strictEqual(data[0].includes('ary.1'), false);
244+
assert.strictEqual(data[0].includes('ary.2'), false);
245245
}));
246246

247247
// Make sure tab completion does not include integer keys in an object
248248
putIn.run(['.clear']);
249249
putIn.run(['var obj = {1:"a","1a":"b",a:"b"};']);
250250

251251
testMe.complete('obj.', common.mustCall(function(error, data) {
252-
assert.strictEqual(data[0].indexOf('obj.1'), -1);
253-
assert.strictEqual(data[0].indexOf('obj.1a'), -1);
254-
assert.notStrictEqual(data[0].indexOf('obj.a'), -1);
252+
assert.strictEqual(data[0].includes('obj.1'), false);
253+
assert.strictEqual(data[0].includes('obj.1a'), false);
254+
assert(data[0].includes('obj.a'));
255255
}));
256256

257257
// Don't try to complete results of non-simple expressions
@@ -265,9 +265,9 @@ putIn.run(['.clear']);
265265
putIn.run(['var obj = {1:"a","1a":"b",a:"b"};']);
266266

267267
testMe.complete(' obj.', common.mustCall((error, data) => {
268-
assert.strictEqual(data[0].indexOf('obj.1'), -1);
269-
assert.strictEqual(data[0].indexOf('obj.1a'), -1);
270-
assert.notStrictEqual(data[0].indexOf('obj.a'), -1);
268+
assert.strictEqual(data[0].includes('obj.1'), false);
269+
assert.strictEqual(data[0].includes('obj.1a'), false);
270+
assert(data[0].includes('obj.a'));
271271
}));
272272

273273
// Works inside assignments

test/parallel/test-tls-interleave.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const server = tls.createServer(options, function(c) {
3838

3939
data = data.toString();
4040
while (data.length !== 0) {
41-
assert.strictEqual(data.indexOf(writes[receivedWrites]), 0);
41+
assert(data.startsWith(writes[receivedWrites]));
4242
data = data.slice(writes[receivedWrites].length);
4343

4444
if (++receivedWrites === writes.length) {

test/pummel/test-dtrace-jsstack.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ dtrace.on('exit', function(code) {
6565
for (let i = 0; i < lines.length; i++) {
6666
const line = lines[i];
6767

68-
if (line.indexOf(sentinel) === -1 || frames.length === 0)
68+
if (!line.includes(sentinel) || frames.length === 0)
6969
continue;
7070

7171
const frame = line.substr(line.indexOf(sentinel) + sentinel.length);
7272
const top = frames.shift();
7373

74-
assert.strictEqual(frame.indexOf(top), 0,
75-
`unexpected frame where ${top} was expected`);
74+
assert(frame.startsWith(top),
75+
`unexpected frame where ${top} was expected`);
7676
}
7777

7878
assert.strictEqual(frames.length, 0,

test/pummel/test-regress-GH-814.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const tail = require('child_process').spawn('tail', ['-f', testFileName]);
2929
tail.stdout.on('data', tailCB);
3030

3131
function tailCB(data) {
32-
PASS = data.toString().indexOf('.') < 0;
32+
PASS = !data.toString().includes('.');
3333
}
3434

3535

test/pummel/test-regress-GH-814_2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const tailProc = require('child_process').spawn('tail', ['-f', testFileName]);
1313
tailProc.stdout.on('data', tailCB);
1414

1515
function tailCB(data) {
16-
PASS = data.toString().indexOf('.') < 0;
16+
PASS = !data.toString().includes('.');
1717

1818
if (PASS) {
1919
//console.error('i');

0 commit comments

Comments
 (0)