C0 code coverage information
Generated on Sun Jul 06 22:30:58 +0900 2008 with rcov 0.8.1.2
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
| Name |
Total lines |
Lines of code |
Total coverage |
Code coverage |
| spec/core_spec.rb
|
316
|
248
|
|
|
1 #!/usr/bin/env ruby
2 require File.dirname(__FILE__) + '/spec_helper.rb'
3 require "yaml"
4 require "pp"
5
6 include Citrus
7 include Net::IRC
8 include Constants
9
10 describe Citrus do
11 it "should raises ArgumentError when some configs are missing" do
12 proc { Citrus.run({}) }.should raise_error(ArgumentError)
13 end
14 end
15
16 describe Core, "config" do
17 it "should accept filename" do
18 proc { Core.new("config.yaml.notfound") }.should raise_error(Errno::ENOENT)
19 end
20
21 it "should accept hash" do
22 proc { Core.new({}) }.should raise_error(ArgumentError)
23 end
24 end
25
26 describe Core do
27 before :all do
28 @prefix = Prefix.new("foo!bar@localhost")
29
30 @core = DummyCore.new({
31 "plugins" => {
32 "System" => { "operator" => "foo!bar@localhost" },
33 "Foo" => { "disabled" => "#disabled" },
34 "Bar" => { "enabled" => "#enabled" },
35 "Test" => nil,
36 "Unknown" => nil,
37 }
38 })
39
40 @pdir = Pathname.new(@core.config.general["plugin_dir"])
41 @ddir = Pathname.new(@core.config.general["data_dir"])
42 @conf = Pathname.new(@core.instance_variable_get(:@config_file))
43
44 (@pdir + "system.rb").open("w") do |f|
45 f.puts File.read("plugins/system.rb")
46 end
47
48 %w(Foo Bar).each do |name|
49 (@pdir + "#{name.downcase}.rb").open("w") do |f|
50 f << <<-EOS.unindent
51 require "thread"
52 class #{name}
53 include Net::IRC
54 include Constants
55
56 attr_reader :config
57
58 def initialize(core, config)
59 @core, @config = core, config[self.class.name.sub(/.+::/, "")] || {}
60 @messages = {}
61 end
62
63 def method_missing(method, *args)
64 @messages[method] = args
65 end
66
67 def m
68 @messages
69 end
70 end
71 EOS
72 end
73 end
74
75 (@pdir + "test.rb").open("w") do |f|
76 f << <<-EOS.unindent
77 require "thread"
78 class Test < Citrus::Plugin
79
80 attr_reader :config
81
82 def on_notice(prefix, channel, message)
83 eval(message) if channel == "#eval"
84 end
85
86 def on_message(m)
87 @messages ||= SizedQueue.new(1)
88 @messages << m
89 end
90 end
91 EOS
92 end
93
94 @core.instance_variable_get(:@logger).level = Logger::FATAL
95 @core.on_connected
96 @core.instance_variable_get(:@logger).level = Logger::INFO
97
98 @core.plugins.loaded_plugins["System"].should_not be_nil
99 @core.plugins.loaded_plugins["Foo"].should_not be_nil
100 @core.plugins.loaded_plugins["Bar"].should_not be_nil
101
102 @dplug = @core.plugins.loaded_plugins["Foo"].m
103 end
104
105 it "should run initializing plugins and on_uped event." do
106 @dplug[:on_uped].should == []
107 end
108
109 it "should translate PRIVMSG to on_privmsg event." do
110 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#channel", "message"]))
111 @dplug[:on_privmsg].should == ["foo!bar@localhost", "#channel", "message"]
112 end
113
114 it "should translate PRIVMSG to on_talk event." do
115 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["channel", "message"]))
116 @dplug[:on_talk].should == ["foo!bar@localhost", "channel", "message"]
117 end
118
119 it "should translate PRIVMSG to on_ctcp event." do
120 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#channel", ctcp_encoding("ACTION message")]))
121 @dplug[:on_ctcp].should == ["foo!bar@localhost", "#channel", "ACTION message"]
122 end
123
124 it "should translate NOTICE to on_notice event." do
125 @core.on_notice(Message.new(@prefix, NOTICE, ["#channel", "message"]))
126 @dplug[:on_notice].should == ["foo!bar@localhost", "#channel", "message"]
127 end
128
129 it "should translate JOIN to on_join event." do
130 @core.on_join(Message.new(@prefix, JOIN, ["#channel"]))
131 @dplug[:on_join].should == ["foo!bar@localhost", "#channel"]
132 end
133
134 it "should translate PART to on_part event." do
135 @core.on_part(Message.new(@prefix, PART, ["#channel", "reason"]))
136 @dplug[:on_part].should == ["foo!bar@localhost", "#channel", "reason"]
137 end
138
139 it "should translate KICK to on_kick event." do
140 @core.on_kick(Message.new(@prefix, KICK, ["#channel", "nick", "reason"]))
141 @dplug[:on_kick].should == ["foo!bar@localhost", ["#channel"], ["nick"], "reason"]
142
143 @core.on_kick(Message.new(@prefix, KICK, ["#channel,#channel1", "nick,nick1", "reason"]))
144 @dplug[:on_kick].should == ["foo!bar@localhost", ["#channel", "#channel1"], ["nick", "nick1"], "reason"]
145 end
146
147 it "should translate INVITE to on_invite event." do
148 @core.on_invite(Message.new(@prefix, INVITE, ["nick", "#channel"]))
149 @dplug[:on_invite].should == ["foo!bar@localhost", "nick", "#channel"]
150 end
151
152 it "should translate NICK to on_nick event." do
153 @core.on_nick(Message.new(@prefix, NICK, ["nick"]))
154 @dplug[:on_nick].should == ["foo!bar@localhost", "nick"]
155 end
156
157 it "should translate MODE to on_mode event." do
158 @core.on_mode(Message.new(@prefix, MODE, ["#channel", "+ooo", "nick1", "nick2", "nick3"]))
159 @dplug[:on_mode].should == ["foo!bar@localhost", "#channel", [
160 [:o, "nick1"],
161 [:o, "nick2"],
162 [:o, "nick3"],
163 ], [
164 ]]
165 end
166
167 it "should call on_message for all message" do
168 m = Message.new(@prefix, PRIVMSG, ["#channel", "message"])
169 @core.on_message(m)
170 @dplug[:on_message].should == [m]
171 end
172
173 it "should rescue plugins' exception" do
174 @core.instance_variable_get(:@logger).level = Logger::FATAL
175 proc {
176 @core.on_notice(Message.new(@prefix, NOTICE, ["#eval", "raise RuntimeError"]))
177 }.should_not raise_error
178 @core.instance_variable_get(:@logger).level = Logger::INFO
179 end
180
181 it "should run on_disconnected" do
182 @core.on_disconnected
183 @dplug[:on_downed].should == []
184 end
185
186 it "should supports enabling/disabling plugins per channel" do
187 # 1
188 @core.config.plugins["Foo"]["disabled"] = ["#foo"]
189 @core.config.plugins["Foo"]["enabled"] = nil
190
191 @core.plugins.loaded_plugins["Foo"].m.clear
192 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"]))
193 @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should_not == ["foo!bar@localhost", "#foo", "message"]
194
195 @core.plugins.loaded_plugins["Foo"].m.clear
196 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#bar", "message"]))
197 @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should == ["foo!bar@localhost", "#bar", "message"]
198
199 #2
200 @core.config.plugins["Bar"]["disabled"] = nil
201 @core.config.plugins["Bar"]["enabled"] = ["#foo"]
202
203 @core.plugins.loaded_plugins["Bar"].m.clear
204 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"]))
205 @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should == ["foo!bar@localhost", "#foo", "message"]
206
207 @core.plugins.loaded_plugins["Bar"].m.clear
208 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#bar", "message"]))
209 @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should_not == ["foo!bar@localhost", "#bar", "message"]
210
211 #3 regexp
212 @core.config.plugins["Foo"]["disabled"] = [/-ja$/]
213 @core.config.plugins["Foo"]["enabled"] = nil
214 @core.config.plugins["Bar"]["disabled"] = nil
215 @core.config.plugins["Bar"]["enabled"] = [/-ja$/]
216
217 @core.plugins.loaded_plugins["Foo"].m.clear
218 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo-ja", "message"]))
219 @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should be_nil
220
221 @core.plugins.loaded_plugins["Foo"].m.clear
222 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"]))
223 @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should == ["foo!bar@localhost", "#foo", "message"]
224
225 @core.plugins.loaded_plugins["Bar"].m.clear
226 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo-ja", "message"]))
227 @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should == ["foo!bar@localhost", "#foo-ja", "message"]
228
229 @core.plugins.loaded_plugins["Bar"].m.clear
230 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"]))
231 @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should be_nil
232 end
233
234 it "has plugins (re)loading features" do
235 proc { @core.reload_plugin("Unknown") }.should raise_error(Plugins::UnknownPlugin)
236
237 plugins = @core.plugins
238
239 @core.load_plugin("Foo")
240 @core.plugins.loaded_plugins["Foo"].config.should == @core.config.plugins["Foo"]
241
242 @core.reload_plugin("Foo")
243 @core.plugins.loaded_plugins["Foo"].config.should == @core.config.plugins["Foo"]
244
245 (@pdir + "foo.rb").utime(Time.now+10, Time.now+10)
246 i = @core.reload_plugins
247 i.should have_key("Foo")
248 @core.plugins.loaded_plugins["Foo"].config.should == @core.config.plugins["Foo"]
249
250 @core.unload_plugin("Foo")
251 @core.plugins.loaded_plugins["Foo"].should be_nil
252
253 @core.load_plugin("Foo")
254 @core.plugins.loaded_plugins["Foo"].should_not be_nil
255 end
256
257 it "should support reload config and load new plugin" do
258 @conf.open("w") do |f|
259 f << <<-EOS.unindent
260 ---
261 general:
262 host: charlotte
263 port: 6669
264 user: chokan
265 nick: chokan
266 real: Citrus bot via Tiarra
267 error: #chokan
268 plugin_dir: #{@pdir}
269 data_dir: #{@ddir}
270 charset:
271 default: utf-8
272
273 plugins:
274 System:
275 operator: "foo!bar@localhost"
276
277 Foo:
278 disabled:
279 - "#disabled"
280
281 Bar:
282 enabled:
283 - "#enabled"
284
285 Test:
286 Baz:
287 EOS
288 end
289
290 (@pdir + "baz.rb").open("w") do |f|
291 f << <<-EOS.unindent
292 require "thread"
293 class Baz < Citrus::Plugin
294
295 attr_reader :config
296
297 def on_notice(prefix, channel, message)
298 eval(message) if channel == "#eval"
299 end
300
301 def on_message(m)
302 @messages ||= SizedQueue.new(1)
303 @messages << m
304 end
305 end
306 EOS
307 end
308
309 @core.reload_config
310 @core.config.plugins.should have_key("Baz")
311 @core.plugins.loaded_plugins.should_not have_key("Baz")
312
313 @core.reload_plugin("Baz")
314 @core.plugins.loaded_plugins.should have_key("Baz")
315 end
316 end
Generated using the rcov code coverage analysis tool for Ruby
version 0.8.1.2.