github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/slice_struct_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 "github.com/v2pro/plz/reflect2" 6 ) 7 8 func Test_slice_struct(t *testing.T) { 9 var pInt = func(val int) *int { 10 return &val 11 } 12 t.Run("SetIndex", testOp(func(api reflect2.API) interface{} { 13 type TestObject struct { 14 Field1 float64 15 Field2 float64 16 } 17 obj := []TestObject{{}, {}} 18 valType := api.TypeOf(obj).(reflect2.SliceType) 19 valType.SetIndex(obj, 0, &TestObject{1, 3}) 20 valType.SetIndex(obj, 1, &TestObject{2, 4}) 21 return obj 22 })) 23 t.Run("SetIndex single ptr struct", testOp(func(api reflect2.API) interface{} { 24 type TestObject struct { 25 Field1 *int 26 } 27 obj := []TestObject{{}, {}} 28 valType := api.TypeOf(obj).(reflect2.SliceType) 29 valType.SetIndex(obj, 0, &TestObject{pInt(1)}) 30 valType.SetIndex(obj, 1, &TestObject{pInt(2)}) 31 return obj 32 })) 33 t.Run("SetIndex single chan struct", testOp(func(api reflect2.API) interface{} { 34 type TestObject struct { 35 Field1 chan int 36 } 37 obj := []TestObject{{}, {}} 38 valType := api.TypeOf(obj).(reflect2.SliceType) 39 valType.SetIndex(obj, 0, TestObject{}) 40 valType.SetIndex(obj, 1, TestObject{}) 41 return obj 42 })) 43 t.Run("SetIndex single func struct", testOp(func(api reflect2.API) interface{} { 44 type TestObject struct { 45 Field1 func() 46 } 47 obj := []TestObject{{}, {}} 48 valType := api.TypeOf(obj).(reflect2.SliceType) 49 valType.SetIndex(obj, 0, TestObject{}) 50 valType.SetIndex(obj, 1, TestObject{}) 51 return obj 52 })) 53 }