File: test_transform.py

package info (click to toggle)
svgwrite 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,092 kB
  • sloc: python: 12,603; makefile: 117; sh: 5
file content (126 lines) | stat: -rw-r--r-- 3,634 bytes parent folder | download | duplicates (2)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
#coding:utf-8
# Author:  mozman --<mozman@gmx.at>
# Purpose: test ITransform interface
# Created: 25.09.2010
# Copyright (C) 2010, Manfred Moitzi
# License: MIT License

import sys
import unittest

from svgwrite.params import Parameter
from svgwrite.base import BaseElement
from svgwrite.mixins import Transform

class Mock(BaseElement, Transform):
    elementname = 'g'
    _parameter = Parameter(True, 'full')

class TestITransfer(unittest.TestCase):
    def test_mock_class(self):
        m = Mock()
        self.assertEqual(m.tostring(), '<g />')

    def test_translate_tx(self):
        m = Mock()
        m.translate(tx=10)
        self.assertEqual(m.tostring(), '<g transform="translate(10)" />')

    def test_translate_tx_ty(self):
        m = Mock()
        # strings allowed
        m.translate(tx='10', ty=20)
        self.assertEqual(m.tostring(), '<g transform="translate(10,20)" />')

    def test_translate_err(self):
        m = Mock()
        # no units allowed
        self.assertRaises(TypeError, m.translate, '10cm')

    def test_rotate(self):
        m = Mock()
        m.rotate(angle=30)
        self.assertEqual(m.tostring(), '<g transform="rotate(30)" />')

    def test_rotate_center(self):
        m = Mock()
        # strings allowed
        m.rotate(angle='30', center=(1,2))
        self.assertEqual(m.tostring(), '<g transform="rotate(30,1,2)" />')

    def test_rotate_err(self):
        m = Mock()
        # no units allowed
        self.assertRaises(TypeError, m.rotate, '30deg')
        self.assertRaises(TypeError, m.rotate, '30', center=('1cm', '1cm'))

    def test_scale_sx(self):
        m = Mock()
        m.scale(sx=3)
        self.assertEqual(m.tostring(), '<g transform="scale(3)" />')

    def test_scale_sx_sy(self):
        m = Mock()
        m.scale(sx='3', sy=2)
        self.assertEqual(m.tostring(), '<g transform="scale(3,2)" />')

    def test_scale_err(self):
        m = Mock()
        # no units allowed
        self.assertRaises(TypeError, m.scale, '3cm')
        self.assertRaises(TypeError, m.scale, '3', '2cm')

    def test_skewX(self):
        m = Mock()
        m.skewX(angle=30)
        self.assertEqual(m.tostring(), '<g transform="skewX(30)" />')

    def test_skewX_str(self):
        m = Mock()
        m.skewX(angle='30')
        self.assertEqual(m.tostring(), '<g transform="skewX(30)" />')

    def test_skewX_err(self):
        m = Mock()
        # no units allowed
        self.assertRaises(TypeError, m.skewX, '3deg')

    def test_skewY(self):
        m = Mock()
        m.skewY(angle=30)
        self.assertEqual(m.tostring(), '<g transform="skewY(30)" />')

    def test_skewY_str(self):
        m = Mock()
        m.skewY(angle='30')
        self.assertEqual(m.tostring(), '<g transform="skewY(30)" />')

    def test_skewY_err(self):
        m = Mock()
        # no units allowed
        self.assertRaises(TypeError, m.skewY, '3deg')

    def test_matrix(self):
        m = Mock()
        m.matrix(1,2,3,4,5,6)
        self.assertEqual(m.tostring(), '<g transform="matrix(1,2,3,4,5,6)" />')

    def test_matrix_too_few_params(self):
        m = Mock()
        self.assertRaises(TypeError, m.matrix, 1, 2, 3, 4, 5)

    def test_combine_tranformation(self):
        m = Mock()
        m.translate(10,20)
        m.scale(2,2)
        self.assertEqual(m.tostring(), '<g transform="translate(10,20) scale(2,2)" />')

    def test_delete_transformation(self):
        m = Mock()
        m.translate(10,20)
        del m.attribs['transform']
        self.assertEqual(m.tostring(), '<g />')

if __name__=='__main__':
    unittest.main()