Skip to content

Commit 50ee4bd

Browse files
Nataly Shritsaddaleax
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 26ed901 commit 50ee4bd

22 files changed

+40
-43
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ dInput._read = function _read(size) {
5454
};
5555

5656
dOutput._write = function _write(chunk, encoding, cb) {
57-
if (chunk.toString().indexOf('cb_ran') === 0)
57+
if (chunk.toString().startsWith('cb_ran'))
5858
cb_ran = true;
5959
cb();
6060
};

test/disabled/test-sendfd.js

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

9595
assert.strictEqual(rd.pid, cpp);
96-
assert.strictEqual(seenOrdinals.indexOf(rd.ord), -1);
96+
assert.strictEqual(seenOrdinals.includes(rd.ord), false)
9797

9898
seenOrdinals.unshift(rd.ord);
9999
});

test/doctool/test-doctool-html.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ testData.forEach((item) => {
117117
const actual = output.replace(spaces, '');
118118
// Assert that the input stripped of all whitespace contains the
119119
// expected list
120-
assert.notStrictEqual(actual.indexOf(expected), -1);
120+
assert(actual.includes(expected));
121121

122122
// Testing the insertion of Google Analytics script when
123123
// an analytics id is provided. Should not be present by default
124124
if (includeAnalytics) {
125-
assert.notStrictEqual(actual.indexOf('google-analytics.com'), -1,
126-
'Google Analytics script was not present');
125+
assert(actual.includes('google-analytics.com'),
126+
'Google Analytics script was not present');
127127
} else {
128-
assert.strictEqual(actual.indexOf('google-analytics.com'), -1,
128+
assert.strictEqual(actual.includes('google-analytics.com'), false,
129129
'Google Analytics script was present');
130130
}
131131
}));

test/internet/test-dns-any.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const checkers = {
6060
assert.ok(Array.isArray(r.entries));
6161
assert.ok(r.entries.length > 0);
6262
r.entries.forEach((txt) => {
63-
assert.strictEqual(txt.indexOf('v=spf1'), 0);
63+
assert(txt.startsWith('v=spf1'));
6464
});
6565
assert.strictEqual(r.type, 'TXT');
6666
},

test/internet/test-dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ TEST(function test_resolveTxt(done) {
366366
assert.ifError(err);
367367
assert.strictEqual(records.length, 1);
368368
assert.ok(util.isArray(records[0]));
369-
assert.strictEqual(records[0][0].indexOf('v=spf1'), 0);
369+
assert(records[0][0].startsWith('v=spf1'));
370370
done();
371371
});
372372

test/parallel/test-buffer-write.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ encodings
3030
const len = Buffer.byteLength('foo', encoding);
3131
assert.strictEqual(buf.write('foo', 0, len, encoding), len);
3232

33-
if (encoding.indexOf('-') !== -1)
33+
if (encoding.includes('-'))
3434
encoding = encoding.replace('-', '');
3535

3636
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));

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

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

3737
exec(pwdcommand, {cwd: dir}, common.mustCall(function(err, stdout, stderr) {
3838
assert.ifError(err);
39-
assert.strictEqual(stdout.indexOf(dir), 0);
39+
assert(stdout.startsWith(dir));
4040
}));

