-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_kernFeatureWriter.py
More file actions
executable file
·491 lines (410 loc) · 14 KB
/
test_kernFeatureWriter.py
File metadata and controls
executable file
·491 lines (410 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import defcon
import pytest
import sys
from afdko.fdkutils import get_temp_dir_path
from pathlib import Path
sys.path.append("..")
from kernFeatureWriter import *
TEST_DIR = Path(__file__).parent
TEMP_DIR = Path(get_temp_dir_path())
def run_local(input, args):
if input is None:
a = None
elif isinstance(input, defcon.Font):
a = UFOKernAdapter(input)
else:
a = DesignspaceKernAdapter(input)
return run(a, args)
class Dummy(object):
'''
for ad-hoc arguments
'''
pass
def read_file(path):
'''
Read a file, split lines into a list, close the file.
'''
with open(path, 'r', encoding='utf-8') as f:
data = f.read().splitlines()
return data
# unit tests
# ----------
def test_get_args():
# args through argparse
input_ufo = str(TEST_DIR / 'kern_example.ufo')
argparse_args = vars(get_args([input_ufo]))
# hard-coded dummy arguments
dummy_args = Defaults().__dict__
dummy_args['input_file'] = input_ufo
assert argparse_args == dummy_args
def test_make_header():
kfw = run_local(None, None)
dummy_args = Dummy()
dummy_args.min_value = 1
dummy_args.write_timestamp = False
header = kfw.make_header(dummy_args)
assert len(header) == 2
assert header[0] == '# PS Name: None'
dummy_args.write_timestamp = True
header = kfw.make_header(dummy_args)
assert header[0].startswith('# Created:')
def test_dict2pos():
ufo_path = TEST_DIR / 'kern_example.ufo'
f = defcon.Font(ufo_path)
kfw = run_local(f, None)
kfw.write_trimmed_pairs = False
pv_dict = {
('A', 'V'): 3,
('A', 'X'): 2,
('A', 'A'): 1,
}
assert kfw._dict2pos(pv_dict) == (
'pos A A 1;\n'
'pos A V 3;\n'
'pos A X 2;'
)
assert kfw._dict2pos(pv_dict, minimum=2) == (
'pos A V 3;\n'
'pos A X 2;'
)
assert kfw._dict2pos(pv_dict, rtl=True) == (
'pos A A <1 0 1 0>;\n'
'pos A V <3 0 3 0>;\n'
'pos A X <2 0 2 0>;'
)
kfw.write_trimmed_pairs = True
assert kfw._dict2pos(pv_dict, minimum=2) == (
'# pos A A 1;\n'
'pos A V 3;\n'
'pos A X 2;'
)
def test_remap_name():
ufo_path = TEST_DIR / 'kern_example.ufo'
a = UFOKernAdapter(defcon.Font(ufo_path))
kp = KernProcessor(a)
assert kp._remap_name('public.kern1.example') == '@MMK_L_example'
assert kp._remap_name('public.kern1.@MMK_L_example') == '@MMK_L_example'
assert kp._remap_name('public.kern2.example') == '@MMK_R_example'
assert kp._remap_name('public.kern2.@MMK_R_example') == '@MMK_R_example'
assert kp._remap_name('@example') == '@example'
def test_remap_groups():
ufo_path = TEST_DIR / 'kern_example.ufo'
f = defcon.Font(ufo_path)
groups_l = {
gr: gl for gr, gl in f.groups.items() if gr.startswith('public.kern1')}
groups_r = {
gr: gl for gr, gl in f.groups.items() if gr.startswith('public.kern2')}
groups_other = {
gr: gl for gr, gl in f.groups.items() if gr not in groups_l.keys() | groups_r.keys()}
expected_groups_l = {
gr.replace('public.kern1.', '@MMK_L_'): gl for gr, gl in groups_l.items()}
expected_groups_r = {
gr.replace('public.kern2.', '@MMK_R_'): gl for gr, gl in groups_r.items()}
kp = KernProcessor(UFOKernAdapter(f))
assert kp._remap_groups(groups_l) == expected_groups_l
assert kp._remap_groups(groups_r) == expected_groups_r
assert kp._remap_groups(groups_other) == groups_other
def test_remap_kerning():
ufo_path = TEST_DIR / 'kern_example.ufo'
f = defcon.Font(ufo_path)
# https://stackoverflow.com/a/15175239
import re
remapped_pairs = []
replacements = {
'public.kern1.': '@MMK_L_',
'public.kern2.': '@MMK_R_'
}
regex = re.compile("(%s)" % "|".join(map(re.escape, replacements.keys())))
for pair in f.kerning.keys():
new_pair = regex.sub(
lambda mo: replacements[mo.group()], ' '.join(pair)).split()
remapped_pairs.append(tuple(new_pair))
kp = KernProcessor(UFOKernAdapter(f))
assert list(kp._remap_kerning(f.kerning).keys()) == remapped_pairs
def test_sanityCheck(capsys):
'''
somehow trigger that sanity check (not sure how useful)
'''
ufo_path = TEST_DIR / 'kern_example.ufo'
f = defcon.Font(ufo_path)
kp = KernProcessor(UFOKernAdapter(f))
kp.pairs_processed = ['some pair']
kp.kerning = f.kerning
kp._sanityCheck()
out, err = capsys.readouterr()
assert 'Something went wrong' in out
# integration tests
# -----------------
def test_no_kerning(capsys):
ufo_path = TEST_DIR / 'kern_example.ufo'
f = defcon.Font(ufo_path)
f.kerning.clear()
args = Defaults()
run_local(f, args)
out, err = capsys.readouterr()
assert f'has no kerning' in out
def test_all_zero(capsys):
ufo_path = TEST_DIR / 'kern_all_zero_value.ufo'
f = defcon.Font(ufo_path)
args = Defaults()
run_local(f, args)
out, err = capsys.readouterr()
assert f'All kerning values are zero' in out
def test_default():
'''
normal LTR test with no options
'''
args = Defaults()
ufo_path = TEST_DIR / 'kern_example.ufo'
fea_example = TEST_DIR / 'kern_example.fea'
fea_temp = TEMP_DIR / fea_example.name
args.input_file = ufo_path
args.output_name = fea_temp
f = defcon.Font(ufo_path)
run_local(f, args)
assert read_file(fea_temp) == read_file(fea_example)
'''
test with --dissolve_single option, which should not make a difference
for this UFO (no single-item groups)
'''
args.dissolve_single = True
run_local(f, args)
assert read_file(fea_temp) == read_file(fea_example)
def test_default_ufo2():
'''
normal LTR test for a UFO2 file
'''
args = Defaults()
ufo_path = TEST_DIR / 'kern_example_ufo2.ufo'
fea_example = TEST_DIR / 'kern_example.fea'
fea_temp = TEMP_DIR / fea_example.name
args.input_file = ufo_path
args.output_name = fea_temp
f = defcon.Font(ufo_path)
run_local(f, args)
assert read_file(fea_temp) == read_file(fea_example)
def test_main():
'''
same as test_default, using the main() path into the module
'''
ufo_path = TEST_DIR / 'kern_example.ufo'
fea_example = TEST_DIR / 'kern_example.fea'
fea_temp = TEMP_DIR / fea_example.name
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
main([str(ufo_path), '--output_name', str(fea_temp)])
assert read_file(fea_example) == read_file(fea_temp)
def test_phantom_input_ufo(capsys):
'''
non-existent input UFO
'''
ufo_path = TEST_DIR / 'phantom.ufo'
args = Defaults()
args.input_file = ufo_path
with pytest.raises(SystemExit):
main([str(ufo_path)])
out, err = capsys.readouterr()
assert 'phantom.ufo does not exist' in err
def test_invalid_input_file(capsys):
'''
invalid input file
'''
ufo_path = TEST_DIR / 'some_file.xxx'
args = Defaults()
args.input_file = ufo_path
with pytest.raises(SystemExit):
main([str(ufo_path)])
out, err = capsys.readouterr()
assert 'Unrecognized input file type' in err
def test_default_rtl():
args = Defaults()
ufo_path = TEST_DIR / 'kern_example_rtl.ufo'
fea_example = TEST_DIR / 'kern_example_rtl.fea'
fea_temp = TEMP_DIR / fea_example.name
args.input_file = ufo_path
args.output_name = fea_temp
f = defcon.Font(ufo_path)
run_local(f, args)
assert read_file(fea_temp) == read_file(fea_example)
def test_subtable():
'''
test writing a file with subtable breaks
'''
args = Defaults()
ufo_path = TEST_DIR / 'kern_example.ufo'
fea_example = TEST_DIR / 'kern_example_subs.fea'
fea_temp = TEMP_DIR / fea_example.name
args.input_file = ufo_path
args.write_subtables = True
args.subtable_size = 128
args.output_name = fea_temp
f = defcon.Font(ufo_path)
run_local(f, args)
assert read_file(fea_temp) == read_file(fea_example)
def test_subtable_rtl():
'''
test writing a file with subtable breaks
'''
args = Defaults()
ufo_path = TEST_DIR / 'kern_example_rtl.ufo'
fea_example = TEST_DIR / 'kern_example_rtl_subs.fea'
fea_temp = TEMP_DIR / fea_example.name
args.input_file = ufo_path
args.write_subtables = True
args.subtable_size = 128
args.output_name = fea_temp
f = defcon.Font(ufo_path)
run_local(f, args)
assert read_file(fea_temp) == read_file(fea_example)
def test_dissolve():
'''
test dissolving single-glyph groups
'''
args = Defaults()
ufo_path = TEST_DIR / 'kern_AV.ufo'
fea_example_singletons = TEST_DIR / 'kern_AV_singletons.fea'
fea_temp_singletons = TEMP_DIR / fea_example_singletons.name
fea_example_dissolved = TEST_DIR / 'kern_AV_dissolved.fea'
fea_temp_dissolved = TEMP_DIR / fea_example_dissolved.name
args.input_file = ufo_path
args.output_name = fea_temp_singletons
f = defcon.Font(ufo_path)
run_local(f, args)
assert read_file(fea_temp_singletons) == read_file(fea_example_singletons)
args.dissolve_single = True
args.output_name = fea_temp_dissolved
run_local(f, args)
assert read_file(fea_temp_dissolved) == read_file(fea_example_dissolved)
def test_left_side_exception():
'''
test a kerning exception of a single member of a left-side group
(Adieresis for the A-group, Oslash for the O-group) to a right-side item.
'''
args = Defaults()
ufo_path = TEST_DIR / 'kern_left_side_exception.ufo'
fea_example = TEST_DIR / 'kern_left_side_exception.fea'
fea_temp = TEMP_DIR / fea_example.name
args.input_file = ufo_path
args.output_name = fea_temp
f = defcon.Font(ufo_path)
run_local(f, args)
assert read_file(fea_temp) == read_file(fea_example)
def test_unused_groups():
ufo_path = TEST_DIR / 'kern_unused_groups.ufo'
fea_example = TEST_DIR / 'kern_unused_groups.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)
def test_ignored_groups():
'''
group/group kern value is 0, all pairs are exceptions
'''
ufo_path = TEST_DIR / 'kern_ignored_groups.ufo'
fea_example = TEST_DIR / 'kern_ignored_groups.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)
def test_no_groups():
'''
input file with no groups at all
'''
ufo_path = TEST_DIR / 'kern_no_groups.ufo'
fea_example = TEST_DIR / 'kern_no_groups.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)
def test_ss4_exceptions():
'''
This contains most exceptions from SS4.
'''
ufo_path = TEST_DIR / 'kern_ss4_exceptions.ufo'
fea_example = TEST_DIR / 'kern_ss4_exceptions.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)
def test_mock_rtl():
'''
A mock RTL project (mirrored version of ss4_exceptions)
'''
ufo_path = TEST_DIR / 'kern_mock_rtl.ufo'
fea_example = TEST_DIR / 'kern_mock_rtl.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)
def test_example_trim(capsys):
ufo_path = TEST_DIR / 'kern_example.ufo'
fea_example = TEST_DIR / 'kern_example_trim.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
args.min_value = 100
args.write_trimmed_pairs = True
run_local(f, args)
out, err = capsys.readouterr()
assert 'Trimmed pairs: 33' in out
assert read_file(fea_example) == read_file(fea_temp)
def test_nightmare(capsys):
'''
A nightmare project which contains many problems in groups and kerning
'''
ufo_path = TEST_DIR / 'kern_nightmare.ufo'
fea_example = TEST_DIR / 'kern_nightmare.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)
out, err = capsys.readouterr()
print(out)
expected_output = (
'group public.kern1.empty is empty\n'
'group public.kern2.empty is empty\n'
'group public.kern1.invalid contains extraneous glyph(s): [a]\n'
'group public.kern1.lowercase contains extraneous glyph(s): [x, y, z]\n'
'pair (A public.kern2.invalid) references invalid group public.kern2.invalid\n'
'pair (public.kern1.LAT_A a) references non-existent glyph a\n'
'pair (public.kern1.empty a) references invalid group public.kern1.empty\n'
'pair (public.kern1.empty a) references non-existent glyph a\n'
)
assert expected_output in out
def test_ignore_suffix():
ufo_path = TEST_DIR / 'kern_suffix.ufo'
fea_example = TEST_DIR / 'kern_suffix_present.fea'
fea_temp = TEMP_DIR / fea_example.name
f = defcon.Font(ufo_path)
args = Defaults()
args.input_file = ufo_path
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)
fea_example = TEST_DIR / 'kern_suffix_ignored.fea'
fea_temp = TEMP_DIR / fea_example.name
args.ignore_suffix = '.cxt'
args.output_name = fea_temp
run_local(f, args)
assert read_file(fea_example) == read_file(fea_temp)