Imported Upstream version 2.4.0
[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         def get_font_extension(self):
32                 font_base_name = os.path.basename(self.font_path)
33                 font_base_name_parts = os.path.splitext(font_base_name)
34                 return font_base_name_parts[1]
35
36 # A group of tests to perform on the subsetter. Each test
37 # Identifies a font a subsetting profile, and a subset to be cut.
38 class SubsetTestSuite:
39
40         def __init__(self, test_path, definition):
41                 self.test_path = test_path
42                 self.fonts = set()
43                 self.profiles = set()
44                 self.subsets = set()
45                 self._parse(definition)
46
47         def get_output_directory(self):
48                 test_name = os.path.splitext(os.path.basename(self.test_path))[0]
49                 data_dir = os.path.join(os.path.dirname(self.test_path), "..")
50
51                 output_dir = os.path.normpath(os.path.join(data_dir, "expected", test_name))
52                 if not os.path.exists(output_dir):
53                         os.mkdir(output_dir)
54                 if not os.path.isdir(output_dir):
55                         raise Exception("%s is not a directory." % output_dir)
56
57                 return output_dir
58
59         def tests(self):
60                 for font in self.fonts:
61                         font = os.path.join(self._base_path(), "fonts", font)
62                         for profile in self.profiles:
63                                 profile = os.path.join(self._base_path(), "profiles", profile)
64                                 for subset in self.subsets:
65                                         yield Test(font, profile, subset)
66
67         def _base_path(self):
68                 return os.path.dirname(os.path.dirname(self.test_path))
69
70         def _parse(self, definition):
71                 destinations = {
72                                 "FONTS:": self.fonts,
73                                 "PROFILES:": self.profiles,
74                                 "SUBSETS:": self.subsets
75                 }
76
77                 current_destination = None
78                 for line in definition.splitlines():
79                         line = line.strip()
80
81                         if line.startswith("#"):
82                                 continue
83
84                         if not line:
85                                 continue
86
87                         if line in destinations:
88                                 current_destination = destinations[line]
89                         elif current_destination is not None:
90                                 current_destination.add(line)
91                         else:
92                                 raise Exception("Failed to parse test suite file.")