• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.misc.textTools import deHexStr
4from fontTools.ttLib.tables.otBase import OTTableReader, OTTableWriter
5import unittest
6
7
8class OTTableReaderTest(unittest.TestCase):
9    def test_readShort(self):
10        reader = OTTableReader(deHexStr("CA FE"))
11        self.assertEqual(reader.readShort(), -13570)
12        self.assertEqual(reader.pos, 2)
13
14    def test_readLong(self):
15        reader = OTTableReader(deHexStr("CA FE BE EF"))
16        self.assertEqual(reader.readLong(), -889274641)
17        self.assertEqual(reader.pos, 4)
18
19    def test_readUInt8(self):
20        reader = OTTableReader(deHexStr("C3"))
21        self.assertEqual(reader.readUInt8(), 0xC3)
22        self.assertEqual(reader.pos, 1)
23
24    def test_readUShort(self):
25        reader = OTTableReader(deHexStr("CA FE"))
26        self.assertEqual(reader.readUShort(), 0xCAFE)
27        self.assertEqual(reader.pos, 2)
28
29    def test_readUShortArray(self):
30        reader = OTTableReader(deHexStr("DE AD BE EF CA FE"))
31        self.assertEqual(list(reader.readUShortArray(3)),
32                         [0xDEAD, 0xBEEF, 0xCAFE])
33        self.assertEqual(reader.pos, 6)
34
35    def test_readUInt24(self):
36        reader = OTTableReader(deHexStr("C3 13 37"))
37        self.assertEqual(reader.readUInt24(), 0xC31337)
38        self.assertEqual(reader.pos, 3)
39
40    def test_readULong(self):
41        reader = OTTableReader(deHexStr("CA FE BE EF"))
42        self.assertEqual(reader.readULong(), 0xCAFEBEEF)
43        self.assertEqual(reader.pos, 4)
44
45    def test_readTag(self):
46        reader = OTTableReader(deHexStr("46 6F 6F 64"))
47        self.assertEqual(reader.readTag(), "Food")
48        self.assertEqual(reader.pos, 4)
49
50    def test_readData(self):
51        reader = OTTableReader(deHexStr("48 65 6C 6C 6F"))
52        self.assertEqual(reader.readData(5), b"Hello")
53        self.assertEqual(reader.pos, 5)
54
55    def test_getSubReader(self):
56        reader = OTTableReader(deHexStr("CAFE F00D"))
57        sub = reader.getSubReader(2)
58        self.assertEqual(sub.readUShort(), 0xF00D)
59        self.assertEqual(reader.readUShort(), 0xCAFE)
60
61
62class OTTableWriterTest(unittest.TestCase):
63    def test_writeShort(self):
64        writer = OTTableWriter()
65        writer.writeShort(-12345)
66        self.assertEqual(writer.getData(), deHexStr("CF C7"))
67
68    def test_writeLong(self):
69        writer = OTTableWriter()
70        writer.writeLong(-12345678)
71        self.assertEqual(writer.getData(), deHexStr("FF 43 9E B2"))
72
73    def test_writeUInt8(self):
74        writer = OTTableWriter()
75        writer.writeUInt8(0xBE)
76        self.assertEqual(writer.getData(), deHexStr("BE"))
77
78    def test_writeUShort(self):
79        writer = OTTableWriter()
80        writer.writeUShort(0xBEEF)
81        self.assertEqual(writer.getData(), deHexStr("BE EF"))
82
83    def test_writeUInt24(self):
84        writer = OTTableWriter()
85        writer.writeUInt24(0xBEEF77)
86        self.assertEqual(writer.getData(), deHexStr("BE EF 77"))
87
88    def test_writeULong(self):
89        writer = OTTableWriter()
90        writer.writeULong(0xBEEFCAFE)
91        self.assertEqual(writer.getData(), deHexStr("BE EF CA FE"))
92
93
94if __name__ == "__main__":
95    import sys
96    sys.exit(unittest.main())
97