diora-web/tui/tests/test_blocks.py

54 lines
2.2 KiB
Python
Raw Permalink Normal View History

"""The block list must count and read exactly like the web reader (static/js/app/44-books.js,
40-epub.js sanitizeEpubHtml): anchors are block indexes shared between all clients. The
real-book comparison against the web reader's own code is tools/parity/."""
from diora_tui.blocks import anchor_is_further, format_anchor, load_flat_book, parse_anchor
def texts(book):
return [(b.tag, b.text) for b in book.blocks]
def test_block_selector_and_nesting(epub):
book = load_flat_book(epub([
"<h1>Titel</h1><p>Eins</p><blockquote><p>Zitat</p></blockquote><ul><li>a</li></ul>"
"<div>Blatt</div><div><span>kein Blatt</span></div><div><p>Behälter</p></div>"
]))
# a div counts only when it has no element children; blockquote and its p both count
assert texts(book) == [
("h1", "Titel"), ("p", "Eins"), ("blockquote", "Zitat"), ("p", "Zitat"),
("li", "a"), ("div", "Blatt"), ("p", "Behälter"),
]
def test_all_spine_documents_count_even_without_text(epub):
book = load_flat_book(epub(["<p>a</p>", "<p>b</p>"]))
assert [b.chapter_index for b in book.blocks] == [0, 1]
def test_comments_are_not_text(epub):
book = load_flat_book(epub(['<h1><!-- d="kap1" -->Titel</h1><p>x<!-- weg -->y</p>']))
assert texts(book) == [("h1", "Titel"), ("p", "xy")]
def test_whitespace_is_javascripts(epub):
# \s in JS: no-break space and BOM collapse like spaces; \x85 does not
book = load_flat_book(epub(["<p>a  bc</p><p>\x85</p>"]))
assert texts(book)[0] == ("p", "a b c")
assert texts(book)[1][1] == "\x85"
def test_footnote_marker_adds_no_space(epub):
book = load_flat_book(epub(['<p>Satz.<sup><a href="#n1">1</a></sup>Weiter</p><p id="n1">Fußnote</p>']))
assert book.blocks[0].text == "Satz.1Weiter"
assert book.blocks[0].footnotes[0].marker == "1"
assert book.footnote_targets["n1"] == 1
def test_anchor_format_and_order():
assert format_anchor(12, 0.5) == "12:0.500000"
assert parse_anchor("12:0.500000") == (12, 0.5)
assert parse_anchor("kaputt") is None
assert anchor_is_further("5:0.1", "4:0.9")
assert anchor_is_further("5:0.5", "5:0.4")
assert not anchor_is_further("4:0.9", "5:0.1")