Skip to content
Extraits de code Groupes Projets
Valider 0f511baf rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

Tests unitaires et sytème de plugin !

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).
parent 635b77f9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
# 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
......
#!/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
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
#!/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
$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
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter