root/lang/ruby/citrus/trunk/plugins/system.rb

Revision 6594, 4.2 kB (checked in by cho45, 7 months ago)

lang/ruby/chokan/branches/citrus/plugins:

テスト時の @socket には Queue をつかうように。

  • スレッドを扱うテストをスマートかつ確実に書けるように

Line 
1
2class System < Citrus::Plugin
3        def initialize(*args)
4                super
5
6                if @config['operator'].kind_of?(String)
7                        @config['operator'] = Regexp.new(@config['operator'])
8                end
9        end
10
11        def on_privmsg(prefix, channel, message)
12                return unless @config['operator'] === prefix
13                case message
14                when /^reload(?:\s+([a-z]+))?$/i
15                        log "#{prefix}: call reloading"
16                        begin
17                                @core.reload_config
18                        rescue => e
19                                log e.message
20                        end
21                        name = Regexp.last_match[1]
22                        log name
23                        if name
24                                begin
25                                        instances = [@core.reload_plugin(name)]
26                                rescue Citrus::Plugins::UnknownPlugin => e
27                                        notice channel, e.message
28                                        instances = []
29                                end
30                        else
31                                instances = @core.reload_plugins.values
32                        end
33                        if instances.empty?
34                                notice channel, "No plugins to reload."
35                        else
36                                reloaded = instances.map {|i|
37                                        i.class.name.sub(/^.+::/, "")
38                                }.join(" ")
39                                notice channel, "Reloaded: " + reloaded
40                        end
41
42                when /^chokan: join to (\S+)(?: (\S+))?/
43                        chan, pass = Regexp.last_match.captures
44                        log "Joining to '#{chan}' with '#{pass}'"
45                        join(chan.to_s, pass.to_s)
46
47                when "chokan: part"
48                        part(channel, "lambda....")
49
50                when "operator?"
51                        notice channel, "You are an operator for me."
52
53                when "Gem.clear_paths"
54                        r = Gem.clear_paths
55                        notice channel, "Gem.clear_paths #{r.inspect}"
56                end
57        end
58end
59
60tests do
61
62        describe System do
63                before do
64                        @core     = DummyCore.new({
65                                "plugins" => { "Foo" => nil, "Bar" => nil }
66                        })
67                        @pdir = Pathname.new(@core.config.general["plugin_dir"])
68
69                        %w(Foo Bar).each do |name|
70                                (@pdir + "#{name.downcase}.rb").open("w") do |f|
71                                        f << <<-EOS.unindent
72                                                require "thread"
73                                                class #{name}
74                                                        include Net::IRC
75                                                        include Constants
76
77                                                        attr_reader :config
78
79                                                        def initialize(core, config)
80                                                                @core, @config = core, config[self.class.name.sub(/.+::/, "")] || {}
81                                                                @messages = {}
82                                                        end
83
84                                                        def method_missing(method, *args)
85                                                                @messages[method] = args
86                                                        end
87
88                                                        def m
89                                                                @messages
90                                                        end
91                                                end
92                                        EOS
93                                end
94                        end
95
96                        @socket   = @core.socket
97                        @prefix   = Net::IRC::Prefix.new("foo!foo@localhost")
98                        @prefixop = Net::IRC::Prefix.new("foo!bar@localhost")
99
100                        @plugin = System.new(@core, { "System" => {
101                                "operator" => "foo!bar@localhost",
102                        } })
103
104                        @core.init_plugins
105                end
106
107                it "should response to operator" do
108                        @socket.clear
109                        @plugin.on_privmsg(@prefix, "#test", "operator?")
110                        @socket.should be_empty
111
112                        @socket.clear
113                        @plugin.on_privmsg(@prefixop, "#test", "operator?")
114                        @socket.pop.to_s.should == "NOTICE #test :You are an operator for me.\r\n"
115
116                        @plugin = System.new(@core, { "System" => {
117                                "operator" => "foo!bar@.+",
118                        } })
119
120                        @socket.clear
121                        @plugin.on_privmsg(@prefixop, "#test", "operator?")
122                        @socket.pop.to_s.should == "NOTICE #test :You are an operator for me.\r\n"
123
124                        @plugin = System.new(@core, { "System" => {
125                                "operator" => /foo!bar@.+/,
126                        } })
127
128                        @socket.clear
129                        @plugin.on_privmsg(@prefixop, "#test", "operator?")
130                        @socket.pop.to_s.should == "NOTICE #test :You are an operator for me.\r\n"
131                end
132
133                it "can reload_plugins" do
134                        @socket.clear
135                        @plugin.on_privmsg(@prefixop, "#test", "reload")
136                        @socket.pop.to_s.should match(/^NOTICE #test /)
137
138                        @socket.clear
139                        @plugin.on_privmsg(@prefixop, "#test", "reload Foo")
140                        @socket.pop.to_s.should match(/^NOTICE #test /)
141
142                        @socket.clear
143                        @plugin.on_privmsg(@prefixop, "#test", "reload Unknown")
144                        @socket.pop.to_s.should match(/^NOTICE #test /)
145
146                        def @core.reload_config
147                                raise "config error"
148                        end
149
150                        @socket.clear
151                        @plugin.on_privmsg(@prefixop, "#test", "reload")
152                        @socket.pop.to_s.should match(/^NOTICE #test /)
153                end
154
155                it "can operate join/part" do
156                        @socket.clear
157                        @plugin.on_privmsg(@prefixop, "#test", "chokan: part")
158                        @socket.pop.to_s.should match(/^PART #test /)
159
160                        @socket.clear
161                        @plugin.on_privmsg(@prefixop, "#test", "chokan: join to #foobar")
162                        @socket.pop.to_s.should match(/^JOIN #foobar /)
163
164                        @socket.clear
165                        @plugin.on_privmsg(@prefixop, "#test", "chokan: join to #foobar password")
166                        @socket.pop.to_s.should match(/^JOIN #foobar password/)
167                end
168
169                it "can Gem.clear_paths" do
170                        @socket.clear
171                        @plugin.on_privmsg(@prefixop, "#test", "Gem.clear_paths")
172                        @socket.pop.to_s.should match(/^NOTICE #test :Gem.clear_paths/)
173                end
174        end
175
176end
177
Note: See TracBrowser for help on using the browser.