Coverage for tests/test_converter.py: 100.00%
20 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-18 18:20 +0200
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-18 18:20 +0200
1"""Tests for the Markdown converter."""
3from __future__ import annotations
5import re
6from textwrap import dedent
7from typing import TYPE_CHECKING
9import pytest
10from markdown.extensions.toc import TocExtension
12from markdown_exec.rendering import MarkdownConfig, markdown_config
14if TYPE_CHECKING:
15 from markdown import Markdown
18def test_rendering_nested_blocks(md: Markdown) -> None:
19 """Assert nested blocks are properly handled.
21 Parameters:
22 md: A Markdown instance (fixture).
23 """
24 html = md.convert(
25 dedent(
26 """
27 ````md exec="1"
28 ```python exec="1"
29 print("**Bold!**")
30 ```
31 ````
32 """,
33 ),
34 )
35 assert html == "<p><strong>Bold!</strong></p>"
38def test_instantiating_config_singleton() -> None:
39 """Assert that the Markdown config instances act as a singleton."""
40 assert MarkdownConfig() is markdown_config
41 markdown_config.save([], {})
42 markdown_config.reset()
45@pytest.mark.parametrize(
46 ("id", "id_prefix", "expected"),
47 [
48 ("", None, 'id="exec-\\d+--heading"'),
49 ("", "", 'id="heading"'),
50 ("", "some-prefix-", 'id="some-prefix-heading"'),
51 ("some-id", None, 'id="some-id-heading"'),
52 ("some-id", "", 'id="heading"'),
53 ("some-id", "some-prefix-", 'id="some-prefix-heading"'),
54 ],
55)
56def test_prefixing_headings(md: Markdown, id: str, id_prefix: str | None, expected: str) -> None: # noqa: A002
57 """Assert that we prefix headings as specified.
59 Parameters:
60 md: A Markdown instance (fixture).
61 id: The code block id.
62 id_prefix: The code block id prefix.
63 expected: The id we expect to find in the HTML.
64 """
65 TocExtension().extendMarkdown(md)
66 prefix = f'idprefix="{id_prefix}"' if id_prefix is not None else ""
67 html = md.convert(
68 dedent(
69 f"""
70 ```python exec="1" id="{id}" {prefix}
71 print("# HEADING")
72 ```
73 """,
74 ),
75 )
76 assert re.search(expected, html)