def test_message_types(privmsg, actionmsg, servermsg): # privmsg assert privmsg.type == 'PRIVMSG' assert str(privmsg) == privmsg.message assert repr(privmsg) == SAMPLE_PRIV assert privmsg.host assert privmsg.nick assert privmsg.realname assert privmsg.dest assert privmsg.message # server message assert servermsg.type == 366 assert str(servermsg) == SAMPLE_SERVER assert repr(servermsg) == SAMPLE_SERVER # action message assert actionmsg.type == 'ACTION' assert str(actionmsg) == actionmsg.message assert repr(actionmsg) == SAMPLE_ACTION assert actionmsg.host assert actionmsg.nick assert actionmsg.realname assert actionmsg.dest assert actionmsg.message # comparison privmsg_again = Message(SAMPLE_PRIV) assert privmsg != servermsg, '__eq__ method of Message is broken' assert privmsg != actionmsg, '__eq__ method of Message is broken' assert privmsg == privmsg_again, '__eq__ method of Message is broken' assert servermsg != privmsg_again, '__eq__ method of Message is broken' assert servermsg != actionmsg, '__eq__ method of Message is broken' # a malformed hostmask will execute "regex f****d up" codepath malformed_hm = ':[email protected] PRIVMSG #channel :something' with pytest.raises(AttributeError): malformed = Message(malformed_hm)
def test_handle(command_bot): fs = command_bot.conn.conn command_bot._enable_builtin_commands() inspired = fs.text.splitlines() for idx, i in enumerate(inspired): m = Message(i) command_bot.handle(m) command_bot.conn.conn = None for i in inspired: m = Message(i) assert not command_bot.handle(m)
def test_messageify(connected_bot): fs = connected_bot.conn.conn inspired = _get_log('InspIRCd-2.0.log') inspired_messages = [Message(line) for line in inspired.splitlines()] for idx, mify_object in enumerate( connected_bot._messageify(inspired.splitlines())): assert mify_object == inspired_messages[idx]
def test_builtin_join(connected_bot): log = ':[email protected] PRIVMSG #defaultchannel :.join newch' bad_log = ':[email protected] PRIVMSG #defaultchannel :.join' bad_reply = 'PRIVMSG #defaultchannel :Usage: ^\\.join #channelname' fs = connected_bot.conn.conn fs._set_reply_text(log) m = Message(log) assert not connected_bot.handle(m), 'Commands not yet activated!' with connected_bot.conn: # try out a proper command connected_bot._enable_builtin_commands() assert connected_bot.handle(m) assert _wrap('JOIN #newch') in fs.sent # try the case where the user doesn't provide an arg m = Message(bad_log) assert connected_bot.handle(m) assert _wrap(bad_reply) in fs.sent
def test_builtin_help(connected_bot): log = ':[email protected] PRIVMSG #defaultchannel :.help' fs = connected_bot.conn.conn fs._set_reply_text(log) m = Message(log) assert not connected_bot.handle(m), 'Commands not yet activated!' with connected_bot.conn: connected_bot._enable_builtin_commands() assert connected_bot.handle(m)
def test_builtin_ping(connected_bot): log = ':[email protected] PRIVMSG #defaultchannel :.ping' reply = 'PRIVMSG #defaultchannel :foo: pong' fs = connected_bot.conn.conn fs._set_reply_text(log) m = Message(log) assert not connected_bot.handle(m), 'Commands not yet activated!' with connected_bot.conn: connected_bot._enable_builtin_commands() assert connected_bot.handle(m) assert _wrap(reply) in fs.sent
def test_no_callback(command_bot): def no(message): pass command_bot.command('.*')(no) command_bot._enable_builtin_commands() fs = command_bot.conn.conn inspired = fs.text.splitlines() for idx, i in enumerate(inspired): m = Message(i) assert not command_bot.handle(m)
def test_weird_callback(command_bot): def weird(message): return [None, None] command_bot.command('.*')(weird) command_bot._enable_builtin_commands() fs = command_bot.conn.conn inspired = fs.text.splitlines() for idx, i in enumerate(inspired): m = Message(i) assert not command_bot.handle(m)
def test_builtin_version(connected_bot): log = ':[email protected] PRIVMSG #defaultchannel :.version' reply = 'PRIVMSG #defaultchannel :foo: {} version {}'.format( NICK, __version__) fs = connected_bot.conn.conn fs._set_reply_text(log) m = Message(log) assert not connected_bot.handle(m), 'Commands not yet activated!' with connected_bot.conn: connected_bot._enable_builtin_commands() assert connected_bot.handle(m) assert _wrap(reply) in fs.sent
def test_command(connected_bot): fs = connected_bot.conn.conn def test_function(message): if m.type != 'PRIVMSG': return message else: return None inspired = _get_log('InspIRCd-2.0.log') connected_bot.command('pattern')(test_function) test_command = Command('pattern', test_function) assert test_command in connected_bot.commands i = connected_bot.commands.index(test_command) other = connected_bot.commands[i] assert test_command.callback == other.callback for i in range(RANDOM_TEST_RUNS): m = Message(choice(inspired.splitlines())) assert test_command.match(m) == other.match(m) assert test_command.callback(m) == other.callback(m)
def _messageify(self, text_stream): """turn the raw text coming off the socket into a stream of objects""" for raw_message in text_stream: yield Message(raw_message)
def servermsg(): return Message(SAMPLE_SERVER)
def actionmsg(): return Message(SAMPLE_ACTION)
def privmsg(): return Message(SAMPLE_PRIV)