Skip to content

Commit

Permalink
test: replace function with arrow function
Browse files Browse the repository at this point in the history
1. Among the list of Code and Learn,
I solved the unfinished task of replacing function with arrow function:
nodejs/code-and-learn#72 (comment)

2. Replace arrow function with shorter property syntax
Arrow function makes `this` lexical scope.
But toString expects evaluate `this` in runtime.

3. Replace this with null
makeBlock does not need `this`.
update `this` with `null` to clarify the intent.

PR-URL: #17345
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Yosuke Furukawa <[email protected]>
  • Loading branch information
Leko authored and gibfahn committed Dec 20, 2017
1 parent 1802f3f commit f5cd808
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
18 changes: 9 additions & 9 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const a = assert;

function makeBlock(f) {
const args = Array.prototype.slice.call(arguments, 1);
return function() {
return f.apply(this, args);
return () => {
return f.apply(null, args);
};
}

Expand Down Expand Up @@ -183,7 +183,7 @@ assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));

// having an identical prototype property
const nbRoot = {
toString: function() { return `${this.first} ${this.last}`; }
toString() { return `${this.first} ${this.last}`; }
};

function nameBuilder(first, last) {
Expand Down Expand Up @@ -458,10 +458,10 @@ assert.throws(makeBlock(thrower, TypeError));
'a.doesNotThrow is not catching type matching errors');
}

