subset_test_suite.py 3.4 KB

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