From 2a2db0278b466f5eb701baa735cea03e85bd7897 Mon Sep 17 00:00:00 2001 From: Nataly Shrits Date: Wed, 21 Jun 2017 21:09:37 +0300 Subject: [PATCH] test: replace indexOf with includes and startsWith PR-URL: https://github.com/nodejs/node/pull/13852 Refs: https://github.com/nodejs/node/issues/12586 Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: Vse Mozhet Byt --- test/addons/repl-domain-abort/test.js | 2 +- test/disabled/test-sendfd.js | 2 +- test/doctool/test-doctool-html.js | 8 +++---- test/internet/test-dns.js | 2 +- test/parallel/test-child-process-exec-cwd.js | 2 +- test/parallel/test-console.js | 4 ++-- .../test-dgram-error-message-address.js | 2 +- ...st-domain-top-level-error-handler-throw.js | 7 ++---- .../test-domain-uncaught-exception.js | 2 +- .../test-http-keepalive-maxsockets.js | 2 +- test/parallel/test-http-methods.js | 6 ++--- test/parallel/test-http-write-head.js | 2 +- test/parallel/test-net-server-connections.js | 3 +-- test/parallel/test-path-parse-format.js | 2 +- test/parallel/test-process-getgroups.js | 2 +- test/parallel/test-repl-tab-complete.js | 22 +++++++++---------- test/parallel/test-tls-interleave.js | 2 +- test/pummel/test-dtrace-jsstack.js | 6 ++--- test/pummel/test-regress-GH-814.js | 2 +- test/pummel/test-regress-GH-814_2.js | 2 +- 20 files changed, 39 insertions(+), 43 deletions(-) diff --git a/test/addons/repl-domain-abort/test.js b/test/addons/repl-domain-abort/test.js index 969beec0d12442..1d0e5202b6a6f2 100644 --- a/test/addons/repl-domain-abort/test.js +++ b/test/addons/repl-domain-abort/test.js @@ -33,7 +33,7 @@ dInput._read = function _read(size) { }; dOutput._write = function _write(chunk, encoding, cb) { - if (chunk.toString().indexOf('cb_ran') === 0) + if (chunk.toString().startsWith('cb_ran')) cb_ran = true; cb(); }; diff --git a/test/disabled/test-sendfd.js b/test/disabled/test-sendfd.js index 8de6045cb27d90..68d40993716093 100644 --- a/test/disabled/test-sendfd.js +++ b/test/disabled/test-sendfd.js @@ -74,7 +74,7 @@ pipeReadStream.on('data', function(data) { var rd = JSON.parse(d); assert.strictEqual(rd.pid, cpp); - assert.strictEqual(seenOrdinals.indexOf(rd.ord), -1); + assert.strictEqual(seenOrdinals.includes(rd.ord), false) seenOrdinals.unshift(rd.ord); }); diff --git a/test/doctool/test-doctool-html.js b/test/doctool/test-doctool-html.js index b28d34b57e6edc..35fa921a3e9971 100644 --- a/test/doctool/test-doctool-html.js +++ b/test/doctool/test-doctool-html.js @@ -106,15 +106,15 @@ testData.forEach((item) => { const actual = output.replace(spaces, ''); // Assert that the input stripped of all whitespace contains the // expected list - assert.notStrictEqual(actual.indexOf(expected), -1); + assert(actual.includes(expected)); // Testing the insertion of Google Analytics script when // an analytics id is provided. Should not be present by default if (includeAnalytics) { - assert.notStrictEqual(actual.indexOf('google-analytics.com'), -1, - 'Google Analytics script was not present'); + assert(actual.includes('google-analytics.com'), + 'Google Analytics script was not present'); } else { - assert.strictEqual(actual.indexOf('google-analytics.com'), -1, + assert.strictEqual(actual.includes('google-analytics.com'), false, 'Google Analytics script was present'); } })); diff --git a/test/internet/test-dns.js b/test/internet/test-dns.js index ba40146c58342b..68365e59c107c3 100644 --- a/test/internet/test-dns.js +++ b/test/internet/test-dns.js @@ -360,7 +360,7 @@ TEST(function test_resolveTxt(done) { if (err) throw err; assert.strictEqual(records.length, 1); assert.ok(util.isArray(records[0])); - assert.strictEqual(records[0][0].indexOf('v=spf1'), 0); + assert(records[0][0].startsWith('v=spf1')); done(); }); diff --git a/test/parallel/test-child-process-exec-cwd.js b/test/parallel/test-child-process-exec-cwd.js index 95feb42314fc8d..d8d64776551278 100644 --- a/test/parallel/test-child-process-exec-cwd.js +++ b/test/parallel/test-child-process-exec-cwd.js @@ -15,5 +15,5 @@ if (common.isWindows) { exec(pwdcommand, {cwd: dir}, common.mustCall(function(err, stdout, stderr) { assert.ifError(err); - assert.strictEqual(stdout.indexOf(dir), 0); + assert(stdout.startsWith(dir)); })); diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 4618bbd28cf091..7d298674273ecf 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -115,8 +115,8 @@ assert.strictEqual("{ foo: 'bar', inspect: [Function: inspect] }\n", strings.shift()); assert.strictEqual("{ foo: 'bar', inspect: [Function: inspect] }\n", strings.shift()); -assert.notStrictEqual(-1, strings.shift().indexOf('foo: [Object]')); -assert.strictEqual(-1, strings.shift().indexOf('baz')); +assert.ok(strings.shift().includes('foo: [Object]')); +assert.strictEqual(strings.shift().includes('baz'), false); assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim())); diff --git a/test/parallel/test-dgram-error-message-address.js b/test/parallel/test-dgram-error-message-address.js index 211adcffcc67b5..84b21d0f851347 100644 --- a/test/parallel/test-dgram-error-message-address.js +++ b/test/parallel/test-dgram-error-message-address.js @@ -26,7 +26,7 @@ socket_ipv6.on('listening', common.mustNotCall()); socket_ipv6.on('error', common.mustCall(function(e) { // EAFNOSUPPORT or EPROTONOSUPPORT means IPv6 is disabled on this system. const allowed = ['EADDRNOTAVAIL', 'EAFNOSUPPORT', 'EPROTONOSUPPORT']; - assert.notStrictEqual(allowed.indexOf(e.code), -1); + assert(allowed.includes(e.code)); assert.strictEqual(e.port, undefined); assert.strictEqual(e.message, `bind ${e.code} 111::1`); assert.strictEqual(e.address, '111::1'); diff --git a/test/parallel/test-domain-top-level-error-handler-throw.js b/test/parallel/test-domain-top-level-error-handler-throw.js index 53a73eac426232..86df8db5b7423f 100644 --- a/test/parallel/test-domain-top-level-error-handler-throw.js +++ b/test/parallel/test-domain-top-level-error-handler-throw.js @@ -37,11 +37,8 @@ if (process.argv[2] === 'child') { }); child.on('close', function onChildClosed() { - assert.notStrictEqual( - stderrOutput.indexOf(domainErrHandlerExMessage), - -1 - ); - assert.strictEqual(stderrOutput.indexOf(internalExMessage), -1); + assert(stderrOutput.includes(domainErrHandlerExMessage)); + assert.strictEqual(stderrOutput.includes(internalExMessage), false); }); child.on('exit', function onChildExited(exitCode, signal) { diff --git a/test/parallel/test-domain-uncaught-exception.js b/test/parallel/test-domain-uncaught-exception.js index 594cee542f80fb..bb6dd33e37b73a 100644 --- a/test/parallel/test-domain-uncaught-exception.js +++ b/test/parallel/test-domain-uncaught-exception.js @@ -190,7 +190,7 @@ if (process.argv[2] === 'child') { if (test.messagesReceived) { test.messagesReceived.forEach(function(receivedMessage) { - if (test.expectedMessages.indexOf(receivedMessage) === -1) { + if (!test.expectedMessages.includes(receivedMessage)) { assert(false, `test ${test.fn.name} should not have sent message: ${ receivedMessage} but did`); } diff --git a/test/parallel/test-http-keepalive-maxsockets.js b/test/parallel/test-http-keepalive-maxsockets.js index fd01fa02750853..de3bdea54f1f5d 100644 --- a/test/parallel/test-http-keepalive-maxsockets.js +++ b/test/parallel/test-http-keepalive-maxsockets.js @@ -7,7 +7,7 @@ const http = require('http'); const serverSockets = []; const server = http.createServer(function(req, res) { - if (serverSockets.indexOf(req.socket) === -1) { + if (!serverSockets.includes(req.socket)) { serverSockets.push(req.socket); } res.end(req.url); diff --git a/test/parallel/test-http-methods.js b/test/parallel/test-http-methods.js index 6ba02807f8706b..fb49add6a2ad24 100644 --- a/test/parallel/test-http-methods.js +++ b/test/parallel/test-http-methods.js @@ -6,7 +6,7 @@ const util = require('util'); assert(Array.isArray(http.METHODS)); assert(http.METHODS.length > 0); -assert.notStrictEqual(http.METHODS.indexOf('GET'), -1); -assert.notStrictEqual(http.METHODS.indexOf('HEAD'), -1); -assert.notStrictEqual(http.METHODS.indexOf('POST'), -1); +assert(http.METHODS.includes('GET')); +assert(http.METHODS.includes('HEAD')); +assert(http.METHODS.includes('POST')); assert.deepStrictEqual(util._extend([], http.METHODS), http.METHODS.sort()); diff --git a/test/parallel/test-http-write-head.js b/test/parallel/test-http-write-head.js index 2ceba8965c0ea4..c6b2a5e41ce820 100644 --- a/test/parallel/test-http-write-head.js +++ b/test/parallel/test-http-write-head.js @@ -46,7 +46,7 @@ function runTest() { http.get({ port: this.address().port }, common.mustCall((response) => { response.on('end', common.mustCall(() => { assert.strictEqual(response.headers['test'], '2'); - assert.notStrictEqual(response.rawHeaders.indexOf('Test'), -1); + assert(response.rawHeaders.includes('Test')); s.close(); })); response.resume(); diff --git a/test/parallel/test-net-server-connections.js b/test/parallel/test-net-server-connections.js index 7ee1df1a544b95..491a97bddc3ffa 100644 --- a/test/parallel/test-net-server-connections.js +++ b/test/parallel/test-net-server-connections.js @@ -6,7 +6,6 @@ const net = require('net'); // test that server.connections property is no longer enumerable now that it // has been marked as deprecated - const server = new net.Server(); -assert.strictEqual(Object.keys(server).indexOf('connections'), -1); +assert.strictEqual(Object.keys(server).includes('connections'), false); diff --git a/test/parallel/test-path-parse-format.js b/test/parallel/test-path-parse-format.js index f5d9881727f40f..a914f815410d2e 100644 --- a/test/parallel/test-path-parse-format.js +++ b/test/parallel/test-path-parse-format.js @@ -154,7 +154,7 @@ trailingTests.forEach(function(test) { if (!failed) { for (let i = 0; i < actualKeys.length; ++i) { const key = actualKeys[i]; - if (expectedKeys.indexOf(key) === -1 || actual[key] !== expected[key]) { + if (!expectedKeys.includes(key) || actual[key] !== expected[key]) { failed = true; break; } diff --git a/test/parallel/test-process-getgroups.js b/test/parallel/test-process-getgroups.js index 390f188cf5bf4d..fae363e2196628 100644 --- a/test/parallel/test-process-getgroups.js +++ b/test/parallel/test-process-getgroups.js @@ -24,7 +24,7 @@ if (typeof process.getgroups === 'function') { } function check(a, b) { - for (let i = 0; i < a.length; ++i) assert.notStrictEqual(b.indexOf(a[i]), -1); + for (let i = 0; i < a.length; ++i) assert(b.includes(a[i])); } function unique(groups) { diff --git a/test/parallel/test-repl-tab-complete.js b/test/parallel/test-repl-tab-complete.js index e2b9b363fa53c6..11858564ef1035 100644 --- a/test/parallel/test-repl-tab-complete.js +++ b/test/parallel/test-repl-tab-complete.js @@ -186,7 +186,7 @@ putIn.run(['.clear']); testMe.complete('require(\'', common.mustCall(function(error, data) { assert.strictEqual(error, null); repl._builtinLibs.forEach(function(lib) { - assert.notStrictEqual(data[0].indexOf(lib), -1, `${lib} not found`); + assert(data[0].includes(lib), `${lib} not found`); }); })); @@ -194,7 +194,7 @@ testMe.complete('require(\'n', common.mustCall(function(error, data) { assert.strictEqual(error, null); assert.strictEqual(data.length, 2); assert.strictEqual(data[1], 'n'); - assert.notStrictEqual(data[0].indexOf('net'), -1); + assert(data[0].includes('net')); // It's possible to pick up non-core modules too data[0].forEach(function(completion) { if (completion) @@ -239,9 +239,9 @@ putIn.run(['.clear']); putIn.run(['var ary = [1,2,3];']); testMe.complete('ary.', common.mustCall(function(error, data) { - assert.strictEqual(data[0].indexOf('ary.0'), -1); - assert.strictEqual(data[0].indexOf('ary.1'), -1); - assert.strictEqual(data[0].indexOf('ary.2'), -1); + assert.strictEqual(data[0].includes('ary.0'), false); + assert.strictEqual(data[0].includes('ary.1'), false); + assert.strictEqual(data[0].includes('ary.2'), false); })); // Make sure tab completion does not include integer keys in an object @@ -249,9 +249,9 @@ putIn.run(['.clear']); putIn.run(['var obj = {1:"a","1a":"b",a:"b"};']); testMe.complete('obj.', common.mustCall(function(error, data) { - assert.strictEqual(data[0].indexOf('obj.1'), -1); - assert.strictEqual(data[0].indexOf('obj.1a'), -1); - assert.notStrictEqual(data[0].indexOf('obj.a'), -1); + assert.strictEqual(data[0].includes('obj.1'), false); + assert.strictEqual(data[0].includes('obj.1a'), false); + assert(data[0].includes('obj.a')); })); // Don't try to complete results of non-simple expressions @@ -265,9 +265,9 @@ putIn.run(['.clear']); putIn.run(['var obj = {1:"a","1a":"b",a:"b"};']); testMe.complete(' obj.', common.mustCall((error, data) => { - assert.strictEqual(data[0].indexOf('obj.1'), -1); - assert.strictEqual(data[0].indexOf('obj.1a'), -1); - assert.notStrictEqual(data[0].indexOf('obj.a'), -1); + assert.strictEqual(data[0].includes('obj.1'), false); + assert.strictEqual(data[0].includes('obj.1a'), false); + assert(data[0].includes('obj.a')); })); // Works inside assignments diff --git a/test/parallel/test-tls-interleave.js b/test/parallel/test-tls-interleave.js index 60d778b77de268..1bbba9aff73d44 100644 --- a/test/parallel/test-tls-interleave.js +++ b/test/parallel/test-tls-interleave.js @@ -38,7 +38,7 @@ const server = tls.createServer(options, function(c) { data = data.toString(); while (data.length !== 0) { - assert.strictEqual(data.indexOf(writes[receivedWrites]), 0); + assert(data.startsWith(writes[receivedWrites])); data = data.slice(writes[receivedWrites].length); if (++receivedWrites === writes.length) { diff --git a/test/pummel/test-dtrace-jsstack.js b/test/pummel/test-dtrace-jsstack.js index ad58aaf7615081..9833324e072f93 100644 --- a/test/pummel/test-dtrace-jsstack.js +++ b/test/pummel/test-dtrace-jsstack.js @@ -65,14 +65,14 @@ dtrace.on('exit', function(code) { for (let i = 0; i < lines.length; i++) { const line = lines[i]; - if (line.indexOf(sentinel) === -1 || frames.length === 0) + if (!line.includes(sentinel) || frames.length === 0) continue; const frame = line.substr(line.indexOf(sentinel) + sentinel.length); const top = frames.shift(); - assert.strictEqual(frame.indexOf(top), 0, - `unexpected frame where ${top} was expected`); + assert(frame.startsWith(top), + `unexpected frame where ${top} was expected`); } assert.strictEqual(frames.length, 0, diff --git a/test/pummel/test-regress-GH-814.js b/test/pummel/test-regress-GH-814.js index a3700c500b4f53..58ba57dc6c88e8 100644 --- a/test/pummel/test-regress-GH-814.js +++ b/test/pummel/test-regress-GH-814.js @@ -29,7 +29,7 @@ const tail = require('child_process').spawn('tail', ['-f', testFileName]); tail.stdout.on('data', tailCB); function tailCB(data) { - PASS = data.toString().indexOf('.') < 0; + PASS = !data.toString().includes('.'); } diff --git a/test/pummel/test-regress-GH-814_2.js b/test/pummel/test-regress-GH-814_2.js index da315e3fc2b007..48f5de63efdc1c 100644 --- a/test/pummel/test-regress-GH-814_2.js +++ b/test/pummel/test-regress-GH-814_2.js @@ -13,7 +13,7 @@ const tailProc = require('child_process').spawn('tail', ['-f', testFileName]); tailProc.stdout.on('data', tailCB); function tailCB(data) { - PASS = data.toString().indexOf('.') < 0; + PASS = !data.toString().includes('.'); if (PASS) { //console.error('i');