Test simple struct serialization.
()
| 461 | |
| 462 | |
| 463 | def test_simple_struct(): |
| 464 | """Test simple struct serialization.""" |
| 465 | data_file = get_data_file() |
| 466 | with open(data_file, "rb") as f: |
| 467 | data_bytes = f.read() |
| 468 | |
| 469 | fory = pyfory.Fory(xlang=True, compatible=True) |
| 470 | fory.register_type(Color, type_id=101) |
| 471 | fory.register_type(Item, type_id=102) |
| 472 | fory.register_type(SimpleStruct, type_id=103) |
| 473 | |
| 474 | expected = SimpleStruct( |
| 475 | f1={1: 1.0, 2: 2.0}, |
| 476 | f2=39, |
| 477 | f3=Item(name="item"), |
| 478 | f4="f4", |
| 479 | f5=Color.White, |
| 480 | f6=["f6"], |
| 481 | f7=40, |
| 482 | f8=41, |
| 483 | last=42, |
| 484 | ) |
| 485 | |
| 486 | debug_print(f"Java bytes length: {len(data_bytes)}") |
| 487 | debug_print(f"Java bytes (first 50): {data_bytes[:50].hex()}") |
| 488 | |
| 489 | obj = fory.deserialize(data_bytes) |
| 490 | debug_print(f"Deserialized: {obj}") |
| 491 | assert obj == expected, f"Mismatch: {obj} != {expected}" |
| 492 | |
| 493 | new_bytes = fory.serialize(obj) |
| 494 | debug_print(f"Python bytes length: {len(new_bytes)}") |
| 495 | debug_print(f"Python bytes (first 50): {new_bytes[:50].hex()}") |
| 496 | debug_print(f"Bytes match: {data_bytes == new_bytes}") |
| 497 | new_value = fory.deserialize(new_bytes) |
| 498 | assert new_value == expected, f"new_value: {new_value},\n expected: {expected}" |
| 499 | |
| 500 | with open(data_file, "wb") as f: |
| 501 | f.write(new_bytes) |
| 502 | |
| 503 | |
| 504 | def test_named_simple_struct(): |
nothing calls this directly
no test coverage detected