From f96acdfefdaec693b251f59bebd07650d39a7042 Mon Sep 17 00:00:00 2001 From: Fionn Kelleher Date: Fri, 20 Dec 2013 16:35:22 +0000 Subject: [PATCH] Fix incorrect logic in #parseHostmaskFromPrefix(). Extend #toString() tests, add misc. tests for utility methods. --- lib/message.coffee | 5 +++-- test/misc.coffee | 43 +++++++++++++++++++++++++++++++++++++++++++ test/tostring.coffee | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 test/misc.coffee diff --git a/lib/message.coffee b/lib/message.coffee index b876e53..840ba4e 100644 --- a/lib/message.coffee +++ b/lib/message.coffee @@ -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 diff --git a/test/misc.coffee b/test/misc.coffee new file mode 100644 index 0000000..3dc1b3e --- /dev/null +++ b/test/misc.coffee @@ -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 ":test!test@something.com 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 ":test!test@something.com NOTICE * :Looking up your hostname..." + "should return `false`": (topic) -> + topic.prefixIsServer().should.be.false + + "#parseHostmaskFromPrefix()": + "when prefix is 'james!james@my.awesome-rdns.co'": + topic: (Message ":james!baz@my.awesome-rdns.co 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) + diff --git a/test/tostring.coffee b/test/tostring.coffee index 91cf53b..af2d889 100644 --- a/test/tostring.coffee +++ b/test/tostring.coffee @@ -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 ":expr!textual@213.233.jgs.m 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(), ":expr!textual@213.233.jgs.m 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 ":test!me@test.ing PRIVMSG #Test :This is a test" + "should return ':test!me@test.ing PRIVMSG #Test :This is a test'": (topic) -> + topic.toString().should.equal ":test!me@test.ing 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 :test!me@test.ing FOO bar baz quux :This is a test" + "should return '@test=super;single :test!me@test.ing FOO bar baz quux :This is a test'": (topic) -> + topic.toString().should.equal "@test=super;single :test!me@test.ing FOO bar baz quux :This is a test" ).export(module) \ No newline at end of file