summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Schoenfelder <paulschoenfelder@gmail.com>2013-12-06 16:09:50 -0600
committerPaul Schoenfelder <paulschoenfelder@gmail.com>2013-12-06 16:09:50 -0600
commite90d6e995016e39ebba2c6ba603b892ec074efd1 (patch)
tree21650e48561f108fb052cc2ede5029322b09b3f6
parentFix all of the compilation issues. Need tests (diff)
Start adding utils tests. Fix some utils bugs
-rw-r--r--lib/exirc/utils.ex42
-rw-r--r--test/exirc_test.exs7
-rw-r--r--test/utils_test.exs17
3 files changed, 37 insertions, 29 deletions
diff --git a/lib/exirc/utils.ex b/lib/exirc/utils.ex
index 9aebbe0..bdc283a 100644
--- a/lib/exirc/utils.ex
+++ b/lib/exirc/utils.ex
@@ -6,23 +6,23 @@ defmodule ExIrc.Utils do
Parse IRC message data
"""
def parse(raw_data) do
- data = String.slice(raw_data, 1, String.length(raw_data) - 2)
+ data = Enum.slice(raw_data, 1, Enum.count(raw_data) - 2)
case data do
- <<":", _ :: binary>> ->
- [<<":", from :: binary>>, rest] = String.split(data, " ")
+ [?:, _] ->
+ [[?: | from] | rest] = :string.tokens(data, ' ')
get_cmd rest, parse_from(from, IrcMessage.new(ctcp: false))
data ->
- get_cmd String.split(data, " "), IrcMessage.new(ctcp: false)
+ get_cmd :string.tokens(data, ' '), IrcMessage.new(ctcp: false)
end
end
def parse_from(from, msg) do
case Regex.split(%r/(!|@|\.)/, from) do
- [nick, "!", user, "@", host | host_rest] ->
+ [nick, '!', user, '@', host | host_rest] ->
msg.nick(nick).user(user).host(host ++ host_rest)
- [nick, "@", host | host_rest] ->
+ [nick, '@', host | host_rest] ->
msg.nick(nick).host(host ++ host_rest)
- [_, "." | _] ->
+ [_, '.' | _] ->
# from is probably a server name
msg.server(from)
[nick] ->
@@ -87,10 +87,8 @@ defmodule ExIrc.Utils do
state.network(network)
end
def isup_param('PREFIX=' ++ user_prefixes, state) do
- {:match, [{p1, l1}, {p2, l2}]} = Regex.run(%r/\((.*)\)(.*)/, user_prefixes, [:capture, :all_but_first])
- group1 = String.slice(user_prefixes, p1 + 1, l1)
- group2 = String.slice(user_prefixes, p2 + 1, l2)
- state.user_prefixes(Enum.zip(group1, group2))
+ prefixes = Regex.run(%r/\((.*)\)(.*)/, user_prefixes, capture: :all_but_first) |> List.zip
+ state.user_prefixes(prefixes)
end
def isup_param(_, state) do
state
@@ -100,18 +98,18 @@ defmodule ExIrc.Utils do
@months_of_year ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
def ctcp_time({{y, m, d}, {h, n, s}}) do
[:lists.nth(:calendar.day_of_the_week(y,m,d), @days_of_week),
- " ",
+ ' ',
:lists.nth(m, @months_of_year),
- " ",
- :io_lib.format('~2..0s',[integer_to_list(d)]),
- " ",
- :io_lib.format('~2..0s',[integer_to_list(h)]),
- ":",
- :io_lib.format('~2..0s',[integer_to_list(n)]),
- ":",
- :io_lib.format('~2..0s',[integer_to_list(s)]),
- " ",
- integer_to_list(y)]
+ ' ',
+ :io_lib.format("~2..0s", [integer_to_list(d)]),
+ ' ',
+ :io_lib.format("~2..0s", [integer_to_list(h)]),
+ ':',
+ :io_lib.format("~2..0s", [integer_to_list(n)]),
+ ':',
+ :io_lib.format("~2..0s", [integer_to_list(s)]),
+ ' ',
+ integer_to_list(y)] |> List.flatten
end
end \ No newline at end of file
diff --git a/test/exirc_test.exs b/test/exirc_test.exs
deleted file mode 100644
index c5d6ea1..0000000
--- a/test/exirc_test.exs
+++ /dev/null
@@ -1,7 +0,0 @@
-defmodule ExircTest do
- use ExUnit.Case
-
- test "the truth" do
- assert(true)
- end
-end
diff --git a/test/utils_test.exs b/test/utils_test.exs
new file mode 100644
index 0000000..3086f7d
--- /dev/null
+++ b/test/utils_test.exs
@@ -0,0 +1,17 @@
+defmodule ExIrc.UtilsTest do
+ use ExUnit.Case
+ alias ExIrc.Utils, as: Utils
+ alias ExIrc.Client.IrcMessage, as: IrcMessage
+ alias ExIrc.Client.ClientState, as: ClientState
+
+ test "Given a local date/time as a tuple, can retrieve get the CTCP formatted time" do
+ local_time = {{2013,12,6},{14,5,00}}
+ assert Utils.ctcp_time(local_time) == 'Fri Dec 06 14:05:00 2013'
+ end
+
+ test "Can parse an IRC message" do
+ message = ':irc.example.org 005 nick PREFIX=(ov)@+ CHANTYPES=#&'
+ assert IrcMessage[server: 'irc.example.org', cmd: '005', args: ['nick', 'PREFIX=(ov)@+', 'CHANTYPES=#&']] = Utils.parse(message)
+ end
+
+end