1#!/usr/bin/env python3 2 3import os 4 5# A single test in a subset test suite. Identifies a font 6# a subsetting profile, and a subset to be cut. 7class Test: 8 def __init__(self, font_path, profile_path, subset): 9 self.font_path = font_path 10 self.profile_path = profile_path 11 self.subset = subset 12 13 def unicodes(self): 14 if self.subset == '*': 15 return self.subset[0] 16 else: 17 return ",".join("%X" % ord(c) for (i, c) in enumerate(self.subset)) 18 19 def get_profile_flags(self): 20 with open (self.profile_path, mode="r", encoding="utf-8") as f: 21 return f.read().splitlines() 22 23 def get_font_name(self): 24 font_base_name = os.path.basename(self.font_path) 25 font_base_name_parts = os.path.splitext(font_base_name) 26 profile_name = os.path.splitext(os.path.basename(self.profile_path))[0] 27 28 if self.unicodes() == "*": 29 return "%s.%s.retain-all-codepoint%s" % (font_base_name_parts[0], 30 profile_name, 31 font_base_name_parts[1]) 32 else: 33 return "%s.%s.%s%s" % (font_base_name_parts[0], 34 profile_name, 35 self.unicodes(), 36 font_base_name_parts[1]) 37 38 def get_font_extension(self): 39 font_base_name = os.path.basename(self.font_path) 40 font_base_name_parts = os.path.splitext(font_base_name) 41 return font_base_name_parts[1] 42 43# A group of tests to perform on the subsetter. Each test 44# Identifies a font a subsetting profile, and a subset to be cut. 45class SubsetTestSuite: 46 47 def __init__(self, test_path, definition): 48 self.test_path = test_path 49 self.fonts = [] 50 self.profiles = [] 51 self.subsets = [] 52 self._parse(definition) 53 54 def get_output_directory(self): 55 test_name = os.path.splitext(os.path.basename(self.test_path))[0] 56 data_dir = os.path.join(os.path.dirname(self.test_path), "..") 57 58 output_dir = os.path.normpath(os.path.join(data_dir, "expected", test_name)) 59 if not os.path.exists(output_dir): 60 os.mkdir(output_dir) 61 if not os.path.isdir(output_dir): 62 raise Exception("%s is not a directory." % output_dir) 63 64 return output_dir 65 66 def tests(self): 67 for font in self.fonts: 68 font = os.path.join(self._base_path(), "fonts", font) 69 for profile in self.profiles: 70 profile = os.path.join(self._base_path(), "profiles", profile) 71 for subset in self.subsets: 72 yield Test(font, profile, subset) 73 74 def _base_path(self): 75 return os.path.dirname(os.path.dirname(self.test_path)) 76 77 def _parse(self, definition): 78 destinations = { 79 "FONTS:": self.fonts, 80 "PROFILES:": self.profiles, 81 "SUBSETS:": self.subsets 82 } 83 84 current_destination = None 85 for line in definition.splitlines(): 86 line = line.strip() 87 88 if line.startswith("#"): 89 continue 90 91 if not line: 92 continue 93 94 if line in destinations: 95 current_destination = destinations[line] 96 elif current_destination is not None: 97 current_destination.append(line) 98 else: 99 raise Exception("Failed to parse test suite file.") 100