summaryrefslogtreecommitdiffstats
path: root/share/extensions/tests/test_dxf_outlines.py
blob: 9639c287dfc8abf8c9fe4d490e0ed9ddc4d6208f (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding=utf-8
from io import BytesIO
from dxf_outlines import DxfOutlines
from inkex.tester import ComparisonMixin, InkscapeExtensionTestMixin, TestCase
from inkex.tester.filters import WindowsTextCompat
from inkex.elements._parser import load_svg

from inkex.utils import AbortExtension
from inkex.base import SvgOutputMixin
from inkex.elements import Rectangle


class DFXOutlineBasicTest(ComparisonMixin, InkscapeExtensionTestMixin, TestCase):
    effect_class = DxfOutlines
    comparisons = [
        (),
        ("--id=p1", "--id=r3"),
        ("--POLY=true",),
        ("--ROBO=true",),
    ]
    compare_filters = [WindowsTextCompat()]


def run_extension(document, *args) -> str:
    output = BytesIO()
    ext = DxfOutlines()
    ext.parse_arguments([*args])
    ext.svg = document.getroot()
    ext.document = document
    ext.effect()
    ext.save(output)
    output.seek(0)
    return output.read()


class DXFDeeplyNestedTest(TestCase):
    """Check that a deeply nested SVG raises an AbortExtension"""

    @staticmethod
    def create_deep_svg(amount):
        """Create a very deep svg and test getting ancestors"""
        svg = '<svg xmlns="http://www.w3.org/2000/svg">'
        for i in range(amount):
            svg += f'<g id="{i}">'
        svg = load_svg(svg + ("</g>" * amount) + "</svg>")
        return svg

    def test_deeply_nested(self):
        "Run test"
        with self.assertRaisesRegex(AbortExtension, "Deep Ungroup"):
            run_extension(self.create_deep_svg(1500))


class TestDxfUnits(TestCase):
    """Test ensuring that units work properly"""

    def test_mm(self):
        """Test that the documents created with/without scaling and base units are
        identical."""
        document = SvgOutputMixin.get_template(width=210, height=297, unit="mm")
        document.getroot().namedview.set("inkscape:document-units", "mm")
        document.getroot().add(Rectangle.new(200, 0, 10, 16))
        out1 = run_extension(document)

        document = SvgOutputMixin.get_template(width=210, height=297, unit="mm")
        document.getroot().add(Rectangle.new(200, 0, 10, 16))
        out2 = run_extension(document, "--unit_from_document=False", "--units=mm")

        self.assertEqual(out1, out2)
        # Now with scaling - should result in the same document
        document = SvgOutputMixin.get_template(width=210, height=297, unit="mm")
        document.getroot().set("viewBox", "0 0 105 148.5")
        document.getroot().add(Rectangle.new(100, 0, 5, 8))
        out3 = run_extension(document, "--unit_from_document=False", "--units=mm")

        self.assertEqual(out1, out3)