|
| 1 | +require "helper" |
| 2 | + |
| 3 | +module Cinch |
| 4 | + class CinchTestPluginWithoutName |
| 5 | + include Cinch::Plugin |
| 6 | + end |
| 7 | +end |
| 8 | + |
| 9 | +class PluginTest < TestCase |
| 10 | + def setup |
| 11 | + @bot = Cinch::Bot.new { |
| 12 | + self.loggers.clear |
| 13 | + } |
| 14 | + @plugin = Class.new { include Cinch::Plugin } |
| 15 | + @bot.config.plugins.options = {@plugin => {:key => :value}} |
| 16 | + |
| 17 | + @plugin.plugin_name = "testplugin" |
| 18 | + @plugin_instance = @plugin.new(@bot) |
| 19 | + end |
| 20 | + |
| 21 | + test "should be able to specify matchers" do |
| 22 | + @plugin.match(/pattern/) |
| 23 | + matcher = @plugin.matchers.last |
| 24 | + |
| 25 | + assert_equal(1, @plugin.matchers.size, "Shoult not forget existing matchers") |
| 26 | + assert_equal Cinch::Plugin::ClassMethods::Matcher.new(/pattern/, true, true, :execute), matcher |
| 27 | + |
| 28 | + matcher = @plugin.match(/pattern/, use_prefix: false, use_suffix: false, method: :some_method) |
| 29 | + assert_equal Cinch::Plugin::ClassMethods::Matcher.new(/pattern/, false, false, :some_method), matcher |
| 30 | + end |
| 31 | + |
| 32 | + test "should be able to listen to events" do |
| 33 | + @plugin.listen_to(:event1, :event2) |
| 34 | + @plugin.listen_to(:event3, method: :some_method) |
| 35 | + |
| 36 | + listeners = @plugin.listeners |
| 37 | + assert_equal 3, listeners.size |
| 38 | + assert_equal [:event1, :event2, :event3], listeners.map(&:event) |
| 39 | + assert_equal [:listen, :listen, :some_method], listeners.map(&:method) |
| 40 | + end |
| 41 | + |
| 42 | + test "should be able to create CTCP commands" do |
| 43 | + @plugin.ctcp("FOO") |
| 44 | + @plugin.ctcp("BAR") |
| 45 | + |
| 46 | + assert_equal 2, @plugin.ctcps.size |
| 47 | + assert_equal ["FOO", "BAR"], @plugin.ctcps |
| 48 | + end |
| 49 | + |
| 50 | + test "CTCP commands should always be uppercase" do |
| 51 | + @plugin.ctcp("foo") |
| 52 | + assert_equal "FOO", @plugin.ctcps.last |
| 53 | + end |
| 54 | + |
| 55 | + test "should return an empty array of timers" do |
| 56 | + assert_equal [], @plugin.timers |
| 57 | + end |
| 58 | + |
| 59 | + test "should return an empty array of listeners" do |
| 60 | + assert_equal [], @plugin.listeners |
| 61 | + end |
| 62 | + |
| 63 | + test "should return an empty array of CTCPs" do |
| 64 | + assert_equal [], @plugin.ctcps |
| 65 | + end |
| 66 | + |
| 67 | + test "should be able to set timers" do |
| 68 | + @plugin.timer(1, method: :foo) |
| 69 | + @plugin.timer(2, method: :bar, :threaded => false) |
| 70 | + |
| 71 | + timers = @plugin.timers |
| 72 | + assert_equal 2, timers.size |
| 73 | + assert_equal [1, 2], timers.map(&:interval) |
| 74 | + assert_equal [:foo, :bar], timers.map {|t| t.options[:method]} |
| 75 | + assert_equal [true, false], timers.map {|t| t.options[:threaded]} |
| 76 | + end |
| 77 | + |
| 78 | + test "should be able to register hooks" do |
| 79 | + @plugin.hook(:pre) |
| 80 | + @plugin.hook(:post, :for => [:match]) |
| 81 | + @plugin.hook(:post, :method => :some_method) |
| 82 | + |
| 83 | + hooks = @plugin.hooks.values.flatten |
| 84 | + assert_equal [:pre, :post, :post], hooks.map(&:type) |
| 85 | + assert_equal [:match], hooks[1].for |
| 86 | + assert_equal :some_method, hooks.last.method |
| 87 | + assert_equal :hook, hooks.first.method |
| 88 | + end |
| 89 | + |
| 90 | + test "should have access to plugin configuration" do |
| 91 | + assert_equal :value, @plugin_instance.config[:key] |
| 92 | + end |
| 93 | + |
| 94 | + test "should be able to set a prefix with a block" do |
| 95 | + block = lambda {|m| "^"} |
| 96 | + @plugin.prefix = block |
| 97 | + assert_equal block, @plugin.prefix |
| 98 | + end |
| 99 | + |
| 100 | + test "should be able to set a suffix with a block" do |
| 101 | + block = lambda {|m| "^"} |
| 102 | + @plugin.suffix = block |
| 103 | + assert_equal block, @plugin.suffix |
| 104 | + end |
| 105 | + |
| 106 | + test "should support `set(key, value)`" do |
| 107 | + @plugin.set :help, "some help message" |
| 108 | + @plugin.set :prefix, "some prefix" |
| 109 | + @plugin.set :suffix, "some suffix" |
| 110 | + @plugin.set :plugin_name, "some plugin" |
| 111 | + @plugin.set :reacting_on, :event1 |
| 112 | + |
| 113 | + assert_equal "some help message", @plugin.help |
| 114 | + assert_equal "some prefix", @plugin.prefix |
| 115 | + assert_equal "some suffix", @plugin.suffix |
| 116 | + assert_equal "some plugin", @plugin.plugin_name |
| 117 | + assert_equal :event1, @plugin.reacting_on |
| 118 | + end |
| 119 | + |
| 120 | + test "should support `set(key => value, key => value, ...)`" do |
| 121 | + @plugin.set(:help => "some help message", |
| 122 | + :prefix => "some prefix", |
| 123 | + :suffix => "some suffix", |
| 124 | + :plugin_name => "some plugin", |
| 125 | + :reacting_on => :event1) |
| 126 | + |
| 127 | + assert_equal "some help message", @plugin.help |
| 128 | + assert_equal "some prefix", @plugin.prefix |
| 129 | + assert_equal "some suffix", @plugin.suffix |
| 130 | + assert_equal "some plugin", @plugin.plugin_name |
| 131 | + assert_equal :event1, @plugin.reacting_on |
| 132 | + end |
| 133 | + |
| 134 | + test "should support `self.key = value`" do |
| 135 | + @plugin.help = "some help message" |
| 136 | + @plugin.prefix = "some prefix" |
| 137 | + @plugin.suffix = "some suffix" |
| 138 | + @plugin.plugin_name = "some plugin" |
| 139 | + @plugin.reacting_on = :event1 |
| 140 | + |
| 141 | + assert_equal "some help message", @plugin.help |
| 142 | + assert_equal "some prefix", @plugin.prefix |
| 143 | + assert_equal "some suffix", @plugin.suffix |
| 144 | + assert_equal "some plugin", @plugin.plugin_name |
| 145 | + assert_equal :event1, @plugin.reacting_on |
| 146 | + end |
| 147 | + |
| 148 | + test "should support querying attributes" do |
| 149 | + @plugin.plugin_name = "foo" |
| 150 | + @plugin.help = "I am a help message" |
| 151 | + @plugin.prefix = "^" |
| 152 | + @plugin.suffix = "!" |
| 153 | + @plugin.react_on(:event1) |
| 154 | + |
| 155 | + assert_equal "foo", @plugin.plugin_name |
| 156 | + assert_equal "I am a help message", @plugin.help |
| 157 | + assert_equal "^", @plugin.prefix |
| 158 | + assert_equal "!", @plugin.suffix |
| 159 | + assert_equal :event1, @plugin.reacting_on |
| 160 | + end |
| 161 | + |
| 162 | + test "should have a default name" do |
| 163 | + assert_equal "cinchtestpluginwithoutname", Cinch::CinchTestPluginWithoutName.plugin_name |
| 164 | + end |
| 165 | + |
| 166 | + test "should check for the right number of arguments for `set`" do |
| 167 | + assert_raises(ArgumentError) { @plugin.set() } |
| 168 | + assert_raises(ArgumentError) { @plugin.set(1, 2, 3) } |
| 169 | + end |
| 170 | +end |
0 commit comments