Skip to content

Commit

Permalink
test: replace indexOf with includes
Browse files Browse the repository at this point in the history
Start the transition to Array.prototype.includes() and
String.prototype.includes().  This commit refactors most of the
comparisons of Array.prototype.indexOf() and String.prototype.indexOf()
return values with -1 to the former methods in tests.

PR-URL: #12604
Refs: #12586
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
  • Loading branch information
gwer authored and aqrln committed Apr 30, 2017
1 parent 0324ac6 commit 0142276
Show file tree
Hide file tree
Showing 23 changed files with 83 additions and 83 deletions.
16 changes: 8 additions & 8 deletions test/addons-napi/test_constructor/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ const propertyNames = [];
for (const name in test_object) {
propertyNames.push(name);
}
assert.ok(propertyNames.indexOf('echo') >= 0);
assert.ok(propertyNames.indexOf('readwriteValue') >= 0);
assert.ok(propertyNames.indexOf('readonlyValue') >= 0);
assert.ok(propertyNames.indexOf('hiddenValue') < 0);
assert.ok(propertyNames.indexOf('readwriteAccessor1') < 0);
assert.ok(propertyNames.indexOf('readwriteAccessor2') < 0);
assert.ok(propertyNames.indexOf('readonlyAccessor1') < 0);
assert.ok(propertyNames.indexOf('readonlyAccessor2') < 0);
assert.ok(propertyNames.includes('echo'));
assert.ok(propertyNames.includes('readwriteValue'));
assert.ok(propertyNames.includes('readonlyValue'));
assert.ok(!propertyNames.includes('hiddenValue'));
assert.ok(!propertyNames.includes('readwriteAccessor1'));
assert.ok(!propertyNames.includes('readwriteAccessor2'));
assert.ok(!propertyNames.includes('readonlyAccessor1'));
assert.ok(!propertyNames.includes('readonlyAccessor2'));

// The napi_writable attribute should be ignored for accessors.
test_object.readwriteAccessor1 = 1;
Expand Down
16 changes: 8 additions & 8 deletions test/addons-napi/test_properties/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const propertyNames = [];
for (const name in test_object) {
propertyNames.push(name);
}
assert.ok(propertyNames.indexOf('echo') >= 0);
assert.ok(propertyNames.indexOf('readwriteValue') >= 0);
assert.ok(propertyNames.indexOf('readonlyValue') >= 0);
assert.ok(propertyNames.indexOf('hiddenValue') < 0);
assert.ok(propertyNames.indexOf('readwriteAccessor1') < 0);
assert.ok(propertyNames.indexOf('readwriteAccessor2') < 0);
assert.ok(propertyNames.indexOf('readonlyAccessor1') < 0);
assert.ok(propertyNames.indexOf('readonlyAccessor2') < 0);
assert.ok(propertyNames.includes('echo'));
assert.ok(propertyNames.includes('readwriteValue'));
assert.ok(propertyNames.includes('readonlyValue'));
assert.ok(!propertyNames.includes('hiddenValue'));
assert.ok(!propertyNames.includes('readwriteAccessor1'));
assert.ok(!propertyNames.includes('readwriteAccessor2'));
assert.ok(!propertyNames.includes('readonlyAccessor1'));
assert.ok(!propertyNames.includes('readonlyAccessor2'));

