Skip to content

Commit

Permalink
test: use ES6 classes instead of util.inherits
Browse files Browse the repository at this point in the history
Backport-PR-URL: #17068
PR-URL: #16938
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
  • Loading branch information
tniessen authored and MylesBorins committed Nov 28, 2017
1 parent eb11a70 commit f2108fa
Show file tree
Hide file tree
Showing 24 changed files with 373 additions and 428 deletions.
11 changes: 5 additions & 6 deletions test/parallel/test-crypto-lazy-transform-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ if (!common.hasCrypto)
const assert = require('assert');
const crypto = require('crypto');
const Stream = require('stream');
const util = require('util');

const hasher1 = crypto.createHash('sha256');
const hasher2 = crypto.createHash('sha256');
Expand All @@ -18,12 +17,12 @@ hasher1.end();

const expected = hasher1.read().toString('hex');

function OldStream() {
Stream.call(this);

this.readable = true;
class OldStream extends Stream {
constructor() {
super();
this.readable = true;
}
}
util.inherits(OldStream, Stream);

const stream = new OldStream();

Expand Down
34 changes: 17 additions & 17 deletions test/parallel/test-crypto-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ if (!common.hasCrypto)

const assert = require('assert');
const stream = require('stream');
const util = require('util');
const crypto = require('crypto');

// Small stream to buffer converter
function Stream2buffer(callback) {
stream.Writable.call(this);

this._buffers = [];
this.once('finish', function() {
callback(null, Buffer.concat(this._buffers));
});
}
util.inherits(Stream2buffer, stream.Writable);

Stream2buffer.prototype._write = function(data, encodeing, done) {
this._buffers.push(data);
return done(null);
};

if (!common.hasFipsCrypto) {
// Small stream to buffer converter
class Stream2buffer extends stream.Writable {
constructor(callback) {
super();

this._buffers = [];
this.once('finish', function() {
callback(null, Buffer.concat(this._buffers));
});
}

_write(data, encodeing, done) {
this._buffers.push(data);
return done(null);
}
}

// Create an md5 hash of "Hallo world"
const hasher1 = crypto.createHash('md5');
hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) {
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
require('../common');
const assert = require('assert');
const events = require('events');
const util = require('util');

function listener() {}
function listener2() {}
class TestStream { constructor() { } }
util.inherits(TestStream, events.EventEmitter);

{
const ee = new events.EventEmitter();
Expand Down Expand Up @@ -47,6 +44,7 @@ util.inherits(TestStream, events.EventEmitter);
}

{
class TestStream extends events.EventEmitter {}
const s = new TestStream();
assert.deepStrictEqual(s.listeners('foo'), []);
}
46 changes: 21 additions & 25 deletions test/parallel/test-http-client-read-in-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,34 @@
require('../common');
const net = require('net');
const http = require('http');
const util = require('util');

function Agent() {
http.Agent.call(this);
}
util.inherits(Agent, http.Agent);

Agent.prototype.createConnection = function() {
const self = this;
const socket = new net.Socket();
class Agent extends http.Agent {
createConnection() {
const socket = new net.Socket();

socket.on('error', function() {
socket.push('HTTP/1.1 200\r\n\r\n');
});
socket.on('error', function() {
socket.push('HTTP/1.1 200\r\n\r\n');
});

socket.on('newListener', function onNewListener(name) {
if (name !== 'error')
return;
socket.removeListener('newListener', onNewListener);
let onNewListener;
socket.on('newListener', onNewListener = (name) => {
if (name !== 'error')
return;
socket.removeListener('newListener', onNewListener);

// Let other listeners to be set up too
process.nextTick(function() {
self.breakSocket(socket);
// Let other listeners to be set up too
process.nextTick(() => {
this.breakSocket(socket);
});
});
});

return socket;
};
return socket;
}

Agent.prototype.breakSocket = function breakSocket(socket) {
socket.emit('error', new Error('Intentional error'));
};
breakSocket(socket) {
socket.emit('error', new Error('Intentional error'));
}
}

const agent = new Agent();

Expand Down
58 changes: 27 additions & 31 deletions test/parallel/test-http-client-readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,37 @@
const common = require('../common');
const assert = require('assert');
const http = require('http');
const util = require('util');

const Duplex = require('stream').Duplex;

function FakeAgent() {
http.Agent.call(this);
class FakeAgent extends http.Agent {
createConnection() {
const s = new Duplex();
let once = false;

s._read = function() {
if (once)
return this.push(null);
once = true;

this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
this.push('b\r\nhello world\r\n');
this.readable = false;
this.push('0\r\n\r\n');
};

// Blackhole
s._write = function(data, enc, cb) {
cb();
};

s.destroy = s.destroySoon = function() {
this.writable = false;
};

return s;
}
}
util.inherits(FakeAgent, http.Agent);

FakeAgent.prototype.createConnection = function() {
const s = new Duplex();
let once = false;

s._read = function() {
if (once)
return this.push(null);
once = true;

this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
this.push('b\r\nhello world\r\n');
this.readable = false;
this.push('0\r\n\r\n');
};

// Blackhole
s._write = function(data, enc, cb) {
cb();
};

s.destroy = s.destroySoon = function() {
this.writable = false;
};

return s;
};

let received = '';

Expand Down
13 changes: 5 additions & 8 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ const common = require('../common');
const assert = require('assert');
const readline = require('readline');
const EventEmitter = require('events').EventEmitter;
const inherits = require('util').inherits;
const { Writable, Readable } = require('stream');

function FakeInput() {
EventEmitter.call(this);
class FakeInput extends EventEmitter {
resume() {}
pause() {}
write() {}
end() {}
}
inherits(FakeInput, EventEmitter);
FakeInput.prototype.resume = () => {};
FakeInput.prototype.pause = () => {};
FakeInput.prototype.write = () => {};
FakeInput.prototype.end = () => {};

function isWarned(emitter) {
for (const name in emitter) {
Expand Down
7 changes: 1 addition & 6 deletions test/parallel/test-readline-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
const common = require('../common');
const PassThrough = require('stream').PassThrough;
const assert = require('assert');
const inherits = require('util').inherits;
const Interface = require('readline').Interface;


function FakeInput() {
PassThrough.call(this);
}
inherits(FakeInput, PassThrough);
class FakeInput extends PassThrough {}

function extend(k) {
return Object.assign({ ctrl: false, meta: false, shift: false }, k);
Expand Down
33 changes: 14 additions & 19 deletions test/parallel/test-stream-big-packet.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
'use strict';
require('../common');
const assert = require('assert');
const util = require('util');
const stream = require('stream');

let passed = false;

function PassThrough() {
stream.Transform.call(this);
}
util.inherits(PassThrough, stream.Transform);
PassThrough.prototype._transform = function(chunk, encoding, done) {
this.push(chunk);
done();
};

function TestStream() {
stream.Transform.call(this);
class PassThrough extends stream.Transform {
_transform(chunk, encoding, done) {
this.push(chunk);
done();
}
}
util.inherits(TestStream, stream.Transform);
TestStream.prototype._transform = function(chunk, encoding, done) {
if (!passed) {
// Char 'a' only exists in the last write
passed = chunk.toString().includes('a');

class TestStream extends stream.Transform {
_transform(chunk, encoding, done) {
if (!passed) {
// Char 'a' only exists in the last write
passed = chunk.toString().includes('a');
}
done();
}
done();
};
}

const s1 = new PassThrough();
const s2 = new PassThrough();
Expand Down
29 changes: 13 additions & 16 deletions test/parallel/test-stream-events-prepend.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
'use strict';
const common = require('../common');
const stream = require('stream');
const util = require('util');

function Writable() {
this.writable = true;
stream.Writable.call(this);
this.prependListener = undefined;
class Writable extends stream.Writable {
constructor() {
super();
this.prependListener = undefined;
}

_write(chunk, end, cb) {
cb();
}
}
util.inherits(Writable, stream.Writable);
Writable.prototype._write = function(chunk, end, cb) {
cb();
};

function Readable() {
this.readable = true;
stream.Readable.call(this);
class Readable extends stream.Readable {
_read() {
this.push(null);
}
}
util.inherits(Readable, stream.Readable);
Readable.prototype._read = function() {
this.push(null);
};

const w = new Writable();
w.on('pipe', common.mustCall());
Expand Down
Loading

0 comments on commit f2108fa

Please sign in to comment.