1# Copyright (c) 2018-2019 Manfred Moitzi
2# License: MIT License
3from binascii import unhexlify
4from ezdxf.lldxf.types import DXFBinaryTag
5
6
7def test_init():
8    tag = DXFBinaryTag.from_string(310, 'FFFF')
9    assert tag == (310, b"\xff\xff")
10
11    tag = DXFBinaryTag(310, b"\xff\xff")
12    assert tag == (310, b"\xff\xff")
13
14    tag2 = DXFBinaryTag.from_string(code=310, value='FFFF')
15    assert tag2 == (310, b"\xff\xff")
16
17
18def test_index_able():
19    tag = DXFBinaryTag.from_string(310, 'FFFF')
20    assert tag[0] == 310
21    assert tag[1] == b"\xff\xff"
22
23
24def test_dxf_str():
25    assert DXFBinaryTag.from_string(310, 'FFFF').tostring() == "FFFF"
26    assert DXFBinaryTag.from_string(310, 'FFFF').dxfstr() == "310\nFFFF\n"
27
28
29def test_long_string():
30    tag = DXFBinaryTag.from_string(310, '414349532042696E61727946696C652855000000000000020000000C00000007104175746F6465736B204175746F434144071841534D203231392E302E302E3536303020556E6B6E6F776E071853756E204D61792020342031353A34373A3233203230313406000000000000F03F068DEDB5A0F7C6B03E06BBBDD7D9DF7CDB')
31    assert len(tag.value) == 127
32    clone = tag.clone()
33    assert tag.value == clone.value
34
35
36def test_hexstr_to_bytes():
37    import array
38    s = ''.join('%0.2X' % byte for byte in range(256))
39    assert len(s) == 512
40    bytes_ = unhexlify(s)
41    assert len(bytes_) == 256
42    # works in Python 2.7 & Python 3
43    for x, y in zip(array.array('B', bytes_), range(256)):
44        assert x == y
45    tag = DXFBinaryTag(310, bytes_)
46    assert tag.tostring() == s
47