From 18d050e0286c05052005991e4c695107f62dbe0d Mon Sep 17 00:00:00 2001 From: Schamper <1254028+Schamper@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:34:56 +0100 Subject: [PATCH] Separate test --- tests/test_util.py | 65 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 8656519..2ee3b93 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -23,12 +23,40 @@ def test_hexdump(capsys): def test_dumpstruct(capsys, compiled): - cdef_basic = """ + cdef = """ struct test { uint32 testval; }; """ - cdef_anonymous = """ + + cs = cstruct.cstruct() + cs.load(cdef, compiled=compiled) + + assert verify_compiled(cs.test, compiled) + + buf = b"\x39\x05\x00\x00" + obj = cs.test(buf) + + dumpstruct(cs.test, buf) + captured_1 = capsys.readouterr() + + dumpstruct(obj) + captured_2 = capsys.readouterr() + + assert captured_1.out == captured_2.out + + out_1 = dumpstruct(cs.test, buf, output="string") + out_2 = dumpstruct(obj, output="string") + + assert out_1 == out_2 + + with pytest.raises(ValueError) as excinfo: + dumpstruct(obj, output="generator") + assert str(excinfo.value) == "Invalid output argument: 'generator' (should be 'print' or 'string')." + + +def test_dumpstruct_anonymous(capsys, compiled): + cdef = """ struct test { struct { uint32 testval; @@ -36,28 +64,27 @@ def test_dumpstruct(capsys, compiled): }; """ - for cdef in [cdef_basic, cdef_anonymous]: - cs = cstruct.cstruct() - cs.load(cdef, compiled=compiled) + cs = cstruct.cstruct() + cs.load(cdef, compiled=compiled) - assert verify_compiled(cs.test, compiled) + assert verify_compiled(cs.test, compiled) - buf = b"\x39\x05\x00\x00" - obj = cs.test(buf) + buf = b"\x39\x05\x00\x00" + obj = cs.test(buf) - dumpstruct(cs.test, buf) - captured_1 = capsys.readouterr() + dumpstruct(cs.test, buf) + captured_1 = capsys.readouterr() - dumpstruct(obj) - captured_2 = capsys.readouterr() + dumpstruct(obj) + captured_2 = capsys.readouterr() - assert captured_1.out == captured_2.out + assert captured_1.out == captured_2.out - out_1 = dumpstruct(cs.test, buf, output="string") - out_2 = dumpstruct(obj, output="string") + out_1 = dumpstruct(cs.test, buf, output="string") + out_2 = dumpstruct(obj, output="string") - assert out_1 == out_2 + assert out_1 == out_2 - with pytest.raises(ValueError) as excinfo: - dumpstruct(obj, output="generator") - assert str(excinfo.value) == "Invalid output argument: 'generator' (should be 'print' or 'string')." + with pytest.raises(ValueError) as excinfo: + dumpstruct(obj, output="generator") + assert str(excinfo.value) == "Invalid output argument: 'generator' (should be 'print' or 'string')."