Skip to content

Commit 7fbabf0

Browse files
committed
Adding new tests.
1 parent 25a6e26 commit 7fbabf0

File tree

6 files changed

+350
-80
lines changed

6 files changed

+350
-80
lines changed

domdf_python_tools/pagesizes/utils.py

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@
3535

3636
# stdlib
3737
import re
38-
from decimal import Decimal
3938
from typing import Sequence, Tuple, Union, overload
4039

4140
# this package
4241
from ._types import AnyNumber
43-
from .units import Unit, cm, inch, mm, pc, pica, pt, um
42+
from .units import Unit, cm, inch, mm, pc, pt, um
4443

4544
# from .units import Unit
4645

@@ -85,7 +84,7 @@ def _sequence_convert_from(seq: Sequence[AnyNumber], from_: AnyNumber) -> Tuple[
8584
return tuple(float(x) * from_ for x in seq)
8685

8786

88-
_measurement_re = re.compile(r"(\d*\.?\d+) *([A-Za-z]*)")
87+
_measurement_re = re.compile(r"(\d*\.?\d+) *([A-Za-zμµ\"']*)")
8988

9089

9190
def parse_measurement(measurement: str) -> Union[float, Tuple[float, ...]]:
@@ -99,56 +98,31 @@ def parse_measurement(measurement: str) -> Union[float, Tuple[float, ...]]:
9998
"""
10099

101100
# TODO: docstring
102-
match = _measurement_re.findall(measurement)[0]
103-
# print(match)
104-
# print(len(match))
105-
if len(match) < 2:
101+
all_matches = _measurement_re.findall(measurement)
102+
103+
if len(all_matches) > 1:
104+
raise ValueError("Too many measurements")
105+
elif len(all_matches) == 0:
106106
raise ValueError("Unable to parse measurement")
107-
else:
108-
val = Decimal(str(match[0]))
109-
unit = match[1]
110-
if unit == "mm":
111-
return convert_from(val, mm)
112-
elif unit == "cm":
113-
return convert_from(val, cm)
114-
elif unit in {"um", "μm", "µm"}:
115-
return convert_from(val, um)
116-
elif unit == "pt":
117-
return convert_from(val, pt)
118-
elif unit == "inch":
119-
return convert_from(val, inch)
120-
elif unit == "in":
121-
return convert_from(val, inch)
122-
elif unit == "pc":
123-
return convert_from(val, pc)
124-
raise ValueError("Unknown unit")
125-
126-
127-
def to_length(s):
128-
"""
129-
Convert a string to a length.
130107

131-
:param s:
132-
:type s:
133-
:return:
134-
:rtype:
108+
val, unit = all_matches[0]
135109

136-
# TODO: combine with parse_measurement
137-
"""
110+
if "" in {val, unit}:
111+
raise ValueError("Unable to parse measurement")
138112

139-
try:
140-
if s[-2:] == "cm":
141-
return float(s[:-2]) * cm
142-
if s[-2:] == "in":
143-
return float(s[:-2]) * inch
144-
if s[-2:] == "pt":
145-
return float(s[:-2])
146-
if s[-1:] == 'i':
147-
return float(s[:-1]) * inch
148-
if s[-2:] == "mm":
149-
return float(s[:-2]) * mm
150-
if s[-4:] == "pica":
151-
return float(s[:-4]) * pica
152-
return float(s)
153-
except:
154-
raise ValueError(f"Can't convert_to '{s}' to length")
113+
val = float(val)
114+
115+
if unit == "mm":
116+
return val * mm
117+
elif unit == "cm":
118+
return val * cm
119+
elif unit in {"um", "μm", "µm"}: # second is mu, third is micro
120+
return val * um
121+
elif unit == "pt":
122+
return val * pt
123+
elif unit in {"inch", "in", '"'}:
124+
return val * inch
125+
elif unit in ("pc", "pica"):
126+
return val * pc
127+
128+
raise ValueError("Unknown unit")

domdf_python_tools/paths.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def copytree(
6060
dst: PathLike,
6161
symlinks: bool = False,
6262
ignore: Optional[Callable] = None,
63-
):
63+
) -> PathLike:
6464
"""
6565
Alternative to :func:`shutil.copytree` to support copying to a directory that already exists.
6666
@@ -95,9 +95,11 @@ def copytree(
9595
s = os.path.join(src, item)
9696
d = os.path.join(dst, item)
9797
if os.path.isdir(s):
98-
return shutil.copytree(s, d, symlinks, ignore)
98+
shutil.copytree(s, d, symlinks, ignore)
9999
else:
100-
return shutil.copy2(s, d)
100+
shutil.copy2(s, d)
101+
102+
return dst
101103

102104

103105
def delete(filename: PathLike, **kwargs):

tests/test_doctools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def partially_documented_function(a: float, b: float, c: float, d: float) -> Non
154154
This function works like ``documented_function`` except it returns the result telepathically.
155155
"""
156156

157-
d * c * b * a # pylint: disable=expression-not-assigned
157+
d * c * b * a # pylint: disable=pointless-statement
158158

159159

160160
class DummyClass:

tests/test_pagesizes.py

Lines changed: 114 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
parse_measurement,
3636
pc,
3737
pt,
38-
sizes,
3938
um
4039
)
40+
from domdf_python_tools.pagesizes.utils import _measurement_re
4141

4242

4343
@pytest.mark.parametrize(
@@ -116,14 +116,6 @@ def test_convert_size(unit):
116116
assert isclose(getattr(size, unit_str)[1], 34, abs_tol=1e-8)
117117

118118

119-
#
120-
# def test_parse_measurement():
121-
# assert parse_measurement("12mm") == (12 * mm).as_pt()
122-
# assert parse_measurement("12 mm") == (12 * mm).as_pt()
123-
# assert parse_measurement("12.34 mm") == (12.34 * mm).as_pt()
124-
# assert parse_measurement("5inch") == (5 * inch).as_pt()
125-
# assert parse_measurement("5in") == (5 * inch).as_pt()
126-
#
127119
#
128120
# @pytest.mark.parametrize(
129121
# "size, expected",
@@ -147,20 +139,46 @@ def test_convert_size(unit):
147139
#
148140
# # TODO: tests for Unit
149141
#
150-
def test_convert_from():
151-
assert convert_from(1, pt) == 1
152-
assert convert_from(1, inch) == 72
153-
assert convert_from(1, cm) == 28.3464566929
154-
assert convert_from(1, mm) == 2.83464566929
155-
assert convert_from(1, um) == 0.00283464566929
156-
assert convert_from(1, pc) == 12
157-
158-
assert convert_from(5, pt) == 1 * 5
159-
assert convert_from(5, inch) == 72 * 5
160-
assert convert_from(5, cm) == 28.3464566929 * 5
161-
assert convert_from(5, mm) == 2.83464566929 * 5
162-
assert convert_from(5, um) == 0.00283464566929 * 5
163-
assert convert_from(5, pc) == 12 * 5
142+
143+
144+
@pytest.mark.parametrize(
145+
"value, unit, expects",
146+
[
147+
(1, pt, 1),
148+
(1, inch, 72),
149+
(1, cm, 28.3464566929),
150+
(1, mm, 2.83464566929),
151+
(1, um, 0.00283464566929),
152+
(1, pc, 12),
153+
(5, pt, 1 * 5),
154+
(5, inch, 72 * 5),
155+
(5, cm, 28.3464566929 * 5),
156+
(5, mm, 2.83464566929 * 5),
157+
(5, um, 0.00283464566929 * 5),
158+
(5, pc, 12 * 5),
159+
([1], pt, (1, )),
160+
([1], inch, (72, )),
161+
([1], cm, (28.3464566929, )),
162+
([1], mm, (2.83464566929, )),
163+
([1], um, (0.00283464566929, )),
164+
([1], pc, (12, )),
165+
([5], pt, (1 * 5, )),
166+
([5], inch, (72 * 5, )),
167+
([5], cm, (28.3464566929 * 5, )),
168+
([5], mm, (2.83464566929 * 5, )),
169+
([5], um, (0.00283464566929 * 5, )),
170+
([5], pc, (12 * 5, )),
171+
([1, 5], pt, (1, 1 * 5)),
172+
([1, 5], inch, (72, 72 * 5)),
173+
([1, 5], cm, (28.3464566929, 28.3464566929 * 5)),
174+
([1, 5], mm, (2.83464566929, 2.83464566929 * 5)),
175+
([1, 5], um, (0.00283464566929, 0.00283464566929 * 5)),
176+
([1, 5], pc, (12, 12 * 5)),
177+
pytest.param(2, 5, 10, id="not isinstance(from_, Unit)"),
178+
]
179+
)
180+
def test_convert_from(value, unit, expects):
181+
assert convert_from(value, unit) == expects
164182

165183

166184
@pytest.mark.parametrize(
@@ -172,3 +190,76 @@ def test_convert_from():
172190
def test_from_size(size, expected, class_):
173191
print(class_.from_size(size))
174192
assert class_.from_size(size) == expected
193+
194+
195+
@pytest.mark.parametrize(
196+
"string, expects",
197+
[
198+
("12.34mm", [("12.34", "mm")]),
199+
("12.34 mm", [("12.34", "mm")]),
200+
(".34 mm", [(".34", "mm")]),
201+
("12.34in", [("12.34", "in")]),
202+
("12.34 in", [("12.34", "in")]),
203+
(".34 in", [(".34", "in")]),
204+
("12.34\"", [("12.34", "\"")]),
205+
("12.34 \"", [("12.34", "\"")]),
206+
(".34 \"", [(".34", "\"")]),
207+
("12.34mm .34\"", [("12.34", "mm"), (".34", "\"")]),
208+
("12", [("12", '')]),
209+
('', []),
210+
('10μm', [('10', 'μm')]),
211+
]
212+
)
213+
def test_measurement_re(string, expects):
214+
assert _measurement_re.findall(string) == expects
215+
216+
217+
def test_parse_measurement_errors():
218+
with pytest.raises(ValueError, match="Too many measurements"):
219+
parse_measurement("12.34mm .34\"")
220+
with pytest.raises(ValueError, match="Unable to parse measurement"):
221+
parse_measurement("")
222+
with pytest.raises(ValueError, match="Unable to parse measurement"):
223+
parse_measurement("bananas")
224+
with pytest.raises(ValueError, match="Unable to parse measurement"):
225+
parse_measurement('')
226+
with pytest.raises(ValueError, match="Unable to parse measurement"):
227+
parse_measurement("12")
228+
with pytest.raises(ValueError, match="Unable to parse measurement"):
229+
parse_measurement("mm")
230+
with pytest.raises(ValueError, match="Unknown unit"):
231+
parse_measurement("12'")
232+
233+
234+
@pytest.mark.parametrize(
235+
"string, expects",
236+
[
237+
("12mm", mm(12)),
238+
("12 mm", mm(12)),
239+
("12.34 mm", mm(12.34)),
240+
("12 um", um(12)),
241+
("12um", um(12)),
242+
("12 μm", um(12)),
243+
("12μm", um(12)),
244+
("12 µm", um(12)),
245+
("12µm", um(12)),
246+
("12 in", inch(12)),
247+
("12 inch", inch(12)),
248+
("12\"", inch(12)),
249+
("12 cm", cm(12)),
250+
("12cm", cm(12)),
251+
("12 pc", pc(12)),
252+
("12pc", pc(12)),
253+
("12 pica", pc(12)),
254+
("12pica", pc(12)),
255+
("12 pt", pt(12)),
256+
("12pt", pt(12)),
257+
("12mm", 12 * mm),
258+
("12 mm", 12 * mm),
259+
("12.34 mm", 12.34 * mm),
260+
("5inch", 5 * inch),
261+
("5in", 5 * inch),
262+
]
263+
)
264+
def test_parse_measurement(string, expects):
265+
assert parse_measurement(string) == expects

0 commit comments

Comments
 (0)