-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow registering absolute URLs for autorefs
For now this is not used for anything, but the refactor is good for whatever plan we decide to go through regarding inventories. PR #8: #8
- Loading branch information
Showing
4 changed files
with
99 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Tests for the plugin module.""" | ||
import pytest | ||
|
||
from mkdocs_autorefs.plugin import AutorefsPlugin | ||
|
||
|
||
def test_url_registration(): | ||
"""Check that URLs can be registered, then obtained.""" | ||
plugin = AutorefsPlugin() | ||
plugin.register_anchor(identifier="foo", page="foo1.html") | ||
plugin.register_url(identifier="bar", url="https://example.org/bar.html") | ||
|
||
assert plugin.get_item_url("foo") == "foo1.html#foo" | ||
assert plugin.get_item_url("bar") == "https://example.org/bar.html" | ||
with pytest.raises(KeyError): | ||
plugin.get_item_url("baz") | ||
|
||
|
||
def test_url_registration_with_from_url(): | ||
"""Check that URLs can be registered, then obtained, relative to a page.""" | ||
plugin = AutorefsPlugin() | ||
plugin.register_anchor(identifier="foo", page="foo1.html") | ||
plugin.register_url(identifier="bar", url="https://example.org/bar.html") | ||
|
||
assert plugin.get_item_url("foo", from_url="a/b.html") == "../foo1.html#foo" | ||
assert plugin.get_item_url("bar", from_url="a/b.html") == "https://example.org/bar.html" | ||
with pytest.raises(KeyError): | ||
plugin.get_item_url("baz", from_url="a/b.html") | ||
|
||
|
||
def test_url_registration_with_fallback(): | ||
"""Check that URLs can be registered, then obtained through a fallback.""" | ||
plugin = AutorefsPlugin() | ||
plugin.register_anchor(identifier="foo", page="foo1.html") | ||
plugin.register_url(identifier="bar", url="https://example.org/bar.html") | ||
|
||
assert plugin.get_item_url("baz", fallback=lambda s: "foo") == "foo1.html#foo" | ||
assert plugin.get_item_url("baz", fallback=lambda s: "bar") == "https://example.org/bar.html" | ||
with pytest.raises(KeyError): | ||
plugin.get_item_url("baz", fallback=lambda s: "baaaa") | ||
with pytest.raises(KeyError): | ||
plugin.get_item_url("baz", fallback=lambda s: None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters