File: test_text.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 (114 lines) | stat: -rw-r--r-- 4,617 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
#!/usr/bin/env python
#coding:utf-8
# Author:  mozman --<mozman@gmx.at>
# Purpose: test text module
# Created: 25.09.2010
# Copyright (C) 2010, Manfred Moitzi
# License: MIT License
from __future__ import unicode_literals

import sys
import unittest

from svgwrite.utils import PYTHON3, to_unicode
from svgwrite.text import TSpan, TRef, TextPath


class TestTSpan(unittest.TestCase):
    def test_constructor(self):
        txt = TSpan('testtext')
        self.assertEqual(txt.tostring(), '<tspan>testtext</tspan>')

    def test_insert(self):
        txt = TSpan('testtext', insert=(1,1))
        self.assertEqual(txt.tostring(), '<tspan x="1" y="1">testtext</tspan>')

    def test_subelement(self):
        txt = TSpan('testtext')
        txt.add(TSpan('subtext1'))
        txt.add(TSpan('subtext2'))
        self.assertEqual(txt.tostring(), '<tspan>testtext<tspan>subtext1</tspan><tspan>subtext2</tspan></tspan>')

    def test_x_values(self):
        txt = TSpan('text', x=[1, 2, 3, 4])
        self.assertEqual(txt.tostring(), '<tspan x="1 2 3 4">text</tspan>')

    def test_y_values(self):
        txt = TSpan('text', y=[1, 2, 3, 4])
        self.assertEqual(txt.tostring(), '<tspan y="1 2 3 4">text</tspan>')

    def test_dx_values(self):
        txt = TSpan('text', dx=[1, 2, 3, 4])
        self.assertEqual(txt.tostring(), '<tspan dx="1 2 3 4">text</tspan>')

    def test_dy_values(self):
        txt = TSpan('text', dy=[1, 2, 3, 4])
        self.assertEqual(txt.tostring(), '<tspan dy="1 2 3 4">text</tspan>')

    def test_rotate_values(self):
        txt = TSpan('text', rotate=[1, 2, 3, 4])
        self.assertEqual(txt.tostring(), '<tspan rotate="1 2 3 4">text</tspan>')

    def test_subelement_tspan(self):
        txt = TSpan('text')
        txt.add(TSpan('subtext'))
        self.assertEqual(txt.tostring(), '<tspan>text<tspan>subtext</tspan></tspan>')

    def test_non_us_ascii_chars(self):
        txt = TSpan('öäü')
        self.assertEqual(txt.tostring(), to_unicode('<tspan>öäü</tspan>'))

    def test_errors(self):
        # None for x, y, dx, dy, rotate is valid - willl be ignored
        self.assertRaises(TypeError, TSpan,"txt", x=1)
        self.assertRaises(TypeError, TSpan,"txt", y=1)
        self.assertRaises(TypeError, TSpan,"txt", dx=1)
        self.assertRaises(TypeError, TSpan,"txt", dy=1)
        self.assertRaises(TypeError, TSpan,"txt", rotate=1)

    def test_insert_errors(self):
        self.assertRaises(TypeError, TSpan, "txt", insert=1)
        self.assertRaises(TypeError, TSpan, "txt", insert='1')
        # do not use 'insert' and 'x' or 'y' at the same time
        self.assertRaises(ValueError, TSpan, "txt", insert=(1,1), x=[1])
        self.assertRaises(ValueError, TSpan, "txt", insert=(1,1), y=[1])

    def test_text_decoration(self):
        with self.assertRaises(TypeError):
            TSpan('text', text_decoration='xxx')
        self.assertTrue(TSpan('text', text_decoration='none'))
        self.assertTrue(TSpan('text', text_decoration='inherit'))
        self.assertTrue(TSpan('text', text_decoration='underline'))
        self.assertTrue(TSpan('text', text_decoration='overline'))
        self.assertTrue(TSpan('text', text_decoration='line-through'))
        self.assertTrue(TSpan('text', text_decoration='blink'))

        # multiple decoration are allowed
        self.assertTrue(TSpan('text', text_decoration='underline blink overline'))

        with self.assertRaises(TypeError):  # can not mix none with decoration styles
            TSpan('text', text_decoration='none underline')

        with self.assertRaises(TypeError):  # can not mix inherit with decoration styles
            TSpan('text', text_decoration='inherit underline')


class TestTRef(unittest.TestCase):
    def test_constructor(self):
        tref = TRef('#test')
        self.assertEqual(tref.tostring(), '<tref xlink:href="#test" />')


class TestTextPath(unittest.TestCase):
    def test_constructor(self):
        tref = TextPath('#test', 'The Text', startOffset=10, spacing='auto', method='stretch')
        self.assertEqual(tref.tostring(), '<textPath method="stretch" spacing="auto"' \
                         ' startOffset="10" xlink:href="#test">The Text</textPath>')

    def test_subelement_tspan(self):
        txt = TextPath('#test', 'text')
        txt.add(TSpan('subtext'))
        self.assertEqual(txt.tostring(), '<textPath xlink:href="#test">text<tspan>subtext</tspan></textPath>')

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