xref: /aosp_15_r20/external/fonttools/Tests/ttLib/ttVisitor_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1from fontTools.ttLib import TTFont
2from fontTools.ttLib.ttVisitor import TTVisitor
3import os
4import pytest
5
6
7class TestVisitor(TTVisitor):
8    def __init__(self):
9        self.value = []
10        self.depth = 0
11
12    def _add(self, s):
13        self.value.append(s)
14
15    def visit(self, obj, target_depth):
16        if self.depth == target_depth:
17            self._add(obj)
18        self.depth += 1
19        super().visit(obj, target_depth)
20        self.depth -= 1
21
22
23class TTVisitorTest(object):
24    @staticmethod
25    def getpath(testfile):
26        path = os.path.dirname(__file__)
27        return os.path.join(path, "data", testfile)
28
29    def test_ttvisitor(self):
30        font = TTFont(self.getpath("TestVGID-Regular.otf"))
31        visitor = TestVisitor()
32
33        # Count number of objects at depth 1:
34        # That is, number of font tables, including GlyphOrder.
35        visitor.visit(font, 1)
36
37        assert len(visitor.value) == 14
38