diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2019-08-10 13:06:31 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-08-19 13:54:52 +0200 |
commit | 4bd00376f84b673e397a1abe629d330a0444af35 (patch) | |
tree | 6084cbd9029ae0dc5ec751ec348b157ffb8a623c /tests/message | |
parent | 122049b58c258346ef59e474f48940f5d12a158a (diff) | |
download | pylint-git-4bd00376f84b673e397a1abe629d330a0444af35.tar.gz |
[pylint.message] Create a MessageIdStore class
Diffstat (limited to 'tests/message')
-rw-r--r-- | tests/message/__init__.py | 0 | ||||
-rw-r--r-- | tests/message/generic_fixtures.py | 77 | ||||
-rw-r--r-- | tests/message/unittest_message_id_store.py | 72 |
3 files changed, 149 insertions, 0 deletions
diff --git a/tests/message/__init__.py b/tests/message/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/message/__init__.py diff --git a/tests/message/generic_fixtures.py b/tests/message/generic_fixtures.py new file mode 100644 index 000000000..bb12b02ab --- /dev/null +++ b/tests/message/generic_fixtures.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- + +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/master/COPYING + +import pytest + +from pylint.checkers import BaseChecker +from pylint.message import MessageDefinitionStore, MessageIdStore + + +@pytest.fixture +def msgid(): + return "W1234" + + +@pytest.fixture +def symbol(): + return "msg-symbol" + + +@pytest.fixture +def empty_store(): + return MessageDefinitionStore() + + +@pytest.fixture +def store(): + store = MessageDefinitionStore() + + class Checker(BaseChecker): + name = "achecker" + msgs = { + "W1234": ( + "message", + "msg-symbol", + "msg description.", + {"old_names": [("W0001", "old-symbol")]}, + ), + "E1234": ( + "Duplicate keyword argument %r in %s call", + "duplicate-keyword-arg", + "Used when a function call passes the same keyword argument multiple times.", + {"maxversion": (2, 6)}, + ), + } + + store.register_messages_from_checker(Checker()) + return store + + +@pytest.fixture +def message_definitions(store): + return store.messages + + +@pytest.fixture +def msgids(): + return { + "W1234": "warning-symbol", + "W1235": "warning-symbol-two", + "C1234": "convention-symbol", + "E1234": "error-symbol", + } + + +@pytest.fixture +def empty_msgid_store(): + return MessageIdStore() + + +@pytest.fixture +def msgid_store(msgids): + msgid_store = MessageIdStore() + for msgid, symbol in msgids.items(): + msgid_store.add_msgid_and_symbol(msgid, symbol) + return msgid_store diff --git a/tests/message/unittest_message_id_store.py b/tests/message/unittest_message_id_store.py new file mode 100644 index 000000000..00fbd7028 --- /dev/null +++ b/tests/message/unittest_message_id_store.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/master/COPYING + +import pytest + +from pylint.exceptions import InvalidMessageError, UnknownMessageError +from pylint.message import MessageIdStore + +from .generic_fixtures import ( + empty_msgid_store, + message_definitions, + msgid_store, + msgids, + store, +) + + +def test_len_str(msgid_store, msgids): + assert len(msgid_store) == len(msgids) + str_result = str(msgid_store) + assert "MessageIdStore: [" in str_result + assert " - W1234 (warning-symbol)" in str_result + assert " - W1235 (warning-symbol-two)" in str_result + assert " - C1234 (convention-symbol)" in str_result + assert " - E1234 (error-symbol)" in str_result + assert "]" in str_result + + +def test_get_message_ids(msgid_store, msgids): + """We can get message id even with capitalization problem.""" + msgid = list(msgids.keys())[0] + msgids_result = msgid_store.get_active_msgids(msgid.lower()) + assert len(msgids_result) == 1 + assert msgid == msgids_result[0] + + +def test_get_message_ids_not_existing(empty_msgid_store): + with pytest.raises(UnknownMessageError) as error: + w9876 = "W9876" + empty_msgid_store.get_active_msgids(w9876) + assert w9876 in str(error.value) + + +def test_register_message_definitions(empty_msgid_store, message_definitions): + number_of_msgid = len(message_definitions) + for i, message_definition in enumerate(message_definitions): + empty_msgid_store.register_message_definition(message_definition) + if message_definition.old_names: + number_of_msgid += len(message_definition.old_names) + assert len(empty_msgid_store) == number_of_msgid + + +def test_duplicate_symbol(empty_msgid_store): + empty_msgid_store.add_msgid_and_symbol("W1234", "warning-symbol") + with pytest.raises(InvalidMessageError) as error: + empty_msgid_store.check_msgid_and_symbol("W1234", "other-symbol") + assert ( + "Message id 'W1234' cannot have both 'other-symbol' and 'warning-symbol' as symbolic name." + in str(error.value) + ) + + +def test_duplicate_msgid(msgid_store): + msgid_store.add_msgid_and_symbol("W1234", "warning-symbol") + with pytest.raises(InvalidMessageError) as error: + msgid_store.check_msgid_and_symbol("W1235", "warning-symbol") + assert ( + "Message symbol 'warning-symbol' cannot be used for 'W1234' and 'W1235'" + in str(error.value) + ) |