The suite takes its data from the vectors and fixtures: 3635 tests, 97.9% branch coverage, and paperspec check and paperspec stress give the same results as before. Fixed: receipts.verify_signature raised KeyError for a record without a format; traces.DOC_TAG matched a tag followed by a line feed; paperspec load and paperspec check left SQLite connections open; the site builder copied the output folder's subfolders into itself. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8cL4zN4XpaQF5cBtASzfq
252 lines
8.6 KiB
Python
252 lines
8.6 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# Copyright 2026 Scienting
|
|
#
|
|
# Source Code: https://git.scient.ing/papers/paperspec
|
|
# See the LICENSE.md file for full license terms.
|
|
|
|
"""The node kinds, their rules and the document structure that walks the tree (spec/03)."""
|
|
|
|
import pytest
|
|
|
|
from helpers_lib1 import (
|
|
HALE,
|
|
build_doc,
|
|
document,
|
|
heading,
|
|
ids_of_kind,
|
|
math_run,
|
|
package_state,
|
|
paragraph,
|
|
section,
|
|
sentence,
|
|
text_run,
|
|
)
|
|
from paperspec import model
|
|
from paperspec.model import Doc
|
|
|
|
|
|
class TestKindTables:
|
|
def test_every_kind_has_a_row_in_the_child_table(self):
|
|
assert set(model.CHILDREN) == model.KINDS
|
|
assert "document" in model.KINDS
|
|
|
|
def test_every_allowed_child_is_a_known_kind(self):
|
|
for kind, kids in model.CHILDREN.items():
|
|
assert kids <= model.KINDS, kind
|
|
|
|
def test_every_kind_but_the_document_can_be_somebodys_child(self):
|
|
children = set().union(*model.CHILDREN.values())
|
|
assert model.KINDS - children == {"document"}
|
|
|
|
def test_leaves_and_containers_with_content_are_kinds_and_do_not_overlap(self):
|
|
assert model.LEAVES <= model.KINDS
|
|
assert model.CONTAINERS_WITH_CONTENT <= model.KINDS
|
|
assert not (model.LEAVES & model.CONTAINERS_WITH_CONTENT)
|
|
assert {"document", "paragraph", "row", "list"} <= (
|
|
model.KINDS - model.LEAVES - model.CONTAINERS_WITH_CONTENT
|
|
)
|
|
|
|
def test_the_leaf_kinds_are_runs_equations_and_verbatim_text(self):
|
|
assert model.LEAVES == model.RUN_KINDS | model.MATH_KINDS | model.VERBATIM_KINDS
|
|
assert not (model.RUN_KINDS & model.MATH_KINDS)
|
|
assert not (model.RUN_KINDS & model.VERBATIM_KINDS)
|
|
assert {"equation", "equation-line"} == model.MATH_KINDS
|
|
assert {"code", "algorithm"} == model.VERBATIM_KINDS
|
|
|
|
@pytest.mark.parametrize(
|
|
"table",
|
|
[
|
|
model.NONEMPTY,
|
|
model.AT_MOST_ONCE,
|
|
model.LABELLED,
|
|
model.CONTAINERS_WITH_CONTENT,
|
|
set(model.ATTRS),
|
|
],
|
|
)
|
|
def test_every_rule_table_names_known_kinds(self, table):
|
|
assert set(table) <= model.KINDS
|
|
|
|
def test_a_kind_that_must_have_children_may_have_some(self):
|
|
for kind in model.NONEMPTY:
|
|
assert model.CHILDREN[kind], kind
|
|
|
|
def test_levels_run_from_f0_to_f3(self):
|
|
assert model.LEVELS == ["F0", "F1", "F2", "F3"]
|
|
|
|
def test_run_kinds_and_styles(self):
|
|
assert {
|
|
"text",
|
|
"math",
|
|
"cite",
|
|
"xref",
|
|
"fnmark",
|
|
"link",
|
|
"equation",
|
|
} == model.RUNS
|
|
assert {"em", "strong", "sup", "sub", "sc", "mono"} == model.STYLES
|
|
|
|
@pytest.mark.parametrize(
|
|
("kind", "cls"),
|
|
[
|
|
("sentence", "leaf"),
|
|
("cell", "leaf"),
|
|
("equation", "leaf"),
|
|
("equation-line", "leaf"),
|
|
("code", "leaf"),
|
|
("reference", "leaf"),
|
|
("section", "container-with-content"),
|
|
("table", "container-with-content"),
|
|
("figure", "container-with-content"),
|
|
("footnote", "container-with-content"),
|
|
("paragraph", "container-without-content"),
|
|
("document", "container-without-content"),
|
|
("row", "container-without-content"),
|
|
("list", "container-without-content"),
|
|
],
|
|
)
|
|
def test_anchor_class(self, kind, cls):
|
|
assert model.anchor_class(kind) == cls
|
|
|
|
def test_every_kind_has_an_anchor_class(self):
|
|
classes = {model.anchor_class(k) for k in model.KINDS}
|
|
assert classes == {
|
|
"leaf",
|
|
"container-with-content",
|
|
"container-without-content",
|
|
}
|
|
|
|
|
|
def _nodes():
|
|
"""document > section > [heading > sentence, paragraph > sentence, equation > line]."""
|
|
return [
|
|
{"id": "ddddddd", "kind": "document", "parent": None, "ord": 0},
|
|
{"id": "sssssss", "kind": "section", "parent": "ddddddd", "ord": 0},
|
|
{"id": "ppppppp", "kind": "paragraph", "parent": "sssssss", "ord": 1},
|
|
{"id": "hhhhhhh", "kind": "heading", "parent": "sssssss", "ord": 0},
|
|
{"id": "1111111", "kind": "sentence", "parent": "hhhhhhh", "ord": 0},
|
|
{"id": "2222222", "kind": "sentence", "parent": "ppppppp", "ord": 0},
|
|
{"id": "3333333", "kind": "equation", "parent": "2222222", "ord": 0},
|
|
{"id": "eeeeeee", "kind": "equation", "parent": "sssssss", "ord": 2},
|
|
{"id": "lllllll", "kind": "equation-line", "parent": "eeeeeee", "ord": 0},
|
|
{"id": "ccccccc", "kind": "code", "parent": "sssssss", "ord": 3},
|
|
]
|
|
|
|
|
|
class TestDoc:
|
|
def test_children_come_in_ord_order_not_listing_order(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.kids("sssssss") == ["hhhhhhh", "ppppppp", "eeeeeee", "ccccccc"]
|
|
assert doc.order[2] == "ppppppp"
|
|
|
|
def test_the_root_is_the_first_node_without_a_parent(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.root == "ddddddd"
|
|
assert doc.key == "k"
|
|
|
|
def test_an_empty_document_has_no_root_and_no_preorder(self):
|
|
doc = Doc("k", [])
|
|
assert doc.root is None
|
|
assert doc.order == []
|
|
assert doc.preorder() == []
|
|
assert doc.kids("anything") == []
|
|
|
|
def test_a_leaf_has_no_kids_and_an_unknown_id_has_none(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.kids("1111111") == []
|
|
assert doc.kids("zzzzzzz") == []
|
|
|
|
def test_preorder_visits_a_node_before_its_children_in_order(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.preorder() == [
|
|
"ddddddd",
|
|
"sssssss",
|
|
"hhhhhhh",
|
|
"1111111",
|
|
"ppppppp",
|
|
"2222222",
|
|
"3333333",
|
|
"eeeeeee",
|
|
"lllllll",
|
|
"ccccccc",
|
|
]
|
|
|
|
def test_preorder_of_a_subtree(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.preorder("ppppppp") == ["ppppppp", "2222222", "3333333"]
|
|
assert doc.preorder("1111111") == ["1111111"]
|
|
|
|
def test_ancestors_run_from_the_root_down_to_the_parent(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.ancestors("3333333") == ["ddddddd", "sssssss", "ppppppp", "2222222"]
|
|
assert doc.ancestors("ddddddd") == []
|
|
|
|
def test_text_leaves_are_the_nodes_whose_text_a_container_is_made_of(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.text_leaves("ddddddd") == [
|
|
"1111111",
|
|
"2222222",
|
|
"eeeeeee",
|
|
"ccccccc",
|
|
]
|
|
assert doc.text_leaves("ppppppp") == ["2222222"]
|
|
|
|
def test_an_equation_inside_a_sentence_is_part_of_the_sentence(self):
|
|
doc = Doc("k", _nodes())
|
|
assert "3333333" not in doc.text_leaves("ddddddd")
|
|
assert doc.text_leaves("3333333") == []
|
|
|
|
def test_an_equation_line_is_never_a_text_leaf(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.text_leaves("lllllll") == []
|
|
assert "lllllll" not in doc.text_leaves("eeeeeee")
|
|
assert doc.text_leaves("eeeeeee") == ["eeeeeee"]
|
|
|
|
def test_a_leaf_is_its_own_text_leaf(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.text_leaves("ccccccc") == ["ccccccc"]
|
|
assert doc.text_leaves("1111111") == ["1111111"]
|
|
|
|
def test_leaves_include_equations_in_sentences_and_equation_lines(self):
|
|
doc = Doc("k", _nodes())
|
|
assert doc.leaves("ddddddd") == [
|
|
"1111111",
|
|
"2222222",
|
|
"3333333",
|
|
"eeeeeee",
|
|
"lllllll",
|
|
"ccccccc",
|
|
]
|
|
assert doc.leaves("ppppppp") == ["2222222", "3333333"]
|
|
assert doc.leaves("hhhhhhh") == ["1111111"]
|
|
assert doc.leaves("1111111") == ["1111111"]
|
|
|
|
def test_the_fixture_documents_are_walked_consistently(self):
|
|
doc = package_state(HALE).doc()
|
|
order = doc.preorder()
|
|
assert order == doc.order
|
|
assert set(order) == set(doc.nodes)
|
|
for nid in doc.text_leaves(doc.root):
|
|
assert doc.nodes[nid]["kind"] in model.LEAVES
|
|
assert set(doc.text_leaves(doc.root)) <= set(doc.leaves(doc.root))
|
|
|
|
def test_a_built_document_keeps_its_structure(self):
|
|
doc = build_doc(
|
|
document(
|
|
section(
|
|
heading("H"),
|
|
paragraph(
|
|
sentence(text_run("a")), sentence(text_run("b"), math_run("x"))
|
|
),
|
|
)
|
|
)
|
|
)
|
|
sentences = ids_of_kind(doc, "sentence")
|
|
assert len(sentences) == 3
|
|
para = ids_of_kind(doc, "paragraph")[0]
|
|
assert doc.kids(para) == sentences[1:]
|
|
assert doc.ancestors(sentences[1]) == [
|
|
doc.root,
|
|
ids_of_kind(doc, "section")[0],
|
|
para,
|
|
]
|