summaryrefslogtreecommitdiff
path: root/tests/test_output.py
blob: 476b73c74cb82bd539ff5511e560e95e3a087ee7 (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
from inkex.tester import TestCase
from inkex import SvgDocumentElement, Rectangle
from inkex.tester.svg import svg

from lib.svg.tags import INKSTITCH_ATTRIBS
from lib import output
from lib.stitch_plan.stitch_plan import StitchPlan, stitch_groups_to_stitch_plan
from lib.elements.utils import node_to_elements


class OutputTest(TestCase):
    def _get_output(self, svg: SvgDocumentElement, format: str) -> bytes:
        # TODO: support SVGs with more than one element
        # (turn InkstitchExtension.elements_to_stitch_groups into a function so we can use it here?)
        assert len(svg) == 1
        [element] = node_to_elements(svg[0])
        stitch_groups = element.embroider(None)
        stitch_plan = stitch_groups_to_stitch_plan(stitch_groups)
        path = self.temp_file(suffix=f".{format}")
        output.write_embroidery_file(path, stitch_plan, svg)
        with open(path, "rb") as f:
            return f.read()

    def test_jef_output_does_not_change(self):
        root: SvgDocumentElement = svg()
        rect = root.add(
            Rectangle(
                attrib={
                    "width": "10",
                    "height": "10",
                }
            )
        )
        output1 = self._get_output(root, "jef")
        output2 = self._get_output(root, "jef")
        assert output1 == output2

    def test_jef_output_includes_trims(self):
        root: SvgDocumentElement = svg()
        rect = root.add(
            Rectangle(
                attrib={
                    "width": "10",
                    "height": "10",
                }
            )
        )
        output1 = self._get_output(root, "jef")

        rect.attrib[INKSTITCH_ATTRIBS["trim_after"]] = "true"

        output2 = self._get_output(root, "jef")
        assert output1 != output2