From 618df277b7cb7d160ba5c86d1a22298a4741ed5f Mon Sep 17 00:00:00 2001 From: Matthias Hecker Date: Mon, 6 Apr 2020 20:20:32 +0200 Subject: tests: mocked bot and messages added, added points tests --- test/mock.rb | 71 +++++++++++++++++++++++++++++++++++++++++++++ test/plugins/test_points.rb | 44 ++++++++++++++++++++++++++++ test/plugins/test_rot13.rb | 53 ++++----------------------------- 3 files changed, 120 insertions(+), 48 deletions(-) create mode 100644 test/mock.rb create mode 100644 test/plugins/test_points.rb (limited to 'test') diff --git a/test/mock.rb b/test/mock.rb new file mode 100644 index 00000000..f1ce92dd --- /dev/null +++ b/test/mock.rb @@ -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 index 00000000..f1a7479d --- /dev/null +++ b/test/plugins/test_points.rb @@ -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 diff --git a/test/plugins/test_rot13.rb b/test/plugins/test_rot13.rb index aa4bb185..fafb58ba 100644 --- a/test/plugins/test_rot13.rb +++ b/test/plugins/test_rot13.rb @@ -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 => encode 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 -- cgit v1.2.3