-
-
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
87 lines (67 loc) · 3.63 KB
/
test_checker_test_case.py
File metadata and controls
87 lines (67 loc) · 3.63 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
# 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)
class TestCheckerTestCase(CheckerTestCase):
CHECKER_CLASS = _DummyChecker
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, match="..."):
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)
def test_assert_does_not_add_messages_failure(self) -> None:
"""Scenario 4: expected not raised / actual raised."""
with pytest.raises(AssertionError, match="..."):
with self.assertDoesNotAddMessages("W9901"):
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("W9901"):
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("W9901"):
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_multiple_unwanted(self) -> None:
"""Fails when any of the several unwanted message IDs is found."""
with pytest.raises(AssertionError, match="..."):
with self.assertDoesNotAddMessages("W9901", "W9902"):
self.linter.add_message("W9902", line=2)
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("W9901"):
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