Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[test] this keyword chain

  • Loading branch information...
commit 2fc3307fc37b218493997bae37926fffa26fe0f0 1 parent eee60f7
@AndreasMadsen authored
Showing with 60 additions and 0 deletions.
  1. +31 −0 test/callbacks.js
  2. +29 −0 test/events.js
View
31 test/callbacks.js
@@ -129,6 +129,37 @@ test("callback hook", function (t) {
server.listen(0, listenCallback);
});
+ t.test("this keyword on callback", function (t) {
+ var callOrder = 0;
+ var server = require('net').createServer();
+
+ // argument passed public to listen
+ function listenCallback() {
+ t.equal(this, server, 'this keyword match');
+ t.equal(callOrder++, 1, 'real callback was executed after patch callback');
+
+ server.close(t.end.bind(t));
+ }
+
+ // attach monkey patch callback
+ hook.callback.attach(function callbackAttach(name, callback) {
+ if (name !== 'net.Server.listen') return callback;
+
+ hook.callback.deattach(callbackAttach);
+
+ // set a new callback
+ return function () {
+ t.equal(this, server, 'this keyword match');
+ t.equal(callOrder++, 0, 'pached callback in correct order');
+
+ // chain the callback
+ return callback.apply(this, arguments);
+ };
+ });
+
+ server.listen(0, listenCallback);
+ });
+
t.test("patch process.nextTick callback", function (t) {
t.plan(4);
View
29 test/events.js
@@ -122,6 +122,35 @@ test("events hook", function (t) {
e.emit('test', obj);
});
+ t.test("this keyword in event handler", function (t) {
+ t.plan(4);
+
+ var e = new EventEmitter();
+ var callOrder = 0;
+
+ function eventHandler() {
+ t.equal(this, e, 'this keyword match');
+ t.equal(callOrder++, 1, 'real callback was executed after patch callback');
+ t.end();
+ }
+
+ // attach monkey patch callback
+ hook.event.attach(function eventAttach(name, callback) {
+ hook.event.deattach(eventAttach);
+
+ return function () {
+ t.equal(this, e, 'this keyword match');
+ t.equal(callOrder++, 0, 'pached callback is executed first');
+
+ // chain the callback
+ return callback.apply(this, arguments);
+ };
+ });
+
+ e.on('test', eventHandler);
+ e.emit('test');
+ });
+
t.test('emitter.addListener and emitter.on are the same', function (t) {
var e = new EventEmitter();
Please sign in to comment.
Something went wrong with that request. Please try again.