|
1 | 1 | # stdlib
|
2 | 2 | import re
|
3 | 3 | from operator import floordiv, truediv
|
| 4 | +from typing import Iterable, List, Union |
4 | 5 |
|
5 | 6 | # 3rd party
|
6 | 7 | import pytest
|
7 | 8 |
|
8 | 9 | # this package
|
9 | 10 | from domdf_python_tools.pagesizes.units import Unit, Unitcm, UnitInch, Unitmm, Unitpc, Unitpt, Unitum
|
10 | 11 |
|
| 12 | +units_of_12: List[Unit] = [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
| 13 | +zero2thousand: List[int] = list(range(0, 1000, 10)) |
| 14 | +one2thousand: List[int] = list(range(1, 1000, 10)) |
| 15 | +units_zero2thousand: List[Unit] = [Unit(x) for x in zero2thousand] |
| 16 | +units_ints_zero2thousand: List[Union[Unit, int]] = [*units_zero2thousand, *zero2thousand] |
11 | 17 |
|
12 |
| -@pytest.mark.parametrize( |
13 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
14 |
| - ) |
| 18 | + |
| 19 | +@pytest.mark.parametrize("unit", units_of_12) |
15 | 20 | def test_repr(unit):
|
16 | 21 | assert re.match(r"<Unit '12\.000 .*': .*pt", repr(unit))
|
17 | 22 |
|
18 | 23 |
|
19 |
| -@pytest.mark.parametrize( |
20 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
21 |
| - ) |
| 24 | +@pytest.mark.parametrize("unit", units_of_12) |
22 | 25 | def test_str(unit):
|
23 | 26 | assert re.match(r"<Unit '12\.000\u205F.*': .*pt", str(unit))
|
24 | 27 |
|
25 | 28 |
|
26 |
| -@pytest.mark.parametrize( |
27 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
28 |
| - ) |
29 |
| -@pytest.mark.parametrize("obj", (Unit(x) for x in range(0, 1000, 10))) |
30 |
| -def test_mul_errors(unit, obj): |
31 |
| - with pytest.raises(NotImplementedError, match="Multiplying a unit by another unit is not allowed."): |
32 |
| - Unit(17) * obj |
33 |
| - |
34 |
| - |
35 |
| -@pytest.mark.parametrize( |
36 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
37 |
| - ) |
38 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
39 |
| -def test_mul(unit, obj: int): |
40 |
| - assert obj * Unit(17) == obj * 17 |
41 |
| - assert obj * Unit(17 / 2) == obj * (17 / 2) |
42 |
| - |
43 |
| - |
44 |
| -@pytest.mark.parametrize( |
45 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
46 |
| - ) |
47 |
| -@pytest.mark.parametrize("obj", (Unit(x) for x in range(0, 1000, 10))) |
48 |
| -def test_rmul_errors(unit, obj): |
49 |
| - with pytest.raises(NotImplementedError, match="Multiplying a unit by another unit is not allowed."): |
50 |
| - obj * Unit(17) |
51 |
| - |
52 |
| - |
53 |
| -@pytest.mark.parametrize( |
54 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
55 |
| - ) |
56 |
| -@pytest.mark.parametrize("obj", range(1, 1000, 10)) |
57 |
| -def test_truediv(unit, obj: int): |
58 |
| - assert truediv(Unit(17), obj) == 17 / obj |
59 |
| - assert truediv(Unit(17 / 2), obj) == (17 / 2) / obj |
60 |
| - |
61 |
| - |
62 |
| -@pytest.mark.parametrize( |
63 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
64 |
| - ) |
65 |
| -@pytest.mark.parametrize("obj", (Unit(x) for x in range(0, 1000, 10))) |
66 |
| -def test_truediv_errors(unit, obj): |
67 |
| - with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
68 |
| - truediv(Unit(17), obj) |
69 |
| - with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
70 |
| - truediv(obj, Unit(17)) |
71 |
| - |
72 |
| - |
73 |
| -@pytest.mark.parametrize( |
74 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
75 |
| - ) |
76 |
| -@pytest.mark.parametrize("obj", range(1, 1000, 10)) |
77 |
| -def test_floordiv(unit, obj: int): |
78 |
| - assert floordiv(Unit(17), obj) == 17 // obj |
79 |
| - assert floordiv(Unit(17 / 2), obj) == (17 / 2) // obj |
80 |
| - |
81 |
| - |
82 |
| -@pytest.mark.parametrize( |
83 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
84 |
| - ) |
85 |
| -@pytest.mark.parametrize("obj", (Unit(x) for x in range(0, 1000, 10))) |
86 |
| -def test_floordiv_errors(unit, obj): |
87 |
| - with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
88 |
| - floordiv(Unit(17), obj) |
89 |
| - with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
90 |
| - floordiv(obj, Unit(17)) |
| 29 | +class TestMul: |
91 | 30 |
|
| 31 | + @pytest.mark.parametrize("obj", units_zero2thousand) |
| 32 | + def test_mul_errors(self, obj): |
| 33 | + with pytest.raises(NotImplementedError, match="Multiplying a unit by another unit is not allowed."): |
| 34 | + Unit(17) * obj |
92 | 35 |
|
93 |
| -# TODO eq |
| 36 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 37 | + def test_mul(self, obj: int): |
| 38 | + assert obj * Unit(17) == obj * 17 |
| 39 | + assert obj * Unit(17 / 2) == obj * (17 / 2) |
| 40 | + |
| 41 | + @pytest.mark.parametrize("obj", units_zero2thousand) |
| 42 | + def test_rmul_errors(self, obj): |
| 43 | + with pytest.raises(NotImplementedError, match="Multiplying a unit by another unit is not allowed."): |
| 44 | + obj * Unit(17) |
94 | 45 |
|
| 46 | + @pytest.mark.parametrize("obj", units_ints_zero2thousand) |
| 47 | + def test_pow_errors(self, obj): |
| 48 | + with pytest.raises(NotImplementedError, match="Powers are not supported for units."): |
| 49 | + Unit(17)**obj |
95 | 50 |
|
96 |
| -@pytest.mark.parametrize( |
97 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
98 |
| - ) |
99 |
| -@pytest.mark.parametrize("obj", (Unit(x) for x in range(0, 1000, 10))) |
100 |
| -def test_modulo_errors(unit, obj): |
101 |
| - with pytest.raises(NotImplementedError, match="Modulo division of a unit by another unit is not allowed."): |
102 |
| - Unit(17) % obj |
103 |
| - with pytest.raises(NotImplementedError, match="Modulo division of a unit by another unit is not allowed."): |
104 |
| - obj % Unit(17) |
| 51 | + if isinstance(obj, Unit): |
| 52 | + with pytest.raises(NotImplementedError, match="Powers are not supported for units."): |
| 53 | + obj**Unit(17) |
105 | 54 |
|
106 | 55 |
|
107 |
| -@pytest.mark.parametrize( |
108 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
109 |
| - ) |
110 |
| -@pytest.mark.parametrize("obj", range(1, 1000, 10)) |
111 |
| -def test_modulo(unit, obj: int): |
112 |
| - assert Unit(17) % obj == 17 % obj |
| 56 | +class TestDiv: |
113 | 57 |
|
| 58 | + @pytest.mark.parametrize("obj", one2thousand) |
| 59 | + def test_truediv(self, obj: int): |
| 60 | + assert truediv(Unit(17), obj) == 17 / obj |
| 61 | + assert truediv(Unit(17 / 2), obj) == (17 / 2) / obj |
114 | 62 |
|
115 |
| -@pytest.mark.parametrize( |
116 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
117 |
| - ) |
118 |
| -@pytest.mark.parametrize("obj", [Unit(x) for x in range(0, 1000, 10)] + list(range(0, 1000, 10))) |
119 |
| -def test_pow_errors(unit, obj): |
120 |
| - with pytest.raises(NotImplementedError, match="Powers are not supported for units."): |
121 |
| - Unit(17)**obj |
| 63 | + @pytest.mark.parametrize("obj", units_zero2thousand) |
| 64 | + def test_truediv_errors(self, obj): |
| 65 | + with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
| 66 | + truediv(Unit(17), obj) |
| 67 | + with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
| 68 | + truediv(obj, Unit(17)) |
122 | 69 |
|
123 |
| - if isinstance(obj, Unit): |
124 |
| - with pytest.raises(NotImplementedError, match="Powers are not supported for units."): |
125 |
| - obj**Unit(17) |
| 70 | + @pytest.mark.parametrize("obj", one2thousand) |
| 71 | + def test_floordiv(self, obj: int): |
| 72 | + assert floordiv(Unit(17), obj) == 17 // obj |
| 73 | + assert floordiv(Unit(17 / 2), obj) == (17 / 2) // obj |
126 | 74 |
|
| 75 | + @pytest.mark.parametrize("obj", units_zero2thousand) |
| 76 | + def test_floordiv_errors(self, obj): |
| 77 | + with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
| 78 | + floordiv(Unit(17), obj) |
| 79 | + with pytest.raises(NotImplementedError, match="Dividing a unit by another unit is not allowed."): |
| 80 | + floordiv(obj, Unit(17)) |
127 | 81 |
|
128 |
| -@pytest.mark.parametrize( |
129 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
130 |
| - ) |
131 |
| -@pytest.mark.parametrize("obj", (range(0, 1000, 10))) |
132 |
| -def test_rtruediv_errors(unit, obj): |
133 |
| - with pytest.raises(NotImplementedError, match="Dividing by a unit is not allowed."): |
134 |
| - truediv(obj, Unit(17)) |
| 82 | + @pytest.mark.parametrize("obj", units_zero2thousand) |
| 83 | + def test_modulo_errors(self, obj): |
| 84 | + with pytest.raises(NotImplementedError, match="Modulo division of a unit by another unit is not allowed."): |
| 85 | + Unit(17) % obj |
| 86 | + with pytest.raises(NotImplementedError, match="Modulo division of a unit by another unit is not allowed."): |
| 87 | + obj % Unit(17) |
135 | 88 |
|
| 89 | + @pytest.mark.parametrize("obj", one2thousand) |
| 90 | + def test_modulo(self, obj: int): |
| 91 | + assert Unit(17) % obj == 17 % obj |
136 | 92 |
|
137 |
| -@pytest.mark.parametrize( |
138 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
139 |
| - ) |
140 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
141 |
| -def test_add_unit(unit, obj: int): |
142 |
| - assert isinstance(unit + Unit(obj), Unit) |
143 |
| - assert (unit + Unit(obj)).name == unit.name |
144 |
| - assert (unit + Unit(obj)).as_pt() == unit.as_pt() + obj |
| 93 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 94 | + def test_rtruediv_errors(self, obj): |
| 95 | + with pytest.raises(NotImplementedError, match="Dividing by a unit is not allowed."): |
| 96 | + truediv(obj, Unit(17)) |
145 | 97 |
|
146 | 98 |
|
147 |
| -@pytest.mark.parametrize( |
148 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
149 |
| - ) |
150 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
151 |
| -def test_radd_unit(unit, obj: int): |
152 |
| - assert isinstance(Unit(obj) + unit, Unit) |
153 |
| - assert (Unit(obj) + unit).name == "pt" |
154 |
| - assert (Unit(obj) + unit).as_pt() == unit.as_pt() + obj |
| 99 | +# TODO eq |
155 | 100 |
|
156 | 101 |
|
157 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
158 |
| -def test_add_number(obj: int): |
| 102 | +@pytest.mark.parametrize("unit", units_of_12) |
| 103 | +class TestAdd: |
159 | 104 |
|
160 |
| - assert isinstance(Unit(12) + obj, Unit) |
161 |
| - assert (Unit(12) + obj).name == "pt" |
162 |
| - assert (Unit(12) + obj).as_pt() == 12 + obj |
| 105 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 106 | + def test_add_unit(self, unit, obj: int): |
| 107 | + assert isinstance(unit + Unit(obj), Unit) |
| 108 | + assert (unit + Unit(obj)).name == unit.name |
| 109 | + assert (unit + Unit(obj)).as_pt() == unit.as_pt() + obj |
163 | 110 |
|
164 |
| - assert isinstance(UnitInch(1) + obj, Unit) |
165 |
| - assert (UnitInch(1) + obj).name == "inch" |
166 |
| - assert (UnitInch(1) + obj) == UnitInch.from_pt(72) + obj |
| 111 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 112 | + def test_radd_unit(self, unit, obj: int): |
| 113 | + assert isinstance(Unit(obj) + unit, Unit) |
| 114 | + assert (Unit(obj) + unit).name == "pt" |
| 115 | + assert (Unit(obj) + unit).as_pt() == unit.as_pt() + obj |
167 | 116 |
|
| 117 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 118 | + def test_add_number(self, unit, obj: int): |
168 | 119 |
|
169 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
170 |
| -def test_radd_number(obj: int): |
171 |
| - assert isinstance(obj + Unit(12), Unit) |
172 |
| - assert (obj + Unit(12)).name == "pt" |
173 |
| - assert (obj + Unit(12)).as_pt() == 12 + obj |
| 120 | + assert isinstance(Unit(12) + obj, Unit) |
| 121 | + assert (Unit(12) + obj).name == "pt" |
| 122 | + assert (Unit(12) + obj).as_pt() == 12 + obj |
174 | 123 |
|
175 |
| - assert isinstance(obj + UnitInch(1), Unit) |
176 |
| - assert (obj + UnitInch(1)).name == "inch" |
177 |
| - assert (obj + UnitInch(1)) == UnitInch.from_pt(72) + obj |
| 124 | + assert isinstance(UnitInch(1) + obj, Unit) |
| 125 | + assert (UnitInch(1) + obj).name == "inch" |
| 126 | + assert (UnitInch(1) + obj) == UnitInch.from_pt(72) + obj |
178 | 127 |
|
| 128 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 129 | + def test_radd_number(self, unit, obj: int): |
| 130 | + assert isinstance(obj + Unit(12), Unit) |
| 131 | + assert (obj + Unit(12)).name == "pt" |
| 132 | + assert (obj + Unit(12)).as_pt() == 12 + obj |
179 | 133 |
|
180 |
| -@pytest.mark.parametrize( |
181 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
182 |
| - ) |
183 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
184 |
| -def test_sub_unit(unit, obj: int): |
185 |
| - assert isinstance(unit + Unit(obj), Unit) |
186 |
| - assert (unit - Unit(obj)).name == unit.name |
187 |
| - assert (unit - Unit(obj)).as_pt() == unit.as_pt() - obj |
| 134 | + assert isinstance(obj + UnitInch(1), Unit) |
| 135 | + assert (obj + UnitInch(1)).name == "inch" |
| 136 | + assert (obj + UnitInch(1)) == UnitInch.from_pt(72) + obj |
188 | 137 |
|
189 | 138 |
|
190 |
| -@pytest.mark.parametrize( |
191 |
| - "unit", [Unit(12), UnitInch(12), Unitcm(12), Unitmm(12), Unitpc(12), Unitpt(12), Unitum(12)] |
192 |
| - ) |
193 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
194 |
| -def test_rsub_unit(unit, obj: int): |
195 |
| - assert isinstance(Unit(obj) - unit, Unit) |
196 |
| - assert (Unit(obj) - unit).name == "pt" |
197 |
| - assert (Unit(obj) - unit).as_pt() == obj - unit.as_pt() |
| 139 | +@pytest.mark.parametrize("unit", units_of_12) |
| 140 | +class TestSub: |
198 | 141 |
|
| 142 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 143 | + def test_sub_unit(self, unit, obj: int): |
| 144 | + assert isinstance(unit + Unit(obj), Unit) |
| 145 | + assert (unit - Unit(obj)).name == unit.name |
| 146 | + assert (unit - Unit(obj)).as_pt() == unit.as_pt() - obj |
199 | 147 |
|
200 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
201 |
| -def test_sub_number(obj: int): |
| 148 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 149 | + def test_rsub_unit(self, unit, obj: int): |
| 150 | + assert isinstance(Unit(obj) - unit, Unit) |
| 151 | + assert (Unit(obj) - unit).name == "pt" |
| 152 | + assert (Unit(obj) - unit).as_pt() == obj - unit.as_pt() |
202 | 153 |
|
203 |
| - assert isinstance(Unit(12) - obj, Unit) |
204 |
| - assert (Unit(12) - obj).name == "pt" |
205 |
| - assert (Unit(12) - obj).as_pt() == 12 - obj |
| 154 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 155 | + def test_sub_number(self, unit, obj: int): |
206 | 156 |
|
207 |
| - assert isinstance(UnitInch(1) - obj, Unit) |
208 |
| - assert (UnitInch(1) - obj).name == "inch" |
209 |
| - assert (UnitInch(1) - obj) == UnitInch.from_pt(72) - obj |
| 157 | + assert isinstance(Unit(12) - obj, Unit) |
| 158 | + assert (Unit(12) - obj).name == "pt" |
| 159 | + assert (Unit(12) - obj).as_pt() == 12 - obj |
210 | 160 |
|
| 161 | + assert isinstance(UnitInch(1) - obj, Unit) |
| 162 | + assert (UnitInch(1) - obj).name == "inch" |
| 163 | + assert (UnitInch(1) - obj) == UnitInch.from_pt(72) - obj |
211 | 164 |
|
212 |
| -@pytest.mark.parametrize("obj", range(0, 1000, 10)) |
213 |
| -def test_rsub_number(obj: int): |
214 |
| - assert isinstance(obj + Unit(12), Unit) |
215 |
| - assert (obj - Unit(12)).name == "pt" |
216 |
| - assert (obj - Unit(12)).as_pt() == obj - 12 |
| 165 | + @pytest.mark.parametrize("obj", zero2thousand) |
| 166 | + def test_rsub_number(self, unit, obj: int): |
| 167 | + assert isinstance(obj + Unit(12), Unit) |
| 168 | + assert (obj - Unit(12)).name == "pt" |
| 169 | + assert (obj - Unit(12)).as_pt() == obj - 12 |
217 | 170 |
|
218 |
| - assert isinstance(obj - UnitInch(1), Unit) |
219 |
| - assert (obj - UnitInch(1)).name == "inch" |
220 |
| - assert (obj - UnitInch(1)) == obj - UnitInch.from_pt(72) |
| 171 | + assert isinstance(obj - UnitInch(1), Unit) |
| 172 | + assert (obj - UnitInch(1)).name == "inch" |
| 173 | + assert (obj - UnitInch(1)) == obj - UnitInch.from_pt(72) |
0 commit comments