def process(self,msg=None,ex=None): self.log.debug('process') if ex: raise ex if not msg: if self.queue: msg = self.queue.pop() if not isinstance(msg,Message): raise msg else: raise QueueEmpty self.log.debug('type: %s, message: %s' % (Message.type_to_str(msg.get_type()),msg.get_text())) self.bot._cb_message(msg)
def process_chat(self, text, typ, direct=False, prefix=None, cond=True): self.bot.opts['only_direct'] = direct self.bot.opts['cmd_prefix'] = prefix user = MockUser('MOCK_USERNAME', typ) msg = Message(typ, user, text) self.bot.protocol.queue_msg(msg) try: self.bot.run_forever() except QueueEmpty: pass if cond: self.assertIn(user.get_base(), self.bot.last_cmd) else: self.assertNotIn(user.get_base(), self.bot.last_cmd)
def test_hooks_fail_status(self): types = [Message.STATUS, Message.PRIVATE, Message.GROUP, Message.ERROR] users = [ Message.PRIVATE, Message.PRIVATE, Message.GROUP, Message.PRIVATE ] hooks = ['HOOK_STATUS', 'HOOK_PRIVATE', 'HOOK_GROUP', 'HOOK_ERROR'] statuses = [Message.AVAILABLE, None, None, None] msgs = ['MOCK_STATUS', None, None, None] for (typ, user, hook, status, msg) in zip(types, users, hooks, statuses, msgs): user = MockUser('MOCK_USERNAME', user) msg = Message(typ, user, 'MOCK', status, msg) self.bot.protocol.queue_msg(msg) self.process([x for x in hooks if x != hook], False)
def test_status_hook_runs(self): user = MockUser('MOCK_USERNAME', Message.PRIVATE) msg = Message(Message.STATUS, user, 'MOCK', Message.AVAILABLE, 'MOCK_STATUS') self.bot.protocol.queue_msg(msg) self.process('HOOK_STATUS')
def test_group_hook_runs(self): user = MockUser('MOCK_USERNAME', Message.GROUP) msg = Message(Message.GROUP, user, 'MOCK') self.bot.protocol.queue_msg(msg) self.process('HOOK_GROUP')
def test_priv_hook_runs(self): user = MockUser('MOCK_USERNAME', Message.PRIVATE) msg = Message(Message.PRIVATE, user, 'MOCK') self.bot.protocol.queue_msg(msg) self.process('HOOK_PRIV')
def queue_msg(self,msg=None): self.log.debug('queue_msg(%s)' % msg) if not msg: user = MockUser('MOCK_USERNAME',Message.PRIVATE) msg = Message(Message.PRIVATE,user,'MOCK_BODY') self.queue.insert(0,msg)