Add basic directory structure for subsetter integration tests. Plus a utility for generating expected output files.
This commit is contained in:
parent
9ccb8366f6
commit
4cdae914e2
Binary file not shown.
BIN
test/subset/data/fonts/Roboto-Regular.abc.ttf
Normal file
BIN
test/subset/data/fonts/Roboto-Regular.abc.ttf
Normal file
Binary file not shown.
0
test/subset/data/profiles/default.txt
Normal file
0
test/subset/data/profiles/default.txt
Normal file
8
test/subset/data/tests/basics.txt
Normal file
8
test/subset/data/tests/basics.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
FONTS:
|
||||||
|
Roboto-Regular.abc.ttf
|
||||||
|
|
||||||
|
PROFILES:
|
||||||
|
default.txt
|
||||||
|
|
||||||
|
SUBSETS:
|
||||||
|
b
|
40
test/subset/generate-expected-outputs.py
Executable file
40
test/subset/generate-expected-outputs.py
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Pre-generates the expected output subset files (via fonttools) for
|
||||||
|
# specified subset test suite(s).
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from subprocess import check_call
|
||||||
|
from subset_test_suite import SubsetTestSuite
|
||||||
|
|
||||||
|
|
||||||
|
def usage():
|
||||||
|
print "Usage: generate-expected-outputs.py <test suite file> ..."
|
||||||
|
|
||||||
|
|
||||||
|
def generate_expected_output(input_file, unicodes, output_path):
|
||||||
|
check_call(["fonttools", "subset",
|
||||||
|
input_file,
|
||||||
|
"--unicodes=%s" % unicodes,
|
||||||
|
"--output-file=%s" % output_path])
|
||||||
|
|
||||||
|
|
||||||
|
args = sys.argv[1:]
|
||||||
|
if not args:
|
||||||
|
usage()
|
||||||
|
|
||||||
|
for path in args:
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
test_suite = SubsetTestSuite(path, f.read())
|
||||||
|
output_directory = test_suite.get_output_directory()
|
||||||
|
|
||||||
|
print "Generating output files for %s" % output_directory
|
||||||
|
for test in test_suite.tests():
|
||||||
|
unicodes = test.unicodes()
|
||||||
|
font_name = test.get_font_name()
|
||||||
|
print "Creating subset %s/%s" % (output_directory, font_name)
|
||||||
|
generate_expected_output(test.font_path, unicodes,
|
||||||
|
os.path.join(output_directory,
|
||||||
|
font_name))
|
82
test/subset/subset_test_suite.py
Normal file
82
test/subset/subset_test_suite.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
# A single test in a subset test suite. Identifies a font
|
||||||
|
# a subsetting profile, and a subset to be cut.
|
||||||
|
class Test:
|
||||||
|
def __init__(self, font_path, profile_path, subset):
|
||||||
|
self.font_path = font_path
|
||||||
|
self.profile_path = profile_path
|
||||||
|
self.subset = subset
|
||||||
|
|
||||||
|
def unicodes(self):
|
||||||
|
return ",".join("%X" % ord(c) for (i, c) in enumerate(self.subset))
|
||||||
|
|
||||||
|
def get_font_name(self):
|
||||||
|
font_base_name = os.path.basename(self.font_path)
|
||||||
|
font_base_name_parts = os.path.splitext(font_base_name)
|
||||||
|
profile_name = os.path.splitext(os.path.basename(self.profile_path))[0]
|
||||||
|
|
||||||
|
return "%s.%s.%s%s" % (font_base_name_parts[0],
|
||||||
|
profile_name,
|
||||||
|
self.unicodes(),
|
||||||
|
font_base_name_parts[1])
|
||||||
|
|
||||||
|
# A group of tests to perform on the subsetter. Each test
|
||||||
|
# Identifies a font a subsetting profile, and a subset to be cut.
|
||||||
|
class SubsetTestSuite:
|
||||||
|
|
||||||
|
def __init__(self, test_path, definition):
|
||||||
|
self.test_path = test_path
|
||||||
|
self.fonts = set()
|
||||||
|
self.profiles = set()
|
||||||
|
self.subsets = set()
|
||||||
|
self._parse(definition)
|
||||||
|
|
||||||
|
def get_output_directory(self):
|
||||||
|
test_name = os.path.splitext(os.path.basename(self.test_path))[0]
|
||||||
|
data_dir = os.path.join(os.path.dirname(self.test_path), "..")
|
||||||
|
|
||||||
|
output_dir = os.path.normpath(os.path.join(data_dir, "expected", test_name))
|
||||||
|
if not os.path.exists(output_dir):
|
||||||
|
os.mkdir(output_dir)
|
||||||
|
if not os.path.isdir(output_dir):
|
||||||
|
raise Error("%s is not a directory." % output_dir)
|
||||||
|
|
||||||
|
return output_dir
|
||||||
|
|
||||||
|
def tests(self):
|
||||||
|
for font in self.fonts:
|
||||||
|
font = os.path.join(self._base_path(), "fonts", font)
|
||||||
|
for profile in self.profiles:
|
||||||
|
profile = os.path.join(self._base_path(), "profiles", profile)
|
||||||
|
for subset in self.subsets:
|
||||||
|
yield Test(font, profile, subset)
|
||||||
|
|
||||||
|
def _base_path(self):
|
||||||
|
return os.path.dirname(os.path.dirname(self.test_path))
|
||||||
|
|
||||||
|
def _parse(self, definition):
|
||||||
|
destinations = {
|
||||||
|
"FONTS:": self.fonts,
|
||||||
|
"PROFILES:": self.profiles,
|
||||||
|
"SUBSETS:": self.subsets
|
||||||
|
}
|
||||||
|
|
||||||
|
current_destination = None
|
||||||
|
for line in definition.splitlines():
|
||||||
|
line = line.strip()
|
||||||
|
|
||||||
|
if line.startswith("#"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line in destinations:
|
||||||
|
current_destination = destinations[line]
|
||||||
|
elif current_destination is not None:
|
||||||
|
current_destination.add(line)
|
||||||
|
else:
|
||||||
|
raise Exception("Failed to parse test suite file.")
|
Loading…
Reference in New Issue
Block a user