1 $:.unshift File.join(File.dirname(__FILE__), '../lib')
9 include Irc::Bot::Plugins
11 class TestRealBotModule < BotModule
16 class MockModule < BotModule
17 attr_reader :test_called_at
18 attr_reader :test_arg_called_at
19 attr_reader :connect_called_at
20 attr_reader :test_arg_val
24 @test_arg_called_at = []
25 @connect_called_at = []
31 @test_called_at << Time.new
36 @test_arg_called_at << Time.new
39 # an connect fast-delegate event
41 @connect_called_at << Time.new
49 class PluginsPriorityTest < Test::Unit::TestCase
53 @mock1 = MockModule.new(1)
54 @mock2 = MockModule.new(2)
55 @mock3 = MockModule.new(3)
56 @mock4 = MockModule.new(4)
57 @mock5 = MockModule.new(5)
59 # This whole thing is a PITA because PluginManagerClass is a singleton
61 @@manager = PluginManagerClass.instance
63 # this is needed because debug is setup in the rbot starter
64 def @@manager.debug(m); puts m; end
65 def @@manager.error(m); puts m; end
66 @@manager.instance_eval { alias real_sort_modules sort_modules }
67 def @@manager.sort_modules
68 @sort_call_count ||= 0
73 @@manager.instance_eval { @sort_call_count = nil }
74 @@manager.mark_priorities_dirty
76 # We add the modules to the lists in the wrong order
77 # on purpose to make sure the sort is working
78 @@manager.plugins.clear
79 @@manager.core_modules.clear
80 @@manager.plugins << @mock1
81 @@manager.plugins << @mock4
82 @@manager.plugins << @mock3
83 @@manager.plugins << @mock2
84 @@manager.plugins << @mock5
86 dlist = @@manager.instance_eval {@delegate_list['connect'.intern]}
96 @@manager.delegate_event('test', :above => 3)
98 assert_equal 0, @mock1.test_called_at.size
99 assert_equal 0, @mock2.test_called_at.size
100 assert_equal 0, @mock3.test_called_at.size
101 assert_equal 1, @mock4.test_called_at.size
102 assert_equal 1, @mock5.test_called_at.size
106 @@manager.delegate_event('test', :below => 3)
108 assert_equal 1, @mock1.test_called_at.size
109 assert_equal 1, @mock2.test_called_at.size
110 assert_equal 0, @mock3.test_called_at.size
111 assert_equal 0, @mock4.test_called_at.size
112 assert_equal 0, @mock5.test_called_at.size
115 def test_fast_delagate_above
116 @@manager.delegate_event('connect', :above => 3)
118 assert_equal 0, @mock1.connect_called_at.size
119 assert_equal 0, @mock2.connect_called_at.size
120 assert_equal 0, @mock3.connect_called_at.size
121 assert_equal 1, @mock4.connect_called_at.size
122 assert_equal 1, @mock5.connect_called_at.size
125 def test_fast_delagate_above
126 @@manager.delegate_event('connect', :below => 3)
128 assert_equal 1, @mock1.connect_called_at.size
129 assert_equal 1, @mock2.connect_called_at.size
130 assert_equal 0, @mock3.connect_called_at.size
131 assert_equal 0, @mock4.connect_called_at.size
132 assert_equal 0, @mock5.connect_called_at.size
135 def test_call_with_args
136 @@manager.delegate_event('test_arg', :above => 3, :args => [1])
138 assert_equal 0, @mock3.test_arg_called_at.size
139 assert_equal 1, @mock4.test_arg_called_at.size
140 assert_equal 1, @mock4.test_arg_val