test/parallel/test-console.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ assert.strictEqual("{ foo: 'bar', inspect: [Function: inspect] }\n",
137137
assert.strictEqual("{ foo: 'bar', inspect: [Function: inspect] }\n",
138138
strings.shift());
139139
assert.ok(strings.shift().includes('foo: [Object]'));
140-
assert.strictEqual(-1, strings.shift().indexOf('baz'));
140+
assert.strictEqual(strings.shift().includes('baz'), false);
141141
assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim()));
142142
assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim()));
143143
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
@@ -47,7 +47,7 @@ socket_ipv6.on('listening', common.mustNotCall());
4747
socket_ipv6.on('error', common.mustCall(function(e) {
4848
// EAFNOSUPPORT or EPROTONOSUPPORT means IPv6 is disabled on this system.
4949
const allowed = ['EADDRNOTAVAIL', 'EAFNOSUPPORT', 'EPROTONOSUPPORT'];
50-
assert.notStrictEqual(allowed.indexOf(e.code), -1);
50+
assert(allowed.includes(e.code));
5151
assert.strictEqual(e.port, undefined);
5252
assert.strictEqual(e.message, `bind ${e.code} 111::1`);
5353
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
@@ -191,7 +191,7 @@ if (process.argv[2] === 'child') {
191191

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

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

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

2929
const serverSockets = [];
3030
const server = http.createServer(function(req, res) {
31-
if (serverSockets.indexOf(req.socket) === -1) {
31+
if (!serverSockets.includes(req.socket)) {
3232
serverSockets.push(req.socket);
3333
}
3434
res.end(req.url);

test/parallel/test-http-methods.js

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

2828
assert(Array.isArray(http.METHODS));
2929
assert(http.METHODS.length > 0);
30-
assert.notStrictEqual(http.METHODS.indexOf('GET'), -1);
31-
assert.notStrictEqual(http.METHODS.indexOf('HEAD'), -1);
32-
assert.notStrictEqual(http.METHODS.indexOf('POST'), -1);
30+
assert(http.METHODS.includes('GET'));
31+
assert(http.METHODS.includes('HEAD'));
32+
assert(http.METHODS.includes('POST'));
3333
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
@@ -67,7 +67,7 @@ function runTest() {
6767
http.get({ port: this.address().port }, common.mustCall((response) => {
6868
response.on('end', common.mustCall(() => {
6969
assert.strictEqual(response.headers['test'], '2');
70-
assert.notStrictEqual(response.rawHeaders.indexOf('Test'), -1);
70+
assert(response.rawHeaders.includes('Test'));
7171
s.close();
7272
}));
7373
response.resume();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ common.expectWarning('DeprecationWarning', expectedWarning);
3434

3535
// test that server.connections property is no longer enumerable now that it
3636
// has been marked as deprecated
37-
assert.strictEqual(Object.keys(server).indexOf('connections'), -1);
37+
assert.strictEqual(Object.keys(server).includes('connections'), false);
3838

3939
assert.strictEqual(server.connections, 0);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ trailingTests.forEach(function(test) {
165165
if (!failed) {
166166
for (let i = 0; i < actualKeys.length; ++i) {
167167
const key = actualKeys[i];
168-
if (expectedKeys.indexOf(key) === -1 || actual[key] !== expected[key]) {
168+
if (!expectedKeys.includes(key) || actual[key] !== expected[key]) {
169169
failed = true;
170170
break;
171171
}

test/parallel/test-process-getgroups.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if (typeof process.getgroups === 'function') {
4545
}
4646

4747
function check(a, b) {
48-
for (let i = 0; i < a.length; ++i) assert.notStrictEqual(b.indexOf(a[i]), -1);
48+
for (let i = 0; i < a.length; ++i) assert(b.includes(a[i]));
4949
}
5050

5151
function unique(groups) {

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ putIn.run(['.clear']);
207207
testMe.complete('require(\'', common.mustCall(function(error, data) {
208208
assert.strictEqual(error, null);
209209
repl._builtinLibs.forEach(function(lib) {
210-
assert.notStrictEqual(data[0].indexOf(lib), -1, `${lib} not found`);
210+
assert(data[0].includes(lib), `${lib} not found`);
211211
});
212212
}));
213213

214214
testMe.complete('require(\'n', common.mustCall(function(error, data) {
215215
assert.strictEqual(error, null);
216216
assert.strictEqual(data.length, 2);
217217
assert.strictEqual(data[1], 'n');
218-
assert.notStrictEqual(data[0].indexOf('net'), -1);
218+
assert(data[0].includes('net'));
219219
// It's possible to pick up non-core modules too
220220
data[0].forEach(function(completion) {
221221
if (completion)
@@ -260,19 +260,19 @@ putIn.run(['.clear']);
260260

261261
putIn.run(['var ary = [1,2,3];']);
262262
testMe.complete('ary.', common.mustCall(function(error, data) {
263-
assert.strictEqual(data[0].indexOf('ary.0'), -1);
264-
assert.strictEqual(data[0].indexOf('ary.1'), -1);
265-
assert.strictEqual(data[0].indexOf('ary.2'), -1);
263+
assert.strictEqual(data[0].includes('ary.0'), false);
264+
assert.strictEqual(data[0].includes('ary.1'), false);
265+
assert.strictEqual(data[0].includes('ary.2'), false);
266266
}));
267267

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

272272
testMe.complete('obj.', common.mustCall(function(error, data) {
273-
assert.strictEqual(data[0].indexOf('obj.1'), -1);
274-
assert.strictEqual(data[0].indexOf('obj.1a'), -1);
275-
assert.notStrictEqual(data[0].indexOf('obj.a'), -1);
273+
assert.strictEqual(data[0].includes('obj.1'), false);
274+
assert.strictEqual(data[0].includes('obj.1a'), false);
275+
assert(data[0].includes('obj.a'));
276276
}));
277277

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

288288
testMe.complete(' obj.', common.mustCall((error, data) => {
289-
assert.strictEqual(data[0].indexOf('obj.1'), -1);
290-
assert.strictEqual(data[0].indexOf('obj.1a'), -1);
291-
assert.notStrictEqual(data[0].indexOf('obj.a'), -1);
289+
assert.strictEqual(data[0].includes('obj.1'), false);
290+
assert.strictEqual(data[0].includes('obj.1a'), false);
291+
assert(data[0].includes('obj.a'));
292292
}));
293293

294294
// Works inside assignments

test/parallel/test-tls-interleave.js

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

6060
data = data.toString();
6161
while (data.length !== 0) {
62-
assert.strictEqual(data.indexOf(writes[receivedWrites]), 0);
62+
assert(data.startsWith(writes[receivedWrites]));
6363
data = data.slice(writes[receivedWrites].length);
6464

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

test/pummel/test-dtrace-jsstack.js

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

89-
if (line.indexOf(sentinel) === -1 || frames.length === 0)
89+
if (!line.includes(sentinel) || frames.length === 0)
9090
continue;
9191

9292
const frame = line.substr(line.indexOf(sentinel) + sentinel.length);
9393
const top = frames.shift();
9494

95-
assert.strictEqual(frame.indexOf(top), 0,
96-
`unexpected frame where ${top} was expected`);
95+
assert(frame.startsWith(top),
96+
`unexpected frame where ${top} was expected`);
9797
}
9898

9999
assert.strictEqual(frames.length, 0,

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

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

5353
function tailCB(data) {
54-
PASS = data.toString().indexOf('.') < 0;
54+
PASS = !data.toString().includes('.');
5555
}
5656

5757

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

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

3737
function tailCB(data) {
38-
PASS = data.toString().indexOf('.') < 0;
38+
PASS = !data.toString().includes('.');
3939

4040
if (PASS) {
4141
//console.error('i');

0 commit comments

Comments
 (0)