# 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. """`doc.math.form`: a source with `form` is that form's own spelling (spec/03 ยง3.7).""" import types import pytest from helpers_cli import HALE, edit_package, rules_of, store from paperspec import package from paperspec.model import Doc EQUATION = "tkxsmts" SENTENCE = "rr20tev" CANONICAL = r"E_{\text{bond}}=\frac{1}{2}K_{r}(r-r_{\text{eq}})^{2}" def spell(source): """A stand-in for the form: it knows one formula, and refuses what it cannot read.""" if "{" in source and source.count("{") != source.count("}"): raise ValueError("a brace does not balance") return CANONICAL if "bond" in source else source def with_form(tmp_path, equation=None, run_source=None): """A copy of the main example whose equation and second math run carry `form`. `form` enters no content value, so the copy is valid but for what the sources hold. """ def edit(nodes): for n in nodes: if n["id"] == EQUATION: n["content"] = dict( n["content"], form=package.MATH_FORM, **({"source": equation} if equation else {}), ) if n["id"] == SENTENCE and run_source is not None: n["content"][3] = dict( n["content"][3], form=package.MATH_FORM, source=run_source ) return nodes return edit_package(tmp_path, HALE, {"nodes.jsonl": edit}) def rules(root, forms): return rules_of(package.validate(root, store(), forms=forms)) def test_a_canonical_source_with_form_breaks_no_form_rule(tmp_path): # Respelling the source changes its hashes, which the copy does not recompute, so only # the form rule is looked at here. root = with_form(tmp_path, equation=CANONICAL) assert "doc.math.form" not in rules(root, {package.MATH_FORM: spell}) def test_adding_form_alone_changes_no_value(tmp_path): root = with_form(tmp_path) assert not package.validate(root, store(), forms={}).errors def test_without_an_implementation_only_what_the_form_never_holds_is_reported(tmp_path): root = with_form(tmp_path) assert "doc.math.form" not in rules(root, {}) @pytest.mark.parametrize( "source", [ r"x=1\tag{2}", r"x=1\label{eq:x}", r"x=1\nonumber", r"\notag x", r"\displaystyle x", r"\dfrac{1}{2}", r"\tfrac{1}{2}", ], ) def test_a_label_or_style_command_is_never_in_the_form(tmp_path, source): root = with_form(tmp_path, run_source=source) report = package.validate(root, store(), forms={}) assert any( e["rule"] == "doc.math.form" and e["where"] == f"{SENTENCE} run 3" for e in report.errors ) @pytest.mark.parametrize( "source", [r"\\tag", r"\text{tagged}", r"\tagx", r"x\\\\notag"] ) def test_an_escaped_backslash_or_a_longer_command_is_not_a_label_command( tmp_path, source ): root = with_form(tmp_path, run_source=source) assert "doc.math.form" not in rules(root, {}) def test_with_an_implementation_a_source_not_its_own_spelling_is_reported(tmp_path): root = with_form(tmp_path) report = package.validate(root, store(), forms={package.MATH_FORM: spell}) [e] = [e for e in report.errors if e["rule"] == "doc.math.form"] assert e["where"] == EQUATION assert "is not its scienting-math/1 spelling, which is" in e["message"] def test_a_source_the_form_cannot_read_is_reported(tmp_path): root = with_form(tmp_path, run_source="x_{1") report = package.validate(root, store(), forms={package.MATH_FORM: spell}) assert any( e["rule"] == "doc.math.form" and "does not read" in e["message"] for e in report.errors ) def test_a_form_without_an_implementation_is_not_checked(): doc = Doc( "k", [ {"id": "r", "kind": "document", "parent": None, "ord": 0}, { "id": "e", "kind": "equation", "parent": "r", "ord": 0, "content": {"notation": "tex", "source": "x y", "form": "other/1"}, }, ], ) rep = package.Report() package.check_forms(doc, rep, {package.MATH_FORM: spell}) assert not rep.errors def test_the_default_forms_are_papermaths_when_it_is_installed(): papermath = pytest.importorskip("papermath") forms = package.default_forms() assert ( forms[package.MATH_FORM](r"x_i^2 \le \dfrac12") == papermath.canonical(r"x_i^2 \le \dfrac12").tex ) def test_the_default_forms_are_none_without_papermath(monkeypatch): def no_papermath(name): raise ImportError(name) monkeypatch.setattr(package.importlib, "import_module", no_papermath) assert package.default_forms() == {} def test_the_default_form_spells_with_papermaths_canonical(monkeypatch): class Canon: def __init__(self, tex): self.tex = tex fake = types.SimpleNamespace(canonical=lambda s: Canon(s.replace(" ", ""))) monkeypatch.setattr(package.importlib, "import_module", lambda name: fake) assert package.default_forms()[package.MATH_FORM]("x + 1") == "x+1" def test_validate_zip_passes_the_forms_on(tmp_path): root = with_form(tmp_path) archive = tmp_path / "form.sct.zip" package.write_zip(root, archive) report = package.validate_zip(archive, store(), forms={package.MATH_FORM: spell}) assert "doc.math.form" in rules_of(report)