[mtiLib] Remove use of MockFont in tests

Tests pass on both Python 2 and 3 now. Yay!

MockFont is handy for mtiLib.__init__:main().  Not sure how useful it is
otherwise, and what to replace it with.
This commit is contained in:
Behdad Esfahbod 2016-12-27 13:28:06 -05:00
parent f6294ba7bd
commit edd46bb14a
6 changed files with 391 additions and 53 deletions

View File

@ -253,7 +253,6 @@ def parseSinglePos(lines, font, _lookupMap=None):
def parsePair(self, lines, font, _lookupMap=None):
self.ValueFormat1 = self.ValueFormat2 = 0
getGlyphID = font.getGlyphID
typ = lines.peek()[0].split()[0].lower()
if typ in ('left', 'right'):
self.Format = 1
@ -265,8 +264,6 @@ def parsePair(self, lines, font, _lookupMap=None):
what = line[0][len(side):].title().replace(' ', '')
mask = valueRecordFormatDict[what][0]
glyph1, glyph2 = makeGlyphs(line[1:3])
getGlyphID(glyph1) # Hack to make MockFont used in tests deterministic
getGlyphID(glyph2) # Hack to make MockFont used in tests deterministic
value = int(line[3])
if not glyph1 in values: values[glyph1] = {}
if not glyph2 in values[glyph1]: values[glyph1][glyph2] = (ValueRecord(),ValueRecord())
@ -643,13 +640,11 @@ def makeClassDef(classDefs, font, klass=ot.Coverage):
def parseClassDef(lines, font, klass=ot.ClassDef):
classDefs = {}
getGlyphID = font.getGlyphID
with lines.between('class definition'):
for line in lines:
glyph = makeGlyph(line[0])
assert glyph not in classDefs, glyph
classDefs[glyph] = int(line[1])
getGlyphID(glyph) # Hack to make MockFont used in tests deterministic
return makeClassDef(classDefs, font, klass)
def makeCoverage(glyphs, font, klass=ot.Coverage):
@ -657,10 +652,7 @@ def makeCoverage(glyphs, font, klass=ot.Coverage):
if isinstance(glyphs, set):
glyphs = sorted(glyphs)
coverage = klass()
getGlyphID = font.getGlyphID
for glyph in glyphs:
getGlyphID(glyph) # Hack to make MockFont used in tests deterministic
coverage.glyphs = sorted(set(glyphs), key=getGlyphID)
coverage.glyphs = sorted(set(glyphs), key=font.getGlyphID)
return coverage
def parseCoverage(lines, font, klass=ot.Coverage):
@ -919,22 +911,18 @@ def parseGPOS(lines, font):
def parseAttachList(lines, font):
points = {}
getGlyphID = font.getGlyphID
with lines.between('attachment list'):
for line in lines:
glyph = makeGlyph(line[0])
getGlyphID(glyph) # Hack to make MockFont used in tests deterministic
assert glyph not in points, glyph
points[glyph] = [int(i) for i in line[1:]]
return otl.buildAttachList(points, font.getReverseGlyphMap())
def parseCaretList(lines, font):
carets = {}
getGlyphID = font.getGlyphID
with lines.between('carets'):
for line in lines:
glyph = makeGlyph(line[0])
getGlyphID(glyph) # Hack to make MockFont used in tests deterministic
assert glyph not in carets, glyph
num = int(line[1])
thisCarets = [int(i) for i in line[2:]]
@ -1150,7 +1138,8 @@ def build(f, font, tableTag=None):
return parseTable(lines, font, tableTag=tableTag)
def main(args):
def main(args, font=None):
import sys
from fontTools import configLogger
from fontTools.misc.testTools import MockFont
@ -1158,7 +1147,10 @@ def main(args):
configLogger()
# comment this out to enable debug messages from mtiLib's logger
# log.setLevel(logging.DEBUG)
font = MockFont()
if font is None:
font = MockFont()
tableTag = None
if args[0].startswith('-t'):
tableTag = args[0][2:]

View File

