Skip to content

Commit 2ded77d

Browse files
committed
add single glyph direction unit tests
1 parent b57045b commit 2ded77d

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

tests/test_direction.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import argparse
2+
import os
3+
import sys
4+
5+
import pytest
6+
7+
from pathins.direction import direction_run
8+
9+
TESTFONT_PATH_1 = os.path.join(
10+
"tests", "testfiles", "fonts", "NotoSans-Regular.subset1.ttf"
11+
)
12+
13+
# instantiate a parser for unit tests in this module
14+
parser = argparse.ArgumentParser()
15+
parser.add_argument("--nocolor", action="store_true", help="no ANSI color")
16+
parser.add_argument("fontpath", type=str, help="font file path")
17+
parser.add_argument(
18+
"glyphname", type=str, help="glyph name (optional, default=all)", nargs="?"
19+
)
20+
21+
22+
def test_direction_run_error_invalid_path(capsys):
23+
test_path = os.path.join("bogus", "path.txt")
24+
args = parser.parse_args([test_path])
25+
26+
with pytest.raises(SystemExit) as e:
27+
direction_run(args)
28+
29+
captured = capsys.readouterr()
30+
assert e.type == SystemExit
31+
assert e.value.code == 1
32+
assert "does not appear to be a file" in captured.err
33+
34+
35+
def test_direction_run_error_non_font_path(capsys):
36+
test_path = os.path.join("tests", "testfiles", "text", "test.txt")
37+
args = parser.parse_args([test_path])
38+
39+
with pytest.raises(SystemExit) as e:
40+
direction_run(args)
41+
42+
captured = capsys.readouterr()
43+
assert e.type == SystemExit
44+
assert e.value.code == 1
45+
assert "does not appear to be a TTF format font" in captured.err
46+
47+
48+
def test_direction_run_fail_invalid_glyphname(capsys):
49+
args = parser.parse_args([TESTFONT_PATH_1, "bogus"])
50+
with pytest.raises(SystemExit) as e:
51+
direction_run(args)
52+
53+
captured = capsys.readouterr()
54+
assert e.type == SystemExit
55+
assert e.value.code == 1
56+
assert "Failed to open glyph" in captured.err
57+
58+
59+
def test_path_run_single_glyph_non_composite_no_contours_default(capsys, monkeypatch):
60+
def mock_isatty():
61+
return True
62+
63+
# apply the monkeypatch for sys.stdout.isatty()
64+
monkeypatch.setattr(sys.stdout, "isatty", mock_isatty)
65+
66+
args = parser.parse_args([TESTFONT_PATH_1, ".notdef"])
67+
direction_run(args)
68+
69+
captured = capsys.readouterr()
70+
# must be in a tty to get ANSI color output
71+
# this is mocked above
72+
assert "[ \x1b[1;36m.notdef\x1b[0m ]: no contours" in captured.out
73+
74+
75+
def test_path_run_single_glyph_non_composite_no_contours_nocolor(capsys, monkeypatch):
76+
def mock_isatty():
77+
return True
78+
79+
# apply the monkeypatch for sys.stdout.isatty()
80+
monkeypatch.setattr(sys.stdout, "isatty", mock_isatty)
81+
82+
args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, ".notdef"])
83+
direction_run(args)
84+
85+
captured = capsys.readouterr()
86+
# must be in a tty to get ANSI color output
87+
# this is mocked above
88+
assert "[ .notdef ]: no contours" in captured.out
89+
90+
91+
def test_path_run_single_glyph_non_composite_clockwise_default(capsys, monkeypatch):
92+
def mock_isatty():
93+
return True
94+
95+
# apply the monkeypatch for sys.stdout.isatty()
96+
monkeypatch.setattr(sys.stdout, "isatty", mock_isatty)
97+
98+
args = parser.parse_args([TESTFONT_PATH_1, "A"])
99+
direction_run(args)
100+
101+
captured = capsys.readouterr()
102+
# must be in a tty to get ANSI color output
103+
# this is mocked above
104+
assert "[ \x1b[1;36mA\x1b[0m ]: clockwise" in captured.out
105+
106+
107+
def test_path_run_single_glyph_non_composite_clockwise_nocolor(capsys, monkeypatch):
108+
def mock_isatty():
109+
return True
110+
111+
# apply the monkeypatch for sys.stdout.isatty()
112+
monkeypatch.setattr(sys.stdout, "isatty", mock_isatty)
113+
114+
args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, "A"])
115+
direction_run(args)
116+
117+
captured = capsys.readouterr()
118+
# must be in a tty to get ANSI color output
119+
# this is mocked above
120+
assert "[ A ]: clockwise" in captured.out
121+
122+
123+
def test_path_run_single_glyph_composite_counter_clockwise_default(capsys, monkeypatch):
124+
def mock_isatty():
125+
return True
126+
127+
# apply the monkeypatch for sys.stdout.isatty()
128+
monkeypatch.setattr(sys.stdout, "isatty", mock_isatty)
129+
130+
args = parser.parse_args([TESTFONT_PATH_1, "uni2E2E"])
131+
direction_run(args)
132+
133+
captured = capsys.readouterr()
134+
# must be in a tty to get ANSI color output
135+
# this is mocked above
136+
assert "[ \x1b[1;36muni2E2E\x1b[0m ]: counter-clockwise" in captured.out
137+
assert "with component 'question' transform: [[-1.0, 0], [0, 1.0]]" in captured.out
138+
139+
140+
def test_path_run_single_glyph_composite_counter_clockwise_nocolor(capsys, monkeypatch):
141+
def mock_isatty():
142+
return True
143+
144+
# apply the monkeypatch for sys.stdout.isatty()
145+
monkeypatch.setattr(sys.stdout, "isatty", mock_isatty)
146+
147+
args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, "uni2E2E"])
148+
direction_run(args)
149+
150+
captured = capsys.readouterr()
151+
# must be in a tty to get ANSI color output
152+
# this is mocked above
153+
assert captured.out == (
154+
"[ uni2E2E ]: counter-clockwise\n"
155+
" with component 'question' transform: [[-1.0, 0], [0, 1.0]]\n"
156+
)

0 commit comments

Comments
 (0)