# tests/test_block_specs.py """block_define request validation: refuse the whole request before any write.""" from __future__ import annotations import pytest from backends.block_specs import ( ALLOWED_TYPES, solid_vertices, validate_attdef_specs, validate_entity_specs, ) def test_allowed_types_are_the_documented_six(): assert ALLOWED_TYPES == ("line", "circle", "arc", "polyline", "text", "solid") def test_empty_entities_is_refused(): with pytest.raises(ValueError, match="empty"): validate_entity_specs([]) def test_each_type_normalises_to_floats_and_layer_zero(): out = validate_entity_specs( [ {"type": "line", "x1": 0, "y1": 0, "x2": 1, "y2": 0}, {"type": "circle", "cx": 0, "cy": 0, "r": 2}, {"type": "arc", "cx": 0, "cy": 0, "r": 2, "start_deg": 0, "end_deg": 180}, {"type": "polyline", "points": [[0, 0], [1, 0], [1, 1]], "closed": True}, {"type": "text", "text": "M", "x": 0, "y": 0, "height": 2}, {"type": "solid", "points": [[0, 0], [1, 0], [0, 1]]}, ] ) assert [e["type"] for e in out] == list(ALLOWED_TYPES) assert out[0] == {"type": "line", "layer": "0", "x1": 0.0, "y1": 0.0, "x2": 1.0, "y2": 0.0} assert out[3]["points"] == [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)] assert out[3]["closed"] is True and out[3]["bulges"] is None assert out[4]["align"] == "left" and out[4]["rotation_deg"] == 0.0 assert out[5]["points"] == [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0)] @pytest.mark.parametrize( "bad, fragment", [ ({"type": "square"}, "type"), ({"type": "line", "x1": 0, "y1": 0, "x2": 1}, "y2"), ({"type": "circle", "cx": 0, "cy": 0, "r": 0}, "r"), ({"type": "circle", "cx": 0, "cy": 0, "r": float("nan")}, "finite"), ({"type": "text", "text": "M", "x": 0, "y": 0, "height": -1}, "height"), ({"type": "text", "text": 5, "x": 0, "y": 0, "height": 1}, "text"), ({"type": "text", "text": "A\nB", "x": 0, "y": 0, "height": 1}, "'text'"), ({"type": "text", "text": "A\rB", "x": 0, "y": 0, "height": 1}, "'text'"), ({"type": "text", "text": "A\x01B", "x": 0, "y": 0, "height": 1}, "'text'"), ({"type": "text", "text": "10^", "x": 0, "y": 0, "height": 1}, "'text'"), ({"type": "text", "text": "M", "x": 0, "y": 0, "height": 1, "align": "top"}, "align"), ({"type": "polyline", "points": [[0, 0]]}, "points"), ({"type": "polyline", "points": [[0, 0], [1]]}, "points[1]"), ({"type": "polyline", "points": [[0, 0], [1, 1]], "bulges": [0.5]}, "bulges"), ({"type": "polyline", "points": [[0, 0], [1, 1]], "closed": "yes"}, "closed"), ({"type": "solid", "points": [[0, 0], [1, 0]]}, "points"), ({"type": "solid", "points": [[0, 0], [1, 0], [1, 1], [0, 1], [2, 2]]}, "at most 4"), ({"type": "line", "x1": 0, "y1": 0, "x2": 1, "y2": 0, "layer": ""}, "layer"), ({"type": "line", "x1": 0, "y1": 0, "x2": 1, "y2": 0, "layer": "BAD