Changeset 6578

Show
Ignore:
Timestamp:
02/12/08 01:33:39 (10 months ago)
Author:
cho45
Message:

lang/ruby/chokan/branches/citrus/lib/citrus/plugin.rb,
lang/ruby/chokan/branches/citrus/spec/core_spec.rb:

Core のテストに DummyCore? をつかわないように。
より実地に近い環境でのテストになってよい

Location:
lang/ruby/chokan/branches/citrus
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/chokan/branches/citrus/lib/citrus/plugin.rb

    r6449 r6578  
    2424                end 
    2525 
    26                 def on_talk(prefix, message) 
     26                def on_talk(prefix, target, message) 
    2727                end 
    2828 
     
    4242                end 
    4343 
    44                 def on_ctcp(prefix, message) 
     44                def on_ctcp(prefix, target, message) 
    4545                end 
    4646 
  • lang/ruby/chokan/branches/citrus/spec/core_spec.rb

    r6486 r6578  
    22require File.dirname(__FILE__) + '/spec_helper.rb' 
    33require "yaml" 
     4require "pp" 
    45 
    56include Citrus 
     
    2425 
    2526describe Core  do 
    26         before do 
    27                 @core   = DummyCore.new 
    28                 @dplug  = @core.plugins["Dummy"].results 
    29                 @socket = @core.socket 
     27        before :all do 
    3028                @prefix = Prefix.new("foo!bar@localhost") 
    31         end 
    32  
    33         after do 
    34                 @dplug.clear 
    35         end 
    36  
    37         it "should rescue plugins' exception" do 
    38                 @core.instance_variable_set(:@plugins, Plugins.new("/dev/null")) 
     29 
     30                @temp = Pathname.tempname 
     31                @temp.mkpath 
     32 
     33                @pdir = @temp + "plugins" 
     34                @pdir.mkpath 
     35 
     36                @ddir = @temp + "data" 
     37                @ddir.mkpath 
     38 
     39                @conf = @temp + "config.yaml" 
     40                @conf.open("w") do |f| 
     41                        f << <<-EOS.gsub(/^\t+/, "") 
     42                                --- 
     43                                general: 
     44                                  host: charlotte 
     45                                  port: 6669 
     46                                  user: chokan 
     47                                  nick: chokan 
     48                                  real: chokan bot via Tiarra 
     49                                  error: "#chokan" 
     50                                  plugin_dir: #{@pdir} 
     51                                  data_dir: #{@ddir} 
     52                                  charset: 
     53                                    default: utf-8 
     54 
     55                                plugins: 
     56                                  System: 
     57                                    operator: "foo!bar@localhost" 
     58 
     59                                  Foo: 
     60                                    disabled: 
     61                                      - "#disabled" 
     62 
     63                                  Bar: 
     64                                    enabled: 
     65                                      - "#enabled" 
     66 
     67                                  Test: 
     68                        EOS 
     69                end 
     70 
     71                (@pdir + "system.rb").open("w") do |f| 
     72                        f.puts File.read("plugins/system.rb") 
     73                end 
     74 
     75                %w(Foo Bar).each do |name| 
     76                        (@pdir + "#{name.downcase}.rb").open("w") do |f| 
     77                                f << <<-EOS.gsub(/^\t+/, "") 
     78                                        require "thread" 
     79                                        class #{name} 
     80                                                include Net::IRC 
     81                                                include Constants 
     82 
     83                                                attr_reader :config 
     84 
     85                                                def initialize(core, config) 
     86                                                        @core, @config = core, config[self.class.name.sub(/.+::/, "")] || {} 
     87                                                        @messages = {} 
     88                                                end 
     89 
     90                                                def method_missing(method, *args) 
     91                                                        @messages[method] = args 
     92                                                end 
     93 
     94                                                def m 
     95                                                        @messages 
     96                                                end 
     97                                        end 
     98                                EOS 
     99                        end 
     100                end 
     101 
     102                (@pdir + "test.rb").open("w") do |f| 
     103                        f << <<-EOS.gsub(/^\t+/, "") 
     104                                require "thread" 
     105                                class Test < Citrus::Plugin 
     106 
     107                                        attr_reader :config 
     108 
     109                                        def on_notice(prefix, channel, message) 
     110                                                eval(message) if channel == "#eval" 
     111                                        end 
     112 
     113                                        def on_message(m) 
     114                                                @messages ||= SizedQueue.new(1) 
     115                                                @messages << m 
     116                                        end 
     117                                end 
     118                        EOS 
     119                end 
     120 
     121                @sock = StringIO.new 
     122 
     123                @core = Core.new(@conf) 
     124                @core.instance_variable_set(:@socket, @sock) 
     125 
    39126                @core.on_connected 
    40         end 
    41  
    42         it "should run on_connected" do 
    43                 @core.on_connected 
     127 
     128                @core.plugins.loaded_plugins["System"].should_not be_nil 
     129                @core.plugins.loaded_plugins["Foo"].should_not be_nil 
     130                @core.plugins.loaded_plugins["Bar"].should_not be_nil 
     131 
     132                @dplug = @core.plugins.loaded_plugins["Foo"].m 
     133        end 
     134 
     135        it "should run initializing plugins and on_uped event." do 
    44136                @dplug[:on_uped].should == [] 
    45         end 
    46  
    47         it "should run on_disconnected" do 
    48                 @core.on_disconnected 
    49                 @dplug[:on_downed].should == [] 
    50137        end 
    51138 
     
    114201        end 
    115202 
     203        it "should rescue plugins' exception" do 
     204                @core.instance_variable_get(:@logger).level = Logger::FATAL 
     205                proc { 
     206                        @core.on_notice(Message.new(@prefix, NOTICE, ["#eval", "raise RuntimeError"])) 
     207                }.should_not raise_error 
     208                @core.instance_variable_get(:@logger).level = Logger::INFO 
     209        end 
     210 
     211        it "should run on_disconnected" do 
     212                @core.on_disconnected 
     213                @dplug[:on_downed].should == [] 
     214        end 
     215 
    116216        it "has plugins features" do 
    117217                proc { @core.reload_plugin("Unknown") }.should raise_error(Plugins::UnknownPlugin) 
     
    119219                plugins = @core.plugins 
    120220 
    121                 @core.load_plugin("Dummy") 
    122                 plugins.instance_variable_get(:@config).should == @core.config.plugins 
    123  
    124                 @core.reload_plugin("Dummy") 
    125                 plugins.instance_variable_get(:@config).should == @core.config.plugins 
     221                # TODO 
     222                @core.load_plugin("Foo") 
     223                @core.plugins.loaded_plugins["Foo"].config.should == @core.config.plugins["Foo"] 
     224 
     225                @core.reload_plugin("Foo") 
     226                @core.plugins.loaded_plugins["Foo"].config.should == @core.config.plugins["Foo"] 
    126227 
    127228                i = @core.reload_plugins 
    128                 plugins.instance_variable_get(:@config).should == @core.config.plugins 
    129  
    130                 @core.unload_plugin("Dummy") 
    131         end 
    132  
    133         it "should rescue plugins error" do 
    134                 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#channel", ctcp_encoding("EVAL raise 'Nice boat.'")])) 
     229                @core.plugins.loaded_plugins["Foo"].config.should == @core.config.plugins["Foo"] 
    135230        end 
    136231 
    137232        it "should supports enabling/disabling plugins per channel" do 
    138233                # 1 
    139                 @core.config.plugins["Foo"]["disabled"].should == ["#foo"] 
    140                 @core.config.plugins["Foo"]["enabled"].should  == nil 
    141  
    142                 @core.plugins["Foo"].results.clear 
    143                 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
    144                 @core.plugins["Foo"].results[:on_privmsg].should_not == ["foo!bar@localhost", "#foo", "message"] 
    145  
    146                 @core.plugins["Foo"].results.clear 
     234                @core.config.plugins["Foo"]["disabled"] = ["#foo"] 
     235                @core.config.plugins["Foo"]["enabled"]  = nil 
     236 
     237                @core.plugins.loaded_plugins["Foo"].m.clear 
     238                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
     239                @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should_not == ["foo!bar@localhost", "#foo", "message"] 
     240 
     241                @core.plugins.loaded_plugins["Foo"].m.clear 
    147242                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#bar", "message"])) 
    148                 @core.plugins["Foo"].results[:on_privmsg].should     == ["foo!bar@localhost", "#bar", "message"] 
     243                @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should     == ["foo!bar@localhost", "#bar", "message"] 
    149244 
    150245                #2 
    151                 @core.config.plugins["Bar"]["disabled"].should == nil 
    152                 @core.config.plugins["Bar"]["enabled"].should  == ["#foo"] 
    153  
    154                 @core.plugins["Bar"].results.clear 
    155                 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
    156                 @core.plugins["Bar"].results[:on_privmsg].should     == ["foo!bar@localhost", "#foo", "message"] 
    157  
    158                 @core.plugins["Bar"].results.clear 
     246                @core.config.plugins["Bar"]["disabled"] = nil 
     247                @core.config.plugins["Bar"]["enabled"]  = ["#foo"] 
     248 
     249                @core.plugins.loaded_plugins["Bar"].m.clear 
     250                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
     251                @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should     == ["foo!bar@localhost", "#foo", "message"] 
     252 
     253                @core.plugins.loaded_plugins["Bar"].m.clear 
    159254                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#bar", "message"])) 
    160                 @core.plugins["Bar"].results[:on_privmsg].should_not == ["foo!bar@localhost", "#bar", "message"] 
     255                @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should_not == ["foo!bar@localhost", "#bar", "message"] 
    161256 
    162257                #3 regexp 
     
    166261                @core.config.plugins["Bar"]["enabled"]         = [/-ja$/] 
    167262 
    168                 @core.plugins["Foo"].results.clear 
     263                @core.plugins.loaded_plugins["Foo"].m.clear 
    169264                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo-ja", "message"])) 
    170                 @core.plugins["Foo"].results[:on_privmsg].should be_nil 
    171  
    172                 @core.plugins["Foo"].results.clear 
    173                 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
    174                 @core.plugins["Foo"].results[:on_privmsg].should     == ["foo!bar@localhost", "#foo", "message"] 
    175  
    176                 @core.plugins["Bar"].results.clear 
     265                @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should be_nil 
     266 
     267                @core.plugins.loaded_plugins["Foo"].m.clear 
     268                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
     269                @core.plugins.loaded_plugins["Foo"].m[:on_privmsg].should     == ["foo!bar@localhost", "#foo", "message"] 
     270 
     271                @core.plugins.loaded_plugins["Bar"].m.clear 
    177272                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo-ja", "message"])) 
    178                 @core.plugins["Bar"].results[:on_privmsg].should     == ["foo!bar@localhost", "#foo-ja", "message"] 
    179  
    180                 @core.plugins["Bar"].results.clear 
    181                 @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
    182                 @core.plugins["Bar"].results[:on_privmsg].should be_nil 
     273                @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should     == ["foo!bar@localhost", "#foo-ja", "message"] 
     274 
     275                @core.plugins.loaded_plugins["Bar"].m.clear 
     276                @core.on_privmsg(Message.new(@prefix, PRIVMSG, ["#foo", "message"])) 
     277                @core.plugins.loaded_plugins["Bar"].m[:on_privmsg].should be_nil 
    183278        end 
    184279end 
    185