github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zdi/di_test.go (about) 1 package zdi_test 2 3 import ( 4 "testing" 5 6 "github.com/sohaha/zlsgo" 7 "github.com/sohaha/zlsgo/zdi" 8 "github.com/sohaha/zlsgo/ztime" 9 ) 10 11 type testSt struct { 12 Msg string `di:""` 13 Num int 14 } 15 16 func TestBase(t *testing.T) { 17 tt := zlsgo.NewTest(t) 18 di := zdi.New() 19 20 now := ztime.Now() 21 test1 := &testSt{Msg: now, Num: 666} 22 override := di.Maps(test1, testSt2{Name: "main"}) 23 tt.Equal(0, len(override)) 24 25 tt.Run("TestSetParent", func(tt *zlsgo.TestUtil) { 26 ndi := zdi.New(di) 27 ndi.Map(testSt2{Name: "Current"}) 28 _, err := ndi.Invoke(func(t2 testSt2, t1 *testSt) { 29 tt.Equal("Current", t2.Name) 30 tt.Equal(666, t1.Num) 31 tt.Equal(now, t1.Msg) 32 t.Log(t2, t1) 33 }) 34 tt.NoError(err) 35 }) 36 } 37 38 func TestMultiple(t *testing.T) { 39 tt := zlsgo.NewTest(t) 40 di := zdi.New() 41 42 test1 := &testSt{Num: 1} 43 test2 := &testSt{Num: 2} 44 test3 := &testSt{Num: 3} 45 46 di.Maps(test1, test2) 47 di.Map(test3) 48 49 _, err := di.Invoke(func(test *testSt) { 50 t.Log(test) 51 }) 52 tt.NoError(err) 53 54 err = di.InvokeWithErrorOnly(func(test []*testSt) error { 55 t.Log(test) 56 return nil 57 }) 58 tt.Log(err) 59 tt.EqualTrue(err != nil) 60 }