From a40ff61447835f3b76dc1a062541e793ad141a50 Mon Sep 17 00:00:00 2001 From: Paul Schoenfelder Date: Sun, 8 Dec 2013 18:36:46 -0600 Subject: Add test suite for Channels --- test/channels_test.exs | 130 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 test/channels_test.exs (limited to 'test') diff --git a/test/channels_test.exs b/test/channels_test.exs new file mode 100644 index 0000000..927a76a --- /dev/null +++ b/test/channels_test.exs @@ -0,0 +1,130 @@ +defmodule ExIrc.ChannelsTest do + use ExUnit.Case, async: true + + alias ExIrc.Channels, as: Channels + + test "Joining a channel adds it to the tree of currently joined channels" do + channels = Channels.init() |> Channels.join("#testchannel") |> Channels.channels + assert Enum.member?(channels, "#testchannel") + end + + test "The channel name is downcased when joining" do + channels = Channels.init() |> Channels.join("#TestChannel") |> Channels.channels + assert Enum.member?(channels, "#testchannel") + end + + test "Joining the same channel twice is a noop" do + channels = Channels.init() |> Channels.join("#TestChannel") |> Channels.join("#testchannel") |> Channels.channels + assert 1 == Enum.count(channels) + end + + test "Parting a channel removes it from the tree of currently joined channels" do + tree = Channels.init() |> Channels.join("#testchannel") + assert Enum.member?(Channels.channels(tree), "#testchannel") + tree = Channels.part(tree, "#testchannel") + refute Enum.member?(Channels.channels(tree), "#testchannel") + end + + test "Parting a channel not in the tree is a noop" do + tree = Channels.init() + {count, _} = Channels.part(tree, "#testchannel") + assert 0 == count + end + + test "Can set the topic for a channel" do + channels = Channels.init() |> Channels.join("#testchannel") |> Channels.set_topic("#testchannel", "Welcome to Test Channel!") + assert "Welcome to Test Channel!" == Channels.chan_topic(channels, "#testchannel") + end + + test "Setting the topic for a channel we haven't joined returns :error" do + channels = Channels.init() |> Channels.set_topic("#testchannel", "Welcome to Test Channel!") + assert {:error, :no_such_channel} == Channels.chan_topic(channels, "#testchannel") + end + + test "Can set the channel type" do + channels = Channels.init() |> Channels.join("#testchannel") |> Channels.set_type("#testchannel", "@") + assert :secret == Channels.chan_type(channels, "#testchannel") + channels = Channels.set_type(channels, "#testchannel", "*") + assert :private == Channels.chan_type(channels, "#testchannel") + channels = Channels.set_type(channels, "#testchannel", "=") + assert :public == Channels.chan_type(channels, "#testchannel") + end + + test "Setting the channel type for a channel we haven't joined returns :error" do + channels = Channels.init() |> Channels.set_type("#testchannel", "@") + assert {:error, :no_such_channel} == Channels.chan_type(channels, "#testchannel") + end + + test "Setting an invalid channel type raises CaseClauseError" do + assert_raise CaseClauseError, "no case clause matching: '!'", fn -> + Channels.init() |> Channels.join("#testchannel") |> Channels.set_type("#testchannel", "!") + end + end + + test "Can join a user to a channel" do + channels = Channels.init() |> Channels.join("#testchannel") |> Channels.user_join("#testchannel", "testnick") + assert Channels.chan_has_user?(channels, "#testchannel", "testnick") + end + + test "Can join multiple users to a channel" do + channels = Channels.init() |> Channels.join("#testchannel") |> Channels.users_join("#testchannel", ["testnick", "anothernick"]) + assert Channels.chan_has_user?(channels, "#testchannel", "testnick") + assert Channels.chan_has_user?(channels, "#testchannel", "anothernick") + end + + test "Joining a users to a channel we aren't in is a noop" do + channels = Channels.init() |> Channels.user_join("#testchannel", "testnick") + assert {:error, :no_such_channel} == Channels.chan_has_user?(channels, "#testchannel", "testnick") + channels = Channels.init() |> Channels.users_join("#testchannel", ["testnick", "anothernick"]) + assert {:error, :no_such_channel} == Channels.chan_has_user?(channels, "#testchannel", "testnick") + end + + test "Can part a user from a channel" do + channels = Channels.init() |> Channels.join("#testchannel") |> Channels.user_join("#testchannel", "testnick") + assert Channels.chan_has_user?(channels, "#testchannel", "testnick") + channels = channels |> Channels.user_part("#testchannel", "testnick") + refute Channels.chan_has_user?(channels, "#testchannel", "testnick") + end + + test "Parting a user from a channel we aren't in is a noop" do + channels = Channels.init() |> Channels.user_part("#testchannel", "testnick") + assert {:error, :no_such_channel} == Channels.chan_has_user?(channels, "#testchannel", "testnick") + end + + test "Can rename a user" do + channels = Channels.init() + |> Channels.join("#testchannel") + |> Channels.join("#anotherchan") + |> Channels.user_join("#testchannel", "testnick") + |> Channels.user_join("#anotherchan", "testnick") + assert Channels.chan_has_user?(channels, "#testchannel", "testnick") + assert Channels.chan_has_user?(channels, "#anotherchan", "testnick") + channels = Channels.user_rename(channels, "testnick", "newnick") + refute Channels.chan_has_user?(channels, "#testchannel", "testnick") + refute Channels.chan_has_user?(channels, "#anotherchan", "testnick") + assert Channels.chan_has_user?(channels, "#testchannel", "newnick") + assert Channels.chan_has_user?(channels, "#anotherchan", "newnick") + end + + test "Renaming a user that doesn't exist is a noop" do + channels = Channels.init() |> Channels.join("#testchannel") |> Channels.user_rename("testnick", "newnick") + refute Channels.chan_has_user?(channels, "#testchannel", "testnick") + refute Channels.chan_has_user?(channels, "#testchannel", "newnick") + end + + test "Can get the current set of channel data as a tuple of the channel name and it's data as a proplist" do + channels = Channels.init() + |> Channels.join("#testchannel") + |> Channels.set_type("#testchannel", "@") + |> Channels.set_topic("#testchannel", "Welcome to Test!") + |> Channels.join("#anotherchan") + |> Channels.set_type("#anotherchan", "=") + |> Channels.set_topic("#anotherchan", "Welcome to Another Channel!") + |> Channels.user_join("#testchannel", "testnick") + |> Channels.user_join("#anotherchan", "testnick") + |> Channels.to_proplist + testchannel = {"#testchannel", [users: ["testnick"], topic: "Welcome to Test!", type: :secret]} + anotherchan = {"#anotherchan", [users: ["testnick"], topic: "Welcome to Another Channel!", type: :public]} + assert [testchannel, anotherchan] == channels + end +end \ No newline at end of file -- cgit v1.2.3