|
| 1 | +# |
| 2 | +# This file is part of libdestruct (https://github.com/mrindeciso/libdestruct). |
| 3 | +# Copyright (c) 2026 Roberto Alessandro Bertolini. All rights reserved. |
| 4 | +# Licensed under the MIT license. See LICENSE file in the project root for details. |
| 5 | +# |
| 6 | + |
| 7 | +import json |
| 8 | +import struct as pystruct |
| 9 | +import unittest |
| 10 | +from enum import IntEnum |
| 11 | + |
| 12 | +from libdestruct import ( |
| 13 | + array_of, |
| 14 | + c_float, |
| 15 | + c_int, |
| 16 | + c_long, |
| 17 | + c_str, |
| 18 | + enum_of, |
| 19 | + inflater, |
| 20 | + ptr_to, |
| 21 | + struct, |
| 22 | +) |
| 23 | +from libdestruct.common.enum import enum |
| 24 | +from libdestruct.common.union import tagged_union, union, union_of |
| 25 | + |
| 26 | + |
| 27 | +class ToDictTest(unittest.TestCase): |
| 28 | + def test_primitive_to_dict(self): |
| 29 | + """Primitive to_dict returns its Python value.""" |
| 30 | + x = c_int.from_bytes(b"\x2a\x00\x00\x00") |
| 31 | + self.assertEqual(x.to_dict(), 42) |
| 32 | + |
| 33 | + def test_struct_to_dict(self): |
| 34 | + """Struct to_dict returns a dict of field names to values.""" |
| 35 | + class point_t(struct): |
| 36 | + x: c_int |
| 37 | + y: c_int |
| 38 | + |
| 39 | + memory = pystruct.pack("<ii", 10, 20) |
| 40 | + point = point_t.from_bytes(memory) |
| 41 | + result = point.to_dict() |
| 42 | + self.assertEqual(result, {"x": 10, "y": 20}) |
| 43 | + |
| 44 | + def test_nested_struct_to_dict(self): |
| 45 | + """Nested struct produces nested dicts.""" |
| 46 | + class vec2(struct): |
| 47 | + x: c_int |
| 48 | + y: c_int |
| 49 | + |
| 50 | + class entity_t(struct): |
| 51 | + id: c_int |
| 52 | + pos: vec2 |
| 53 | + |
| 54 | + memory = pystruct.pack("<iii", 1, 10, 20) |
| 55 | + entity = entity_t.from_bytes(memory) |
| 56 | + result = entity.to_dict() |
| 57 | + self.assertEqual(result, {"id": 1, "pos": {"x": 10, "y": 20}}) |
| 58 | + |
| 59 | + def test_struct_with_ptr_to_dict(self): |
| 60 | + """Pointer field returns its address as int.""" |
| 61 | + class data_t(struct): |
| 62 | + value: c_int |
| 63 | + ref: c_long |
| 64 | + |
| 65 | + memory = pystruct.pack("<iq", 42, 0x1000) |
| 66 | + data = data_t.from_bytes(memory) |
| 67 | + result = data.to_dict() |
| 68 | + self.assertEqual(result, {"value": 42, "ref": 0x1000}) |
| 69 | + |
| 70 | + def test_struct_with_enum_to_dict(self): |
| 71 | + """Enum field returns its integer value.""" |
| 72 | + class Color(IntEnum): |
| 73 | + RED = 0 |
| 74 | + GREEN = 1 |
| 75 | + BLUE = 2 |
| 76 | + |
| 77 | + class pixel_t(struct): |
| 78 | + color: enum = enum_of(Color) |
| 79 | + alpha: c_int |
| 80 | + |
| 81 | + memory = pystruct.pack("<ii", 1, 255) |
| 82 | + pixel = pixel_t.from_bytes(memory) |
| 83 | + result = pixel.to_dict() |
| 84 | + self.assertEqual(result["color"], 1) |
| 85 | + self.assertEqual(result["alpha"], 255) |
| 86 | + |
| 87 | + def test_struct_with_array_to_dict(self): |
| 88 | + """Array field returns a list of values.""" |
| 89 | + class packet_t(struct): |
| 90 | + data: c_int = array_of(c_int, 3) |
| 91 | + |
| 92 | + memory = pystruct.pack("<iii", 10, 20, 30) |
| 93 | + pkt = packet_t.from_bytes(memory) |
| 94 | + result = pkt.to_dict() |
| 95 | + self.assertEqual(result, {"data": [10, 20, 30]}) |
| 96 | + |
| 97 | + def test_struct_with_tagged_union_to_dict(self): |
| 98 | + """Tagged union returns the active variant's value.""" |
| 99 | + class msg_t(struct): |
| 100 | + type: c_int |
| 101 | + payload: union = tagged_union("type", {0: c_int, 1: c_float}) |
| 102 | + |
| 103 | + memory = pystruct.pack("<ii", 0, 42) |
| 104 | + msg = msg_t.from_bytes(memory) |
| 105 | + result = msg.to_dict() |
| 106 | + self.assertEqual(result["type"], 0) |
| 107 | + self.assertEqual(result["payload"], 42) |
| 108 | + |
| 109 | + def test_struct_with_plain_union_to_dict(self): |
| 110 | + """Plain union returns a dict of all variant values.""" |
| 111 | + class packet_t(struct): |
| 112 | + data: union = union_of({"i": c_int, "f": c_float}) |
| 113 | + |
| 114 | + memory = pystruct.pack("<i", 42) |
| 115 | + pkt = packet_t.from_bytes(memory) |
| 116 | + result = pkt.to_dict() |
| 117 | + self.assertIn("i", result["data"]) |
| 118 | + self.assertIn("f", result["data"]) |
| 119 | + self.assertEqual(result["data"]["i"], 42) |
| 120 | + |
| 121 | + def test_to_dict_is_json_serializable(self): |
| 122 | + """to_dict output can be passed to json.dumps.""" |
| 123 | + class point_t(struct): |
| 124 | + x: c_int |
| 125 | + y: c_int |
| 126 | + |
| 127 | + memory = pystruct.pack("<ii", 10, 20) |
| 128 | + point = point_t.from_bytes(memory) |
| 129 | + result = json.dumps(point.to_dict()) |
| 130 | + self.assertEqual(json.loads(result), {"x": 10, "y": 20}) |
| 131 | + |
| 132 | + def test_float_to_dict(self): |
| 133 | + """Float to_dict returns its Python float value.""" |
| 134 | + f = c_float.from_bytes(pystruct.pack("<f", 3.14)) |
| 135 | + self.assertAlmostEqual(f.to_dict(), 3.14, places=2) |
0 commit comments