-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from martynsmith/jirwin/first-pass-at-tests
WIP: fix(tests): A first attempt at a sane pattern to begin testing the handling of the protocol.
- Loading branch information
Showing
4 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
"version": "0.3.7", | ||
"author": "Martyn Smith <[email protected]>", | ||
"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": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
|