Skip to content

Commit 1d5a150

Browse files
committed
Add tests for warning types
1 parent 89bc65d commit 1d5a150

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

tests/test_integration.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@
3131
W = NewType("W", str)
3232

3333

34+
@dataclass
35+
class WarningInfo:
36+
"""Properties and assertion methods for warnings."""
37+
38+
regexp: str
39+
type: str
40+
41+
def assert_regexp(self, message: str) -> None:
42+
regexp = self.regexp
43+
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {message!r}"
44+
assert re.search(regexp, message), msg
45+
46+
def assert_type(self, message: str) -> None:
47+
expected = f"[{self.type}]"
48+
msg = f"Warning did not contain type and subtype.\n Expected: {expected}\n Input: {message}"
49+
assert expected in message, msg
50+
51+
def assert_warning(self, message: str) -> None:
52+
self.assert_regexp(message)
53+
self.assert_type(message)
54+
55+
3456
def expected(expected: str, **options: dict[str, Any]) -> Callable[[T], T]:
3557
def dec(val: T) -> T:
3658
val.EXPECTED = expected
@@ -40,9 +62,9 @@ def dec(val: T) -> T:
4062
return dec
4163

4264

43-
def warns(pattern: str) -> Callable[[T], T]:
65+
def warns(info: WarningInfo) -> Callable[[T], T]:
4466
def dec(val: T) -> T:
45-
val.WARNING = pattern
67+
val.WARNING = info
4668
return val
4769

4870
return dec
@@ -58,7 +80,7 @@ def wrapper(self) -> str: # noqa: ANN001
5880
return wrapper
5981

6082

61-
@warns("Cannot handle as a local function")
83+
@warns(WarningInfo(regexp="Cannot handle as a local function", type="sphinx_autodoc_typehints.local_function"))
6284
@expected(
6385
"""\
6486
class mod.Class(x, y, z=None)
@@ -330,7 +352,11 @@ def function_with_escaped_default(x: str = "\b"): # noqa: ANN201
330352
"""
331353

332354

333-
@warns("Cannot resolve forward reference in type annotations")
355+
@warns(
356+
WarningInfo(
357+
regexp="Cannot resolve forward reference in type annotations", type="sphinx_autodoc_typehints.forward_reference"
358+
)
359+
)
334360
@expected(
335361
"""\
336362
mod.function_with_unresolvable_annotation(x)
@@ -1196,7 +1222,7 @@ def docstring_with_enum_list_after_params(param: int) -> None:
11961222
"""
11971223

11981224

1199-
@warns("Definition list ends without a blank line")
1225+
@warns(WarningInfo(regexp="Definition list ends without a blank line", type="docutils"))
12001226
@expected(
12011227
"""
12021228
mod.docstring_with_definition_list_after_params_no_blank_line(param)
@@ -1457,7 +1483,7 @@ def has_doctest1() -> None:
14571483
Unformatted = TypeVar("Unformatted")
14581484

14591485

1460-
@warns("cannot cache unpickleable configuration value: 'typehints_formatter'")
1486+
@warns(WarningInfo(regexp="cannot cache unpickleable configuration value: 'typehints_formatter'", type="config.cache"))
14611487
@expected(
14621488
"""
14631489
mod.typehints_formatter_applied_to_signature(param: Formatted) -> Formatted
@@ -1525,11 +1551,10 @@ def test_integration(
15251551
app.build()
15261552
assert "build succeeded" in status.getvalue() # Build succeeded
15271553

1528-
regexp = getattr(val, "WARNING", None)
1554+
warning_info: Union[WarningInfo, None] = getattr(val, "WARNING", None)
15291555
value = warning.getvalue().strip()
1530-
if regexp:
1531-
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
1532-
assert re.search(regexp, value), msg
1556+
if warning_info:
1557+
warning_info.assert_warning(value)
15331558
else:
15341559
assert not value
15351560

0 commit comments

Comments
 (0)