Example #1
0
def test_parse_blob():
    length = 10
    data = tuple(range(length))
    pad = padded(length, 8)
    fmt = '>i%iQ' % pad
    s = struct.pack(fmt, length, *(data + (pad - length) * (0, )))
    result = parse(b'b', s)[0]
    assert result == data
Example #2
0
def test_parse_string_encoded():
    assert parse(b's',
                 struct.pack('%is' % padded(len('t')), u'é'.encode('utf8')),
                 encoding='utf8')[0] == u'é'

    s = u'aééééààààa'
    s_ = s.encode('utf8')
    assert parse(b's',
                 struct.pack('%is' % padded(len(s_) + 1), s_ + b'\0'),
                 encoding='utf8')[0] == s

    with raises(UnicodeDecodeError):
        parse(b's',
              struct.pack('%is' % padded(len(s_) + 1), s_ + b'\0'),
              encoding='ascii')[0] == s

    assert parse(b's',
                 struct.pack('%is' % padded(len(s_) + 1), s_ + b'\0'),
                 encoding='ascii',
                 encoding_errors='replace')[0] == u'a����������������a'

    assert parse(b's',
                 struct.pack('%is' % padded(len(s_) + 1), s_ + b'\0'),
                 encoding='ascii',
                 encoding_errors='ignore')[0] == 'aa'
Example #3
0
def test_parse_unknown():
    with raises(ValueError):
        parse(b'H', struct.pack('>f', 1.5))
Example #4
0
def test_parse_inf():
    result = parse(b'I', '')[0]
    assert result == float('inf')
Example #5
0
def test_parse_false():
    result = parse(b'F', '')[0]
    assert result == False
Example #6
0
def test_parse_true():
    result = parse(b'T', '')[0]
    assert result == True
Example #7
0
def test_parse_nil():
    result = parse(b'N', '')[0]
    assert result == None
Example #8
0
def test_parse_midi():
    data = MidiTuple(0, 144, 72, 64)
    result = parse(b'm', struct.pack('>I', format_midi(data)))[0]
    assert result == data
Example #9
0
def test_parse_string():
    assert parse(b's', struct.pack('%is' % padded(len('t')), b't'))[0] == b't'
    s = b'test'
    # XXX should we need to add the null byte ourselves?
    assert parse(b's', struct.pack('%is' % padded(len(s) + 1),
                                   s + b'\0'))[0] == s
Example #10
0
def test_parse_float():
    assert parse(b'f', struct.pack('>f', 1.5))[0] == 1.5
Example #11
0
def test_parse_int():
    assert parse(b'i', struct.pack('>i', 1))[0] == 1