Debian Codegraph @

tests/test_chaining.py

raw
1 from fontFeatures import (
2 Chaining,
3 Positioning,
4 ValueRecord,
5 Routine,
6 Substitution,
7 RoutineReference,
8 FontFeatures,
9 )
10 from lxml import etree
11
12
13 import unittest
14
15
16 class TestChaining(unittest.TestCase):
17 def roundTrip(self, thing, dependencies):
18 f = FontFeatures()
19 f.routines.extend(dependencies)
20 rt = thing.__class__.fromXML(thing.toXML())
21 f.routines.append(Routine(rules=[rt]))
22 f.resolveAllRoutines()
23 self.assertEqual(rt.asFea(), thing.asFea())
24
25 def test_simple_pos(self):
26 v = ValueRecord(xAdvance=120)
27 pos = Positioning(["a"], [v])
28 r = Routine(rules=[pos], name="dummy")
29 rr = RoutineReference(routine=r)
30
31 c = Chaining([["a"], ["b"]], lookups=[[rr], None])
32 self.assertEqual(c.asFea(), "pos a' lookup dummy b';")
33 xml = c.toXML()
34 self.assertEqual(
35 etree.tostring(xml),
36 '<chaining><lookups><slot><routinereference name="dummy"/></slot><slot><lookup/></slot></lookups><input><slot><glyph>a</glyph></slot><slot><glyph>b</glyph></slot></input></chaining>'.encode(
37 "utf-8"
38 ),
39 )
40 self.roundTrip(c, [r])
41
42 def test_simple_sub(self):
43 pos = Substitution(["a"], ["b"])
44 r = Routine(rules=[pos], name="dummy")
45 rr = RoutineReference(routine=r)
46
47 c = Chaining([["a"], ["b"]], lookups=[[rr], None])
48 self.assertEqual(c.asFea(), "sub a' lookup dummy b';")
49
50 def test_ignore(self):
51 c = Chaining([["a"], ["b"]], lookups=[])
52 self.assertEqual(c.asFea(), "ignore sub a' b';")
53
54 def test_complex(self):
55 pos1 = Substitution(["a"], ["b"])
56 pos2 = Substitution(["b"], ["c"])
57 r1 = Routine(rules=[pos1], name="dummy1")
58 r2 = Routine(rules=[pos2], name="dummy2")
59 rr1 = RoutineReference(routine=r1)
60 rr2 = RoutineReference(routine=r2)
61
62 c = Chaining([["a"], ["b"]], lookups=[[rr1, rr2], None])
63 self.assertEqual(c.asFea(), "sub a' lookup dummy1 lookup dummy2 b';")
64
65 def test_complex_pos(self):
66 v = ValueRecord(xAdvance=120)
67 pos1 = Positioning(["a"], [v])
68 r1 = Routine(rules=[pos1])
69 rr1 = RoutineReference(routine=r1)
70
71 c = Chaining([["a"], ["b"]], lookups=[[rr1], None])
72 c.feaPreamble(FontFeatures())
73 self.assertEqual(c.asFea(), "pos a' lookup ChainedRoutine1 b';")