Skip to content

Commit

Permalink
Fix incorrect logic in #parseHostmaskFromPrefix(). Extend #toString()…
Browse files Browse the repository at this point in the history
… tests, add misc. tests for utility methods.
  • Loading branch information
Fionn Kelleher committed Dec 20, 2013
1 parent 2168f8a commit f96acdf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ class IRCMessage
prefixIsHostmask: -> (@prefix.indexOf("@") isnt -1 and @prefix.indexOf("!") isnt -1)
prefixIsServer: -> (@prefix.indexOf("@") is -1 and @prefix.indexOf("!") is -1 and @prefix.indexOf(".") isnt -1)
parseHostmaskFromPrefix: ->
if @prefixIsHostmask
if @prefixIsHostmask()
[nickname, username, hostname] = @prefix.split /[!@]/
return (
nickname: nickname
username: username
hostname: hostname
)
else throw new Error "Prefix is not a parsable hostmask."
else
return null

Message = (line) ->
message = new IRCMessage line
Expand Down
43 changes: 43 additions & 0 deletions test/misc.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Message = require "../"
vows = require "vows"
should = require("chai").should()

vows.describe("Utility methods").addBatch(
"#prefixIsHostmask()":
"when prefix is valid hostname":
topic: Message ":[email protected] PRIVMSG #Test :Testing!"
"should return `true`": (topic) ->
topic.prefixIsHostmask().should.be.true

"when prefix is invalid hostname":
topic: Message ":127.0.0.1 PRIVMSG #Test :Testing!"
"should return `false`": (topic) ->
topic.prefixIsHostmask().should.be.false

"#prefixIsServer()":
"when prefix is valid server hostname":
topic: Message ":127.0.0.1 NOTICE * :Looking up your hostname..."
"should return `true`": (topic) ->
topic.prefixIsServer().should.be.true

"when prefix is invalid server hostname":
topic: Message ":[email protected] NOTICE * :Looking up your hostname..."
"should return `false`": (topic) ->
topic.prefixIsServer().should.be.false

"#parseHostmaskFromPrefix()":
"when prefix is '[email protected]'":
topic: (Message ":[email protected] PRIVMSG #test :Hello!").parseHostmaskFromPrefix()
"should have property 'nickname' of 'james'": (topic) ->
topic.nickname.should.equal "james"
"should have property 'username' of 'baz'": (topic) ->
topic.username.should.equal "baz"
"should have property 'hostname' of 'my.awesome-rdns.co'": (topic) ->
topic.hostname.should.equal "my.awesome-rdns.co"

"when prefix is '127.0.0.1'":
topic: (Message ":127.0.0.1 NOTICE * :Looking up your hostname...").parseHostmaskFromPrefix()
"should return `null`": (topic) ->
(topic == null).should.be.true
).export(module)

38 changes: 31 additions & 7 deletions test/tostring.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
Message = require "../"
vows = require "vows"
assert = require "assert"
should = require("chai").should()

vows.describe("irc-message #toString()").addBatch(
"Invoking #toString() on a parsed message":
"with prefix, command and two parameters":
topic: Message ":[email protected] PRIVMSG #launch :JeffA: go ahead and do another multi line message"
vows.describe("Converting parsed messages to strings").addBatch(
"#toString() on parsed message":
"with command only":
topic: Message "FOO"
"should return 'FOO'": (topic) ->
topic.toString().should.equal "FOO"

"should yield exact same raw line": (topic) ->
assert.equal topic.toString(), ":[email protected] PRIVMSG #launch :JeffA: go ahead and do another multi line message"
"with prefix, command":
topic: Message ":test FOO"
"should return ':test FOO'": (topic) ->
topic.toString().should.equal ":test FOO"

"with prefix, command, middle, trailing parameter":
topic: Message ":[email protected] PRIVMSG #Test :This is a test"
"should return ':[email protected] PRIVMSG #Test :This is a test'": (topic) ->
topic.toString().should.equal ":[email protected] PRIVMSG #Test :This is a test"

"with no prefix, command, middle, trailing with spaces":
topic: Message "PRIVMSG #foo :This is a test"
"should return 'PRIVMSG #foo :This is a test'": (topic) ->
topic.toString().should.equal "PRIVMSG #foo :This is a test"

"with multiple middle params, prefix":
topic: Message ":test FOO bar baz quux"
"should return ':test FOO bar baz quux'": (topic) ->
topic.toString().should.equal ":test FOO bar baz quux"

"with tags, prefix, command, middle params, trailing params":
topic: Message "@test=super;single :[email protected] FOO bar baz quux :This is a test"
"should return '@test=super;single :[email protected] FOO bar baz quux :This is a test'": (topic) ->
topic.toString().should.equal "@test=super;single :[email protected] FOO bar baz quux :This is a test"

).export(module)

0 comments on commit f96acdf

Please sign in to comment.