Coverage for tests/test_backlinks.py: 100.00%
30 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-24 13:40 +0100
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-24 13:40 +0100
1"""Tests for the backlinks module."""
3from __future__ import annotations
5from textwrap import dedent
7from markdown import Markdown
9from mkdocs_autorefs import AUTOREF_RE, AutorefsExtension, AutorefsPlugin, Backlink, BacklinkCrumb
10from mkdocs_autorefs._internal.references import _html_attrs_parser
11from tests.helpers import create_page
14def test_record_backlinks() -> None:
15 """Check that only useful backlinks are recorded."""
16 plugin = AutorefsPlugin()
17 plugin._record_backlink("foo", "referenced-by", "foo", "foo.html")
18 assert "foo" not in plugin._backlinks
20 plugin.register_anchor(identifier="foo", page=create_page("foo.html"), primary=True)
21 plugin._record_backlink("foo", "referenced-by", "foo", "foo.html")
22 assert "foo" in plugin._backlinks
25def test_get_backlinks() -> None:
26 """Check that backlinks can be retrieved."""
27 plugin = AutorefsPlugin()
28 plugin.record_backlinks = True
29 plugin.register_anchor(identifier="foo", page=create_page("foo.html"), primary=True)
30 plugin._record_backlink("foo", "referenced-by", "foo", "foo.html")
31 assert plugin.get_backlinks("foo", from_url="") == {
32 "referenced-by": {
33 Backlink(
34 crumbs=(
35 BacklinkCrumb(title="foo.html", url="foo.html#"),
36 BacklinkCrumb(title="", url="foo.html#foo"),
37 ),
38 ),
39 },
40 }
43def test_backlinks_treeprocessor() -> None:
44 """Check that the backlinks treeprocessor works."""
45 plugin = AutorefsPlugin()
46 plugin.record_backlinks = True
47 plugin.current_page = create_page("foo.html")
48 md = Markdown(extensions=["attr_list", "toc", AutorefsExtension(plugin)])
49 html = md.convert(
50 dedent(
51 """
52 [](){#alias}
53 ## Heading
55 [Foo][foo]
56 """,
57 ),
58 )
59 match = AUTOREF_RE.search(html)
60 assert match
61 attrs = _html_attrs_parser.parse(f"<a {match['attrs']}>")
62 assert "backlink-type" in attrs
63 assert "backlink-anchor" in attrs