diff --git a/src/lpdf/L.py b/src/lpdf/L.py index fdc1d8d..a6ca017 100644 --- a/src/lpdf/L.py +++ b/src/lpdf/L.py @@ -108,7 +108,7 @@ def to_xml(document: PdfDocument) -> str: @staticmethod def document( attrs: DocumentAttr | None = None, - sections: list[SectionNode] | None = None, + nodes: list[SectionNode] | None = None, ) -> PdfDocument: """Build the root document node.""" doc_attrs: dict = options_to_attrs(attrs, skip=("assets", "tokens", "meta", "output", "attachments")) @@ -122,7 +122,7 @@ def document( doc_attrs["output"] = attrs.output.to_dict() if attrs is not None and attrs.attachments is not None: doc_attrs["attachments"] = [options_to_attrs(file) for file in attrs.attachments] - return PdfDocument(doc_attrs, sections or []) + return PdfDocument(doc_attrs, nodes or []) @staticmethod def section( @@ -134,7 +134,7 @@ def section( return SectionNode(options_to_attrs(attrs), nodes or []) @staticmethod - def layout(attrs: LayoutAttr | None, nodes: list | None = None) -> SectionLayout: + def layout(attrs: LayoutAttr | None = None, nodes: list | None = None) -> SectionLayout: """Wrap layout nodes into a layout block, which keeps them inside its margin.""" return SectionLayout(options_to_attrs(attrs), nodes or []) diff --git a/src/lpdf/engine/engine_options.py b/src/lpdf/engine/engine_options.py index 540762c..096da36 100644 --- a/src/lpdf/engine/engine_options.py +++ b/src/lpdf/engine/engine_options.py @@ -18,13 +18,17 @@ class EngineOptions: ``max_memory_mb`` is the megabytes of memory a render may allocate, its data included, before it stops with an ``EngineException`` that names the cap: 1024 when left out, from 1 to 4096. - :raises ValueError: when ``max_memory_mb`` is not a whole number from 1 to 4096. + :raises ValueError: when ``timeout`` is not a whole number from 0, or ``max_memory_mb`` is not a whole number from 1 + to 4096. """ wasm_binary: str | None = None timeout: int | None = None max_memory_mb: int | None = None def __post_init__(self) -> None: + seconds = self.timeout + if seconds is not None and (isinstance(seconds, bool) or not isinstance(seconds, int) or seconds < 0): + raise ValueError(f"timeout must be a whole number of seconds, 0 for no limit, not {seconds}") mb = self.max_memory_mb if mb is not None and (isinstance(mb, bool) or not isinstance(mb, int) or not 1 <= mb <= _MOST_MEMORY_MB): raise ValueError(f"max_memory_mb must be a whole number of megabytes from 1 to {_MOST_MEMORY_MB}, not {mb}") diff --git a/tests/test_kit.py b/tests/test_kit.py index d3459eb..7fa62ba 100644 --- a/tests/test_kit.py +++ b/tests/test_kit.py @@ -82,6 +82,13 @@ def test_layout_wrapper(): assert "nodes" in d +def test_a_layout_needs_no_attributes(): + assert L.layout().to_dict() == {"type": "layout", "attrs": {}, "nodes": []} + assert L.layout(nodes=[L.text(NoAttr, ["hi"])]).to_dict()["nodes"] == [ + {"type": "text", "attrs": {}, "nodes": ["hi"]}, + ] + + def test_layout_margin(): layout = L.layout(LayoutAttr(margin="20pt 30pt"), []) assert layout.to_dict()["attrs"] == {"margin": "20pt 30pt"} @@ -110,6 +117,15 @@ def test_document_includes_version(): assert d["type"] == "document" +def test_a_document_takes_its_sections_as_nodes(): + section = L.section(NoAttr, [L.layout()]) + by_position = L.document(DocumentAttr(size="A4"), [section]).to_dict() + by_name = L.document(DocumentAttr(size="A4"), nodes=[section]).to_dict() + + assert by_name == by_position + assert [n["type"] for n in by_name["nodes"]] == ["section"] + + def test_document_with_meta_and_tokens(): doc = L.document( DocumentAttr( diff --git a/tests/test_wasm_runner.py b/tests/test_wasm_runner.py index 583aa1c..3f5640b 100644 --- a/tests/test_wasm_runner.py +++ b/tests/test_wasm_runner.py @@ -3,6 +3,7 @@ as the engine's binary: the real engine renders the largest fixture in well under a second.""" import hashlib +import re import tempfile import threading import time @@ -106,6 +107,13 @@ def test_a_render_with_a_timeout_of_0_runs_with_no_limit(tmp_path: Path) -> None assert elapsed >= 2 +@pytest.mark.parametrize("seconds", [-1, -60, 1.5, float("nan"), "5", True]) +def test_a_timeout_that_is_not_a_whole_number_from_0_is_refused(seconds: object) -> None: + says = f"timeout must be a whole number of seconds, 0 for no limit, not {seconds}" + with pytest.raises(ValueError, match=f"^{re.escape(says)}$"): + EngineOptions(timeout=seconds) # type: ignore[arg-type] # the wrong types are the point + + def test_a_render_stops_after_5_seconds_when_the_timeout_is_left_out(tmp_path: Path) -> None: engine = PdfEngine(EngineOptions(wasm_binary=_binary(tmp_path, SPIN_FOREVER)))