-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
test_extension.py
162 lines (131 loc) · 5.49 KB
/
test_extension.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Tests for the extension module."""
import sys
from collections import ChainMap
from textwrap import dedent
import pytest
from markdown import Markdown
from mkdocs import config
try:
from mkdocs.config.defaults import get_schema
except ImportError:
def get_schema():
"""Fallback for old versions of MkDocs."""
return config.DEFAULT_SCHEMA
@pytest.fixture(name="ext_markdown")
def fixture_ext_markdown(request, tmp_path):
"""Yield a Markdown instance with MkdocstringsExtension, with config adjustments."""
conf = config.Config(schema=get_schema())
conf_dict = {
"site_name": "foo",
"site_dir": str(tmp_path),
"plugins": [{"mkdocstrings": {"default_handler": "python"}}],
**getattr(request, "param", {}),
}
# Re-create it manually as a workaround for https://github.com/mkdocs/mkdocs/issues/2289
mdx_configs = dict(ChainMap(*conf_dict.get("markdown_extensions", [])))
conf.load_dict(conf_dict)
assert conf.validate() == ([], [])
conf["mdx_configs"] = mdx_configs
conf["markdown_extensions"].insert(0, "toc") # Guaranteed to be added by MkDocs.
conf = conf["plugins"]["mkdocstrings"].on_config(conf)
conf = conf["plugins"]["autorefs"].on_config(conf)
md = Markdown(extensions=conf["markdown_extensions"], extension_configs=conf["mdx_configs"])
yield md
conf["plugins"]["mkdocstrings"].on_post_build(conf)
def test_render_html_escaped_sequences(ext_markdown):
"""Assert HTML-escaped sequences are correctly parsed as XML."""
ext_markdown.convert("::: tests.fixtures.html_escaped_sequences")
@pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"footnotes": {}}]}], indirect=["ext_markdown"])
def test_multiple_footnotes(ext_markdown):
"""Assert footnotes don't get added to subsequent docstrings."""
output = ext_markdown.convert(
dedent(
"""
Top.[^aaa]
::: tests.fixtures.footnotes.func_a
::: tests.fixtures.footnotes.func_b
::: tests.fixtures.footnotes.func_c
[^aaa]: Top footnote
""",
),
)
assert output.count("Footnote A") == 1
assert output.count("Footnote B") == 1
assert output.count("Top footnote") == 1
def test_markdown_heading_level(ext_markdown):
"""Assert that Markdown headings' level doesn't exceed heading_level."""
output = ext_markdown.convert("::: tests.fixtures.headings\n rendering:\n show_root_heading: true")
assert ">Foo</h3>" in output
assert ">Bar</h5>" in output
assert ">Baz</h6>" in output
def test_keeps_preceding_text(ext_markdown):
"""Assert that autodoc is recognized in the middle of a block and preceding text is kept."""
output = ext_markdown.convert("**preceding**\n::: tests.fixtures.headings")
assert "<strong>preceding</strong>" in output
assert ">Foo</h2>" in output
assert ":::" not in output
def test_reference_inside_autodoc(ext_markdown):
"""Assert cross-reference Markdown extension works correctly."""
output = ext_markdown.convert("::: tests.fixtures.cross_reference")
snippet = 'Link to <span data-mkdocstrings-identifier="something.Else">something.Else</span>.'
assert snippet in output
@pytest.mark.skipif(sys.version_info < (3, 8), reason="typing.Literal requires Python 3.8")
def test_quote_inside_annotation(ext_markdown):
"""Assert that inline highlighting doesn't double-escape HTML."""
output = ext_markdown.convert("::: tests.fixtures.string_annotation.Foo")
assert ";hi&" in output
assert "&" not in output
def test_html_inside_heading(ext_markdown):
"""Assert that headings don't double-escape HTML."""
output = ext_markdown.convert("::: tests.fixtures.builtin")
assert "=<" in output
assert "&" not in output
@pytest.mark.parametrize(
("ext_markdown", "expect_permalink"),
[
({"markdown_extensions": [{"toc": {"permalink": "@@@"}}]}, "@@@"),
({"markdown_extensions": [{"toc": {"permalink": "TeSt"}}]}, "TeSt"),
({"markdown_extensions": [{"toc": {"permalink": True}}]}, "¶"),
],
indirect=["ext_markdown"],
)
def test_no_double_toc(ext_markdown, expect_permalink):
"""Assert that the 'toc' extension doesn't apply its modification twice."""
output = ext_markdown.convert(
dedent(
"""
# aa
::: tests.fixtures.headings
rendering:
show_root_toc_entry: false
# bb
"""
)
)
assert output.count(expect_permalink) == 5
assert 'id="tests.fixtures.headings--foo"' in output
assert ext_markdown.toc_tokens == [ # noqa: E1101 (the member gets populated only with 'toc' extension)
{
"level": 1,
"id": "aa",
"name": "aa",
"children": [
{
"level": 2,
"id": "tests.fixtures.headings--foo",
"name": "Foo",
"children": [
{
"level": 4,
"id": "tests.fixtures.headings--bar",
"name": "Bar",
"children": [
{"level": 6, "id": "tests.fixtures.headings--baz", "name": "Baz", "children": []}
],
}
],
}
],
},
{"level": 1, "id": "bb", "name": "bb", "children": []},
]