|
| 1 | +"""Tests for the `handler` module.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from griffe.docstrings.dataclasses import DocstringSectionExamples, DocstringSectionKind |
| 5 | + |
| 6 | +from mkdocstrings_handlers.python.handler import CollectionError, get_handler |
| 7 | + |
| 8 | + |
| 9 | +def test_collect_missing_module(): |
| 10 | + """Assert error is raised for missing modules.""" |
| 11 | + handler = get_handler(theme="material") |
| 12 | + with pytest.raises(CollectionError): |
| 13 | + handler.collect("aaaaaaaa", {}) |
| 14 | + |
| 15 | + |
| 16 | +def test_collect_missing_module_item(): |
| 17 | + """Assert error is raised for missing items within existing modules.""" |
| 18 | + handler = get_handler(theme="material") |
| 19 | + with pytest.raises(CollectionError): |
| 20 | + handler.collect("mkdocstrings.aaaaaaaa", {}) |
| 21 | + |
| 22 | + |
| 23 | +def test_collect_module(): |
| 24 | + """Assert existing module can be collected.""" |
| 25 | + handler = get_handler(theme="material") |
| 26 | + assert handler.collect("mkdocstrings", {}) |
| 27 | + |
| 28 | + |
| 29 | +def test_collect_with_null_parser(): |
| 30 | + """Assert we can pass `None` as parser when collecting.""" |
| 31 | + handler = get_handler(theme="material") |
| 32 | + assert handler.collect("mkdocstrings", {"docstring_style": None}) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize( |
| 36 | + "handler", |
| 37 | + [ |
| 38 | + {"theme": "mkdocs"}, |
| 39 | + {"theme": "readthedocs"}, |
| 40 | + {"theme": {"name": "material"}}, |
| 41 | + ], |
| 42 | + indirect=["handler"], |
| 43 | +) |
| 44 | +def test_render_docstring_examples_section(handler): |
| 45 | + """Assert docstrings' examples section can be rendered. |
| 46 | +
|
| 47 | + Parameters: |
| 48 | + handler: A handler instance (parametrized). |
| 49 | + """ |
| 50 | + section = DocstringSectionExamples( |
| 51 | + value=[ |
| 52 | + (DocstringSectionKind.text, "This is an example."), |
| 53 | + (DocstringSectionKind.examples, ">>> print('Hello')\nHello"), |
| 54 | + ], |
| 55 | + ) |
| 56 | + template = handler.env.get_template("docstring/examples.html") |
| 57 | + rendered = template.render(section=section) |
| 58 | + assert "<p>This is an example.</p>" in rendered |
| 59 | + assert "print" in rendered |
| 60 | + assert "Hello" in rendered |
0 commit comments