Exemplo n.º 1
0
    def test_empty_array(self):
        def spec(f):
            f.array('empty')

        result = fileformat.read(self.file, spec)
        self.assertTrue('empty' in result)
        self.assertEqual(result.empty, [])
Exemplo n.º 2
0
    def test_array(self):
        def spec(f):
            f.array('uints')
            for i in range(len(self.bytes)):
                f.uint('uints', 1)

        result = fileformat.read(self.file, spec)
        self.assertEqual(result, {'uints': [b for b in self.bytes]})
Exemplo n.º 3
0
    def test_skip(self):
        def spec(f):
            f.skip(1)
            f.skip(3)

        result = fileformat.read(self.file, spec)
        self.assertEqual(result,
                         {'__skipped': [self.bytes[0:1], self.bytes[1:4]]})
Exemplo n.º 4
0
    def test_int(self):
        expected_value = int.from_bytes(self.bytes[:4], 'big', signed=True)

        def spec(f):
            value = f.int('int', 4)
            self.assertEqual(value, expected_value)

        result = fileformat.read(self.file, spec)
        self.assertEqual(result, {'int': expected_value})
Exemplo n.º 5
0
    def test_count(self):
        file = io.BytesIO(b'\x05Q12345Q')

        def spec(f):
            count = f.count('count', 'array', 1)
            f.skip(1)
            f.bytes('array', count)

        result = fileformat.read(file, spec)
        self.assertEqual(result.count, 5)
        self.assertEqual(result.array, b'12345')
Exemplo n.º 6
0
    def test_bytes(self):
        def spec(f):
            first = f.bytes('first', 1)
            self.assertEqual(first, self.bytes[:1])
            rest = f.bytes('rest', None)
            self.assertEqual(rest, self.bytes[1:])

        result = fileformat.read(self.file, spec)
        self.assertEqual(result, {
            'first': self.bytes[:1],
            'rest': self.bytes[1:]
        })
Exemplo n.º 7
0
    def test_section(self):
        self.entered_section = False

        def spec(f):
            def section(f):
                self.entered_section = True
                self.assertIsInstance(f, fileformat.BinarySectionReader)
                f.bytes('first', 1)

            section = f.section('section', section)
            self.assertEqual(section, {'first': self.bytes[:1]})

        result = fileformat.read(self.file, spec)
        self.assertTrue(self.entered_section)
        self.assertEqual(result, {'section': {'first': self.bytes[:1]}})
Exemplo n.º 8
0
def read_data(fname):
    with open(fname, 'rb') as fh:
        return fileformat.read(fh, png_root)
Exemplo n.º 9
0
    def test_struct(self):
        def spec(f):
            f.struct('struct', '>?3s')

        result = fileformat.read(self.file, spec)
        self.assertEqual(result, {'struct': struct.unpack('>?3s', self.bytes)})
Exemplo n.º 10
0
    def test_uint_eof(self):
        def spec(f):
            f.uint('too_long', len(self.bytes) + 1)

        with self.assertRaises(EOFError):
            fileformat.read(self.file, spec)