File: test_toc.py

package info (click to toggle)
markdown-exec 1.10.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: python: 2,497; javascript: 180; makefile: 32; sh: 30
file content (89 lines) | stat: -rw-r--r-- 2,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Tests for the logic updating the table of contents."""

from __future__ import annotations

from textwrap import dedent
from typing import TYPE_CHECKING

from markdown.extensions.toc import TocExtension

if TYPE_CHECKING:
    from markdown import Markdown


def test_updating_toc(md: Markdown) -> None:
    """Assert ToC is updated with generated headings.

    Parameters:
        md: A Markdown instance (fixture).
    """
    TocExtension().extendMarkdown(md)
    html = md.convert(
        dedent(
            """
            ```python exec="yes"
            print("# big heading")
            ```
            """,
        ),
    )
    assert "<h1" in html
    assert "big-heading" in md.toc  # type: ignore[attr-defined]


def test_not_updating_toc(md: Markdown) -> None:
    """Assert ToC is not updated with generated headings.

    Parameters:
        md: A Markdown instance (fixture).
    """
    TocExtension().extendMarkdown(md)
    html = md.convert(
        dedent(
            """
            ```python exec="yes" updatetoc="no"
            print("# big heading")
            ```
            """,
        ),
    )
    assert "<h1" in html
    assert "big-heading" not in md.toc  # type: ignore[attr-defined]


def test_both_updating_and_not_updating_toc(md: Markdown) -> None:
    """Assert ToC is not updated with generated headings.

    Parameters:
        md: A Markdown instance (fixture).
    """
    TocExtension().extendMarkdown(md)
    html = md.convert(
        dedent(
            """
            ```python exec="yes" updatetoc="no"
            print("# big heading")
            ```

            ```python exec="yes" updatetoc="yes"
            print("## medium heading")
            ```

            ```python exec="yes" updatetoc="no"
            print("### small heading")
            ```

            ```python exec="yes" updatetoc="yes"
            print("#### tiny heading")
            ```
            """,
        ),
    )
    assert "<h1" in html
    assert "<h2" in html
    assert "<h3" in html
    assert "<h4" in html
    assert "big-heading" not in md.toc  # type: ignore[attr-defined]
    assert "medium-heading" in md.toc  # type: ignore[attr-defined]
    assert "small-heading" not in md.toc  # type: ignore[attr-defined]
    assert "tiny-heading" in md.toc  # type: ignore[attr-defined]