clone

a browser-first diagram editor with live svg previews, local drafts and encrypted, accountless sharing

main
diagramzip / renderers / python / blockdiag-family / test / test_render.py
test_render.py 3.0K
1from pathlib import Path23import pytest45from protocol import ProtocolError, RenderInput6from render import FONT_PATH, render_svg7from svg import sanitize_and_decorate8910FIXTURES = Path(__file__).parents[4] / "examples" / "diagrams"11ENGINE_FIXTURES = {12 "blockdiag": "kroki.diag",13 "seqdiag": "sequence.diag",14 "actdiag": "actions.diag",15 "nwdiag": "network.diag",16 "packetdiag": "packet.diag",17 "rackdiag": "rack.diag",18}192021@pytest.mark.parametrize(("engine", "filename"), ENGINE_FIXTURES.items())22def test_renders_each_family_member_without_a_process(engine, filename):23 source = (FIXTURES / filename).read_text()24 svg, version = render_svg(engine, source)2526 assert "<svg " in svg27 assert "viewBox" in svg28 assert len(svg) > 50029 assert version303132def test_sanitizes_and_applies_presentation():33 source = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10"><script>alert(1)</script><a href="https://example.com"><text>safe</text></a></svg>'34 request = RenderInput("source", {}, "Title", "Description", "#abcdef", 5, True)35 svg = sanitize_and_decorate(source, request)3637 assert "<script" not in svg38 assert "https://example.com" not in svg39 assert 'viewBox="-5 -5 30 20"' in svg40 assert "Title" in svg41 assert "#abcdef" in svg424344def test_uses_bundled_font_and_preserves_upstream_size_option():45 assert FONT_PATH.is_file()46 svg, _ = render_svg("blockdiag", "blockdiag { A -> B; }", {"size": "320x240"})4748 assert 'width="320"' in svg49 assert 'height="240"' in svg50 assert "viewBox=" in svg515253def test_preserves_upstream_svg_doctype_option():54 source = "blockdiag { A -> B; }"55 default_svg, _ = render_svg("blockdiag", source)56 no_doctype_svg, _ = render_svg("blockdiag", source, {"no-doctype": "true"})5758 assert "<!DOCTYPE svg" in default_svg59 assert "<!DOCTYPE svg" not in no_doctype_svg606162def test_png_only_options_do_not_change_svg_output():63 source = "blockdiag { A -> B; }"64 default_svg, _ = render_svg("blockdiag", source)65 antialias_svg, _ = render_svg("blockdiag", source, {"antialias": "true"})66 transparent_svg, _ = render_svg("blockdiag", source, {"no-transparency": "true"})6768 assert antialias_svg == default_svg69 assert transparent_svg == default_svg707172@pytest.mark.parametrize("value", ["320", "320x", "-1x20", "20x20x20"])73def test_rejects_invalid_size_option(value):74 with pytest.raises(ProtocolError, match="WIDTHxHEIGHT"):75 render_svg("blockdiag", "blockdiag { A -> B; }", {"size": value})767778def test_rejects_remote_and_filesystem_images():79 with pytest.raises(ProtocolError, match="diagram images are not supported"):80 render_svg("blockdiag", 'blockdiag { A [background = "https://example.com/image.png"]; }')818283def test_builtin_autoclass_plugin_is_statically_registered():84 svg, _ = render_svg("blockdiag", """blockdiag {85 plugin autoclass;86 class emphasis [style = dashed, color = red];87 A_emphasis -> B_emphasis;88 }""")8990 assert "A" in svg91 assert "B" in svg92 assert "dasharray" in svg