Skip to content

Commit

Permalink
fix: Fix examples rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 20, 2022
1 parent 78f970d commit a06a7e3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{ log.debug("Rendering examples section") }}
<p><strong>{{ section.title or "Examples:" }}</strong></p>
{% for section_type, sub_section in section.value %}
{% if section_type == "markdown" %}
{% if section_type.value == "text" %}
{{ sub_section|convert_markdown(heading_level, html_id) }}
{% elif section_type == "examples" %}
{% elif section_type.value == "examples" %}
{{ sub_section|highlight(language="python", linenums=False) }}
{% endif %}
{% endfor %}
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,18 @@ def fixture_ext_markdown(plugin):
A Markdown instance.
"""
return plugin.md


@pytest.fixture(name="renderer")
def fixture_renderer(plugin):
"""Return a PythonRenderer instance.
Parameters:
plugin: Pytest fixture: [tests.conftest.fixture_plugin][].
Returns:
A renderer instance.
"""
handler = plugin.handlers.get_handler("python")
handler.renderer._update_env(plugin.md, plugin.handlers._config) # noqa: WPS437
return handler.renderer
2 changes: 1 addition & 1 deletion tests/test_collector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for the handlers.python module."""
"""Tests for the `collector` module."""

import pytest

Expand Down
33 changes: 33 additions & 0 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Tests for the `renderer` module."""

import pytest
from griffe.docstrings.dataclasses import DocstringSection, DocstringSectionKind


@pytest.mark.parametrize(
"renderer",
[
{"theme": "mkdocs"},
{"theme": "readthedocs"},
{"theme": {"name": "material"}},
],
indirect=["renderer"],
)
def test_render_docstring_examples_section(renderer):
"""Assert docstrings' examples section can be rendered.
Parameters:
renderer: A renderer instance (parametrized).
"""
section = DocstringSection(
DocstringSectionKind.examples,
value=[
(DocstringSectionKind.text, "This is an example."),
(DocstringSectionKind.examples, ">>> print('Hello')\nHello"),
],
)
template = renderer.env.get_template("docstring/examples.html")
rendered = template.render(section=section)
assert "<p>This is an example.</p>" in rendered
assert "print" in rendered
assert "Hello" in rendered

0 comments on commit a06a7e3

Please sign in to comment.