@ -2,7 +2,7 @@ from __future__ import print_function, division, absolute_import
from __future__ import unicode_literals
from fontTools.misc.py23 import *
from fontTools.misc.xmlWriter import XMLWriter
from fontTools.misc.testTools import MockFont
from fontTools.ttLib import TTFont
from fontTools import mtiLib
import difflib
import os
@ -10,7 +10,341 @@ import sys
import unittest
class BuilderTest(unittest.TestCase):
class MtiTest(unittest.TestCase):
GLYPH_ORDER = [
'a',
'b',
'pakannada',
'phakannada',
'vakannada',
'pevowelkannada',
'phevowelkannada',
'vevowelkannada',
'uvowelsignkannada',
'uuvowelsignkannada',
'uvowelsignaltkannada',
'uuvowelsignaltkannada',
'uuvowelsignsinh',
'uvowelsignsinh',
'rakarsinh',
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'slash',
'fraction',
'A',
'B',
'C',
'fi',
'fl',
'breve',
'acute',
'uniFB01',
'ffi',
'grave',
'commaacent',
'dotbelow',
'dotabove',
'cedilla',
'commaaccent',
'Acircumflex',
'V',
'T',
'acircumflex',
'Aacute',
'Agrave',
'O',
'Oacute',
'Ograve',
'Ocircumflex',
'aacute',
'agrave',
'aimatrabindigurmukhi',
'aimatragurmukhi',
'aimatratippigurmukhi',
'aumatrabindigurmukhi',
'aumatragurmukhi',
'bindigurmukhi',
'eematrabindigurmukhi',
'eematragurmukhi',
'eematratippigurmukhi',
'oomatrabindigurmukhi',
'oomatragurmukhi',
'oomatratippigurmukhi',
'lagurmukhi',
'lanuktagurmukhi',
'nagurmukhi',
'nanuktagurmukhi',
'ngagurmukhi',
'nganuktagurmukhi',
'nnagurmukhi',
'nnanuktagurmukhi',
'tthagurmukhi',
'tthanuktagurmukhi',
'bsuperior',
'isuperior',
'vsuperior',
'wsuperior',
'periodsuperior',
'osuperior',
'tsuperior',
'dollarsuperior',
'fsuperior',
'gsuperior',
'zsuperior',
'dsuperior',
'psuperior',
'hsuperior',
'oesuperior',
'aesuperior',
'centsuperior',
'esuperior',
'lsuperior',
'qsuperior',
'csuperior',
'asuperior',
'commasuperior',
'xsuperior',
'egravesuperior',
'usuperior',
'rsuperior',
'nsuperior',
'ssuperior',
'msuperior',
'jsuperior',
'ysuperior',
'ksuperior',
'guilsinglright',
'guilsinglleft',
'uniF737',
'uniE11C',
'uniE11D',
'uniE11A',
'uni2077',
'uni2087',
'uniE11B',
'uniE119',
'uniE0DD',
'uniE0DE',
'uniF736',
'uniE121',
'uniE122',
'uniE11F',
'uni2076',
'uni2086',
'uniE120',
'uniE11E',
'uniE0DB',
'uniE0DC',
'uniF733',
'uniE12B',
'uniE12C',
'uniE129',
'uni00B3',
'uni2083',
'uniE12A',
'uniE128',
'uniF732',
'uniE133',
'uniE134',
'uniE131',
'uni00B2',
'uni2082',
'uniE132',
'uniE130',
'uniE0F9',
'uniF734',
'uniE0D4',
'uniE0D5',
'uniE0D2',
'uni2074',
'uni2084',
'uniE0D3',
'uniE0D1',
'uniF730',
'uniE13D',
'uniE13E',
'uniE13A',
'uni2070',
'uni2080',
'uniE13B',
'uniE139',
'uniE13C',
'uniF739',
'uniE0EC',
'uniE0ED',
'uniE0EA',
'uni2079',
'uni2089',
'uniE0EB',
'uniE0E9',
'uniF735',
'uniE0CD',
'uniE0CE',
'uniE0CB',
'uni2075',
'uni2085',
'uniE0CC',
'uniE0CA',
'uniF731',
'uniE0F3',
'uniE0F4',
'uniE0F1',
'uni00B9',
'uni2081',
'uniE0F2',
'uniE0F0',
'uniE0F8',
'uniF738',
'uniE0C0',
'uniE0C1',
'uniE0BE',
'uni2078',
'uni2088',
'uniE0BF',
'uniE0BD',
'I',
'Ismall',
't',
'i',
'f',
'IJ',
'J',
'IJsmall',
'Jsmall',
'tt',
'ij',
'j',
'ffb',
'ffh',
'h',
'ffk',
'k',
'ffl',
'l',
'fft',
'fb',
'ff',
'fh',
'fj',
'fk',
'ft',
'janyevoweltelugu',
'kassevoweltelugu',
'jaivoweltelugu',
'nyasubscripttelugu',
'kaivoweltelugu',
'ssasubscripttelugu',
'bayi1',
'jeemi1',
'kafi1',
'ghafi1',
'laami1',
'kafm1',
'ghafm1',
'laamm1',
'rayf2',
'reyf2',
'yayf2',
'zayf2',
'fayi1',
'ayehf2',
'hamzayeharabf2',
'hamzayehf2',
'yehf2',
'ray',
'rey',
'zay',
'yay',
'dal',
'del',
'zal',
'rayf1',
'reyf1',
'yayf1',
'zayf1',
'ayehf1',
'hamzayeharabf1',
'hamzayehf1',
'yehf1',
'dal1',
'del1',
'zal1',
'onehalf',
'onehalf.alt',
'onequarter',
'onequarter.alt',
'threequarters',
'threequarters.alt',
'AlefSuperiorNS',
'DammaNS',
'DammaRflxNS',
'DammatanNS',
'Fatha2dotsNS',
'FathaNS',
'FathatanNS',
'FourDotsAboveNS',
'HamzaAboveNS',
'MaddaNS',
'OneDotAbove2NS',
'OneDotAboveNS',
'ShaddaAlefNS',
'ShaddaDammaNS',
'ShaddaDammatanNS',
'ShaddaFathatanNS',
'ShaddaKasraNS',
'ShaddaKasratanNS',
'ShaddaNS',
'SharetKafNS',
'SukunNS',
'ThreeDotsDownAboveNS',
'ThreeDotsUpAboveNS',
'TwoDotsAboveNS',
'TwoDotsVerticalAboveNS',
'UltapeshNS',
'WaslaNS',
'AinIni.12m_MeemFin.02',
'AinIni_YehBarreeFin',
'AinMed_YehBarreeFin',
'BehxIni_MeemFin',
'BehxIni_NoonGhunnaFin',
'BehxIni_RehFin',
'BehxIni_RehFin.b',
'BehxMed_MeemFin.py',
'BehxMed_NoonGhunnaFin',
'BehxMed_NoonGhunnaFin.cup',
'BehxMed_RehFin',
'BehxMed_RehFin.cup',
'BehxMed_YehxFin',
'FehxMed_YehBarreeFin',
'HahIni_YehBarreeFin',
'KafIni_YehBarreeFin',
'KafMed.12_YehxFin.01',
'KafMed_MeemFin',
'KafMed_YehBarreeFin',
'LamAlefFin',
'LamAlefFin.cup',
'LamAlefFin.cut',
'LamAlefFin.short',
'LamAlefSep',
'LamIni_MeemFin',
'LamIni_YehBarreeFin',
'LamMed_MeemFin',
'LamMed_MeemFin.b',
'LamMed_YehxFin',
'LamMed_YehxFin.cup',
'TahIni_YehBarreeFin',
]
# Feature files in testdata/*.txt; output gets compared to testdata/*.ttx.
TESTS = {
None: (
@ -29,7 +363,7 @@ class BuilderTest(unittest.TestCase):
'mti/scripttable',
'mti/chainedclass',
'mti/chainedcoverage',
'mti/chained-glyph',
#'mti/chained-glyph',
'mti/gsubalternate',
'mti/gsubligature',
'mti/gsubmultiple',
@ -38,7 +372,7 @@ class BuilderTest(unittest.TestCase):
),
'GPOS': (
'mti/scripttable',
'mti/chained-glyph',
#'mti/chained-glyph',
'mti/gposcursive',
'mti/gposkernset',
'mti/gposmarktobase',
@ -91,13 +425,19 @@ class BuilderTest(unittest.TestCase):
sys.stderr.write(line)
self.fail("TTX output is different from expected")
@classmethod
def create_font(celf):
font = TTFont()
font.setGlyphOrder(celf.GLYPH_ORDER)
return font
def check_mti_file(self, name, tableTag=None):
xml_expected_path = self.getpath("%s.ttx" % name + ('.'+tableTag if tableTag is not None else ''))
with open(xml_expected_path, 'rt', encoding="utf-8") as xml_expected_file:
xml_expected = xml_expected_file.read()
font = MockFont()
font = self.create_font()
with open(self.getpath("%s.txt" % name), 'rt', encoding="utf-8") as f:
table = mtiLib.build(f, font, tableTag=tableTag)
@ -134,11 +474,17 @@ def generate_mti_file_test(name, tableTag=None):
return lambda self: self.check_mti_file(os.path.join(*name.split('/')), tableTag=tableTag)
for tableTag,tests in BuilderTest.TESTS.items():
for tableTag,tests in MtiTest.TESTS.items():
for name in tests:
setattr(BuilderTest, "test_MtiFile_%s" % name,
setattr(MtiTest, "test_MtiFile_%s" % name,
generate_mti_file_test(name, tableTag=tableTag))
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
from fontTools.mtiLib import main
font = MtiTest.create_font()
main (sys.argv[1:], font)
sys.exit()
unittest.main()

View File

@ -6,18 +6,18 @@
<!-- MarkSetCount=4 -->
<Coverage index="0" empty="1"/>
<Coverage index="1" Format="1">
<Glyph value="acute"/>
<Glyph value="breve"/>
<Glyph value="acute"/>
<Glyph value="dotabove"/>
</Coverage>
<Coverage index="2" Format="1">
<Glyph value="dotbelow"/>
<Glyph value="cedilla"/>
<Glyph value="commaaccent"/>
<Glyph value="dotbelow"/>
</Coverage>
<Coverage index="3" Format="1">
<Glyph value="dotabove"/>
<Glyph value="dotbelow"/>
<Glyph value="dotabove"/>
</Coverage>
</MarkGlyphSetsDef>
</GDEF>

View File

@ -32,9 +32,9 @@
</PairPos>
<PairPos index="1" Format="2">
<Coverage Format="1">
<Glyph value="A"/>
<Glyph value="Acircumflex"/>
<Glyph value="T"/>
<Glyph value="A"/>
<Glyph value="Aacute"/>
<Glyph value="Agrave"/>
<Glyph value="O"/>
@ -44,7 +44,7 @@
</Coverage>
<ValueFormat1 value="4"/>
<ValueFormat2 value="0"/>
<ClassDef1 Format="1">
<ClassDef1 Format="2">
<ClassDef glyph="A" class="1"/>
<ClassDef glyph="Aacute" class="1"/>
<ClassDef glyph="Acircumflex" class="1"/>

View File

@ -8,16 +8,16 @@
<LookupFlag value="0"/>
<!-- SubTableCount=1 -->
<PairPos index="0" Format="2">
<Coverage Format="2">
<Coverage Format="1">
<Glyph value="A"/>
<Glyph value="Acircumflex"/>
<Glyph value="T"/>
<Glyph value="Aacute"/>
<Glyph value="Agrave"/>
<Glyph value="Acircumflex"/>
<Glyph value="O"/>
<Glyph value="Oacute"/>
<Glyph value="Ograve"/>
<Glyph value="Ocircumflex"/>
<Glyph value="T"/>
</Coverage>
<ValueFormat1 value="4"/>
<ValueFormat2 value="0"/>
@ -32,7 +32,7 @@
<ClassDef glyph="Ograve" class="2"/>
<ClassDef glyph="T" class="3"/>
</ClassDef1>
<ClassDef2 Format="1">
<ClassDef2 Format="2">
<ClassDef glyph="V" class="1"/>
<ClassDef glyph="a" class="2"/>
<ClassDef glyph="aacute" class="2"/>

View File

@ -8,16 +8,16 @@
<LookupFlag value="0"/>
<!-- SubTableCount=1 -->
<PairPos index="0" Format="1">
<Coverage Format="2">
<Coverage Format="1">
<Glyph value="A"/>
<Glyph value="Acircumflex"/>
<Glyph value="T"/>
<Glyph value="Aacute"/>
<Glyph value="Agrave"/>
<Glyph value="Acircumflex"/>
<Glyph value="O"/>
<Glyph value="Oacute"/>
<Glyph value="Ograve"/>
<Glyph value="Ocircumflex"/>
<Glyph value="T"/>
</Coverage>
<ValueFormat1 value="4"/>
<ValueFormat2 value="0"/>
@ -37,10 +37,22 @@
</PairValueRecord>
</PairSet>
<PairSet index="2">
<!-- PairValueCount=1 -->
<!-- PairValueCount=4 -->
<PairValueRecord index="0">
<SecondGlyph value="V"/>
<Value1 XAdvance="-50"/>
<SecondGlyph value="a"/>
<Value1 XAdvance="-35"/>
</PairValueRecord>
<PairValueRecord index="1">
<SecondGlyph value="acircumflex"/>
<Value1 XAdvance="-35"/>
</PairValueRecord>
<PairValueRecord index="2">
<SecondGlyph value="aacute"/>
<Value1 XAdvance="-35"/>
</PairValueRecord>
<PairValueRecord index="3">
<SecondGlyph value="agrave"/>
<Value1 XAdvance="-35"/>
</PairValueRecord>
</PairSet>
<PairSet index="3">
@ -54,7 +66,7 @@
<!-- PairValueCount=1 -->
<PairValueRecord index="0">
<SecondGlyph value="V"/>
<Value1 XAdvance="-10"/>
<Value1 XAdvance="-50"/>
</PairValueRecord>
</PairSet>
<PairSet index="5">
@ -79,22 +91,10 @@
</PairValueRecord>
</PairSet>
<PairSet index="8">
<!-- PairValueCount=4 -->
<!-- PairValueCount=1 -->
<PairValueRecord index="0">
<SecondGlyph value="a"/>
<Value1 XAdvance="-35"/>
</PairValueRecord>
<PairValueRecord index="1">
<SecondGlyph value="aacute"/>
<Value1 XAdvance="-35"/>
</PairValueRecord>
<PairValueRecord index="2">
<SecondGlyph value="agrave"/>
<Value1 XAdvance="-35"/>
</PairValueRecord>
<PairValueRecord index="3">
<SecondGlyph value="acircumflex"/>
<Value1 XAdvance="-35"/>
<SecondGlyph value="V"/>
<Value1 XAdvance="-10"/>
</PairValueRecord>
</PairSet>
</PairPos>