Skip to content

Commit

Permalink
Merge pull request #282 from martynsmith/jirwin/first-pass-at-tests
Browse files Browse the repository at this point in the history
WIP: fix(tests): A first attempt at a sane pattern to begin testing the handling of the protocol.
  • Loading branch information
Chris Nehren committed Jan 9, 2015
2 parents 63ae435 + b0129fc commit a0a5b59
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.3.7",
"author": "Martyn Smith <martyn@dollyfish.net.nz>",
"scripts": {
"test": "./node_modules/faucet/bin/cmd.js test/*.js",
"test": "./node_modules/faucet/bin/cmd.js test/test-*.js",
"lint": "./node_modules/jscs/bin/jscs --preset=airbnb */*.js"
},
"contributors": [
Expand Down
50 changes: 50 additions & 0 deletions test/mockircd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Mock irc server */

var net = require('net');
var util = require('util');
var EventEmitter = require('events').EventEmitter;

var MockIrcd = function(port, encoding) {
var self = this;

this.port = port || 6667;
this.encoding = encoding || 'utf-8';
this.incoming = [];
this.outgoing = [];

this.server = net.createServer(function(c) {
c.on('data', function(data) {
var msg = data.toString(self.encoding);
self.emit('message', msg);
self.incoming = self.incoming.concat(msg.split('\r\n'));
});

self.on('send', function(data) {
self.outgoing.push(data);
c.write(data);
});

c.on('end', function() {
self.emit('end')
});
});

this.server.listen(this.port);
};
util.inherits(MockIrcd, EventEmitter);

MockIrcd.prototype.send = function(data) {
this.emit('send', data);
};

MockIrcd.prototype.close = function() {
this.server.close();
};

MockIrcd.prototype.getIncomingMsgs = function() {
return this.incoming.filter(function(msg) { return msg; });
};

module.exports = function(port, encoding) {
return new MockIrcd(port, encoding);
};
47 changes: 47 additions & 0 deletions test/test-irc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var net = require('net');

var irc = require('../lib/irc');
var test = require('tape');

var MockIrcd = require('./mockircd');

test('connect, register and quit', function(t) {
var client, mock, expected;

t.plan(3);

mock = MockIrcd();
client = new irc.Client('localhost', 'testbot', {});

expected = {
sent: [
['NICK testbot', 'Client sent NICK message'],
['USER nodebot 8 * :nodeJS IRC client', 'Client sent USER message'],
['QUIT :node-irc says goodbye', 'Client sent QUIT message']
],

received: [
[':localhost 001 testbot :Welcome to the Internet Relay Chat Network testbot\r\n', 'Received welcome message']
]
};

t.plan(expected.sent.length + expected.received.length);

mock.server.on('connection', function() {
mock.send(':localhost 001 testbot :Welcome to the Internet Relay Chat Network testbot\r\n');
});

client.on('registered', function() {
t.equal(mock.outgoing[0], expected.received[0][0], expected.received[0][1]);
client.disconnect();
});

mock.on('end', function() {
var msgs = mock.getIncomingMsgs();

for (var i = 0; i < msgs.length; i++) {
t.equal(msgs[i], expected.sent[i][0], expected.sent[i][1]);
}
mock.close();
});
});
2 changes: 0 additions & 2 deletions test/parse-line.js → test/test-parse-line.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// jshint unused:false

var irc = require('../lib/irc.js');
var test = require('tape');

Expand Down

0 comments on commit a0a5b59

Please sign in to comment.