def test_read_table(self): # mock everything to keep this simple r = Reader('') expect( r.read_long ).returns( 42 ) expect( r._check_underflow ).args( 42 ) expect( r._field_shortstr ).returns( 'a' ) expect( r._read_field ).returns( 3.14 ).side_effect( lambda: setattr(r, '_pos', 20) ) expect( r._field_shortstr ).returns( 'b' ) expect( r._read_field ).returns( 'pi' ).side_effect( lambda: setattr(r, '_pos', 42) ) assert_equals( {'a':3.14,'b':'pi'}, r.read_table() )
def test_read_table(self): # mock everything to keep this simple r = Reader('') expect(r.read_long).returns(42) expect(r._check_underflow).args(42) expect(r._field_shortstr).returns('a') expect(r._read_field).returns(3.14).side_effect( lambda: setattr(r, '_pos', 20)) expect(r._field_shortstr).returns('b') expect(r._read_field).returns('pi').side_effect( lambda: setattr(r, '_pos', 42)) assert_equals({'a': 3.14, 'b': 'pi'}, r.read_table())
def test_write_frame_slow_for_standard_properties(self): HeaderFrame.DEFAULT_PROPERTIES = False bit_field = 0 properties = {} now = datetime.utcfromtimestamp( long(time.mktime(datetime.now().timetuple()))) for pname, ptype, reader, writer, mask in HeaderFrame.PROPERTIES: bit_field |= mask if ptype == 'shortstr': properties[pname] = pname elif ptype == 'octet': properties[pname] = 42 elif ptype == 'timestamp': properties[pname] = now elif ptype == 'table': properties[pname] = {'foo': 'bar'} frame = HeaderFrame(42, 5, 6, 7, properties) buf = bytearray() frame.write_frame(buf) HeaderFrame.DEFAULT_PROPERTIES = True reader = Reader(buf) assert_equals(2, reader.read_octet()) assert_equals(42, reader.read_short()) size = reader.read_long() start_pos = reader.tell() assert_equals(5, reader.read_short()) assert_equals(6, reader.read_short()) assert_equals(7, reader.read_longlong()) assert_equals(0b1111111111111100, reader.read_short()) for pname, ptype, rfunc, wfunc, mask in HeaderFrame.PROPERTIES: if ptype == 'shortstr': assertEquals(pname, reader.read_shortstr()) elif ptype == 'octet': assertEquals(42, reader.read_octet()) elif ptype == 'timestamp': assertEquals(now, reader.read_timestamp()) elif ptype == 'table': assertEquals({'foo': 'bar'}, reader.read_table()) end_pos = reader.tell() assert_equals(size, end_pos - start_pos) assert_equals(0xce, reader.read_octet())