コード例 #1
0
ファイル: bot_test.py プロジェクト: fgmacedo/alfredo
class BotTest(unittest.TestCase):
    def setUp(self):
        self.bot = GtalkRobot()

    """
   Should parse the original message:
    "inv arg0 agr1" becomes ('inv', ('arg0, 'arg1))
  """

    def test_parse_message_more_than_one_argument(self):
        self.assertEquals(("inv", ("arg0", "arg1")), self.bot._parse_message("inv arg0 arg1"))

    def test_parse_message_more_one_argument(self):
        self.assertEquals(("inv", ("arg0",)), self.bot._parse_message("inv arg0"))

    def test_parse_message_more_no_arguments(self):
        self.assertEquals(("inv", ()), self.bot._parse_message("inv"))
        self.assertEquals(("inv", ()), self.bot._parse_message("inv  "))

    """
   Alfredo mystr return "command not found" when it does not
   find a command implementor
  """

    def test_command_not_found(self):
        with patch.object(GtalkRobot, "_replyMessage"):
            message = _mock_message("*****@*****.**", "somecommand arg0 arg1")

            self.bot._processMessage(Mock(), message)
            self.bot._replyMessage.assert_called_with(
                "*****@*****.**", "somecommand is not a valid command. Try help to know more"
            )

    def test_call_command(self):
        class EchoCommand(Plugin):
            implements = [ICommand]

            def match_name(self, command):
                return "echo" == command

            def run(self, user, command, *args):
                pass

        with patch.object(ICommand, "implementors"):
            echo = Mock()
            echo.match_name.return_value = True
            ICommand.implementors.return_value = [echo]
            self.bot.conn = Mock()
            self.bot._processMessage(Mock(), _mock_message("user@domain", "echo arg0 arg1"))
            echo.run.assert_called_with("user@domain", "echo", "arg0", "arg1")

    def test_reply_command_output_to_user(self):
        with patch.object(GtalkRobot, "_replyMessage"):
            message = _mock_message("*****@*****.**", "print arg0 arg1")

            self.bot._processMessage(Mock(), message)
            self.bot._replyMessage.assert_called_with("*****@*****.**", "arg0 arg1")

    def test_call_command_zero_arguments(self):
        with patch.object(GtalkRobot, "_replyMessage"):
            message = _mock_message("*****@*****.**", "print")

            self.bot._processMessage(Mock(), message)
            self.bot._replyMessage.assert_called_with("*****@*****.**", "one arg required")

    def test_call_command_raise_exception(self):
        with patch.object(ICommand, "implementors"):
            with patch.object(GtalkRobot, "_replyMessage"):
                echo = Mock()
                echo.match_name.return_value = True
                echo.run.side_effect = Exception("Boom")
                ICommand.implementors.return_value = [echo]
                self.bot.conn = Mock()
                self.bot._processMessage(Mock(), _mock_message("user@domain", "echo arg0 arg1"))
                self.bot._replyMessage.assert_called_with("user@domain", "Error running command: echo arg0 arg1")
コード例 #2
0
ファイル: bot_test.py プロジェクト: fgmacedo/alfredo
 def setUp(self):
     self.bot = GtalkRobot()
コード例 #3
0
ファイル: example.py プロジェクト: cuimuxi/alfredo
import plugnplay
from alfredo.PyGtalkRobot import GtalkRobot

try:
    from bot_config import gmail_account, gmail_pass
except:
    gmail_account = 'your_gmail_account'
    gmail_pass = '******'

plugnplay.set_plugin_dirs('commands')
plugnplay.load_plugins()

bot = GtalkRobot()
bot.start(gmail_account, gmail_pass)