(t *testing.T)
| 416 | } |
| 417 | |
| 418 | func TestSerializeCircularReference(t *testing.T) { |
| 419 | fory := NewFory(WithXlang(true), WithCompatible(false), WithRefTracking(true)) |
| 420 | { |
| 421 | type A struct { |
| 422 | A1 *A |
| 423 | } |
| 424 | require.Nil(t, fory.RegisterStructByName(A{}, "example.A")) |
| 425 | // If use `A{}` instead of `&A{}` and pass `a` instead of `&a`, there will be serialization data duplication |
| 426 | // and can't be deserialized by other languages too. |
| 427 | // TODO(chaokunyang) If pass by value(have a copy) and there are some inner value reference, return a readable |
| 428 | // error instead of panic on `Unmarshal` |
| 429 | a := &A{} |
| 430 | a.A1 = a |
| 431 | bytes, err := fory.Marshal(a) |
| 432 | require.Nil(t, err) |
| 433 | var a1 *A |
| 434 | err = fory.Unmarshal(bytes, &a1) |
| 435 | require.Nil(t, err) |
| 436 | require.Same(t, a1, a1.A1) |
| 437 | } |
| 438 | { |
| 439 | type CircularRefB struct { |
| 440 | F1 string |
| 441 | F2 *CircularRefB |
| 442 | F3 *CircularRefB |
| 443 | } |
| 444 | require.Nil(t, fory.RegisterStructByName(CircularRefB{}, "example.CircularRefB")) |
| 445 | b := &CircularRefB{F1: "str"} |
| 446 | b.F2 = b |
| 447 | b.F3 = b |
| 448 | bytes, err := fory.Marshal(b) |
| 449 | require.Nil(t, err) |
| 450 | var b1 *CircularRefB |
| 451 | err = fory.Unmarshal(bytes, &b1) |
| 452 | require.Nil(t, err) |
| 453 | require.Equal(t, b.F1, b1.F1) |
| 454 | require.Same(t, b1, b1.F2) |
| 455 | require.Same(t, b1.F2, b1.F3) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func TestSerializeComplexReference(t *testing.T) { |
| 460 | fory := NewFory(WithXlang(true), WithCompatible(false), WithRefTracking(true)) |
nothing calls this directly
no test coverage detected