Skip to content

Commit

Permalink
test: tests of HTML's helper assert_valid_hrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 20, 2022
1 parent 1f658e9 commit 907d646
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def assert_valid_hrefs(self):
html = fhtml.read()
for href in re.findall(r""" href=['"]([^'"]*)['"]""", html):
if href.startswith("#"):
assert re.search(rf""" id=['"]{href[1:]}['"]""", html)
assert re.search(rf""" id=['"]{href[1:]}['"]""", html), (
f"Fragment {href!r} in {fname} has no anchor"
)
continue
if "://" in href:
continue
Expand Down Expand Up @@ -1188,3 +1190,21 @@ def test_dynamic_contexts_relative_files(self):
if label == ld.contexts_label or label in (ld.contexts or ())
]
assert sorted(expected) == sorted(actual)


class HtmlHelpersTest(HtmlTestHelpers, CoverageTest):
"""Tests of the helpers in HtmlTestHelpers."""

def test_bad_link(self):
# Does assert_valid_hrefs detect links to non-existent files?
self.make_file("htmlcov/index.html", "<a href='nothing.html'>Nothing</a>")
msg = "These files link to 'nothing.html', which doesn't exist: htmlcov.index.html"
with pytest.raises(AssertionError, match=msg):
self.assert_valid_hrefs()

def test_bad_anchor(self):
# Does assert_valid_hrefs detect fragments that go nowhere?
self.make_file("htmlcov/index.html", "<a href='#nothing'>Nothing</a>")
msg = "Fragment '#nothing' in htmlcov.index.html has no anchor"
with pytest.raises(AssertionError, match=msg):
self.assert_valid_hrefs()

0 comments on commit 907d646

Please sign in to comment.