Skip to content

Commit e52875a

Browse files
Assert number of messages in test_messages_documentation.py (#6145)
Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
1 parent aaeeb33 commit e52875a

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

doc/test_messages_documentation.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44

55
"""Functional tests for the code examples in the messages documentation."""
66

7-
from collections import Counter
7+
import sys
8+
9+
if sys.version_info[:2] >= (3, 9):
10+
from collections import Counter
11+
else:
12+
from collections import Counter as _Counter
13+
14+
class Counter(_Counter):
15+
def total(self):
16+
return len(tuple(self.elements()))
17+
18+
819
from pathlib import Path
920
from typing import Counter as CounterType
1021
from typing import List, Optional, TextIO, Tuple
@@ -75,6 +86,12 @@ def __init__(self, test_file: Tuple[str, Path]) -> None:
7586
def runTest(self) -> None:
7687
self._runTest()
7788

89+
def is_good_test_file(self) -> bool:
90+
return self._test_file[1].name == "good.py"
91+
92+
def is_bad_test_file(self) -> bool:
93+
return self._test_file[1].name == "bad.py"
94+
7895
@staticmethod
7996
def get_expected_messages(stream: TextIO) -> MessageCounter:
8097
"""Parse a file and get expected messages."""
@@ -114,6 +131,10 @@ def _runTest(self) -> None:
114131
self._linter.check([str(self._test_file[1])])
115132
expected_messages = self._get_expected()
116133
actual_messages = self._get_actual()
134+
if self.is_good_test_file():
135+
assert actual_messages.total() == 0 # type: ignore[attr-defined]
136+
if self.is_bad_test_file():
137+
assert actual_messages.total() > 0 # type: ignore[attr-defined]
117138
assert expected_messages == actual_messages
118139

119140

0 commit comments

Comments
 (0)