Files
orisys-sensor-design/tests/test_stroke_pattern.py
T
ykcheungab e8d231ec39 改用相机网格生成笔画纹理
统一笔画配置与彩色 patch 的投影网格,替换旧脚本及产物。
已验证 Blender 导出、16 项相关测试、文档与 lint 门禁。
2026-10-06 10:51:56 +08:00

55 lines
1.8 KiB
Python

"""Host and Blender stroke configuration must stay in sync."""
from __future__ import annotations
import ast
import json
from pathlib import Path
import pytest
import yaml
from orisys_sensor_design import stroke_pattern
from orisys_sensor_design.manifest import repo_root
SCRIPT = repo_root() / "blender" / "scripts" / "camera_grid_stroke.py"
def _constant(name: str):
tree = ast.parse(SCRIPT.read_text(encoding="utf-8"))
for node in tree.body:
if isinstance(node, ast.Assign) and any(
isinstance(target, ast.Name) and target.id == name for target in node.targets
):
return ast.literal_eval(node.value)
raise AssertionError(name)
def test_generator_contract():
assert _constant("DEFAULT_PARAMS") == stroke_pattern.STROKE_DEFAULTS
assert _constant("MODE") == stroke_pattern.STROKE_MODE
@pytest.mark.parametrize("sensor", ["finger", "finger-xynova"])
def test_revision_settings(sensor):
path = repo_root() / "sensors" / sensor / "r01" / "design.yaml"
block = yaml.safe_load(path.read_text(encoding="utf-8"))["stroke_pattern"]
assert stroke_pattern.resolve_stroke_settings(block) == stroke_pattern.STROKE_DEFAULTS
@pytest.mark.parametrize("block", [
{"width_min_mm": 0.2}, {"width_ratio": 0}, {"width_ratio": 0.9},
{"length_ratio": 1}, {"seed": 1.5}, {"proposals": 0},
{"orientation_mode": "unknown"},
])
def test_invalid_settings(block):
assert stroke_pattern.validate_stroke_block(block)
with pytest.raises(stroke_pattern.StrokePatternConfigError):
stroke_pattern.resolve_stroke_settings(block)
def test_params_round_trip(tmp_path: Path):
settings = stroke_pattern.resolve_stroke_settings({"seed": 13})
path = stroke_pattern.write_stroke_params(settings, tmp_path)
assert json.loads(path.read_text(encoding="utf-8")) == settings