-
Notifications
You must be signed in to change notification settings - Fork 726
Expand file tree
/
Copy pathtest_svg_dimension.py
More file actions
51 lines (40 loc) · 1.59 KB
/
test_svg_dimension.py
File metadata and controls
51 lines (40 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations
import io
import re
from typing import TYPE_CHECKING
import pytest
import qrcode
from qrcode.image import svg
from qrcode.tests.consts import UNICODE_TEXT
if TYPE_CHECKING:
from qrcode.image.base import BaseImageWithDrawer
@pytest.mark.parametrize(
"image_factory",
[
svg.SvgFragmentImage,
svg.SvgImage,
svg.SvgFillImage,
svg.SvgPathImage,
svg.SvgPathFillImage,
],
)
def test_svg_no_width_height(image_factory: BaseImageWithDrawer) -> None:
"""Test that SVG output doesn't have width and height attributes."""
qr = qrcode.QRCode()
qr.add_data(UNICODE_TEXT)
# Create a svg with the specified factory and (optional) module drawer
img = qr.make_image(image_factory=image_factory)
svg_str = img.to_string().decode("utf-8")
# Check that width and height attributes are not present in the SVG tag
svg_tag_match = re.search(r"<svg[^>]*>", svg_str)
assert svg_tag_match, "SVG tag not found"
svg_tag = svg_tag_match.group(0)
assert "width=" not in svg_tag, "width attribute should not be present"
assert "height=" not in svg_tag, "height attribute should not be present"
# Check that viewBox is present and uses pixels (no mm suffix)
viewbox_match = re.search(r'viewBox="([^"]*)"', svg_tag)
assert viewbox_match, "viewBox attribute not found"
viewbox = viewbox_match.group(1)
assert "mm" not in viewbox, "viewBox should use pixels, not mm"
# Check that inner elements use pixels (no mm suffix)
assert "mm" not in svg_str, "SVG elements should use pixels, not mm"