Test if structs created by StructMeta subclasses support various function operations.
()
| 235 | |
| 236 | |
| 237 | def test_struct_meta_subclass_functions(): |
| 238 | """Test if structs created by StructMeta subclasses support various function operations.""" |
| 239 | |
| 240 | # Define a custom metaclass |
| 241 | class CustomMeta(StructMeta): |
| 242 | """Custom metaclass that inherits from StructMeta""" |
| 243 | |
| 244 | # Use the custom metaclass to create a struct class |
| 245 | class CustomStruct(metaclass=CustomMeta): |
| 246 | x: int |
| 247 | y: str |
| 248 | z: float = 3.14 |
| 249 | |
| 250 | # Create an instance |
| 251 | obj = CustomStruct(x=1, y="test") |
| 252 | assert obj.x == 1 |
| 253 | assert obj.y == "test" |
| 254 | assert obj.z == 3.14 |
| 255 | |
| 256 | # Test asdict function |
| 257 | d = asdict(obj) |
| 258 | assert isinstance(d, dict) |
| 259 | assert d["x"] == 1 |
| 260 | assert d["y"] == "test" |
| 261 | assert d["z"] == 3.14 |
| 262 | |
| 263 | # Test astuple function |
| 264 | t = astuple(obj) |
| 265 | assert isinstance(t, tuple) |
| 266 | assert t == (1, "test", 3.14) |
| 267 | |
| 268 | # Test replace function |
| 269 | obj2 = replace(obj, y="replaced") |
| 270 | assert obj2.x == 1 |
| 271 | assert obj2.y == "replaced" |
| 272 | assert obj2.z == 3.14 |
| 273 | |
| 274 | # Test force_setattr function |
| 275 | force_setattr(obj, "x", 100) |
| 276 | assert obj.x == 100 |
| 277 | |
| 278 | # Test nested structs |
| 279 | class NestedStruct(metaclass=CustomMeta): |
| 280 | inner: CustomStruct |
| 281 | name: str |
| 282 | |
| 283 | nested = NestedStruct(inner=obj, name="nested") |
| 284 | assert nested.inner.x == 100 |
| 285 | assert nested.inner.y == "test" |
| 286 | assert nested.name == "nested" |
| 287 | |
| 288 | # Test asdict with nested structs |
| 289 | nested_dict = asdict(nested) |
| 290 | assert isinstance(nested_dict, dict) |
| 291 | # Note: asdict doesn't recursively convert nested struct objects, so inner remains a CustomStruct object |
| 292 | assert isinstance(nested_dict["inner"], CustomStruct) |
| 293 | assert nested_dict["inner"].x == 100 |
| 294 | assert nested_dict["inner"].y == "test" |
nothing calls this directly
no test coverage detected
searching dependent graphs…