File: test_gradients.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 (83 lines) | stat: -rw-r--r-- 3,587 bytes parent folder | download
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
#!/usr/bin/env python
# coding:utf-8
# Author:  mozman --<mozman@gmx.at>
# Purpose: test gradients module
# Created: 26.10.2010
# Copyright (C) 2010, Manfred Moitzi
# License: MIT License

import unittest
import re

from svgwrite.gradients import _GradientStop, LinearGradient, RadialGradient


class TestGradientStop(unittest.TestCase):
    def test_constructor1(self):
        stop = _GradientStop(offset=0.5, color='red', opacity=1.0, debug=True, profile='full')
        self.assertEqual(stop.tostring(), '<stop offset="0.5" stop-color="red" stop-opacity="1.0" />')

    def test_constructor2(self):
        stop = _GradientStop(offset='50%', color='red', opacity=0.63, debug=True, profile='full')
        self.assertEqual(stop.tostring(), '<stop offset="50%" stop-color="red" stop-opacity="0.63" />')

    def test_constructor3(self):
        stop = _GradientStop(debug=True, profile='full')
        self.assertEqual(stop.tostring(), '<stop />')


class TestLinearGradient(unittest.TestCase):
    def test_constructor(self):
        lg = LinearGradient(start=(1, 2), end=(10, 20), inherit='#test', debug=True, profile='full')
        self.assertEqual(
            '<linearGradient x1="1" x2="10" xlink:href="#test" y1="2" y2="20" />',
            lg.tostring())

    def test_get_paint_server(self):
        lg = LinearGradient()
        self.assertTrue(re.match(r"^url\(#id\d+\) none$", lg.get_paint_server()))
        self.assertTrue(re.match(r"^url\(#id\d+\) red$", lg.get_paint_server(default='red')))

    def test_add_stop_color(self):
        lg = LinearGradient()
        lg.add_stop_color(offset=0.5, color='red', opacity=1.0)
        self.assertEqual(lg.tostring(),
                         '<linearGradient><stop offset="0.5" stop-color="red" stop-opacity="1.0" /></linearGradient>')

    def test_add_colors(self):
        lg = LinearGradient()
        lg.add_colors(['white', 'red', 'blue', 'green'], opacity=0.5)
        result = '<linearGradient>' \
                 '<stop offset="0.0" stop-color="white" stop-opacity="0.5" />' \
                 '<stop offset="0.333" stop-color="red" stop-opacity="0.5" />' \
                 '<stop offset="0.667" stop-color="blue" stop-opacity="0.5" />' \
                 '<stop offset="1.0" stop-color="green" stop-opacity="0.5" />' \
                 '</linearGradient>'
        self.assertEqual(lg.tostring(), result)

    def test_inherit(self):
        inherit_from = LinearGradient(id='test')
        lg = LinearGradient(inherit=inherit_from)
        self.assertTrue('<linearGradient xlink:href="#test"/>', lg.tostring())


class TestRadialGradient(unittest.TestCase):
    def test_constructor(self):
        rg = RadialGradient(center=(10, 20), r=10, focal=(15, 25), inherit='#test', debug=True, profile='full')
        self.assertEqual(rg.tostring(),
                         '<radialGradient cx="10" cy="20" fx="15" fy="25" r="10" xlink:href="#test" />')

    def test_get_paint_server(self):
        rg = RadialGradient()
        self.assertTrue(re.match(r"^url\(#id\d+\) none$", rg.get_paint_server()))
        self.assertTrue(re.match(r"^url\(#id\d+\) red$", rg.get_paint_server(default='red')))

    def test_add_stop_color(self):
        rg = RadialGradient()
        rg.add_stop_color(offset=0.5, color='red', opacity=1.0)
        self.assertEqual(rg.tostring(),
                         '<radialGradient><stop offset="0.5" stop-color="red" stop-opacity="1.0" /></radialGradient>')


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