assert.throws(function() { assert.ifError(new Error('test error')); },
assert.throws(() => { assert.ifError(new Error('test error')); },
/^Error: test error$/);
assert.doesNotThrow(function() { assert.ifError(null); });
assert.doesNotThrow(function() { assert.ifError(); });
assert.doesNotThrow(() => { assert.ifError(null); });
assert.doesNotThrow(() => { assert.ifError(); });

assert.throws(() => {
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
Expand All @@ -473,7 +473,7 @@ assert.throws(() => {
let threw = false;
try {
assert.throws(
function() {
() => {
throw ({}); // eslint-disable-line no-throw-literal
},
Array
Expand All @@ -488,7 +488,7 @@ assert.throws(() => {
a.throws(makeBlock(thrower, TypeError), /\[object Object\]/);

// use a fn to validate error object
a.throws(makeBlock(thrower, TypeError), function(err) {
a.throws(makeBlock(thrower, TypeError), (err) => {
if ((err instanceof TypeError) && /\[object Object\]/.test(err)) {
return true;
}
Expand Down Expand Up @@ -591,7 +591,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
let threw = false;
try {
// eslint-disable-next-line no-restricted-syntax
assert.throws(function() {
assert.throws(() => {
assert.ifError(null);
});
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const domain = require('domain');
*/
const d = domain.create();

d.on('error', common.mustCall(function() {
process.nextTick(function() {
d.on('error', common.mustCall(() => {
process.nextTick(() => {
// Scheduling a callback with process.nextTick will enter a _new_ domain,
// and the callback will be called after the domain that handled the error
// was exited. So there should be only one domain on the domains stack if
Expand All @@ -29,6 +29,6 @@ d.on('error', common.mustCall(function() {
});
}));

d.run(function() {
d.run(() => {
throw new Error('Error from domain');
});
24 changes: 12 additions & 12 deletions test/parallel/test-querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const qsWeirdObjects = [
[{ regexp: /./g }, 'regexp=', { 'regexp': '' }],
// eslint-disable-next-line no-unescaped-regexp-dot
[{ regexp: new RegExp('.', 'g') }, 'regexp=', { 'regexp': '' }],
[{ fn: function() {} }, 'fn=', { 'fn': '' }],
[{ fn: () => {} }, 'fn=', { 'fn': '' }],
[{ fn: new Function('') }, 'fn=', { 'fn': '' }],
[{ math: Math }, 'math=', { 'math': '' }],
[{ e: extendedFunction }, 'e=', { 'e': '' }],
Expand Down Expand Up @@ -189,7 +189,7 @@ function check(actual, expected, input) {
`Expected keys: ${inspect(expectedKeys)}`;
}
assert.deepStrictEqual(actualKeys, expectedKeys, msg);
expectedKeys.forEach(function(key) {
expectedKeys.forEach((key) => {
if (typeof input === 'string') {
msg = `Input: ${inspect(input)}\n` +
`Key: ${inspect(key)}\n` +
Expand All @@ -203,21 +203,21 @@ function check(actual, expected, input) {
}

// test that the canonical qs is parsed properly.
qsTestCases.forEach(function(testCase) {
qsTestCases.forEach((testCase) => {
check(qs.parse(testCase[0]), testCase[2], testCase[0]);
});

// test that the colon test cases can do the same
qsColonTestCases.forEach(function(testCase) {
qsColonTestCases.forEach((testCase) => {
check(qs.parse(testCase[0], ';', ':'), testCase[2], testCase[0]);
});

// test the weird objects, that they get parsed properly
qsWeirdObjects.forEach(function(testCase) {
qsWeirdObjects.forEach((testCase) => {
check(qs.parse(testCase[1]), testCase[2], testCase[1]);
});

qsNoMungeTestCases.forEach(function(testCase) {
qsNoMungeTestCases.forEach((testCase) => {
assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '='));
});

Expand Down Expand Up @@ -255,15 +255,15 @@ qsNoMungeTestCases.forEach(function(testCase) {
// now test stringifying

// basic
qsTestCases.forEach(function(testCase) {
qsTestCases.forEach((testCase) => {
assert.strictEqual(testCase[1], qs.stringify(testCase[2]));
});

qsColonTestCases.forEach(function(testCase) {
qsColonTestCases.forEach((testCase) => {
assert.strictEqual(testCase[1], qs.stringify(testCase[2], ';', ':'));
});

qsWeirdObjects.forEach(function(testCase) {
qsWeirdObjects.forEach((testCase) => {
assert.strictEqual(testCase[1], qs.stringify(testCase[0]));
});

Expand Down Expand Up @@ -292,7 +292,7 @@ assert.strictEqual('foo=', qs.stringify({ foo: Infinity }));
assert.strictEqual(f, 'a=b&q=x%3Dy%26y%3Dz');
}

assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
qs.parse(undefined);
});

Expand Down Expand Up @@ -423,15 +423,15 @@ check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' });
}

// Test QueryString.unescapeBuffer
qsUnescapeTestCases.forEach(function(testCase) {
qsUnescapeTestCases.forEach((testCase) => {
assert.strictEqual(qs.unescape(testCase[0]), testCase[1]);
assert.strictEqual(qs.unescapeBuffer(testCase[0]).toString(), testCase[1]);
});

// test overriding .unescape
{
const prevUnescape = qs.unescape;
qs.unescape = function(str) {
qs.unescape = (str) => {
return str.replace(/o/g, '_');
};
check(
Expand Down
24 changes: 12 additions & 12 deletions test/parallel/test-writeint.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ function test8(clazz) {
assert.strictEqual(0xfb, buffer[1]);

/* Make sure we handle truncation correctly */
assert.throws(function() {
assert.throws(() => {
buffer.writeInt8(0xabc, 0);
}, errorOutOfBounds);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt8(0xabc, 0);
}, errorOutOfBounds);

Expand All @@ -50,10 +50,10 @@ function test8(clazz) {

assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt8(0x7f + 1, 0);
}, errorOutOfBounds);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt8(-0x80 - 1, 0);
}, errorOutOfBounds);
}
Expand Down Expand Up @@ -90,10 +90,10 @@ function test16(clazz) {
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0x80, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt16BE(0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt16BE(-0x8000 - 1, 0);
}, errorOutOfBounds);

Expand All @@ -103,10 +103,10 @@ function test16(clazz) {
assert.strictEqual(0x7f, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x80, buffer[3]);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt16LE(0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt16LE(-0x8000 - 1, 0);
}, errorOutOfBounds);
}
Expand Down Expand Up @@ -159,10 +159,10 @@ function test32(clazz) {
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt32BE(0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt32BE(-0x80000000 - 1, 0);
}, errorOutOfBounds);

Expand All @@ -176,10 +176,10 @@ function test32(clazz) {
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x80, buffer[7]);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt32LE(0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(function() {
assert.throws(() => {
buffer.writeInt32LE(-0x80000000 - 1, 0);
}, errorOutOfBounds);
}
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-zerolengthbufferbug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
const common = require('../common');
const http = require('http');

const server = http.createServer(function(req, res) {
const server = http.createServer((req, res) => {
const buffer = Buffer.alloc(0);
// FIXME: WTF gjslint want this?
res.writeHead(200, { 'Content-Type': 'text/html',
'Content-Length': buffer.length });
res.end(buffer);
});

server.listen(0, common.mustCall(function() {
http.get({ port: this.address().port }, common.mustCall(function(res) {
server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall((res) => {

res.on('data', common.mustNotCall());

res.on('end', function(d) {
res.on('end', (d) => {
server.close();
});
}));
Expand Down

0 comments on commit f5cd808

Please sign in to comment.