Fix T104270: Error in materials_utils "unregister" function
[blender-addons.git] / io_curve_svg / svg_util_test.py
blob976d9c3f321f4dff72ba23c46fbf708ccbb64cfe
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # XXX Not really nice, but that hack is needed to allow execution of that test
5 # from both automated CTest and by directly running the file manually.
6 if __name__ == '__main__':
7 from svg_util import (parse_array_of_floats, read_float, parse_coord,)
8 else:
9 from .svg_util import (parse_array_of_floats, read_float, parse_coord,)
10 import unittest
12 class ParseArrayOfFloatsTest(unittest.TestCase):
13 def test_empty(self):
14 self.assertEqual(parse_array_of_floats(""), [])
15 self.assertEqual(parse_array_of_floats(" "), [])
17 def test_single_value(self):
18 self.assertEqual(parse_array_of_floats("123"), [123])
19 self.assertEqual(parse_array_of_floats(" \t 123 \t"), [123])
21 def test_single_value_exponent(self):
22 self.assertEqual(parse_array_of_floats("12e+3"), [12000])
23 self.assertEqual(parse_array_of_floats("12e-3"), [0.012])
25 def test_space_separated_values(self):
26 self.assertEqual(parse_array_of_floats("123 45 6 89"),
27 [123, 45, 6, 89])
28 self.assertEqual(parse_array_of_floats(" 123 45 6 89 "),
29 [123, 45, 6, 89])
31 def test_comma_separated_values(self):
32 self.assertEqual(parse_array_of_floats("123,45,6,89"),
33 [123, 45, 6, 89])
34 self.assertEqual(parse_array_of_floats(" 123,45,6,89 "),
35 [123, 45, 6, 89])
37 def test_mixed_separated_values(self):
38 self.assertEqual(parse_array_of_floats("123,45 6,89"),
39 [123, 45, 6, 89])
40 self.assertEqual(parse_array_of_floats(" 123 45,6,89 "),
41 [123, 45, 6, 89])
43 def test_omitted_value_with_comma(self):
44 self.assertEqual(parse_array_of_floats("1,,3"), [1, 0, 3])
45 self.assertEqual(parse_array_of_floats(",,3"), [0, 0, 3])
47 def test_sign_as_separator(self):
48 self.assertEqual(parse_array_of_floats("1-3"), [1, -3])
49 self.assertEqual(parse_array_of_floats("1+3"), [1, 3])
51 def test_all_commas(self):
52 self.assertEqual(parse_array_of_floats(",,,"), [0, 0, 0, 0])
54 def test_value_with_decimal_separator(self):
55 self.assertEqual(parse_array_of_floats("3.5"), [3.5])
57 def test_comma_separated_values_with_decimal_separator(self):
58 self.assertEqual(parse_array_of_floats("2.75,8.5"), [2.75, 8.5])
60 def test_missing_decimal(self):
61 self.assertEqual(parse_array_of_floats(".92"), [0.92])
62 self.assertEqual(parse_array_of_floats(".92e+1"), [9.2])
64 self.assertEqual(parse_array_of_floats("-.92"), [-0.92])
65 self.assertEqual(parse_array_of_floats("-.92e+1"), [-9.2])
68 class ReadFloatTest(unittest.TestCase):
69 def test_empty(self):
70 value, endptr = read_float("", 0)
71 self.assertEqual(value, "0")
72 self.assertEqual(endptr, 0)
74 def test_empty_spaces(self):
75 value, endptr = read_float(" ", 0)
76 self.assertEqual(value, "0")
77 self.assertEqual(endptr, 4)
79 def test_single_value(self):
80 value, endptr = read_float("1.2", 0)
81 self.assertEqual(value, "1.2")
82 self.assertEqual(endptr, 3)
84 def test_scientific_value(self):
85 value, endptr = read_float("1.2e+3", 0)
86 self.assertEqual(value, "1.2e+3")
87 self.assertEqual(endptr, 6)
89 def test_scientific_value_no_sign(self):
90 value, endptr = read_float("1.2e3", 0)
91 self.assertEqual(value, "1.2e3")
92 self.assertEqual(endptr, 5)
94 def test_middle(self):
95 value, endptr = read_float("1.2 3.4 5.6", 3)
96 self.assertEqual(value, "3.4")
97 self.assertEqual(endptr, 8)
99 def test_comma(self):
100 value, endptr = read_float("1.2 ,,3.4 5.6", 3)
101 self.assertEqual(value, "3.4")
102 self.assertEqual(endptr, 10)
104 def test_not_a_number(self):
105 # TODO(sergey): Make this catch more concrete.
106 with self.assertRaises(Exception):
107 read_float("1.2eV", 3)
109 def test_missing_fractional(self):
110 value, endptr = read_float("1.", 0)
111 self.assertEqual(value, "1.")
112 self.assertEqual(endptr, 2)
114 value, endptr = read_float("2. 3", 0)
115 self.assertEqual(value, "2.")
116 self.assertEqual(endptr, 2)
118 def test_missing_decimal(self):
119 value, endptr = read_float(".92", 0)
120 self.assertEqual(value, ".92")
121 self.assertEqual(endptr, 3)
123 value, endptr = read_float("-.92", 0)
124 self.assertEqual(value, "-.92")
125 self.assertEqual(endptr, 4)
127 value, endptr = read_float(".92e+3", 0)
128 self.assertEqual(value, ".92e+3")
129 self.assertEqual(endptr, 6)
131 value, endptr = read_float("-.92e+3", 0)
132 self.assertEqual(value, "-.92e+3")
133 self.assertEqual(endptr, 7)
135 # TODO(sergey): Make these catch more concrete.
136 with self.assertRaises(Exception):
137 read_float(".", 0)
138 with self.assertRaises(Exception):
139 read_float(".e+1", 0)
142 class ParseCoordTest(unittest.TestCase):
143 def test_empty(self):
144 self.assertEqual(parse_coord("", 200), 0)
146 def test_empty_spaces(self):
147 self.assertEqual(parse_coord(" ", 200), 0)
149 def test_no_units(self):
150 self.assertEqual(parse_coord("1.2", 200), 1.2)
152 def test_unit_cm(self):
153 self.assertAlmostEqual(parse_coord("1.2cm", 200), 42.51968503937008)
155 def test_unit_ex(self):
156 self.assertAlmostEqual(parse_coord("1.2ex", 200), 1.2)
158 def test_unit_percentage(self):
159 self.assertEqual(parse_coord("1.2%", 200), 2.4)
162 if __name__ == '__main__':
163 unittest.main(verbosity=2)