def test_account_tag(): bot = MagicMock() bot.loop = asyncio.get_event_loop() conn = MockConn(bot) data = { "conn": conn, "irc_tags": TagList.from_dict({"account": "foo"}), "nick": "bar", } user = chan_track.get_users(conn).getuser("bar") assert user.account is None res = call_with_args(chan_track.handle_tags, data) assert res is None assert dict(chan_track.get_users(conn)) == {"bar": user} assert user.account == "foo" data = { "conn": conn, "irc_tags": TagList.from_dict({"account": "*"}), "nick": "bar", } res = call_with_args(chan_track.handle_tags, data) assert res is None assert dict(chan_track.get_users(conn)) == {"bar": user} assert user.account is None
def handle_tags(conn: IrcClient, nick: str, irc_tags: TagList) -> None: users = get_users(conn) if irc_tags: account_tag = irc_tags.get("account") # type: MessageTag if account_tag: user_data = users.getuser(nick) user_data.account = account_tag.value
def test_parse(self, text, tags): tag_list = TagList.parse(text) assert len(tag_list) == len(tags) for name, value in tags: tag = tag_list[name] assert tag.name == name assert tag.value == value
def test_eq_str(self, tags, text): """Test equals strings""" assert TagList(tags) == text assert text == TagList(tags)
def test_ne_list(self, tags): """Test not-equals list""" b = TagList(tags) != tags assert not b b1 = tags != TagList(tags) assert not b1
def test_eq_list(self, tags): """Test equals list""" assert TagList(tags) == tags assert tags == TagList(tags)
def test_ne(self, tags): """Test not-equals""" b = TagList(tags) != TagList(tags) assert not b
def test_eq(self, tags): """Test equals""" assert TagList(tags) == TagList(tags)
def test_ne_str(self, tags, text): assert not (TagList(tags) != tags) assert not (tags != TagList(tags))
def test_eq_str(self, tags, text): assert TagList(tags) == tags assert tags == TagList(tags)
def test_ne_list(self, tags): assert not (TagList(tags) != tags) assert not (tags != TagList(tags))
def test_eq_list(self, tags): assert TagList(tags) == tags assert tags == TagList(tags)
def test_ne(self, tags): assert not (TagList(tags) != TagList(tags))
def test_eq(self, tags): assert TagList(tags) == TagList(tags)
def test_ne_str(self, tags, text): """Test not-equals strings""" b = TagList(tags) != text assert not b b1 = text != TagList(tags) assert not b1
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
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
class TestTagList: """Test parsing a tag list""" @pytest.mark.parametrize( "text,tags", [ ("a;", [("a", "")]), ("a=1;", [("a", "1")]), ("a=1;b;c", [("a", "1"), ("b", ""), ("c", "")]), ("a=1;b;c;", [("a", "1"), ("b", ""), ("c", "")]), ("", []), (";", []), (r"a=ab\r\s\n\:\\;b=abc", [("a", "ab\r \n;\\"), ("b", "abc")]), ], ) def test_parse(self, text, tags): """Test parsing from string""" tag_list = TagList.parse(text) assert len(tag_list) == len(tags) for name, value in tags: tag = tag_list[name] assert tag.name == name assert tag.value == value @pytest.mark.parametrize( "tags", [ [], [MessageTag("a")], [MessageTag("b", "c")], ], ) def test_eq(self, tags): """Test equals""" assert TagList(tags) == TagList(tags) @pytest.mark.parametrize( "tags", [ [], [MessageTag("a")], [MessageTag("b", "c")], ], ) def test_ne(self, tags): """Test not-equals""" b = TagList(tags) != TagList(tags) assert not b @pytest.mark.parametrize( "tags", [ [], [MessageTag("a")], [MessageTag("b", "c")], ], ) def test_eq_list(self, tags): """Test equals list""" assert TagList(tags) == tags assert tags == TagList(tags) @pytest.mark.parametrize( "tags", [ [], [MessageTag("a")], [MessageTag("b", "c")], ], ) def test_ne_list(self, tags): """Test not-equals list""" b = TagList(tags) != tags assert not b b1 = tags != TagList(tags) assert not b1 @pytest.mark.parametrize( "obj,other", [ (TagList(), {}), (TagList([MessageTag("a")]), {"a": None}), (TagList([MessageTag("b", "c")]), {"b": "c"}), ], ) def test_eq_dict(self, obj, other): """Test equals dict""" assert obj == other assert other == obj @pytest.mark.parametrize( "obj,other", [ (TagList(), {}), (TagList([MessageTag("a")]), {"a": None}), (TagList([MessageTag("b", "c")]), {"b": "c"}), ], ) def test_ne_dict(self, obj, other): """Test not-equals dict""" b = obj != other assert not b b1 = other != obj assert not b1 @pytest.mark.parametrize( "tags,text", [ ([], ""), ([MessageTag("a")], "a"), ([MessageTag("b", "c")], "b=c"), ], ) def test_eq_str(self, tags, text): """Test equals strings""" assert TagList(tags) == text assert text == TagList(tags) @pytest.mark.parametrize( "tags,text", [ ([], ""), ([MessageTag("a")], "a"), ([MessageTag("b", "c")], "b=c"), ], ) def test_ne_str(self, tags, text): """Test not-equals strings""" b = TagList(tags) != text assert not b b1 = text != TagList(tags) assert not b1 @pytest.mark.parametrize( "obj,other", [ (TagList(), 0), (TagList(), None), ], ) def test_no_cmp(self, obj, other): """Test not-equals other types""" assert obj != other assert other != obj assert not obj == other assert not other == obj
class TestTagList: @pytest.mark.parametrize('text,tags', [ ("a;", [('a', None)]), ("a=1;", [('a', '1')]), ("a=1;b;c", [('a', '1'), ('b', None), ('c', None)]), ("a=1;b;c;", [('a', '1'), ('b', None), ('c', None)]), ("", []), (";", []), (r"a=ab\r\s\n\:\\;b=abc", [('a', 'ab\r \n;\\'), ('b', 'abc')]), ]) def test_parse(self, text, tags): tag_list = TagList.parse(text) assert len(tag_list) == len(tags) for name, value in tags: tag = tag_list[name] assert tag.name == name assert tag.value == value @pytest.mark.parametrize('tags', [ [], [MessageTag('a')], [MessageTag('b', 'c')], ]) def test_eq(self, tags): assert TagList(tags) == TagList(tags) @pytest.mark.parametrize('tags', [ [], [MessageTag('a')], [MessageTag('b', 'c')], ]) def test_ne(self, tags): assert not (TagList(tags) != TagList(tags)) @pytest.mark.parametrize('tags', [ [], [MessageTag('a')], [MessageTag('b', 'c')], ]) def test_eq_list(self, tags): assert TagList(tags) == tags assert tags == TagList(tags) @pytest.mark.parametrize('tags', [ [], [MessageTag('a')], [MessageTag('b', 'c')], ]) def test_ne_list(self, tags): assert not (TagList(tags) != tags) assert not (tags != TagList(tags)) @pytest.mark.parametrize('obj,other', [ (TagList(), {}), (TagList([MessageTag('a')]), { 'a': None }), (TagList([MessageTag('b', 'c')]), { 'b': 'c' }), ]) def test_eq_dict(self, obj, other): assert obj == other assert other == obj @pytest.mark.parametrize('obj,other', [ (TagList(), {}), (TagList([MessageTag('a')]), { 'a': None }), (TagList([MessageTag('b', 'c')]), { 'b': 'c' }), ]) def test_ne_dict(self, obj, other): assert not (obj != other) assert not (other != obj) @pytest.mark.parametrize('tags,text', [ ([], ''), ([MessageTag('a')], 'a'), ([MessageTag('b', 'c')], 'a;b'), ]) def test_eq_str(self, tags, text): assert TagList(tags) == tags assert tags == TagList(tags) @pytest.mark.parametrize('tags,text', [ ([], ''), ([MessageTag('a')], 'a'), ([MessageTag('b', 'c')], 'a;b'), ]) def test_ne_str(self, tags, text): assert not (TagList(tags) != tags) assert not (tags != TagList(tags)) @pytest.mark.parametrize('obj,other', [ (TagList(), 0), (TagList(), None), ]) def test_no_cmp(self, obj, other): assert obj != other assert other != obj assert not (obj == other) assert not (other == obj)