From 0f511bafdedf80bf065927941251e74750376418 Mon Sep 17 00:00:00 2001 From: Alexandre Morignot <erdnaxeli@gmail.com> Date: Tue, 1 Jan 2013 19:33:58 +0100 Subject: [PATCH] =?UTF-8?q?Tests=20unitaires=20et=20syt=C3=A8me=20de=20plu?= =?UTF-8?q?gin=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pour les tests unitaires, on utilise RSpec. Pour le système de plugin, une nouvelle class SitePlugin a fait son apparition. Chaque plugin doit hériter de cette class et implémenter la méthode can_handle?(site). --- README.md | 9 ++++++ lib/playbot.rb | 20 ++++++++++---- lib/site_plugin.rb | 15 ++++++++++ playbot.rb | 60 ---------------------------------------- spec/site_plugin_spec.rb | 28 +++++++++++++++++++ 5 files changed, 67 insertions(+), 65 deletions(-) create mode 100644 lib/site_plugin.rb delete mode 100755 playbot.rb create mode 100644 spec/site_plugin_spec.rb diff --git a/README.md b/README.md index ea00924..77b81fa 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,14 @@ # PlayBot +## ATTENTION + +Cette branche a pour but la réécriture du PlayBot en ruby. Avec cette réécriture, un système de plugin et des tests unitaires seront de la partie. + +Les dépendances nécéssaires sont : + * net-yail + * rspec + + ## Description diff --git a/lib/playbot.rb b/lib/playbot.rb index c39a383..3f9203b 100644 --- a/lib/playbot.rb +++ b/lib/playbot.rb @@ -1,15 +1,20 @@ -#!/usr/bin/ruby - - require 'rubygems' require 'net/yail/irc_bot' +require 'site_plugin' + +# +# Add plugins folder to LOAD_PATH and subsequently require all plugins. +# +dir = '../plugins' +$LOAD_PATH << dir +Dir[File.join(dir, '*.rb')].each {|file| require File.basename(file) } class PlayBot < IRCBot BOTNAME = 'PlayBot' public - # Star a new instance + # Start a new instance # # Options: # * <tt>:address</tt>: irc server @@ -39,7 +44,7 @@ class PlayBot < IRCBot # This metod is called by IRCBot#connect_socket def add_custom_handlers() @irc.hearing_welcome self.method(:_in_welcome) - #@irc.on_msg self.method(:_in_msg) + @irc.on_msg self.method(:_in_msg) end private @@ -57,4 +62,9 @@ class PlayBot < IRCBot msg('NickServ', "identify #{nick_passwd}") end + + def _in_msg(event) + # we don't care of private messages + return if event.pm? + end end diff --git a/lib/site_plugin.rb b/lib/site_plugin.rb new file mode 100644 index 0000000..c59d975 --- /dev/null +++ b/lib/site_plugin.rb @@ -0,0 +1,15 @@ +class SitePlugin + @@repository = [] + + def self.repository + @@repository + end + + def self.inherited(target) + @@repository << target + end + + def self.for_site(site) + @@repository.find {|handler| handler.can_handle? site } + end +end diff --git a/playbot.rb b/playbot.rb deleted file mode 100755 index 76db536..0000000 --- a/playbot.rb +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/ruby - - -require 'rubygems' -require 'net/yail/irc_bot' - - -class PlayBot < IRCBot - BOTNAME = 'PlayBot' - - public - # Star a new instance - # - # Options: - # * <tt>:address</tt>: irc server - # * <tt>:port</tt>: port number, default to 6667 - # * <tt>:nicknames</tt>: array of nicknames to cycle through - # * <tt>:nick_passwd</tt>: password for the first nick of :nicknames - # if we are not connected with this nick, we will use ghost and take this nick - # * <tt>:channels</tt>: the channels we are going to connect to - # * <tt>:admin</tt>: the nick of the user who can command the bot - def initialize(options = {}) - @admin = options.delete(:master) - (!@admin) ? raise "You must provide an admin !" - - if options[:nick_passwd] - @nick = options[:nicknames].first - @nick_paswd = options.delete[:nick_passwd] - end - - options[:username] = BOTNAME - options[:realname] = BOTNAME - - self.connect_socket - self.start_listening - end - - # This metod is called by IRCBot#connect_socket - def add_custom_handlers() - @irc.on_welcome self.method(:_in_welcome) - @irc.on_msg self.method(:_in_msg) - @irc.saying_join self.method(:_out_join) - end - - private - # Welcome event handler - # - # We use it to identify us against NickServ - def _in_welcome(event) - return if !@nick - - if self.bot_name != @nick - msg('NickServ', "ghost #{nick} #{nick_passwd}") - sleep 30 - nick @nick - end - - msg('NickServ', "identify #{nick_passwd}") - end -end diff --git a/spec/site_plugin_spec.rb b/spec/site_plugin_spec.rb new file mode 100644 index 0000000..612cf31 --- /dev/null +++ b/spec/site_plugin_spec.rb @@ -0,0 +1,28 @@ +$LOAD_PATH << '../lib' +require 'site_plugin' + +describe SitePlugin do + describe 'inherance' do + it 'add the class to the repository' do + class TestPlugin1 < SitePlugin + def self.can_handle?(site) + false + end + end + + SitePlugin.repository.empty?.should be_false + end + end + + describe '.for_site' do + it 'return plugin that can handle a given site"' do + class TestPlugin2 < SitePlugin + def self.can_handle?(site) + site =~ /test/ + end + end + + SitePlugin.for_site('OfCourseICanHandle_test_').nil?.should be_false + end + end +end -- GitLab