github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_struct_loop_test.go (about) 1 package jzon 2 3 import ( 4 "testing" 5 ) 6 7 type testLoopStruct struct { 8 *testLoopStruct 9 } 10 11 type testLoopStruct2 struct { 12 *testLoopStruct3 13 } 14 15 type testLoopStruct3 struct { 16 *testLoopStruct2 17 } 18 19 type testLoopStruct4 struct { 20 A *testLoopStruct4 `json:"a"` 21 } 22 23 type testLoopIFace interface{} 24 25 type testLoopStruct5 struct { 26 testLoopIFace 27 } 28 29 func TestValDecoder_Native_Struct_Loop(t *testing.T) { 30 f := func(t *testing.T, data string, ex error, p1, p2 interface{}) { 31 t.Log(">>>>> initValues >>>>>") 32 printValue(t, "p1", p1) 33 printValue(t, "p2", p2) 34 t.Log(">>>>>>>>>>>>>>>>>>>>>>") 35 checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2) 36 t.Log("<<<<< initValues <<<<<") 37 printValue(t, "p1", p1) 38 printValue(t, "p2", p2) 39 t.Log("<<<<<<<<<<<<<<<<<<<<<<") 40 } 41 t.Run("self nested", func(t *testing.T) { 42 var t1 testLoopStruct 43 t1.testLoopStruct = &t1 44 var t2 testLoopStruct 45 t2.testLoopStruct = &t2 46 f(t, "{}", nil, &t1, &t2) 47 }) 48 t.Run("cross nested", func(t *testing.T) { 49 var t1 testLoopStruct2 50 var t1t testLoopStruct3 51 t1.testLoopStruct3 = &t1t 52 t1t.testLoopStruct2 = &t1 53 var t2 testLoopStruct2 54 var t2t testLoopStruct3 55 t2.testLoopStruct3 = &t2t 56 t2t.testLoopStruct2 = &t2 57 f(t, "{}", nil, &t1, &t2) 58 }) 59 t.Run("field nested", func(t *testing.T) { 60 var t1 testLoopStruct4 61 t1.A = &t1 62 var t2 testLoopStruct4 63 t2.A = &t2 64 f(t, `{"a":{"a":{}}}`, nil, &t1, &t2) 65 }) 66 t.Run("interface nested", func(t *testing.T) { 67 var t1 testLoopStruct5 68 t1.testLoopIFace = &t1 69 var t2 testLoopStruct5 70 t2.testLoopIFace = &t2 71 f(t, `{}`, nil, &t1, &t2) 72 }) 73 }