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 :connect_called_at
22 @connect_called_at = []
27 @test_called_at << Time.new
30 # an connect fast-delegate event
32 @connect_called_at << Time.new
40 class PluginsPriorityTest < Test::Unit::TestCase
44 @mock1 = MockModule.new(1)
45 @mock2 = MockModule.new(2)
46 @mock3 = MockModule.new(3)
47 @mock4 = MockModule.new(4)
48 @mock5 = MockModule.new(5)
50 # This whole thing is a PITA because PluginManagerClass is a singleton
52 @@manager = PluginManagerClass.instance
54 # this is needed because debug is setup in the rbot starter
55 def @@manager.debug(m); puts m; end
56 @@manager.instance_eval { alias real_sort_modules sort_modules }
57 def @@manager.sort_modules
58 @sort_call_count ||= 0
63 @@manager.instance_eval { @sort_call_count = nil }
64 @@manager.mark_priorities_dirty
66 # We add the modules to the lists in the wrong order
67 # on purpose to make sure the sort is working
68 @@manager.plugins.clear
69 @@manager.core_modules.clear
70 @@manager.plugins << @mock1
71 @@manager.plugins << @mock4
72 @@manager.plugins << @mock3
73 @@manager.plugins << @mock2
74 @@manager.plugins << @mock5
76 dlist = @@manager.instance_eval {@delegate_list['connect'.intern]}
85 def test_default_priority
86 plugin = TestRealBotModule.new
87 assert_equal 1, plugin.priority
91 @@manager.delegate('test')
93 assert @@manager.instance_eval { @sort_call_count }
96 def test_sort_called_once
97 @@manager.delegate('test')
98 @@manager.delegate('test')
99 @@manager.delegate('test')
100 @@manager.delegate('test')
102 assert_equal 1, @@manager.instance_eval { @sort_call_count }
106 plugins = @@manager.plugins
107 assert_equal @mock1, plugins[0]
108 assert_equal @mock4, plugins[1]
109 assert_equal @mock3, plugins[2]
110 assert_equal @mock2, plugins[3]
111 assert_equal @mock5, plugins[4]
113 @@manager.sort_modules
114 plugins = @@manager.instance_eval { @sorted_modules }
116 assert_equal @mock1, plugins[0]
117 assert_equal @mock2, plugins[1]
118 assert_equal @mock3, plugins[2]
119 assert_equal @mock4, plugins[3]
120 assert_equal @mock5, plugins[4]
123 def test_fast_delegate_sort
124 list = @@manager.instance_eval {@delegate_list['connect'.intern]}
125 assert_equal @mock1, list[0]
126 assert_equal @mock4, list[1]
127 assert_equal @mock3, list[2]
128 assert_equal @mock2, list[3]
129 assert_equal @mock5, list[4]
131 @@manager.sort_modules
132 assert_equal @mock1, list[0]
133 assert_equal @mock2, list[1]
134 assert_equal @mock3, list[2]
135 assert_equal @mock4, list[3]
136 assert_equal @mock5, list[4]
139 def test_slow_called_in_order
140 @@manager.delegate('test')
141 assert_equal 1, @mock1.test_called_at.size
142 assert_equal 1, @mock2.test_called_at.size
143 assert_equal 1, @mock3.test_called_at.size
144 assert_equal 1, @mock4.test_called_at.size
145 assert_equal 1, @mock5.test_called_at.size
147 assert @mock1.test_called_at.first < @mock2.test_called_at.first
148 assert @mock2.test_called_at.first < @mock3.test_called_at.first
149 assert @mock3.test_called_at.first < @mock4.test_called_at.first
150 assert @mock4.test_called_at.first < @mock5.test_called_at.first
153 def test_fast_called_in_order
154 @@manager.delegate('connect')
155 assert_equal 1, @mock1.connect_called_at.size
156 assert_equal 1, @mock2.connect_called_at.size
157 assert_equal 1, @mock3.connect_called_at.size
158 assert_equal 1, @mock4.connect_called_at.size
159 assert_equal 1, @mock5.connect_called_at.size
161 assert @mock1.connect_called_at.first < @mock2.connect_called_at.first
162 assert @mock2.connect_called_at.first < @mock3.connect_called_at.first
163 assert @mock3.connect_called_at.first < @mock4.connect_called_at.first
164 assert @mock4.connect_called_at.first < @mock5.connect_called_at.first
167 def test_add_botmodule
168 @@manager.sort_modules
169 mock_n1 = MockModule.new(-1)
170 @@manager.add_botmodule mock_n1
171 @@manager.delegate('test')
172 assert mock_n1.test_called_at.first < @mock1.test_called_at.first