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