Skip to content

Instantly share code, notes, and snippets.

@robotarmy
Created December 31, 2010 00:15
Show Gist options
  • Save robotarmy/760524 to your computer and use it in GitHub Desktop.
Save robotarmy/760524 to your computer and use it in GitHub Desktop.
o_o:bunuq/ (master✗) $ vows test/unit/speak_to_test.js --spec
♢ SpeakTo
EVENT EMIT
no args
✓ nothing happens
no messages
✓ nothing happens
EVENT EMIT
TOPIC
private messages a user a set of messages
✗ 3 messages at a time
» expected 3,
got 4 (==) // speak_to_test.js:44
✗ Broken » 2 honored ∙ 1 broken (1.007s)
o_o:bunuq/ (master✗) $
var IrcMock = function() {
var that = this
that.sent = []
return {
say : function(target,message) {
that.sent[that.sent.length]=message
},
sent : function() {
return that.sent
}
}
}
exports.mock = function() {
return new IrcMock()
}
var SpeakTo = function(opts) {
for(var x in opts) {
this[x] = opts[x]
}
this.timeout = this.timeout || 1000
this.callback = this.callback || function() {}
}
SpeakTo.prototype.speak_array = function(target,messages) {
if (messages.length != 0) {
var msgs = messages.splice(0,3)
for(var x in msgs) {
this.irc.say(target,JSON.stringify(msgs[x]))
}
this.callback(this)
if (this.timeout > 0) {
var that = this
setTimeout(function() {
that.speak_array(target,messages)
},this.timeout)
}
}
}
SpeakTo.prototype.speak_to = function(target,messages) {
if (target && messages) {
if (messages instanceof Array) {
this.speak_array(target,messages)
}
}
return this;
}
exports.create = function(_irc,_timeout,_callback) {
var speaker = new SpeakTo({irc:_irc,timeout:_timeout,callback:_callback});
return speaker
}
exports.bang_speaker = function(_irc,_bang,_timeout,_callback) {
var speaker = exports.create(_irc,_timeout,_callback)
var bang = _bang
return function(from,maybe_bang_message) {
speaker.speak_to(from,
bang.run(maybe_bang_message))
return speaker
}
}
// Encoding: UTF8
var SpeakTo = require('../../lib/speak_to')
var irc = require('../../test_helpers/irc_mock')
var vows = require('vows')
var assert = require('assert')
var suite = vows.describe('SpeakTo')
var events = require('events')
suite.addBatch({
'no args': {
topic: function () {
var s = SpeakTo.create(irc.mock(),-1)
return s.speak_to(null,null)
},
'nothing happens': function (topic) {
assert.equal(topic.irc.sent().length,0)
},
},
'no messages': {
topic: function () {
var s = SpeakTo.create(irc.mock(),-1)
var who = 'fred'
var messages = []
return s.speak_to(who,messages)
},
'nothing happens': function (topic) {
assert.equal(topic.irc.sent().length,0)
},
},
'private messages a user a set of messages': {
topic: function () {
var promise = new(events.EventEmitter)
var s = SpeakTo.create(irc.mock(),1000,function(self){
console.log('EVENT EMIT')
promise.emit('success',self)
})
var who = 'fred'
var messages = ['one', 'two', 'three', 'four']
s.speak_to(who,messages)
return promise
},
'3 messages at a time': function (topic) {
console.log('TOPIC ')
assert.equal(topic.irc.sent().length,3)
},
},
}).export(module)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment