Skip to content

Commit d020d20

Browse files
committed
Added new pagesizes tests.
1 parent fd48ead commit d020d20

File tree

3 files changed

+127
-176
lines changed

3 files changed

+127
-176
lines changed

domdf_python_tools/pagesizes/units.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def __floordiv__(self, other: Union[float, "Unit"]) -> "Unit":
184184

185185
return self.__class__(super().__floordiv__(other))
186186

187-
def __eq__(self, other: Union[float, "Unit"]) -> bool:
187+
def __eq__(self, other: object) -> bool:
188188
if isinstance(other, Unit):
189189
if self._in_pt != 1:
190190
self_value = self.as_pt()

tests/test_terminal.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55

66
# 3rd party
77
import pytest
8-
from faker import Faker
8+
from faker import Faker # type: ignore
9+
from faker.providers import bank, company, internet, phone_number, python # type: ignore
910

10-
fake = Faker()
11-
# 3rd party
12-
from faker.providers import bank, company, internet, phone_number, python
11+
# this package
12+
from domdf_python_tools.terminal import Echo, br, clear, interrupt, overtype
1313

14+
fake = Faker()
1415
fake.add_provider(internet)
1516
fake.add_provider(bank)
1617
fake.add_provider(company)
1718
fake.add_provider(phone_number)
1819
fake.add_provider(python)
1920

20-
# this package
21-
from domdf_python_tools.terminal import Echo, br, clear, interrupt, overtype
22-
2321

2422
def test_br(capsys):
2523
br()

tests/test_units.py

Lines changed: 121 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,220 +1,173 @@
11
# stdlib
22
import re
33
from operator import floordiv, truediv
4+
from typing import Iterable, List, Union
45

56
# 3rd party
67
import pytest
78

89
# this package
910
from domdf_python_tools.pagesizes.units import Unit, Unitcm, UnitInch, Unitmm, Unitpc, Unitpt, Unitum
1011

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]
1117

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)
1520
def test_repr(unit):
1621
assert re.match(r"<Unit '12\.000 .*': .*pt", repr(unit))
1722

1823

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)
2225
def test_str(unit):
2326
assert re.match(r"<Unit '12\.000\u205F.*': .*pt", str(unit))
2427

2528

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:
9130

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
9235

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)
9445

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
9550

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)
10554

10655

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:
11357

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
11462

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))
12269

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
12674

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))
12781

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)
13588

89+
@pytest.mark.parametrize("obj", one2thousand)
90+
def test_modulo(self, obj: int):
91+
assert Unit(17) % obj == 17 % obj
13692

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))
14597

14698

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
155100

156101

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:
159104

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
163110

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
167116

117+
@pytest.mark.parametrize("obj", zero2thousand)
118+
def test_add_number(self, unit, obj: int):
168119

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
174123

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
178127

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
179133

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
188137

189138

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:
198141

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
199147

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()
202153

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):
206156

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
210160

161+
assert isinstance(UnitInch(1) - obj, Unit)
162+
assert (UnitInch(1) - obj).name == "inch"
163+
assert (UnitInch(1) - obj) == UnitInch.from_pt(72) - obj
211164

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
217170

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

Comments
 (0)