Skip to content

Commit d19a6c6

Browse files
committed
add segments module unit tests
1 parent ce2ada6 commit d19a6c6

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

tests/test_segments.py

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import argparse
2+
import os
3+
from os import O_TRUNC
4+
5+
import pytest
6+
from fontTools.ttLib import TTFont
7+
from pathins.segments import segments_run
8+
import pathins.stringbuilder
9+
10+
11+
TESTFONT_PATH_1 = os.path.join(
12+
"tests", "testfiles", "fonts", "NotoSans-Regular.subset1.ttf"
13+
)
14+
15+
# instantiate a parser for unit tests in this module
16+
parser = argparse.ArgumentParser()
17+
parser.add_argument("--nocolor", action="store_true", help="no ANSI color")
18+
parser.add_argument("fontpath", type=str, help="font file path")
19+
parser.add_argument(
20+
"glyphname", type=str, help="glyph name (optional, default=all)", nargs="?"
21+
)
22+
23+
24+
def test_segments_run_error_invalid_path(capsys):
25+
test_path = os.path.join("bogus", "path.txt")
26+
args = parser.parse_args([test_path])
27+
28+
with pytest.raises(SystemExit) as e:
29+
segments_run(args)
30+
31+
captured = capsys.readouterr()
32+
assert e.type == SystemExit
33+
assert e.value.code == 1
34+
assert "does not appear to be a file" in captured.err
35+
36+
37+
def test_segments_run_error_non_font_path(capsys):
38+
test_path = os.path.join("tests", "testfiles", "text", "test.txt")
39+
args = parser.parse_args([test_path])
40+
41+
with pytest.raises(SystemExit) as e:
42+
segments_run(args)
43+
44+
captured = capsys.readouterr()
45+
assert e.type == SystemExit
46+
assert e.value.code == 1
47+
assert "does not appear to be a TTF format font" in captured.err
48+
49+
50+
def test_contours_run_fail_invalid_glyphname(capsys):
51+
args = parser.parse_args([TESTFONT_PATH_1, "bogus"])
52+
with pytest.raises(SystemExit) as e:
53+
segments_run(args)
54+
55+
captured = capsys.readouterr()
56+
assert e.type == SystemExit
57+
assert e.value.code == 1
58+
assert "Failed to open glyph" in captured.err
59+
60+
61+
def test_segments_run_single_glyph_nocontours_default(capsys, monkeypatch):
62+
def mock_isatty():
63+
return True
64+
65+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
66+
67+
args = parser.parse_args([TESTFONT_PATH_1, ".notdef"])
68+
segments_run(args)
69+
70+
captured = capsys.readouterr()
71+
# must be in a tty to get ANSI color output
72+
# this is mocked above
73+
assert "\033[1;96m'.notdef' segments\033[0m" in captured.out
74+
assert "No contours" in captured.out
75+
76+
77+
def test_segments_run_single_glyph_nocontours_nocolor(capsys, monkeypatch):
78+
def mock_isatty():
79+
return True
80+
81+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
82+
83+
args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, ".notdef"])
84+
segments_run(args)
85+
86+
captured = capsys.readouterr()
87+
# must be in a tty to get ANSI color output
88+
# this is mocked above
89+
assert "'.notdef' segments" in captured.out
90+
assert "No contours" in captured.out
91+
92+
93+
def test_segments_run_single_glyph_noncomposite_default(capsys, monkeypatch):
94+
def mock_isatty():
95+
return True
96+
97+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
98+
99+
args = parser.parse_args([TESTFONT_PATH_1, "A"])
100+
segments_run(args)
101+
102+
captured = capsys.readouterr()
103+
# must be in a tty to get ANSI color output
104+
# this is mocked above
105+
assert "\033[1;96m'A' segments\033[0m" in captured.out
106+
107+
assert (
108+
f"\033[32m(545,0)\033[0m (459,221): \033[1;96mLINE\033[0m 237.14 units{os.linesep}"
109+
f"(459,221) (176,221): \033[1;96mLINE\033[0m 283.00 units"
110+
) in captured.out
111+
112+
assert (
113+
f"(287,517) (206,301): \033[1;96mLINE\033[0m 230.69 units{os.linesep}"
114+
f"\033[31m(206,301)\033[0m \033[32m(432,301)\033[0m: \033[1;96mLINE\033[0m 226.00 units"
115+
) in captured.out
116+
117+
118+
def test_segments_run_single_glyph_noncomposite_nocolor(capsys, monkeypatch):
119+
def mock_isatty():
120+
return True
121+
122+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
123+
124+
args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, "A"])
125+
segments_run(args)
126+
127+
captured = capsys.readouterr()
128+
# must be in a tty to get ANSI color output
129+
# this is mocked above
130+
assert "'A' segments" in captured.out
131+
132+
assert (
133+
f"(545,0) (459,221): LINE 237.14 units{os.linesep}"
134+
f"(459,221) (176,221): LINE 283.00 units"
135+
) in captured.out
136+
137+
assert (
138+
f"(287,517) (206,301): LINE 230.69 units{os.linesep}"
139+
f"(206,301) (432,301): LINE 226.00 units"
140+
) in captured.out
141+
142+
143+
def test_segments_run_single_glyph_composite_default(capsys, monkeypatch):
144+
def mock_isatty():
145+
return True
146+
147+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
148+
149+
args = parser.parse_args([TESTFONT_PATH_1, "uni2E2E"])
150+
segments_run(args)
151+
152+
captured = capsys.readouterr()
153+
# must be in a tty to get ANSI color output
154+
# this is mocked above
155+
assert "\033[1;96m'uni2E2E' segments\033[0m" in captured.out
156+
157+
assert (
158+
f"\033[32m(303,201)\033[0m (303,228): \033[1;96mLINE\033[0m 27.00 units{os.linesep}"
159+
f"(303,228) (303,266) (296,294): \033[1;96mQCURVE\033[0m 66.53 units"
160+
) in captured.out
161+
162+
assert (
163+
"(309,2) \033[31m(326,18)\033[0m \033[32m(326,54)\033[0m: \033[1;96mQCURVE\033[0m 56.25 units"
164+
in captured.out
165+
)
166+
167+
168+
def test_segments_run_single_glyph_composite_nocolor(capsys, monkeypatch):
169+
def mock_isatty():
170+
return True
171+
172+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
173+
174+
args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, "uni2E2E"])
175+
segments_run(args)
176+
177+
captured = capsys.readouterr()
178+
# must be in a tty to get ANSI color output
179+
# this is mocked above
180+
assert "'uni2E2E' segments" in captured.out
181+
182+
assert (
183+
f"(303,201) (303,228): LINE 27.00 units{os.linesep}"
184+
f"(303,228) (303,266) (296,294): QCURVE 66.53 units"
185+
) in captured.out
186+
187+
assert "(309,2) (326,18) (326,54): QCURVE 56.25 units" in captured.out
188+
189+
190+
def test_segments_run_all_glyphs_default(capsys, monkeypatch):
191+
def mock_isatty():
192+
return True
193+
194+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
195+
196+
args = parser.parse_args([TESTFONT_PATH_1])
197+
segments_run(args)
198+
199+
captured = capsys.readouterr()
200+
# must be in a tty to get ANSI color output
201+
# this is mocked above
202+
assert "\033[1;96m'.notdef' segments\033[0m" in captured.out
203+
assert "\033[1;96m'space' segments\033[0m" in captured.out
204+
assert "\033[1;96m'comma' segments\033[0m" in captured.out
205+
assert "\033[1;96m'question' segments\033[0m" in captured.out
206+
assert "\033[1;96m'A' segments\033[0m" in captured.out
207+
assert "\033[1;96m'uni2E2E' segments\033[0m" in captured.out
208+
209+
assert "\033[1mTotal\033[0m: 671.74 units" in captured.out
210+
assert "\033[1mTotal\033[0m: 2245.15 units" in captured.out
211+
assert "\033[1mTotal\033[0m: 3471.07 units" in captured.out
212+
assert "\033[1mTotal\033[0m: 2246.05 units" in captured.out
213+
214+
215+
def test_segments_run_all_glyphs_nocolor(capsys, monkeypatch):
216+
def mock_isatty():
217+
return True
218+
219+
monkeypatch.setattr(pathins.stringbuilder, "IS_A_TTY", mock_isatty)
220+
221+
args = parser.parse_args(["--nocolor", TESTFONT_PATH_1])
222+
segments_run(args)
223+
224+
captured = capsys.readouterr()
225+
# must be in a tty to get ANSI color output
226+
# this is mocked above
227+
assert "\033[1;96m'.notdef' segments\033[0m" not in captured.out
228+
assert "\033[1;96m'space' segments\033[0m" not in captured.out
229+
assert "\033[1;96m'comma' segments\033[0m" not in captured.out
230+
assert "\033[1;96m'question' segments\033[0m" not in captured.out
231+
assert "\033[1;96m'A' segments\033[0m" not in captured.out
232+
assert "\033[1;96m'uni2E2E' segments\033[0m" not in captured.out
233+
234+
assert "'.notdef' segments" in captured.out
235+
assert "'space' segments" in captured.out
236+
assert "'comma' segments" in captured.out
237+
assert "'question' segments" in captured.out
238+
assert "'A' segments" in captured.out
239+
assert "'uni2E2E' segments" in captured.out
240+
241+
assert "Total: 671.74 units" in captured.out
242+
assert "Total: 2245.15 units" in captured.out
243+
assert "Total: 3471.07 units" in captured.out
244+
assert "Total: 2246.05 units" in captured.out

0 commit comments

Comments
 (0)