Skip to content

Commit aec4522

Browse files
committed
initial set of unit tests
1 parent 58eec5a commit aec4522

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "rake/testtask"
2+
Rake::TestTask.new do |t|
3+
t.libs << "test"
4+
t.test_files = Dir["test/lib/**/*.rb"]
5+
end
6+
7+
task :default => :test

test/helper.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
if ENV["SIMPLECOV"]
2+
begin
3+
require 'simplecov'
4+
SimpleCov.start
5+
rescue LoadError
6+
end
7+
end
8+
9+
unless Object.const_defined? 'Cinch'
10+
$:.unshift File.expand_path('../../lib', __FILE__)
11+
require 'cinch'
12+
end
13+
14+
require 'minitest/autorun'
15+
16+
class TestCase < MiniTest::Unit::TestCase
17+
def self.test(name, &block)
18+
define_method("test_" + name, &block) if block
19+
end
20+
end

test/lib/cinch/mask.rb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require "helper"
2+
3+
class MaskTest < TestCase
4+
DefaultMask = "foo*!bar?@baz"
5+
def setup
6+
@mask = Cinch::Mask.new(DefaultMask.dup)
7+
end
8+
test "Two equal masks should be equal" do
9+
mask2 = Cinch::Mask.new(DefaultMask.dup)
10+
11+
assert @mask == mask2
12+
assert @mask.eql?(mask2)
13+
end
14+
15+
test "A Mask's hash should depend on the mask" do
16+
mask2 = Cinch::Mask.new(DefaultMask.dup)
17+
18+
assert_equal @mask.hash, mask2.hash
19+
end
20+
21+
test "A Mask should match a User only if it has matching attributes" do
22+
user = Cinch::User.new("foo", nil)
23+
user2 = Cinch::User.new("foobar", nil)
24+
user3 = Cinch::User.new("barfoo", nil)
25+
26+
# bar? -> bar, baz -> baz
27+
user.end_of_whois(user: "bar", host: "baz")
28+
assert @mask.match(user)
29+
30+
# bar? -> bar2, baz -> baz
31+
user.end_of_whois(user: "bar2", host: "baz")
32+
assert @mask.match(user)
33+
34+
# bar? !-> bar22, baz -> baz
35+
user.end_of_whois(user: "bar22", host: "baz")
36+
assert !@mask.match(user)
37+
38+
# bar? -> bar, baz !-> meow
39+
user.end_of_whois(user: "bar", host: "meow")
40+
assert !@mask.match(user)
41+
42+
# foo* -> foobar
43+
user2.end_of_whois(user: "bar", host: "baz")
44+
assert @mask.match(user2)
45+
46+
# foo* !-> barfoo
47+
user3.end_of_whois(user: "bar", host: "baz")
48+
assert !@mask.match(user3)
49+
end
50+
51+
test "A mask's string representation should equal the original mask string" do
52+
assert_equal DefaultMask.dup, @mask.to_s
53+
end
54+
55+
test "A Mask can be created from Strings" do
56+
assert_equal @mask, Cinch::Mask.from(DefaultMask.dup)
57+
end
58+
59+
test "A Mask can be created from objects that have a mask" do
60+
mask = Cinch::Mask.new("foo!bar@baz")
61+
user = Cinch::User.new("foo", nil)
62+
user.end_of_whois(user: "bar", host: "baz")
63+
new_mask = Cinch::Mask.from(user)
64+
65+
assert_equal mask, new_mask
66+
end
67+
end

