|
| 1 | +from contextlib import contextmanager |
1 | 2 | from docstring_to_markdown import convert, UnknownFormatError
|
| 3 | +from docstring_to_markdown.types import Converter |
| 4 | +from docstring_to_markdown.cpython import CPythonConverter |
| 5 | +from importlib_metadata import EntryPoint, entry_points, distribution |
| 6 | +from unittest.mock import patch |
| 7 | +import docstring_to_markdown |
2 | 8 | import pytest
|
3 | 9 |
|
4 | 10 | CPYTHON = """\
|
@@ -55,3 +61,72 @@ def test_convert_rst():
|
55 | 61 | def test_unknown_format():
|
56 | 62 | with pytest.raises(UnknownFormatError):
|
57 | 63 | convert('ARGS [arg1, arg2] RETURNS: str OR None')
|
| 64 | + |
| 65 | + |
| 66 | +class HighPriorityConverter(Converter): |
| 67 | + priority = 120 |
| 68 | + |
| 69 | + def convert(self, docstring): |
| 70 | + return "HighPriority" |
| 71 | + |
| 72 | + def can_convert(self, docstring): |
| 73 | + return True |
| 74 | + |
| 75 | + |
| 76 | +class MockEntryPoint(EntryPoint): |
| 77 | + def load(self): |
| 78 | + return self.value |
| 79 | + |
| 80 | + dist = None |
| 81 | + |
| 82 | + |
| 83 | +class DistMockEntryPoint(MockEntryPoint): |
| 84 | + # Pretend it is contributed by `pytest`. |
| 85 | + # It could be anything else, but `pytest` |
| 86 | + # is guaranteed to be installed during tests. |
| 87 | + dist = distribution('pytest') |
| 88 | + |
| 89 | + |
| 90 | +class CustomCPythonConverter(CPythonConverter): |
| 91 | + priority = 10 |
| 92 | + |
| 93 | + def convert(self, docstring): |
| 94 | + return 'CustomCPython' |
| 95 | + |
| 96 | + def can_convert(self, docstring): |
| 97 | + return True |
| 98 | + |
| 99 | + |
| 100 | +@contextmanager |
| 101 | +def custom_entry_points(entry_points): |
| 102 | + old = docstring_to_markdown._CONVERTERS |
| 103 | + docstring_to_markdown._CONVERTERS = None |
| 104 | + with patch.object(docstring_to_markdown, 'entry_points', return_value=entry_points): |
| 105 | + yield |
| 106 | + docstring_to_markdown._CONVERTERS = old |
| 107 | + |
| 108 | + |
| 109 | +def test_adding_entry_point(): |
| 110 | + original_entry_points = entry_points(group="docstring_to_markdown") |
| 111 | + mock_entry_point = MockEntryPoint( |
| 112 | + name='high-priority-converter', |
| 113 | + group='docstring_to_markdown', |
| 114 | + value=HighPriorityConverter, |
| 115 | + ) |
| 116 | + with custom_entry_points([*original_entry_points, mock_entry_point]): |
| 117 | + assert convert('test') == 'HighPriority' |
| 118 | + |
| 119 | + |
| 120 | +def test_replacing_entry_point(): |
| 121 | + assert convert(CPYTHON) == CPYTHON_MD |
| 122 | + original_entry_points = entry_points(group="docstring_to_markdown") |
| 123 | + mock_entry_point = DistMockEntryPoint( |
| 124 | + name='cpython', |
| 125 | + group='docstring_to_markdown', |
| 126 | + value=CustomCPythonConverter |
| 127 | + ) |
| 128 | + with custom_entry_points([*original_entry_points, mock_entry_point]): |
| 129 | + assert convert('test') == 'test' |
| 130 | + assert convert(GOOGLE) == GOOGLE_MD |
| 131 | + assert convert(RST) == RST_MD |
| 132 | + assert convert(CPYTHON) == 'CustomCPython' |
0 commit comments