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
319 lines
12 KiB
Python
319 lines
12 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 target set of targeted F3 (spec/08 §8.10)."""
|
|
|
|
import pytest
|
|
|
|
from helpers_lib1 import (
|
|
build_doc,
|
|
document,
|
|
heading,
|
|
ids_of_kind,
|
|
math_run,
|
|
package_state,
|
|
paragraph,
|
|
read_cases,
|
|
sentence,
|
|
text_run,
|
|
vectors,
|
|
)
|
|
from paperspec import hashing, targets
|
|
from paperspec.model import LEAVES, MATH_KINDS
|
|
|
|
|
|
def _doc():
|
|
"""Two sections, a table and an equation, with digits in some sentences."""
|
|
return build_doc(
|
|
document(
|
|
{
|
|
"kind": "front",
|
|
"children": [
|
|
{"kind": "title", "children": [sentence(text_run("Title 1"))]}
|
|
],
|
|
},
|
|
{
|
|
"kind": "section",
|
|
"attrs": {"level": 1},
|
|
"children": [
|
|
heading("Intro"),
|
|
paragraph(
|
|
sentence(text_run("No numbers here.")),
|
|
sentence(
|
|
text_run("Three "), math_run("x_2"), text_run(" items.")
|
|
),
|
|
),
|
|
{
|
|
"kind": "equation",
|
|
"content": {"notation": "tex", "source": "a=b"},
|
|
"children": [
|
|
{
|
|
"kind": "equation-line",
|
|
"content": {"notation": "tex", "source": "a=b"},
|
|
}
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
"kind": "section",
|
|
"attrs": {"level": 1},
|
|
"children": [
|
|
heading("Methods"),
|
|
paragraph(
|
|
sentence(text_run("We used 5 runs.")),
|
|
sentence(text_run("Nothing numeric.")),
|
|
),
|
|
{
|
|
"kind": "table",
|
|
"children": [
|
|
{
|
|
"kind": "table-body",
|
|
"children": [
|
|
{
|
|
"kind": "row",
|
|
"children": [
|
|
{
|
|
"kind": "cell",
|
|
"content": [text_run("3.5")],
|
|
},
|
|
{
|
|
"kind": "cell",
|
|
"content": [text_run("n/a")],
|
|
},
|
|
],
|
|
}
|
|
],
|
|
}
|
|
],
|
|
},
|
|
],
|
|
},
|
|
)
|
|
)
|
|
|
|
|
|
def _named(doc):
|
|
"""IDs by a short name."""
|
|
sentences = {hashing.plain(doc, s): s for s in ids_of_kind(doc, "sentence")}
|
|
cells = ids_of_kind(doc, "cell")
|
|
return {
|
|
"title": sentences["Title 1"],
|
|
"plain": sentences["No numbers here."],
|
|
"three": next(s for t, s in sentences.items() if t.startswith("Three")),
|
|
"five": sentences["We used 5 runs."],
|
|
"none": sentences["Nothing numeric."],
|
|
"num_cell": cells[0],
|
|
"text_cell": cells[1],
|
|
"eq": ids_of_kind(doc, "equation")[0],
|
|
"line": ids_of_kind(doc, "equation-line")[0],
|
|
"intro": ids_of_kind(doc, "section")[0],
|
|
"methods": ids_of_kind(doc, "section")[1],
|
|
"front": ids_of_kind(doc, "front")[0],
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("case", vectors("targets"))
|
|
def test_targets_vector(case):
|
|
doc = package_state(case["package"]).doc()
|
|
got = targets.target_set(doc, case["anchored"], case["digit_rule"])
|
|
assert got == case["expect"]["nodes"]
|
|
|
|
|
|
def test_the_three_digit_rules_nest_on_the_example_paper():
|
|
cases = {c["digit_rule"]: c for c in read_cases("targets")}
|
|
assert set(cases) == set(targets.DIGIT_RULES)
|
|
sets = {rule: {n for n, _ in c["expect"]["nodes"]} for rule, c in cases.items()}
|
|
assert sets["none"] <= sets["anchored-sections"] <= sets["all"]
|
|
assert sets["none"] < sets["all"]
|
|
|
|
|
|
@pytest.mark.parametrize("case", vectors("targets"))
|
|
def test_targets_are_leaves_and_equations_are_equation_kinds(case):
|
|
doc = package_state(case["package"]).doc()
|
|
got = targets.target_set(doc, case["anchored"], case["digit_rule"])
|
|
assert all(doc.nodes[n]["kind"] in LEAVES for n, _ in got)
|
|
assert all(doc.nodes[n]["kind"] in MATH_KINDS for n, r in got if r == "equation")
|
|
position = {nid: i for i, nid in enumerate(doc.preorder())}
|
|
assert [position[n] for n, _ in got] == sorted(position[n] for n, _ in got)
|
|
|
|
|
|
class TestTargetSet:
|
|
def test_all_takes_every_sentence_and_cell_with_a_digit(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
got = targets.target_set(doc, [], "all")
|
|
assert got == [
|
|
[n["title"], "digits"],
|
|
[n["three"], "digits"],
|
|
[n["eq"], "equation"],
|
|
[n["line"], "equation"],
|
|
[n["five"], "digits"],
|
|
[n["num_cell"], "digits"],
|
|
]
|
|
|
|
def test_none_takes_only_equations_and_anchored_leaves(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
assert targets.target_set(doc, [], "none") == [
|
|
[n["eq"], "equation"],
|
|
[n["line"], "equation"],
|
|
]
|
|
assert targets.target_set(doc, [n["none"]], "none") == [
|
|
[n["eq"], "equation"],
|
|
[n["line"], "equation"],
|
|
[n["none"], "anchored"],
|
|
]
|
|
|
|
def test_anchored_sections_takes_digits_only_where_something_is_anchored(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
got = targets.target_set(doc, [n["plain"]], "anchored-sections")
|
|
assert got == [
|
|
[n["plain"], "anchored"],
|
|
[n["three"], "digits"],
|
|
[n["eq"], "equation"],
|
|
[n["line"], "equation"],
|
|
]
|
|
|
|
def test_an_anchored_leaf_is_anchored_even_if_it_has_digits_or_is_an_equation(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
got = dict(targets.target_set(doc, [n["three"], n["eq"]], "all"))
|
|
assert got[n["three"]] == "anchored"
|
|
assert got[n["eq"]] == "anchored"
|
|
assert got[n["line"]] == "equation"
|
|
|
|
def test_an_anchored_container_contributes_its_leaves(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
got = dict(targets.target_set(doc, [n["methods"]], "none"))
|
|
leaves = set(doc.leaves(n["methods"]))
|
|
assert {k for k, v in got.items() if v == "anchored"} == leaves
|
|
assert got[n["num_cell"]] == "anchored"
|
|
|
|
def test_a_container_anchor_with_a_digit_rule_keeps_the_anchored_reason(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
got = dict(targets.target_set(doc, [n["methods"]], "all"))
|
|
assert got[n["five"]] == "anchored"
|
|
assert got[n["three"]] == "digits"
|
|
|
|
def test_anchored_sections_counts_a_section_an_anchored_container_sits_in(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
got = dict(targets.target_set(doc, [n["front"]], "anchored-sections"))
|
|
assert got[n["title"]] == "anchored"
|
|
assert n["five"] not in got
|
|
assert n["three"] not in got
|
|
|
|
def test_the_result_is_in_document_order_whatever_the_order_of_anchors(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
a = targets.target_set(doc, [n["five"], n["plain"], n["none"]], "none")
|
|
b = targets.target_set(doc, [n["none"], n["plain"], n["five"]], "none")
|
|
assert a == b
|
|
order = doc.preorder()
|
|
assert [order.index(x) for x, _ in a] == sorted(order.index(x) for x, _ in a)
|
|
|
|
def test_the_default_rule_is_all(self):
|
|
doc = _doc()
|
|
assert targets.target_set(doc, []) == targets.target_set(doc, [], "all")
|
|
|
|
def test_a_node_given_twice_counts_once(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
assert targets.target_set(
|
|
doc, [n["plain"], n["plain"]], "none"
|
|
) == targets.target_set(doc, [n["plain"]], "none")
|
|
|
|
@pytest.mark.parametrize("rule", ["some", "", "ALL", None])
|
|
def test_an_unknown_digit_rule_is_refused(self, rule):
|
|
with pytest.raises(
|
|
ValueError, match="digit rule must be one of all, anchored-sections, none"
|
|
):
|
|
targets.target_set(_doc(), [], rule)
|
|
|
|
def test_anchored_nodes_must_be_in_the_document(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
with pytest.raises(
|
|
ValueError, match="anchored nodes are not live: zzzzzzz, zzzzzzzz"
|
|
):
|
|
targets.target_set(doc, ["zzzzzzzz", n["plain"], "zzzzzzz"], "all")
|
|
|
|
|
|
class TestHasDigitRun:
|
|
@pytest.mark.parametrize(
|
|
("content", "expected"),
|
|
[
|
|
([{"kind": "text", "text": "a1"}], True),
|
|
([{"kind": "text", "text": "abc"}], False),
|
|
([{"kind": "math", "source": "x_2"}], True),
|
|
([{"kind": "math", "source": "x"}], False),
|
|
([{"kind": "math", "source": None}], False),
|
|
([{"kind": "text", "text": "٣"}], True),
|
|
([{"kind": "text", "text": "²"}], False),
|
|
([{"kind": "cite", "refs": ["a"], "text": "12"}], False),
|
|
([{"kind": "xref", "text": "Table 1"}], False),
|
|
([{"kind": "equation", "node": "aaaaaaa"}], False),
|
|
([{"kind": "text", "text": "x"}, {"kind": "text", "text": "7"}], True),
|
|
(["7", None, 7], False),
|
|
([{"kind": "text"}], False),
|
|
([], False),
|
|
(None, False),
|
|
],
|
|
)
|
|
def test_only_text_and_math_runs_count(self, content, expected):
|
|
assert targets.has_digit_run({"content": content}) is expected
|
|
|
|
def test_a_node_without_content_has_no_digit_run(self):
|
|
assert targets.has_digit_run({}) is False
|
|
|
|
|
|
class TestEnclosingSection:
|
|
def test_a_section_encloses_itself_and_what_it_holds(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
assert targets.enclosing_section(doc, n["intro"]) == n["intro"]
|
|
assert targets.enclosing_section(doc, n["three"]) == n["intro"]
|
|
assert targets.enclosing_section(doc, n["num_cell"]) == n["methods"]
|
|
|
|
def test_front_matter_is_its_own_section(self):
|
|
doc = _doc()
|
|
n = _named(doc)
|
|
assert targets.enclosing_section(doc, n["title"]) == n["front"]
|
|
|
|
def test_the_nearest_section_wins(self):
|
|
doc = build_doc(
|
|
document(
|
|
{
|
|
"kind": "section",
|
|
"attrs": {"level": 1},
|
|
"children": [
|
|
heading("Outer"),
|
|
{
|
|
"kind": "section",
|
|
"attrs": {"level": 2},
|
|
"children": [
|
|
heading("Inner"),
|
|
paragraph(sentence(text_run("x"))),
|
|
],
|
|
},
|
|
],
|
|
}
|
|
)
|
|
)
|
|
outer, inner = ids_of_kind(doc, "section")
|
|
leaf = ids_of_kind(doc, "paragraph")[0]
|
|
assert targets.enclosing_section(doc, leaf) == inner
|
|
assert targets.enclosing_section(doc, inner) == inner
|
|
assert targets.enclosing_section(doc, outer) == outer
|
|
|
|
def test_a_node_outside_every_section_is_enclosed_by_the_root(self):
|
|
doc = build_doc(document({"kind": "back", "children": []}))
|
|
assert targets.enclosing_section(doc, doc.root) == doc.root
|