test_extension.py
1 """Tests for the `extension` module.""" 2 3 from __future__ import annotations 4 5 import logging 6 from textwrap import dedent 7 8 import pytest 9 from griffe import DocstringAdmonition, DocstringSectionAdmonition, load_extensions, temporary_visited_module 10 11 from griffe_warnings_deprecated.extension import WarningsDeprecatedExtension 12 13 14 @pytest.mark.parametrize( 15 "code", 16 [ 17 """ 18 @warnings.deprecated("message", category=DeprecationWarning) 19 def hello(): ... 20 """, 21 """ 22 @warnings.deprecated("message") 23 def hello(): ... 24 """, 25 """ 26 @warnings.deprecated( 27 "message", 28 category=DeprecationWarning, 29 ) 30 def hello(): ... 31 """, 32 """ 33 @warnings.deprecated( 34 "mes" 35 "sage", 36 category=DeprecationWarning, 37 ) 38 def hello(): ... 39 """, 40 """ 41 @warnings.deprecated("message", category=DeprecationWarning) 42 def hello(): 43 '''Summary.''' 44 """, 45 """ 46 47 @warnings.deprecated("message", category=DeprecationWarning) 48 def hello(): 49 '''Summary. 50 51 Description. 52 ''' 53 """, 54 """ 55 @warnings.deprecated("message", category=DeprecationWarning) 56 def hello(): 57 '''Summary. 58 59 Description. 60 61 Note: 62 Hello. 63 ''' 64 """, 65 """ 66 @warnings.deprecated("message", category=DeprecationWarning) 67 class hello: ... 68 """, 69 ], 70 ) 71 def test_extension(code: str) -> None: 72 """Test the extension. 73 74 Parameters: 75 code: Code to test (parametrized). 76 """ 77 code = f"import warnings\n{dedent(code)}" 78 with temporary_visited_module(code, extensions=load_extensions(WarningsDeprecatedExtension)) as module: 79 adm = module["hello"].docstring.parsed[0] 80 assert isinstance(adm, DocstringSectionAdmonition) 81 assert isinstance(adm.value, DocstringAdmonition) 82 assert adm.title == "Deprecated" 83 assert adm.value.kind == "danger" 84 assert adm.value.contents == "message" 85 86 87 def test_extension_fstring(caplog: pytest.LogCaptureFixture) -> None: 88 """Test the extension with an f-string as the deprecation message.""" 89 code = dedent( 90 """ 91 import warnings 92 @warnings.deprecated(f"message") 93 def hello(): ... 94 """, 95 ) 96 with ( 97 caplog.at_level(logging.DEBUG), 98 temporary_visited_module(code, extensions=load_extensions(WarningsDeprecatedExtension)) as module, 99 ): 100 adm = module["hello"].docstring 101 102 # Expect no deprecation message in the docstring. 103 assert adm is None 104 assert "f'message' is not a static string" in caplog.records[0].message