def test_param_construct(): msg = Message(None, None, "PRIVMSG", "#channel", "Message thing") assert str(msg) == "PRIVMSG #channel :Message thing" msg = Message(None, None, "PRIVMSG", ["#channel", "Message thing"]) assert str(msg) == "PRIVMSG #channel :Message thing" msg = Message(None, None, "PRIVMSG", ["#channel", ":Message thing"]) assert str(msg) == "PRIVMSG #channel ::Message thing" msg = Message(None, None, "PRIVMSG", [""]) assert str(msg) == "PRIVMSG :"
def test_check_send_key(mock_db): conn = make_conn() db = mock_db.session() chan_key_db.table.create(mock_db.engine) msg = Message(None, None, "JOIN", ["#foo,#bar", "bing"]) assert chan_key_db.check_send_key(conn, msg, db) is msg assert conn.get_channel_key("#foo") == "bing" msg = Message(None, None, "PRIVMSG", ["#foo,#bar", "bing"]) assert chan_key_db.check_send_key(conn, msg, db) is msg msg = Message(None, None, "JOIN", ["#foo,#bar"]) assert chan_key_db.check_send_key(conn, msg, db) is msg assert conn.get_channel_key("#foo") == "bing"
def cmd(self, command, *params): """ Sends a raw IRC command of type <command> with params <params> :param command: The IRC command to send :param params: The params to the IRC command :type command: str :type params: (str) """ params = list(map(str, params)) # turn the tuple of parameters into a list self.send(str(Message(None, None, command, params)))
def test_msg_join(data): """ Ensure that message building passes all tests from the irc-parser-tests library. """ atoms = data["atoms"] msg = Message( atoms.pop("tags", None), atoms.pop("source", None), atoms.pop("verb", None), atoms.pop("params", []), ) assert not atoms, "Not all atoms were handled" matches = data["matches"] assert str(msg) in matches
def test_msg_join(data): atoms = data['atoms'] msg = Message( atoms.pop('tags', None), atoms.pop('source', None), atoms.pop('verb', None), atoms.pop('params', []), ) assert not atoms, "Not all atoms were handled" matches = data['matches'] if len(matches) > 1: assert any(str(msg) == match for match in data['matches']) else: # With single matches, make it easier to debug assert str(msg) == matches[0]
def test_ne(self, tags, prefix, command, params): """Test not-equals""" b = Message(tags, prefix, command, params) != Message( tags, prefix, command, params ) assert not b
def test_eq(self, tags, prefix, command, params): """Test equals""" assert Message(tags, prefix, command, params) == Message( tags, prefix, command, params )
class TestMessage: """Test parsing an entire IRC message""" def test_parse_bytes(self): """Test parsing bytes""" line = Message.parse(b"COMMAND some params :and stuff") assert line.command == "COMMAND" assert line.parameters == ["some", "params", "and stuff"] @pytest.mark.parametrize( "obj,text", [ (Message(None, None, None), ""), (Message(None, None, None, None), ""), (Message(None, None, None, []), ""), (Message(None, None, "COMMAND"), "COMMAND"), (Message(["a=b"], None, "COMMAND"), "@a=b COMMAND"), (Message([MessageTag("a", "b")], None, "COMMAND"), "@a=b COMMAND"), (Message({"a": "b"}, None, "COMMAND"), "@a=b COMMAND"), (Message({"a": "b"}, "nick", "COMMAND"), "@a=b :nick COMMAND"), (Message(None, ("nick",), "COMMAND"), ":nick COMMAND"), (Message(None, ("nick", "user"), "COMMAND"), ":nick!user COMMAND"), ( Message(None, ("nick", "user", "host"), "COMMAND"), ":nick!user@host COMMAND", ), ( Message({"a": "b"}, "nick", "COMMAND", "a", "b"), "@a=b :nick COMMAND a b", ), ], ) def test_str(self, obj, text): """Test string conversion""" assert str(obj) == text @pytest.mark.parametrize( "tags,prefix,command,params", [ (None, None, None, None), ("some tag", None, "COMMAND", ["param", ""]), ], ) def test_eq(self, tags, prefix, command, params): """Test equals""" assert Message(tags, prefix, command, params) == Message( tags, prefix, command, params ) @pytest.mark.parametrize( "tags,prefix,command,params", [ (None, None, None, None), ("some tag", None, "COMMAND", ["param", ""]), ], ) def test_ne(self, tags, prefix, command, params): """Test not-equals""" b = Message(tags, prefix, command, params) != Message( tags, prefix, command, params ) assert not b @pytest.mark.parametrize( "obj,other", [ (Message(None, None, None), 0), (Message(None, None, None), None), (Message(None, None, None), ()), ], ) def test_no_cmp(self, obj, other): """Test Message.__ne__""" assert obj != other assert other != obj assert not obj == other assert not other == obj @pytest.mark.parametrize( "obj", [ Message(None, None, "COMMAND"), ], ) def test_bool(self, obj): """Test the cases where bool(Message) should return True""" assert obj @pytest.mark.parametrize( "obj", [ Message(None, None, None), Message(None, None, ""), Message(None, "", ""), Message("", "", ""), Message([], [], "", []), Message({}, [], "", []), Message(TagList(), Prefix(), "", ParamList()), ], ) def test_bool_false(self, obj): """Test all the cases where bool(Message) should return False""" assert not obj
def test_ne(self, tags, prefix, command, params): assert not (Message(tags, prefix, command, params) != Message( tags, prefix, command, params))
def test_eq(self, tags, prefix, command, params): assert Message(tags, prefix, command, params) == Message(tags, prefix, command, params)
class TestMessage: def test_parse_bytes(self): line = Message.parse(b"COMMAND some params :and stuff") assert line.command == 'COMMAND' assert line.parameters == ['some', 'params', 'and stuff'] @pytest.mark.parametrize('obj,text', [ (Message(None, None, None), ''), (Message(None, None, None, None), ''), (Message(None, None, None, []), ''), (Message(None, None, 'COMMAND'), 'COMMAND'), (Message(['a=b'], None, 'COMMAND'), '@a=b COMMAND'), (Message([MessageTag('a', 'b')], None, 'COMMAND'), '@a=b COMMAND'), (Message({'a': 'b'}, None, 'COMMAND'), '@a=b COMMAND'), (Message({'a': 'b'}, 'nick', 'COMMAND'), '@a=b :nick COMMAND'), (Message(None, ('nick', ), 'COMMAND'), ':nick COMMAND'), (Message(None, ('nick', 'user'), 'COMMAND'), ':nick!user COMMAND'), (Message(None, ('nick', 'user', 'host'), 'COMMAND'), ':nick!user@host COMMAND'), (Message({'a': 'b'}, 'nick', 'COMMAND', 'a', 'b'), '@a=b :nick COMMAND a b'), ]) def test_str(self, obj, text): assert str(obj) == text @pytest.mark.parametrize('tags,prefix,command,params', [ (None, None, None, None), ('some tag', None, 'COMMAND', ['param', '']), ]) def test_eq(self, tags, prefix, command, params): assert Message(tags, prefix, command, params) == Message(tags, prefix, command, params) @pytest.mark.parametrize('tags,prefix,command,params', [ (None, None, None, None), ('some tag', None, 'COMMAND', ['param', '']), ]) def test_ne(self, tags, prefix, command, params): assert not (Message(tags, prefix, command, params) != Message( tags, prefix, command, params)) @pytest.mark.parametrize('obj,other', [ (Message(None, None, None), 0), (Message(None, None, None), None), (Message(None, None, None), ()), ]) def test_no_cmp(self, obj, other): assert obj != other assert other != obj assert not (obj == other) assert not (other == obj) @pytest.mark.parametrize('obj', [ Message(None, None, 'COMMAND'), ]) def test_bool(self, obj): assert obj @pytest.mark.parametrize('obj', [ Message(None, None, None), Message(None, None, ''), Message(None, '', ''), Message('', '', ''), Message([], [], '', []), Message({}, [], '', []), Message(TagList(), Prefix(), '', ParamList()), ]) def test_bool_false(self, obj): assert not obj