// The napi_writable attribute should be ignored for accessors.
test_object.readwriteAccessor1 = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/inspector/test-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function checkBadPath(err, response) {
function expectMainScriptSource(result) {
const expected = helper.mainScriptSource();
const source = result['scriptSource'];
assert(source && (source.indexOf(expected) >= 0),
assert(source && (source.includes(expected)),
'Script source is wrong: ' + source);
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ child.stdout.on('data', function(chunk) {
});

process.on('exit', function() {
assert.ok(response.indexOf('HELLO=WORLD') >= 0,
assert.ok(response.includes('HELLO=WORLD'),
'spawn did not use process.env as default');
});
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ child.stdout.on('data', function(chunk) {
});

process.on('exit', function() {
assert.ok(response.indexOf('HELLO=WORLD') >= 0);
assert.ok(response.indexOf('FOO=BAR') >= 0);
assert.ok(response.includes('HELLO=WORLD'));
assert.ok(response.includes('FOO=BAR'));
});
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-exec-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ process.on('exit', function() {
console.log('response: ', response);
assert.strictEqual(1, success_count);
assert.strictEqual(0, error_count);
assert.ok(response.indexOf('HELLO=WORLD') >= 0);
assert.ok(response.includes('HELLO=WORLD'));
});
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-spawnsync-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function verifyBufOutput(ret) {
assert.deepStrictEqual(ret.stderr, msgErrBuf);
}

if (process.argv.indexOf('spawnchild') !== -1) {
if (process.argv.includes('spawnchild')) {
switch (process.argv[3]) {
case '1':
ret = spawnSync(process.execPath, args, { stdio: 'inherit' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE = 42;

if (process.argv[2] === 'child') {
process.on('uncaughtException', common.mustCall(function onUncaught() {
if (process.execArgv.indexOf('--abort-on-uncaught-exception') !== -1) {
if (process.execArgv.includes('--abort-on-uncaught-exception')) {
// When passing --abort-on-uncaught-exception to the child process,
// we want to make sure that this handler (the process' uncaughtException
// event handler) wasn't called. Unfortunately we can't parse the child
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-domain-with-abort-on-uncaught-exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ if (process.argv[2] === 'child') {
d.on('error', function(err) {
// Swallowing the error on purpose if 'throwInDomainErrHandler' is not
// set
if (process.argv.indexOf('throwInDomainErrHandler') !== -1) {
if (process.argv.includes('throwInDomainErrHandler')) {
// If useTryCatch is set, wrap the throw in a try/catch block.
// This is to make sure that a caught exception does not trigger
// an abort.
if (process.argv.indexOf('useTryCatch') !== -1) {
if (process.argv.includes('useTryCatch')) {
try {
throw new Error(domainErrHandlerExMessage);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-error-reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function errExec(script, callback) {
assert.ok(stderr.split('\n').length > 2);

// Assert the script is mentioned in error output.
assert.ok(stderr.indexOf(script) >= 0);
assert.ok(stderr.includes(script));

// Proxy the args for more tests.
callback(err, stdout, stderr);
Expand Down
72 changes: 36 additions & 36 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,66 +34,66 @@ const existingDir2 = path.join(common.fixturesDir, 'keys');

fs.stat(fn, function(err) {
assert.strictEqual(fn, err.path);
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.lstat(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.readlink(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.link(fn, 'foo', function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.link(existingFile, existingFile2, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
assert.ok(err.message.includes(existingFile));
assert.ok(err.message.includes(existingFile2));
});

fs.symlink(existingFile, existingFile2, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
assert.ok(err.message.includes(existingFile));
assert.ok(err.message.includes(existingFile2));
});

fs.unlink(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.rename(fn, 'foo', function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.rename(existingDir, existingDir2, function(err) {
assert.ok(0 <= err.message.indexOf(existingDir));
assert.ok(0 <= err.message.indexOf(existingDir2));
assert.ok(err.message.includes(existingDir));
assert.ok(err.message.includes(existingDir2));
});

fs.rmdir(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.mkdir(existingFile, 0o666, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(err.message.includes(existingFile));
});

fs.rmdir(existingFile, function(err) {
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(err.message.includes(existingFile));
});

fs.chmod(fn, 0o666, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.open(fn, 'r', 0o666, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

fs.readFile(fn, function(err) {
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
});

// Sync
Expand All @@ -106,122 +106,122 @@ try {
fs.statSync(fn);
} catch (err) {
errors.push('stat');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.mkdirSync(existingFile, 0o666);
} catch (err) {
errors.push('mkdir');
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(err.message.includes(existingFile));
}

try {
++expected;
fs.chmodSync(fn, 0o666);
} catch (err) {
errors.push('chmod');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.lstatSync(fn);
} catch (err) {
errors.push('lstat');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.readlinkSync(fn);
} catch (err) {
errors.push('readlink');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.linkSync(fn, 'foo');
} catch (err) {
errors.push('link');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.linkSync(existingFile, existingFile2);
} catch (err) {
errors.push('link');
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
assert.ok(err.message.includes(existingFile));
assert.ok(err.message.includes(existingFile2));
}

try {
++expected;
fs.symlinkSync(existingFile, existingFile2);
} catch (err) {
errors.push('symlink');
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(0 <= err.message.indexOf(existingFile2));
assert.ok(err.message.includes(existingFile));
assert.ok(err.message.includes(existingFile2));
}

try {
++expected;
fs.unlinkSync(fn);
} catch (err) {
errors.push('unlink');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.rmdirSync(fn);
} catch (err) {
errors.push('rmdir');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.rmdirSync(existingFile);
} catch (err) {
errors.push('rmdir');
assert.ok(0 <= err.message.indexOf(existingFile));
assert.ok(err.message.includes(existingFile));
}

try {
++expected;
fs.openSync(fn, 'r');
} catch (err) {
errors.push('opens');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.renameSync(fn, 'foo');
} catch (err) {
errors.push('rename');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

try {
++expected;
fs.renameSync(existingDir, existingDir2);
} catch (err) {
errors.push('rename');
assert.ok(0 <= err.message.indexOf(existingDir));
assert.ok(0 <= err.message.indexOf(existingDir2));
assert.ok(err.message.includes(existingDir));
assert.ok(err.message.includes(existingDir2));
}

try {
++expected;
fs.readdirSync(fn);
} catch (err) {
errors.push('readdir');
assert.ok(0 <= err.message.indexOf(fn));
assert.ok(err.message.includes(fn));
}

process.on('exit', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-client-parse-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ net.createServer(function(c) {
path: '/'
}).on('error', function(e) {
console.log('got error from client');
assert.ok(e.message.indexOf('Parse Error') >= 0);
assert.ok(e.message.includes('Parse Error'));
assert.strictEqual(e.code, 'HPE_INVALID_CONSTANT');
parseErrors++;
}).end();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-extra-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const server = net.createServer(function(socket) {
socket.on('data', function(chunk) {
postBody += chunk;

if (postBody.indexOf('\r\n') > -1) {
if (postBody.includes('\r\n')) {
socket.write(fullResponse);
// omg, I wrote the response twice, what a terrible HTTP server I am.
socket.end(fullResponse);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-get-pipeline-problem.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function checkFiles() {

for (let i = 0; i < total; i++) {
const fn = i + '.jpg';
assert.ok(files.indexOf(fn) >= 0, "couldn't find '" + fn + "'");
assert.ok(files.includes(fn), "couldn't find '" + fn + "'");
const stat = fs.statSync(common.tmpDir + '/' + fn);
assert.strictEqual(image.length, stat.size,
"size doesn't match on '" + fn +
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-https-strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ function makeReq(path, port, error, host, ca) {
options.agent = agent0;
} else {
if (!Array.isArray(ca)) ca = [ca];
if (-1 !== ca.indexOf(ca1) && -1 !== ca.indexOf(ca2)) {
if (ca.includes(ca1) && ca.includes(ca2)) {
options.agent = agent3;
} else if (-1 !== ca.indexOf(ca1)) {
} else if (ca.includes(ca1)) {
options.agent = agent1;
} else if (-1 !== ca.indexOf(ca2)) {
} else if (ca.includes(ca2)) {
options.agent = agent2;
} else {
options.agent = agent0;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-intl.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (enablei18n === undefined) {
// Else, returns false
function haveLocale(loc) {
const locs = process.config.variables.icu_locales.split(',');
return locs.indexOf(loc) !== -1;
return locs.includes(loc);
}

// Always run these. They should always pass, even if the locale
Expand Down
Loading

0 comments on commit 0142276

Please sign in to comment.