Debian Codegraph @

tests/test_substitution.py

raw
1 from fontFeatures import Substitution
2 from lxml import etree
3
4 import unittest
5
6
7 class TestSubstitution(unittest.TestCase):
8 def roundTrip(self, thing):
9 rt = thing.__class__.fromXML(thing.toXML())
10 self.assertEqual(rt.asFea(), thing.asFea())
11
12 def test_single(self):
13 s = Substitution(["a"], ["b"])
14 self.assertEqual(s.asFea(), "sub a by b;")
15 self.assertEqual(s.involved_glyphs, set(["a", "b"]))
16 self.assertEqual(
17 etree.tostring(s.toXML()),
18 "<substitution><from><slot><glyph>a</glyph></slot></from><to><slot><glyph>b</glyph></slot></to></substitution>".encode(
19 "utf-8"
20 ),
21 )
22 self.roundTrip(s)
23
24 def test_single_classes(self):
25 s = Substitution([["a", "b"]], [["c", "d"]])
26 self.assertEqual(s.asFea(), "sub [a b] by [c d];")
27 self.assertEqual(s.involved_glyphs, set(["a", "b", "c", "d"]))
28 self.roundTrip(s)
29
30 def test_ligature(self):
31 s = Substitution(["a", "b"], ["c"])
32 self.assertEqual(s.asFea(), "sub a b by c;")
33 self.assertEqual(s.involved_glyphs, set(["a", "b", "c"]))
34 self.roundTrip(s)
35
36 def test_ligature_expansion(self):
37 s = Substitution([["f", "f.ss01"], ["i", "i.ss01"]], [["f_i", "f_i.ss01"]])
38 self.assertEqual(
39 s.asFea(), " sub f i by f_i;\n sub f.ss01 i.ss01 by f_i.ss01;\n"
40 )
41 self.assertEqual(
42 s.involved_glyphs, set(["f", "i", "f_i", "f.ss01", "i.ss01", "f_i.ss01"])
43 )
44 self.roundTrip(s)
45
46 def test_multiple(self):
47 s = Substitution(["a"], ["b", "c"])
48 self.assertEqual(s.asFea(), "sub a by b c;")
49 self.assertEqual(s.involved_glyphs, set(["a", "b", "c"]))
50 self.roundTrip(s)
51
52 def test_multiple_expansion(self):
53 s = Substitution([["a", "b"]], [["a", "b"], "c"])
54 self.assertEqual(s.asFea(), " sub a by a c;\n sub b by b c;\n")
55 self.assertEqual(s.involved_glyphs, set(["a", "b", "c"]))
56 self.roundTrip(s)
57
58 def test_multiple_expansion_middle(self):
59 s = Substitution([["aa", "bb"]], ["d", ["aa", "bb"], "c"])
60 self.assertEqual(s.asFea(), " sub aa by d aa c;\n sub bb by d bb c;\n")
61 self.assertEqual(s.involved_glyphs, set(["aa", "bb", "c", "d"]))
62 self.roundTrip(s)
63
64 def test_alternate(self):
65 s = Substitution(["a"], [["b", "c"]])
66 self.assertEqual(s.asFea(), "sub a from [b c];")
67 self.assertEqual(s.involved_glyphs, set(["a", "b", "c"]))
68 self.roundTrip(s)