]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
tests: mocked bot and messages added, added points tests
authorMatthias Hecker <mail@apoc.cc>
Mon, 6 Apr 2020 18:20:32 +0000 (20:20 +0200)
committerMatthias Hecker <mail@apoc.cc>
Mon, 6 Apr 2020 18:20:32 +0000 (20:20 +0200)
test/mock.rb [new file with mode: 0644]
test/plugins/test_points.rb [new file with mode: 0644]
test/plugins/test_rot13.rb

diff --git a/test/mock.rb b/test/mock.rb
new file mode 100644 (file)
index 0000000..f1ce92d
--- /dev/null
@@ -0,0 +1,71 @@
+$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
+$:.unshift File.join(File.dirname(__FILE__), '..')
+
+
+module Irc
+class Bot
+  module Config
+    @@datadir = File.expand_path(File.dirname(__FILE__) + '/../data/rbot')
+    @@coredir = File.expand_path(File.dirname(__FILE__) + '/../lib/rbot/core')
+  end
+end
+end
+
+
+class MockBot
+  attr_reader :filters, :lang
+
+  def initialize
+    @filters = {}
+    @lang = Irc::Bot::Language.new(self, 'english')
+  end
+
+  def register_filter(name, &block)
+    @filters[name] = block
+  end
+
+  def filter(name, value)
+    @filters[name].call({text: value})[:text]
+  end
+
+  def nick
+    'bot'
+  end
+
+  def path(*components)
+    File.join('/tmp/rbot-test', *(components.map {|c| c.to_s}))
+  end
+
+  def plugins
+    nil
+  end
+
+  def registry_factory
+    Irc::Bot::Registry.new('mem')
+  end
+end
+
+
+class MockMessage
+  attr_reader :message
+  attr_reader :replies
+  attr_reader :channel
+  attr_reader :sourcenick
+
+  def initialize(message='', source='user')
+    @message = message
+    @sourcenick = source
+    @channel = Irc::Channel.new('#test', '', [], server: nil)
+    @replies = []
+  end
+
+  def reply(message)
+    @replies << message
+  end
+
+  def public?
+    true
+  end
+end
+
+
diff --git a/test/plugins/test_points.rb b/test/plugins/test_points.rb
new file mode 100644 (file)
index 0000000..f1a7479
--- /dev/null
@@ -0,0 +1,44 @@
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
+$:.unshift File.join(File.dirname(__FILE__), '..', '..')
+
+require 'test/unit'
+require 'test/mock'
+
+require 'rbot/ircbot'
+require 'rbot/registry'
+require 'rbot/plugins'
+require 'rbot/language'
+
+class PointsPluginTest < Test::Unit::TestCase
+  def setup
+    manager = Irc::Bot::Plugins.manager
+    manager.bot_associate(MockBot.new)
+    manager.load_botmodule_file('./data/rbot/plugins/points.rb')
+    @plugin = manager.get_plugin('points')
+  end
+
+  def test_points
+    assert_not_nil(@plugin)
+    assert_not_empty(@plugin.help(nil))
+
+    m = MockMessage.new('linux++', 'user')
+    @plugin.message(m)
+    assert_equal('linux now has 1 points!', m.replies.first)
+
+    m = MockMessage.new('linux++', 'user')
+    @plugin.message(m)
+    assert_equal('linux now has 2 points!', m.replies.first)
+
+    m = MockMessage.new('linux++', 'linux')
+    @plugin.message(m)
+    assert_empty(m.replies)
+
+    m = MockMessage.new('', 'user')
+    @plugin.points(m, key: 'linux')
+    assert_equal('points for linux: 2', m.replies.first)
+
+    m = MockMessage.new('', 'linux')
+    @plugin.points(m, {})
+    assert_equal('points for linux: 2', m.replies.first)
+  end
+end
index aa4bb18576c11a4f335e679fbeb1e7e468d7b8fa..fafb58bacda670628fc7ac06e93708ea78423de8 100644 (file)
@@ -1,57 +1,14 @@
-$:.unshift File.join(File.dirname(__FILE__), '../lib')
-
-module Irc
-class Bot
-  module Config
-    @@datadir = File.expand_path(File.dirname($0) + '/../data/rbot')
-    @@coredir = File.expand_path(File.dirname($0) + '/../lib/rbot/core')
-  end
-end
-end
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
+$:.unshift File.join(File.dirname(__FILE__), '..', '..')
 
 require 'test/unit'
+require 'test/mock'
+
 require 'rbot/ircbot'
 require 'rbot/registry'
 require 'rbot/plugins'
 
 
-class MockBot
-  attr_reader :filters
-  def initialize
-    @filters = {}
-  end
-
-  def register_filter(name, &block)
-    @filters[name] = block
-  end
-
-  def filter(name, value)
-    @filters[name].call({text: value})[:text]
-  end
-
-  def path
-    ''
-  end
-
-  def registry_factory
-    Irc::Bot::Registry.new('tc')
-  end
-end
-
-
-class MockMessage
-  attr_reader :messages
-
-  def initialize
-    @messages = []
-  end
-
-  def reply(message)
-    @messages << message
-  end
-end
-
-
 class PluginTest < Test::Unit::TestCase
   def setup
     manager = Irc::Bot::Plugins.manager
@@ -65,6 +22,6 @@ class PluginTest < Test::Unit::TestCase
     assert_equal(@plugin.help(nil), "rot13 <string> => encode <string> to rot13 or back")
     m = MockMessage.new
     @plugin.rot13(m, {string: 'Hello World'})
-    assert_equal(m.messages.first, 'Uryyb Jbeyq')
+    assert_equal(m.replies.first, 'Uryyb Jbeyq')
   end
 end