github.com/wfusion/gofusion@v1.1.14/test/common/utils/cases/inspect_test.go (about) 1 package cases 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/suite" 8 "gorm.io/gorm/schema" 9 10 "github.com/wfusion/gofusion/common/utils/inspect" 11 "github.com/wfusion/gofusion/log" 12 13 testUtl "github.com/wfusion/gofusion/test/common/utils" 14 ) 15 16 func TestInspect(t *testing.T) { 17 t.Parallel() 18 testingSuite := &Inspect{Test: new(testUtl.Test)} 19 suite.Run(t, testingSuite) 20 } 21 22 type Inspect struct { 23 *testUtl.Test 24 } 25 26 func (t *Inspect) BeforeTest(suiteName, testName string) { 27 t.Catch(func() { 28 log.Info(context.Background(), "right before %s %s", suiteName, testName) 29 }) 30 } 31 32 func (t *Inspect) AfterTest(suiteName, testName string) { 33 t.Catch(func() { 34 log.Info(context.Background(), "right after %s %s", suiteName, testName) 35 }) 36 } 37 38 func (t *Inspect) TestSetField() { 39 t.Catch(func() { 40 type aStruct struct { 41 aa int 42 namer schema.Namer 43 } 44 45 a := &aStruct{ 46 aa: 1, 47 namer: nil, 48 } 49 inspect.SetField(a, "aa", 2) 50 inspect.SetField(a, "namer", schema.Namer(schema.NamingStrategy{})) 51 52 t.Require().EqualValues(2, a.aa) 53 t.Require().NotNil(a.namer) 54 }) 55 } 56 57 func (t *Inspect) TestFuncOf() { 58 t.Catch(func() { 59 type beforeTest func(t *Inspect, suiteName, testName string) 60 61 fnp := inspect.FuncOf("github.com/wfusion/gofusion/test/common/utils/cases.(*Inspect).BeforeTest") 62 if fnp == nil { 63 t.FailNow("function not found") 64 return 65 } 66 67 fn := *(*beforeTest)(fnp) 68 fn(t, "inspect", "funcof") 69 }) 70 }