Skip to content

Commit 84c1ab1

Browse files
committed
add tests
1 parent 27ac219 commit 84c1ab1

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

Lib/gftools/fontsetter.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,27 @@ def hasmethod(obj, name):
3232
return hasattr(obj, name) and type(getattr(obj, name)) == types.MethodType
3333

3434

35-
def update_all(obj, config):
35+
def set_all(obj, config):
3636
for path, value in config.items():
37-
update(obj, path, value)
37+
setter(obj, path, value)
3838

3939

40-
def update(obj, path, val):
40+
def getter(obj, path):
41+
if len(path) == 0:
42+
return obj
43+
key = path[0]
44+
if hasmethod(obj, key):
45+
import pdb
46+
pdb.set_trace()
47+
return getattr(obj, key)(*path[1])
48+
if isinstance(key, str) and hasattr(obj, key):
49+
return getter(getattr(obj, key), path[1:])
50+
if isinstance(obj, (list, dict, tuple, TTFont)):
51+
return getter(obj[key], path[1:])
52+
return obj
53+
54+
55+
def setter(obj, path, val):
4156
if len(path) == 0:
4257
return
4358
key = path[0]
@@ -52,7 +67,7 @@ def update(obj, path, val):
5267
return
5368

5469
if isinstance(key, str) and hasattr(obj, key):
55-
update(getattr(obj, key), path[1:])
70+
setter(getattr(obj, key), path[1:], val)
5671
elif isinstance(obj, (list, dict, tuple, TTFont)):
5772
is_tuple = False
5873
# convert numeric keys if needed
@@ -65,7 +80,7 @@ def update(obj, path, val):
6580
if isinstance(obj[key], tuple):
6681
is_tuple = True
6782
obj[key] = list(obj[key])
68-
update(obj[key], path[1:], val)
83+
setter(obj[key], path[1:], val)
6984
if is_tuple:
7085
obj[key] = tuple(obj[key])
7186

@@ -78,7 +93,7 @@ def main(args=None):
7893
args = parser.parse_args(args)
7994

8095
config = load_config(args.config)
81-
update_all(args.font, config)
96+
set_all(args.font, config)
8297

8398
if not args.out:
8499
args.out = makeOutputFileName(args.font.reader.file.name)

tests/test_fontsetter.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from gftools.fontsetter import setter, getter
2+
import pytest
3+
from fontTools.ttLib import TTFont
4+
import os
5+
6+
7+
TEST_FONT = TTFont(os.path.join("data", "test", "Inconsolata[wdth,wght].ttf"))
8+
9+
10+
@pytest.mark.parametrize(
11+
"""obj,path,val,res""",
12+
[
13+
(
14+
[10], [0], 100, [100]
15+
),
16+
(
17+
[10, [10]], [1, 0], 100, [10, [100]]
18+
),
19+
(
20+
[(0, [1, (2,)]), [1, 0], 100, (0, [100, (2,)])]
21+
),
22+
(
23+
[[0, (0, (0, ))], [1, 1, 0], 100, [0, (0, (100, ))]]
24+
),
25+
(
26+
{"A": {"B": [0, [10]]}}, ["A", "B", 1], [100], {"A": {"B": [0, [100]]}}
27+
)
28+
]
29+
)
30+
def test_setter(obj, path, val, res):
31+
setter(obj, path, val)
32+
assert obj == res
33+
34+
35+
@pytest.mark.parametrize(
36+
"""obj,path,val""",
37+
[
38+
# simple atttribs
39+
(
40+
TEST_FONT, ["OS/2", "fsSelection"], 64
41+
),
42+
(
43+
TEST_FONT, ["hhea", "ascender"], 1000
44+
),
45+
# attrib then dict
46+
(
47+
TEST_FONT, ["hmtx", "metrics", "A"], (10, 10)
48+
),
49+
# attrib then attrib
50+
(
51+
TEST_FONT, ["OS/2", "panose", "bSerifStyle"], 10
52+
)
53+
]
54+
)
55+
def test_setter_on_fonts(obj, path, val):
56+
setter(obj, path, val)
57+
assert getter(obj, path) == val

0 commit comments

Comments
 (0)