Coverage for tests/test_plugin.py: 98.39%

38 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-01 20:28 +0200

1"""Tests for the plugin module.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6 

7from mkdocs_autorefs.plugin import AutorefsPlugin 

8 

9 

10def test_url_registration() -> None: 

11 """Check that URLs can be registered, then obtained.""" 

12 plugin = AutorefsPlugin() 

13 plugin.register_anchor(identifier="foo", page="foo1.html") 

14 plugin.register_url(identifier="bar", url="https://example.org/bar.html") 

15 

16 assert plugin.get_item_url("foo") == "foo1.html#foo" 

17 assert plugin.get_item_url("bar") == "https://example.org/bar.html" 

18 with pytest.raises(KeyError): 

19 plugin.get_item_url("baz") 

20 

21 

22def test_url_registration_with_from_url() -> None: 

23 """Check that URLs can be registered, then obtained, relative to a page.""" 

24 plugin = AutorefsPlugin() 

25 plugin.register_anchor(identifier="foo", page="foo1.html") 

26 plugin.register_url(identifier="bar", url="https://example.org/bar.html") 

27 

28 assert plugin.get_item_url("foo", from_url="a/b.html") == "../foo1.html#foo" 

29 assert plugin.get_item_url("bar", from_url="a/b.html") == "https://example.org/bar.html" 

30 with pytest.raises(KeyError): 

31 plugin.get_item_url("baz", from_url="a/b.html") 

32 

33 

34def test_url_registration_with_fallback() -> None: 

35 """Check that URLs can be registered, then obtained through a fallback.""" 

36 plugin = AutorefsPlugin() 

37 plugin.register_anchor(identifier="foo", page="foo1.html") 

38 plugin.register_url(identifier="bar", url="https://example.org/bar.html") 

39 

40 # URL map will be updated with baz -> foo1.html#foo 

41 assert plugin.get_item_url("baz", fallback=lambda _: ("foo",)) == "foo1.html#foo" 

42 # as expected, baz is now known as foo1.html#foo 

43 assert plugin.get_item_url("baz", fallback=lambda _: ("bar",)) == "foo1.html#foo" 43 ↛ exitline 43 didn't run the lambda on line 43

44 # unknown identifiers correctly fallback: qux -> https://example.org/bar.html 

45 assert plugin.get_item_url("qux", fallback=lambda _: ("bar",)) == "https://example.org/bar.html" 

46 

47 with pytest.raises(KeyError): 

48 plugin.get_item_url("foobar", fallback=lambda _: ("baaaa",)) 

49 with pytest.raises(KeyError): 

50 plugin.get_item_url("foobar", fallback=lambda _: ()) 

51 

52 

53def test_dont_make_relative_urls_relative_again() -> None: 

54 """Check that URLs are not made relative more than once.""" 

55 plugin = AutorefsPlugin() 

56 plugin.register_anchor(identifier="foo.bar.baz", page="foo/bar/baz.html") 

57 

58 for _ in range(2): 

59 assert ( 

60 plugin.get_item_url("hello", from_url="baz/bar/foo.html", fallback=lambda _: ("foo.bar.baz",)) 

61 == "../../foo/bar/baz.html#foo.bar.baz" 

62 ) 

63 

64 

65@pytest.mark.parametrize( 

66 ("base", "urls", "expected"), 

67 [ 

68 # One URL is closest. 

69 ("", ["x/#b", "#b"], "#b"), 

70 # Several URLs are equally close. 

71 ("a/b", ["x/#e", "a/c/#e", "a/d/#e"], "a/c/#e"), 

72 ("a/b/", ["x/#e", "a/d/#e", "a/c/#e"], "a/d/#e"), 

73 # Two close URLs, one is shorter (closer). 

74 ("a/b", ["x/#e", "a/c/#e", "a/c/d/#e"], "a/c/#e"), 

75 ("a/b/", ["x/#e", "a/c/d/#e", "a/c/#e"], "a/c/#e"), 

76 # Deeper-nested URLs. 

77 ("a/b/c", ["x/#e", "a/#e", "a/b/#e", "a/b/c/#e", "a/b/c/d/#e"], "a/b/c/#e"), 

78 ("a/b/c/", ["x/#e", "a/#e", "a/b/#e", "a/b/c/d/#e", "a/b/c/#e"], "a/b/c/#e"), 

79 # No closest URL, use first one even if longer distance. 

80 ("a", ["b/c/#d", "c/#d"], "b/c/#d"), 

81 ("a/", ["c/#d", "b/c/#d"], "c/#d"), 

82 ], 

83) 

84def test_find_closest_url(base: str, urls: list[str], expected: str) -> None: 

85 """Find closest URLs given a list of URLs.""" 

86 assert AutorefsPlugin._get_closest_url(base, urls) == expected