Coverage for tests/test_handlers.py: 100.00%
40 statements
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-12 18:59 +0200
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-12 18:59 +0200
1"""Tests for the handlers.base module."""
3from __future__ import annotations
5from typing import TYPE_CHECKING
7import pytest
8from jinja2.exceptions import TemplateNotFound
9from markdown import Markdown
11from mkdocstrings.handlers.base import Highlighter
13if TYPE_CHECKING:
14 from pathlib import Path
16 from mkdocstrings.plugin import MkdocstringsPlugin
19@pytest.mark.parametrize("extension_name", ["codehilite", "pymdownx.highlight"])
20def test_highlighter_without_pygments(extension_name: str) -> None:
21 """Assert that it's possible to disable Pygments highlighting.
23 Arguments:
24 extension_name: The "user-chosen" Markdown extension for syntax highlighting.
25 """
26 configs = {extension_name: {"use_pygments": False, "css_class": "hiiii"}}
27 md = Markdown(extensions=[extension_name], extension_configs=configs)
28 hl = Highlighter(md)
29 assert (
30 hl.highlight("import foo", language="python")
31 == '<pre class="hiiii"><code class="language-python">import foo</code></pre>'
32 )
33 assert (
34 hl.highlight("import foo", language="python", inline=True)
35 == f'<code class="{"" if extension_name == "codehilite" else "hiiii"} language-python">import foo</code>'
36 )
39@pytest.mark.parametrize("extension_name", [None, "codehilite", "pymdownx.highlight"])
40@pytest.mark.parametrize("inline", [False, True])
41def test_highlighter_basic(extension_name: str | None, inline: bool) -> None:
42 """Assert that Pygments syntax highlighting works.
44 Arguments:
45 extension_name: The "user-chosen" Markdown extension for syntax highlighting.
46 inline: Whether the highlighting was inline.
47 """
48 md = Markdown(extensions=[extension_name], extension_configs={extension_name: {}}) if extension_name else Markdown()
49 hl = Highlighter(md)
51 actual = hl.highlight("import foo", language="python", inline=inline)
52 assert "import" in actual
53 assert "import foo" not in actual # Highlighting has split it up.
56def test_extended_templates(tmp_path: Path, plugin: MkdocstringsPlugin) -> None:
57 """Test the extended templates functionality.
59 Parameters:
60 tmp_path: Temporary folder.
61 plugin: Instance of our plugin.
62 """
63 handler = plugin._handlers.get_handler("python") # type: ignore[union-attr]
65 # monkeypatch Jinja env search path
66 search_paths = [
67 base_theme := tmp_path / "base_theme",
68 base_fallback_theme := tmp_path / "base_fallback_theme",
69 extended_theme := tmp_path / "extended_theme",
70 extended_fallback_theme := tmp_path / "extended_fallback_theme",
71 ]
72 handler.env.loader.searchpath = search_paths # type: ignore[union-attr]
74 # assert "new" template is not found
75 with pytest.raises(expected_exception=TemplateNotFound):
76 handler.env.get_template("new.html")
78 # check precedence: base theme, base fallback theme, extended theme, extended fallback theme
79 # start with last one and go back up
80 handler.env.cache = None
82 extended_fallback_theme.mkdir()
83 extended_fallback_theme.joinpath("new.html").write_text("extended fallback new")
84 assert handler.env.get_template("new.html").render() == "extended fallback new"
86 extended_theme.mkdir()
87 extended_theme.joinpath("new.html").write_text("extended new")
88 assert handler.env.get_template("new.html").render() == "extended new"
90 base_fallback_theme.mkdir()
91 base_fallback_theme.joinpath("new.html").write_text("base fallback new")
92 assert handler.env.get_template("new.html").render() == "base fallback new"
94 base_theme.mkdir()
95 base_theme.joinpath("new.html").write_text("base new")
96 assert handler.env.get_template("new.html").render() == "base new"