-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_checker_test_case.py
More file actions
150 lines (117 loc) · 6.76 KB
/
test_checker_test_case.py
File metadata and controls
150 lines (117 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
"""Tests for CheckerTestCase assertion methods."""
from __future__ import annotations
import pytest
from pylint.checkers.base_checker import BaseChecker
from pylint.interfaces import UNDEFINED
from pylint.testutils import CheckerTestCase, MessageTest
class _DummyChecker(BaseChecker):
"""A minimal checker used only for testing the test infrastructure."""
name = "dummy-for-testcase"
msgs = {
"W9901": ("Dummy message A", "dummy-msg-a", "Dummy message A."),
"W9902": ("Dummy message B", "dummy-msg-b", "Dummy message B."),
}
_MSG_A = MessageTest("W9901", line=1, node=None, args=None, confidence=UNDEFINED)
_MSG_B = MessageTest("W9902", line=2, node=None, args=None, confidence=UNDEFINED)
class TestCheckerTestCase(CheckerTestCase):
CHECKER_CLASS = _DummyChecker
# -- assertAddsMessages scenarios (1-3) -----------------------------------
def test_assert_adds_messages_success(self) -> None:
"""Scenario 1: expected raised / actual raised."""
with self.assertAddsMessages(_MSG_A):
self.linter.add_message("W9901", line=1)
def test_assert_adds_messages_failure_not_raised(self) -> None:
"""Scenario 2: expected raised / actual not raised."""
with pytest.raises(AssertionError):
with self.assertAddsMessages(_MSG_A):
pass # nothing emitted
def test_assert_adds_messages_failure_wrong_message(self) -> None:
"""Scenario 3: expected raised / actual not raised but another one raised."""
with pytest.raises(AssertionError):
with self.assertAddsMessages(_MSG_A):
self.linter.add_message("W9902", line=2)
# -- assertDoesNotAddMessages scenarios (4-6) -----------------------------
def test_assert_does_not_add_messages_failure(self) -> None:
"""Scenario 4: expected not raised / actual raised."""
with pytest.raises(AssertionError):
with self.assertDoesNotAddMessages(_MSG_A):
self.linter.add_message("W9901", line=1)
def test_assert_does_not_add_messages_success(self) -> None:
"""Scenario 5: expected not raised / actual not raised."""
with self.assertDoesNotAddMessages(_MSG_A):
pass # nothing emitted
def test_assert_does_not_add_messages_success_other_raised(self) -> None:
"""Scenario 6: expected not raised / actual not raised but another one raised."""
with self.assertDoesNotAddMessages(_MSG_A):
self.linter.add_message("W9902", line=2)
# -- additional edge cases ------------------------------------------------
def test_assert_does_not_add_messages_ignore_position(self) -> None:
"""Position mismatch means no match when ignore_position=False."""
# Same msg_id but different line: should pass (not a match)
msg_different_line = MessageTest(
"W9901", line=99, node=None, args=None, confidence=UNDEFINED
)
with self.assertDoesNotAddMessages(msg_different_line):
self.linter.add_message("W9901", line=1)
def test_assert_does_not_add_messages_ignore_position_true(self) -> None:
"""With ignore_position=True, position differences are ignored."""
msg_different_line = MessageTest(
"W9901", line=99, node=None, args=None, confidence=UNDEFINED
)
with pytest.raises(AssertionError):
with self.assertDoesNotAddMessages(
msg_different_line, ignore_position=True
):
self.linter.add_message("W9901", line=1)
def test_assert_does_not_add_messages_multiple_unwanted(self) -> None:
"""Fails when any of several unwanted messages is found."""
with pytest.raises(AssertionError):
with self.assertDoesNotAddMessages(_MSG_A, _MSG_B):
self.linter.add_message("W9902", line=2)
def test_assert_does_not_add_messages_no_args_raises(self) -> None:
"""Calling with no arguments must raise TypeError."""
with pytest.raises(TypeError, match="requires at least one"):
with self.assertDoesNotAddMessages():
pass
def test_assert_does_not_add_messages_exception_in_body_drains_messages(
self,
) -> None:
"""An exception in the with-block must not leak messages to later tests."""
with pytest.raises(RuntimeError):
with self.assertDoesNotAddMessages(_MSG_A):
self.linter.add_message("W9901", line=1)
raise RuntimeError("something went wrong")
# Messages must have been drained; a subsequent assertNoMessages should pass.
with self.assertNoMessages():
pass
# -- _messages_match branch coverage --------------------------------------
def test_messages_match_node_mismatch(self) -> None:
expected = MessageTest("W9901", line=1, node="sentinel", args=None)
actual = MessageTest("W9901", line=1, node=None, args=None)
assert not self._messages_match(expected, actual, ignore_position=False)
def test_messages_match_args_mismatch(self) -> None:
expected = MessageTest("W9901", line=1, node=None, args=("x",))
actual = MessageTest("W9901", line=1, node=None, args=None)
assert not self._messages_match(expected, actual, ignore_position=False)
def test_messages_match_confidence_mismatch(self) -> None:
from pylint.interfaces import HIGH
expected = MessageTest("W9901", line=1, node=None, args=None, confidence=HIGH)
actual = MessageTest(
"W9901", line=1, node=None, args=None, confidence=UNDEFINED
)
assert not self._messages_match(expected, actual, ignore_position=False)
def test_messages_match_col_offset_mismatch(self) -> None:
expected = MessageTest("W9901", line=1, node=None, args=None, col_offset=5)
actual = MessageTest("W9901", line=1, node=None, args=None, col_offset=10)
assert not self._messages_match(expected, actual, ignore_position=False)
def test_messages_match_end_line_mismatch(self) -> None:
expected = MessageTest("W9901", line=1, node=None, args=None, end_line=5)
actual = MessageTest("W9901", line=1, node=None, args=None, end_line=10)
assert not self._messages_match(expected, actual, ignore_position=False)
def test_messages_match_end_col_offset_mismatch(self) -> None:
expected = MessageTest("W9901", line=1, node=None, args=None, end_col_offset=5)
actual = MessageTest("W9901", line=1, node=None, args=None, end_col_offset=10)
assert not self._messages_match(expected, actual, ignore_position=False)