Home | History | Annotate | Download | only in tables
      1 from __future__ import print_function, division, absolute_import
      2 from fontTools.misc.py23 import *
      3 from fontTools.misc.testTools import parseXML
      4 from fontTools.misc.xmlWriter import XMLWriter
      5 import os
      6 import struct
      7 import unittest
      8 from fontTools.ttLib import newTable
      9 
     10 
     11 class Test_l_t_a_g(unittest.TestCase):
     12 
     13 	DATA_ = struct.pack(b">LLLHHHHHH", 1, 0, 3, 24 + 0, 2, 24 + 2, 7, 24 + 2, 2) + b"enzh-Hant"
     14 	TAGS_ = ["en", "zh-Hant", "zh"]
     15 
     16 	def test_addTag(self):
     17 		table = newTable("ltag")
     18 		self.assertEqual(table.addTag("de-CH"), 0)
     19 		self.assertEqual(table.addTag("gsw-LI"), 1)
     20 		self.assertEqual(table.addTag("de-CH"), 0)
     21 		self.assertEqual(table.tags, ["de-CH", "gsw-LI"])
     22 
     23 	def test_decompile_compile(self):
     24 		table = newTable("ltag")
     25 		table.decompile(self.DATA_, ttFont=None)
     26 		self.assertEqual(1, table.version)
     27 		self.assertEqual(0, table.flags)
     28 		self.assertEqual(self.TAGS_, table.tags)
     29 		compiled = table.compile(ttFont=None)
     30 		self.assertEqual(self.DATA_, compiled)
     31 		self.assertIsInstance(compiled, bytes)
     32 
     33 	def test_fromXML(self):
     34 		table = newTable("ltag")
     35 		for name, attrs, content in parseXML(
     36 				'<version value="1"/>'
     37 				'<flags value="777"/>'
     38 				'<LanguageTag tag="sr-Latn"/>'
     39 				'<LanguageTag tag="fa"/>'):
     40 			table.fromXML(name, attrs, content, ttFont=None)
     41 		self.assertEqual(1, table.version)
     42 		self.assertEqual(777, table.flags)
     43 		self.assertEqual(["sr-Latn", "fa"], table.tags)
     44 
     45 	def test_toXML(self):
     46 		writer = XMLWriter(BytesIO())
     47 		table = newTable("ltag")
     48 		table.decompile(self.DATA_, ttFont=None)
     49 		table.toXML(writer, ttFont=None)
     50 		expected = os.linesep.join([
     51 			'<?xml version="1.0" encoding="UTF-8"?>',
     52 			'<version value="1"/>',
     53 			'<flags value="0"/>',
     54 			'<LanguageTag tag="en"/>',
     55 			'<LanguageTag tag="zh-Hant"/>',
     56 			'<LanguageTag tag="zh"/>'
     57 		]) + os.linesep
     58 		self.assertEqual(expected.encode("utf_8"), writer.file.getvalue())
     59 
     60 
     61 if __name__ == '__main__':
     62 	import sys
     63 	sys.exit(unittest.main())
     64