def test_eat_until_sep_multiple_consecutive(self): string = b'\x45\x55\x00\x00\x45\x00' stream_form = io.BytesIO(string) parser = _Parser(stream_form) assert parser._eat_until_sep() == b'\x45\x55' assert parser._eat_until_sep() == b'' assert parser._eat_until_sep() == b'\x45'
def test_read_attrs_with_full_chunk(self): string = chr(ATTRS_START).encode() string += (1).to_bytes(4, 'big') # Number of chunks string += chr(CHUNK_FULL_START).encode() string += (0).to_bytes(4, 'big') # Layer id string += b'attribute' # Attribute name string += chr(SEPARATOR).encode() string += b'value1' string += chr(SEPARATOR).encode() string += b'value2' string += chr(SEPARATOR).encode() string += chr(CHUNK_END).encode() string += chr(ATTRS_END).encode() stream_form = io.BytesIO(string) parser = _Parser(stream_form) # Mock layers and objects parser._temp_layers = [ Layer(0, 'name'), ] layer_object_1 = LayerObject(0, 0) layer_object_2 = LayerObject(1, 0) parser._temp_layer_objects = { 0: [layer_object_1, layer_object_2] } parser._parse_attrs() assert 'attribute' in layer_object_1.attrs assert 'attribute' in layer_object_2.attrs assert layer_object_1.attrs['attribute'] == b'value1' assert layer_object_2.attrs['attribute'] == b'value2'
def test_read_relations(self): string = chr(RELATIONS_START).encode() string += (1).to_bytes(4, 'big') # Number of relations string += chr(RELATION_START).encode() string += (0).to_bytes(4, 'big') # Layer 0 string += (1).to_bytes(4, 'big') # Layer 1 string += (1).to_bytes(4, 'big') # Number of relational pairs for Layer 0 to Layer 1 string += (0).to_bytes(4, 'big') # Entity in Layer 0 string += (0).to_bytes(4, 'big') # Entity in Layer 1 string += chr(RELATION_END).encode() string += chr(RELATIONS_END).encode() stream_form = io.BytesIO(string) parser = _Parser(stream_form) # Mock layers and objects parser._temp_layers = [ Layer(0, 'name'), Layer(1, 'name') ] layer_object_1 = LayerObject(0, 0) layer_object_2 = LayerObject(0, 1) parser._temp_layer_objects = { 0: [layer_object_1], 1: [layer_object_2], } parser._parse_relations() assert len(layer_object_1.children) == 1 assert layer_object_1.children[0] == layer_object_2
def test_read_header(self): string = chr(HEADER_START).encode() string += 'iso-8859-1'.encode() string += chr(SEPARATOR).encode() string += chr(HEADER_END).encode() stream_form = io.BytesIO(string) parser = _Parser(stream_form) parser._parse_header() assert parser.encoding == "iso-8859-1" assert parser.document.header is not None assert parser.document.header.encoding == "iso-8859-1"
def test_read_a_few_layers(self): string = chr(LAYERS_START).encode() string += (1).to_bytes(4, 'big') string += chr(LAYER_START).encode() string += b'LayerName' string += chr(SEPARATOR).encode() string += (50).to_bytes(4, 'big') string += chr(LAYER_END).encode() string += chr(LAYERS_END).encode() stream_form = io.BytesIO(string) parser = _Parser(stream_form) parser._parse_layers() assert len(parser._temp_layers) == 1 assert len(parser._temp_layer_objects[0]) == 50 layer = parser._temp_layers[0] assert isinstance(layer, Layer) assert layer.name == 'LayerName' assert len(layer.objects) == 50 for i in range(len(layer.objects)): assert layer.objects[i].layer == 0 assert layer.objects[i].id == i
def read_as_int(self): string = (1024).to_bytes(4, byteorder='big') stream_form = io.BytesIO(string) parser = _Parser(stream_form) assert parser._read_as_int(4) == 1024
def test_expect_should_not_throw_on_good(self): string = b'\x45\x55' stream_form = io.BytesIO(string) parser = _Parser(stream_form) parser._expect(0x45)
def test_expect_should_throw_on_bad(self): string = b'\x45\x55' stream_form = io.BytesIO(string) parser = _Parser(stream_form) with pytest.raises(TBFParsingException): parser._expect(0x55)
def test_eat_until_sep_stops_at_eof(self): string = b'\x45\x55' stream_form = io.BytesIO(string) parser = _Parser(stream_form) assert parser._eat_until_sep() == b'\x45\x55'
def test_eat_until_sep_eats_sep_also(self): string = b'\x45\x55\x00\x45' stream_form = io.BytesIO(string) parser = _Parser(stream_form) assert parser._eat_until_sep() == b'\x45\x55' assert parser._peek() == b'\x45'