Skip to content

Commit

Permalink
proper unit test to address coverage drop (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
obormot authored Aug 30, 2021
1 parent 588390f commit ad17842
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions dpkt/dpkt.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,15 @@ def unpack(self, buf):


def test_repr():
"""complex test for __repr__, __public_fields__"""

class TestPacket(Packet):
__hdr__ = (('_a_b', 'B', 0),)
__hdr__ = (
('_a_b', 'B', 1), # 'a' and 'b' bit fields
('_rsv', 'B', 0), # hidden reserved field
('_c_flag', 'B', 1), # 'c_flag' property
('d', 'B', 0) # regular field
)

__bit_fields__ = {
'_a_b': (
Expand All @@ -510,10 +517,22 @@ class TestPacket(Packet):
),
}

# default values so no output
@property
def c_flag(self):
return (self.a | self.b)

# init with default values
test_packet = TestPacket()
assert repr(test_packet) == "TestPacket()"

# non-default values
test_packet = TestPacket(b'\x12')
assert repr(test_packet) == "TestPacket(a=1, b=2)"
# test repr with all default values so expect no output
# (except for the explicitly defined property, where dpkt doesn't process defaults yet)
assert repr(test_packet) == "TestPacket(c_flag=1)"

# init with non-default values
test_packet = TestPacket(b'\x12\x11\x00\x04')

# ensure the display fields were cached and propagated via class attribute
assert test_packet.__public_fields__ == ['a', 'b', 'c_flag', 'd']

# verify repr
assert repr(test_packet) == "TestPacket(a=1, b=2, c_flag=3, d=4)"

0 comments on commit ad17842

Please sign in to comment.