Skip to content

Commit

Permalink
test: use ES6 classes instead of util.inherits
Browse files Browse the repository at this point in the history
PR-URL: #16938
Backport-PR-URL: #16946
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 gibfahn committed Nov 14, 2017
1 parent cecd1e3 commit 981a1ef
Show file tree
Hide file tree
Showing 25 changed files with 364 additions and 422 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
30 changes: 15 additions & 15 deletions test/parallel/test-crypto-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,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);
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));
});
}
util.inherits(Stream2buffer, stream.Writable);
this._buffers = [];
this.once('finish', function() {
callback(null, Buffer.concat(this._buffers));
});
}

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

if (!common.hasFipsCrypto) {
// 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 @@ -24,12 +24,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 @@ -81,6 +78,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
50 changes: 23 additions & 27 deletions test/parallel/test-http-client-readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,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);
}
util.inherits(FakeAgent, http.Agent);

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

s._read = function() {
if (once)
return this.push(null);
once = true;
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');
};
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();
};
// Blackhole
s._write = function(data, enc, cb) {
cb();
};

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

return s;
};
return s;
}
}

let received = '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
const common = require('../common');
const http = require('http');
const assert = require('assert');
const util = require('util');
const stream = require('stream');

// Verify that when piping a stream to an `OutgoingMessage` (or a type that
// inherits from `OutgoingMessage`), if data is emitted after the
// `OutgoingMessage` was closed - a `write after end` error is raised

function MyStream() {
stream.call(this);
}
util.inherits(MyStream, stream);
class MyStream extends stream {}

const server = http.createServer(common.mustCall(function(req, res) {
const myStream = new MyStream();
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 @@ -27,17 +27,14 @@ const assert = require('assert');
const readline = require('readline');
const internalReadline = require('internal/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
31 changes: 13 additions & 18 deletions test/parallel/test-stream-big-packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,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);
class PassThrough extends stream.Transform {
_transform(chunk, encoding, done) {
this.push(chunk);
done();
}
}
util.inherits(PassThrough, stream.Transform);
PassThrough.prototype._transform = function(chunk, encoding, done) {
this.push(chunk);
done();
};

function TestStream() {
stream.Transform.call(this);
}
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 981a1ef

Please sign in to comment.