summaryrefslogtreecommitdiffstats
path: root/share/extensions/tests/test_inkex_utils.py
blob: 8917e2326cd8f3b57201df3ade66c64ec937b9e9 (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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# coding=utf-8
"""
Unit test file for ../inkex.py
"""
# Revision history:
#   * 2012-01-27 (jazzynico): check errormsg function.
#
from __future__ import absolute_import, print_function

from argparse import ArgumentTypeError

import pytest

from inkex.utils import (
    debug,
    errormsg,
    filename_arg,
    Boolean,
    parse_percent,
    to,
    strargs,
    math_eval,
    is_number,
)
from inkex.tester import TestCase

from inkex import addNS


class TestInkexBasic(object):
    """Test basic utiltiies of inkex"""

    def test_boolean(self):
        """Inkscape boolean input"""
        assert Boolean("TRUE") is True
        assert Boolean("true") is True
        assert Boolean("True") is True
        assert Boolean("FALSE") is False
        assert Boolean("false") is False
        assert Boolean("False") is False
        assert Boolean("Banana") is None

    def test_debug(self, capsys):
        """Debug messages go to stderr"""
        debug("Hello World")
        assert capsys.readouterr().err == "Hello World\n"

    def test_to(self):
        """Decorator for generators"""

        @to(list)
        def mylist(a, b, c):
            """Yield as a list"""
            yield a
            yield c
            yield b

        assert isinstance(mylist(1, 2, 3), list)
        assert mylist(1, 2, 3) == [1, 3, 2]

        @to(dict)
        def mydict(a, b, c):
            """Yield as a dictionary"""
            yield ("age", a)
            yield ("name", c)
            yield ("home", b)

        assert isinstance(mydict(1, 2, 3), dict)
        assert mydict(1, 2, 3) == {"age": 1, "name": 3, "home": 2}

    def test_filename(self):
        """Filename argument input"""
        assert filename_arg(__file__) == __file__
        with pytest.raises(ArgumentTypeError):
            filename_arg("doesntexist.txt")

    def test_add_ns(self):
        """Test addNS function"""
        assert (
            addNS("inkscape:foo") == "{http://www.inkscape.org/namespaces/inkscape}foo"
        )
        assert (
            addNS("bar", "inkscape")
            == "{http://www.inkscape.org/namespaces/inkscape}bar"
        )
        assert addNS("url", "rdf") == "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}url"
        assert (
            addNS("{http://www.inkscape.org/namespaces/inkscape}bar")
            == "{http://www.inkscape.org/namespaces/inkscape}bar"
        )
        assert (
            addNS("http://www.inkscape.org/namespaces/inkscape:bar")
            == "{http://www.inkscape.org/namespaces/inkscape}bar"
        )
        assert (
            addNS("car", "http://www.inkscape.org/namespaces/inkscape")
            == "{http://www.inkscape.org/namespaces/inkscape}car"
        )
        assert (
            addNS("{http://www.inkscape.org/namespaces/inkscape}bar", "rdf")
            == "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}bar"
        )

    def test_strargs(self):
        """Test strargs function"""
        assert strargs("1.0 2.0 3.0 4.0") == [1.0, 2.0, 3.0, 4.0]
        assert strargs("1 -2 3 -4") == [1.0, -2.0, 3.0, -4.0]
        assert strargs("1,-2,3,-4") == [1.0, -2.0, 3.0, -4.0]
        assert strargs("1-2 3-4") == [1.0, -2.0, 3.0, -4.0]
        assert strargs("1-2,3-4") == [1.0, -2.0, 3.0, -4.0]
        assert strargs("1-2-3-4") == [1.0, -2.0, -3.0, -4.0]
        assert strargs("+1 +2") == [1.0, 2.0]
        assert strargs("1.2.3.4.5") == [1.2, 0.3, 0.4, 0.5]
        assert strargs("1. 2..3.4") == [1.0, 2.0, 0.3, 0.4]
        assert strargs("1.0e-1 -2.0e-1 3.0e10 -4.0e10") == [
            0.1,
            -0.2,
            30000000000,
            -40000000000,
        ]
        assert strargs("1.0e-1,-2.0e-1,3.0e10,-4.0e10") == [
            0.1,
            -0.2,
            30000000000,
            -40000000000,
        ]
        assert strargs("1.0E-1 -2.0E-1 3.0E10 -4.0E10") == [
            0.1,
            -0.2,
            30000000000,
            -40000000000,
        ]
        assert strargs("1.0E-1,-2.0E-1,3.0E10,-4.0E10") == [
            0.1,
            -0.2,
            30000000000,
            -40000000000,
        ]
        assert strargs("1.E2+.3E+4") == [100, 3000]

    def test_ascii(self, capsys):
        """Parse ABCabc"""
        errormsg("ABCabc")
        assert capsys.readouterr().err == "ABCabc\n"

    def test_nonunicode_latin1(self, capsys):
        # Py2 has issues with unicode in docstrings.   *sigh*
        # """Parse Àûïàèé"""
        errormsg("Àûïàèé")
        assert capsys.readouterr().err, "Àûïàèé\n"

    def test_unicode_latin1(self, capsys):
        # Py2 has issues with unicode in docstrings.   *sigh*
        # """Parse Àûïàèé (unicode)"""
        errormsg("Àûïàèé")
        assert capsys.readouterr().err, "Àûïàèé\n"

    def test_parse_percent(self):
        assert parse_percent("75%") == 0.75
        assert parse_percent("0.75") == 0.75
        assert parse_percent("75") == 75


import math


class TestMathFunctions(TestCase):
    def testExp(self):
        """Test if the math_eval function works"""
        function = "exp(x)"
        f = math_eval(function)
        self.assertAlmostEqual(f(1), math.exp(1))

    def testErf(self):
        """Only available in python3"""
        function = "erf(x)"
        f = math_eval(function)
        self.assertAlmostEqual(f(1), math.erf(1))

    def test_is_number(self):
        self.assertTrue(is_number("155"))
        self.assertTrue(is_number("12.5"))
        self.assertTrue(is_number("12.5e-5"))
        self.assertFalse(is_number("10=3"))
        self.assertFalse(is_number(""))
        self.assertFalse(is_number("a"))