test/lib/cinch/plugin.rb

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
require "helper"
2+
3+
module Cinch
4+
class CinchTestPluginWithoutName
5+
include Cinch::Plugin
6+
end
7+
end
8+
9+
class PluginTest < TestCase
10+
def setup
11+
@bot = Cinch::Bot.new {
12+
self.loggers.clear
13+
}
14+
@plugin = Class.new { include Cinch::Plugin }
15+
@bot.config.plugins.options = {@plugin => {:key => :value}}
16+
17+
@plugin.plugin_name = "testplugin"
18+
@plugin_instance = @plugin.new(@bot)
19+
end
20+
21+
test "should be able to specify matchers" do
22+
@plugin.match(/pattern/)
23+
matcher = @plugin.matchers.last
24+
25+
assert_equal(1, @plugin.matchers.size, "Shoult not forget existing matchers")
26+
assert_equal Cinch::Plugin::ClassMethods::Matcher.new(/pattern/, true, true, :execute), matcher
27+
28+
matcher = @plugin.match(/pattern/, use_prefix: false, use_suffix: false, method: :some_method)
29+
assert_equal Cinch::Plugin::ClassMethods::Matcher.new(/pattern/, false, false, :some_method), matcher
30+
end
31+
32+
test "should be able to listen to events" do
33+
@plugin.listen_to(:event1, :event2)
34+
@plugin.listen_to(:event3, method: :some_method)
35+
36+
listeners = @plugin.listeners
37+
assert_equal 3, listeners.size
38+
assert_equal [:event1, :event2, :event3], listeners.map(&:event)
39+
assert_equal [:listen, :listen, :some_method], listeners.map(&:method)
40+
end
41+
42+
test "should be able to create CTCP commands" do
43+
@plugin.ctcp("FOO")
44+
@plugin.ctcp("BAR")
45+
46+
assert_equal 2, @plugin.ctcps.size
47+
assert_equal ["FOO", "BAR"], @plugin.ctcps
48+
end
49+
50+
test "CTCP commands should always be uppercase" do
51+
@plugin.ctcp("foo")
52+
assert_equal "FOO", @plugin.ctcps.last
53+
end
54+
55+
test "should return an empty array of timers" do
56+
assert_equal [], @plugin.timers
57+
end
58+
59+
test "should return an empty array of listeners" do
60+
assert_equal [], @plugin.listeners
61+
end
62+
63+
test "should return an empty array of CTCPs" do
64+
assert_equal [], @plugin.ctcps
65+
end
66+
67+
test "should be able to set timers" do
68+
@plugin.timer(1, method: :foo)
69+
@plugin.timer(2, method: :bar, :threaded => false)
70+
71+
timers = @plugin.timers
72+
assert_equal 2, timers.size
73+
assert_equal [1, 2], timers.map(&:interval)
74+
assert_equal [:foo, :bar], timers.map {|t| t.options[:method]}
75+
assert_equal [true, false], timers.map {|t| t.options[:threaded]}
76+
end
77+
78+
test "should be able to register hooks" do
79+
@plugin.hook(:pre)
80+
@plugin.hook(:post, :for => [:match])
81+
@plugin.hook(:post, :method => :some_method)
82+
83+
hooks = @plugin.hooks.values.flatten
84+
assert_equal [:pre, :post, :post], hooks.map(&:type)
85+
assert_equal [:match], hooks[1].for
86+
assert_equal :some_method, hooks.last.method
87+
assert_equal :hook, hooks.first.method
88+
end
89+
90+
test "should have access to plugin configuration" do
91+
assert_equal :value, @plugin_instance.config[:key]
92+
end
93+
94+
test "should be able to set a prefix with a block" do
95+
block = lambda {|m| "^"}
96+
@plugin.prefix = block
97+
assert_equal block, @plugin.prefix
98+
end
99+
100+
test "should be able to set a suffix with a block" do
101+
block = lambda {|m| "^"}
102+
@plugin.suffix = block
103+
assert_equal block, @plugin.suffix
104+
end
105+
106+
test "should support `set(key, value)`" do
107+
@plugin.set :help, "some help message"
108+
@plugin.set :prefix, "some prefix"
109+
@plugin.set :suffix, "some suffix"
110+
@plugin.set :plugin_name, "some plugin"
111+
@plugin.set :reacting_on, :event1
112+
113+
assert_equal "some help message", @plugin.help
114+
assert_equal "some prefix", @plugin.prefix
115+
assert_equal "some suffix", @plugin.suffix
116+
assert_equal "some plugin", @plugin.plugin_name
117+
assert_equal :event1, @plugin.reacting_on
118+
end
119+
120+
test "should support `set(key => value, key => value, ...)`" do
121+
@plugin.set(:help => "some help message",
122+
:prefix => "some prefix",
123+
:suffix => "some suffix",
124+
:plugin_name => "some plugin",
125+
:reacting_on => :event1)
126+
127+
assert_equal "some help message", @plugin.help
128+
assert_equal "some prefix", @plugin.prefix
129+
assert_equal "some suffix", @plugin.suffix
130+
assert_equal "some plugin", @plugin.plugin_name
131+
assert_equal :event1, @plugin.reacting_on
132+
end
133+
134+
test "should support `self.key = value`" do
135+
@plugin.help = "some help message"
136+
@plugin.prefix = "some prefix"
137+
@plugin.suffix = "some suffix"
138+
@plugin.plugin_name = "some plugin"
139+
@plugin.reacting_on = :event1
140+
141+
assert_equal "some help message", @plugin.help
142+
assert_equal "some prefix", @plugin.prefix
143+
assert_equal "some suffix", @plugin.suffix
144+
assert_equal "some plugin", @plugin.plugin_name
145+
assert_equal :event1, @plugin.reacting_on
146+
end
147+
148+
test "should support querying attributes" do
149+
@plugin.plugin_name = "foo"
150+
@plugin.help = "I am a help message"
151+
@plugin.prefix = "^"
152+
@plugin.suffix = "!"
153+
@plugin.react_on(:event1)
154+
155+
assert_equal "foo", @plugin.plugin_name
156+
assert_equal "I am a help message", @plugin.help
157+
assert_equal "^", @plugin.prefix
158+
assert_equal "!", @plugin.suffix
159+
assert_equal :event1, @plugin.reacting_on
160+
end
161+
162+
test "should have a default name" do
163+
assert_equal "cinchtestpluginwithoutname", Cinch::CinchTestPluginWithoutName.plugin_name
164+
end
165+
166+
test "should check for the right number of arguments for `set`" do
167+
assert_raises(ArgumentError) { @plugin.set() }
168+
assert_raises(ArgumentError) { @plugin.set(1, 2, 3) }
169+
end
170+
end

0 commit comments

Comments
 (0)