@@ -500,8 +500,15 @@ def unpack(self, buf):
500500
501501
502502def test_repr ():
503+ """complex test for __repr__, __public_fields__"""
504+
503505 class TestPacket (Packet ):
504- __hdr__ = (('_a_b' , 'B' , 0 ),)
506+ __hdr__ = (
507+ ('_a_b' , 'B' , 1 ), # 'a' and 'b' bit fields
508+ ('_rsv' , 'B' , 0 ), # hidden reserved field
509+ ('_c_flag' , 'B' , 1 ), # 'c_flag' property
510+ ('d' , 'B' , 0 ) # regular field
511+ )
505512
506513 __bit_fields__ = {
507514 '_a_b' : (
@@ -510,10 +517,22 @@ class TestPacket(Packet):
510517 ),
511518 }
512519
513- # default values so no output
520+ @property
521+ def c_flag (self ):
522+ return (self .a | self .b )
523+
524+ # init with default values
514525 test_packet = TestPacket ()
515- assert repr (test_packet ) == "TestPacket()"
516526
517- # non-default values
518- test_packet = TestPacket (b'\x12 ' )
519- assert repr (test_packet ) == "TestPacket(a=1, b=2)"
527+ # test repr with all default values so expect no output
528+ # (except for the explicitly defined property, where dpkt doesn't process defaults yet)
529+ assert repr (test_packet ) == "TestPacket(c_flag=1)"
530+
531+ # init with non-default values
532+ test_packet = TestPacket (b'\x12 \x11 \x00 \x04 ' )
533+
534+ # ensure the display fields were cached and propagated via class attribute
535+ assert test_packet .__public_fields__ == ['a' , 'b' , 'c_flag' , 'd' ]
536+
537+ # verify repr
538+ assert repr (test_packet ) == "TestPacket(a=1, b=2, c_flag=3, d=4)"
0 commit comments