File: test_render_helpers.py

package info (click to toggle)
python-pdoc 16.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: python: 5,260; javascript: 1,156; makefile: 18; sh: 3
file content (163 lines) | stat: -rw-r--r-- 4,952 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
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
163
from __future__ import annotations

from collections.abc import Mapping

import pytest

from pdoc.render_helpers import edit_url
from pdoc.render_helpers import module_candidates
from pdoc.render_helpers import possible_sources
from pdoc.render_helpers import qualname_candidates
from pdoc.render_helpers import relative_link
from pdoc.render_helpers import split_identifier
from pdoc.render_helpers import to_html


@pytest.mark.parametrize(
    "current,target,relative",
    [
        ("foo", "foo", ""),
        ("foo", "bar", "bar.html"),
        ("foo.foo", "foo", "../foo.html"),
        ("foo.foo", "bar", "../bar.html"),
        ("foo.bar", "foo.bar.baz", "bar/baz.html"),
        ("foo.bar.baz", "foo.qux.quux", "../qux/quux.html"),
    ],
)
def test_relative_link(current, target, relative):
    assert relative_link(current, target) == relative


@pytest.mark.parametrize(
    "context,candidates",
    [
        ("", ["qux"]),
        ("foo", ["foo.qux", "qux"]),
        ("foo.bar", ["foo.bar.qux", "foo.qux", "qux"]),
    ],
)
def test_qualname_candidates(context, candidates):
    assert qualname_candidates("qux", context) == candidates


@pytest.mark.parametrize(
    "identifier,current_module,candidates",
    [
        ["foo.bar.baz", "qux", ["qux", "foo", "foo.bar", "foo.bar.baz"]],
        ["foo.bar.baz", "foo.bar", ["foo.bar", "foo", "foo.bar.baz"]],
        ["foo.bar.baz", "foo", ["foo", "foo.bar", "foo.bar.baz"]],
    ],
)
def test_module_candidates(identifier, current_module, candidates):
    assert list(module_candidates(identifier, current_module)) == candidates


@pytest.mark.parametrize(
    "modulename,is_package,mapping,result",
    [
        ["demo", False, {"dem": "abc"}, None],
        [
            "demo",
            False,
            {"demo": "https://github.com/mhils/pdoc/blob/master/test/testdata/demo"},
            "https://github.com/mhils/pdoc/blob/master/test/testdata/demo.py",
        ],
        [
            "demo",
            True,
            {"demo": "https://github.com/mhils/pdoc/blob/master/test/testdata/demo/"},
            "https://github.com/mhils/pdoc/blob/master/test/testdata/demo/__init__.py",
        ],
    ],
)
def test_edit_url(
    modulename: str, is_package: bool, mapping: Mapping[str, str], result: str | None
):
    assert edit_url(modulename, is_package, mapping) == result


@pytest.mark.parametrize(
    "all_modules,fullname,result",
    [
        [["a", "a.b", "c"], "a.b.c.d", ("a.b", "c.d")],
        [["a", "a.b", "c"], "a.c.b.d", ("a", "c.b.d")],
    ],
)
def test_split_identifier(all_modules, fullname, result):
    with pytest.warns(DeprecationWarning):
        assert split_identifier(all_modules, fullname) == result


@pytest.mark.parametrize(
    "all_modules,fullname,result",
    [
        [["a"], "a.B", [("a", "B")]],
        [["a", "a.b"], "a.b", [("a.b", "")]],
        [["a"], "a", [("a", "")]],
        [["a", "a.b"], "a.b.c.d", [("a.b.c", "d"), ("a.b", "c.d")]],
    ],
)
def test_possible_sources(all_modules, fullname, result):
    assert list(possible_sources(all_modules, fullname)) == result


def test_markdown_toc():
    """
    markdown2 has this weird property that it return a str-like object with a hidden `toc_html` attr.

    It's easy to introduce a `.strip()` in there and this gets washed away, so let's test that it works properly.
    """
    assert to_html("#foo\n#bar").toc_html  # type: ignore


def test_mixed_toc():
    """
    markdown2 can handle mixed markdown and HTML headings.

    Let's test that this works as expected.
    """
    expected = [
        "<ul>",
        '  <li><a href="#foo">foo</a></li>',
        '  <li><a href="#bar">bar</a></li>',
        "</ul>",
        "",
    ]
    assert to_html("#foo\n<h1>bar</h1>").toc_html == "\n".join(expected)


@pytest.mark.parametrize(
    "md,html",
    [
        (
            "https://example.com/",
            '<p><a href="https://example.com/">https://example.com/</a></p>\n',
        ),
        (
            "<https://example.com>",
            '<p><a href="https://example.com">https://example.com</a></p>\n',
        ),
        (
            '<a href="https://example.com">link</a>',
            '<p><a href="https://example.com">link</a></p>\n',
        ),
        (
            "[link](https://example.com)",
            '<p><a href="https://example.com">link</a></p>\n',
        ),
        (
            "See the [Python home page ](https://www.python.org) for info.",
            '<p>See the <a href="https://www.python.org">Python home page </a> for info.</p>\n',
        ),
        (
            "See https://www.python.org.",
            '<p>See <a href="https://www.python.org">https://www.python.org</a>.</p>\n',
        ),
        (
            "See **https://www.python.org**.",
            "<p>See <strong>https://www.python.org</strong>.</p>\n",
        ),
    ],
)
def test_markdown_autolink(md, html):
    assert to_html(md) == html