github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zdi/bind_test.go (about) 1 package zdi_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/sohaha/zlsgo" 8 "github.com/sohaha/zlsgo/zdi" 9 "github.com/sohaha/zlsgo/zlog" 10 "github.com/sohaha/zlsgo/ztime" 11 ) 12 13 func TestBind(t *testing.T) { 14 tt := zlsgo.NewTest(t) 15 di := zdi.New() 16 17 test1 := testSt{Msg: ztime.Now(), Num: 666} 18 o := di.Map(test1) 19 tt.EqualNil(o) 20 zlog.Dump(test1) 21 22 var test2 testSt 23 override := di.Resolve(&test2) 24 25 tt.Equal(test1, test2) 26 zlog.Dump(override, test2) 27 28 // No 29 var test3 testSt 30 override = di.Resolve(test3) 31 tt.EqualTrue(override != nil) 32 zlog.Dump(override, test3) 33 34 test5 := &testSt{Msg: ztime.Now(), Num: 666} 35 o = di.Map(test5) 36 tt.EqualNil(o) 37 38 // No 39 var test4 *testSt 40 err := di.Resolve(test4) 41 tt.EqualTrue(err != nil) 42 zlog.Dump(err, test4) 43 44 var test6 *testSt 45 err = di.Resolve(&test6) 46 tt.NoError(err) 47 tt.Equal(test5, test6) 48 zlog.Dump(err, test6) 49 } 50 51 func TestApply(t *testing.T) { 52 tt := zlsgo.NewTest(t) 53 di := zdi.New() 54 55 val := time.Now().String() 56 o := di.Map(val) 57 tt.EqualNil(o) 58 59 var v testSt 60 err := di.Apply(&v) 61 tt.EqualNil(err) 62 t.Log(val, v) 63 64 var s string 65 err = di.Apply(&s) 66 tt.EqualNil(err) 67 t.Log(s) 68 }