github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/cmd/internal/svc/codegen/httpclient_test.go (about) 1 package codegen 2 3 import ( 4 "github.com/iancoleman/strcase" 5 "github.com/pkg/errors" 6 . "github.com/smartystreets/goconvey/convey" 7 "github.com/unionj-cloud/go-doudou/cmd/internal/astutils" 8 "os" 9 "path/filepath" 10 "testing" 11 ) 12 13 func TestGenGoClient(t *testing.T) { 14 dir := testDir + "client1" 15 InitSvc(dir) 16 defer os.RemoveAll(dir) 17 svcfile := filepath.Join(dir, "svc.go") 18 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 19 20 type args struct { 21 dir string 22 ic astutils.InterfaceCollector 23 } 24 tests := []struct { 25 name string 26 args args 27 }{ 28 { 29 name: "", 30 args: args{ 31 dir: dir, 32 ic: ic, 33 }, 34 }, 35 } 36 for _, tt := range tests { 37 t.Run(tt.name, func(t *testing.T) { 38 GenGoClient(tt.args.dir, tt.args.ic, "", 1, strcase.ToLowerCamel) 39 }) 40 } 41 } 42 43 func TestGenGoClient2(t *testing.T) { 44 svcfile := filepath.Join(testDir, "svc.go") 45 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 46 47 type args struct { 48 dir string 49 ic astutils.InterfaceCollector 50 } 51 tests := []struct { 52 name string 53 args args 54 }{ 55 { 56 name: "", 57 args: args{ 58 dir: testDir, 59 ic: ic, 60 }, 61 }, 62 } 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 GenGoClient(tt.args.dir, tt.args.ic, "", 1, strcase.ToLowerCamel) 66 }) 67 } 68 } 69 70 func TestGenGoClientPanic_Stat(t *testing.T) { 71 Convey("Test GenGoClient panic from Stat", t, func() { 72 MkdirAll = os.MkdirAll 73 Open = os.Open 74 Create = os.Create 75 Stat = os.Stat 76 Stat = func(name string) (os.FileInfo, error) { 77 return nil, errors.New("mock Stat error") 78 } 79 80 svcfile := filepath.Join(testDir, "svc.go") 81 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 82 83 So(func() { 84 GenGoClient(testDir, ic, "", 1, strcase.ToLowerCamel) 85 }, ShouldPanic) 86 }) 87 } 88 89 func TestGenGoClientPanic_Create(t *testing.T) { 90 Convey("Test GenGoClient panic from Create", t, func() { 91 MkdirAll = os.MkdirAll 92 Open = os.Open 93 Create = os.Create 94 Stat = os.Stat 95 Create = func(name string) (*os.File, error) { 96 return nil, errors.New("mock Create error") 97 } 98 svcfile := filepath.Join(testDir, "svc.go") 99 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 100 101 So(func() { 102 GenGoClient(testDir, ic, "", 1, strcase.ToLowerCamel) 103 }, ShouldPanic) 104 }) 105 } 106 107 func TestGenGoClientPanic_Open(t *testing.T) { 108 Convey("Test GenGoClient panic from Open", t, func() { 109 MkdirAll = os.MkdirAll 110 Open = os.Open 111 Create = os.Create 112 Stat = os.Stat 113 Open = func(name string) (*os.File, error) { 114 return nil, errors.New("mock Open error") 115 } 116 svcfile := filepath.Join(testDir, "svc.go") 117 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 118 119 So(func() { 120 GenGoClient(testDir, ic, "", 1, strcase.ToLowerCamel) 121 }, ShouldPanic) 122 }) 123 } 124 125 func TestGenGoClientPanic_MkdirAll(t *testing.T) { 126 Convey("Test GenGoClient panic from MkdirAll", t, func() { 127 MkdirAll = os.MkdirAll 128 Open = os.Open 129 Create = os.Create 130 Stat = os.Stat 131 MkdirAll = func(path string, perm os.FileMode) error { 132 return errors.New("mock MkdirAll error") 133 } 134 svcfile := filepath.Join(testDir, "svc.go") 135 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 136 137 So(func() { 138 GenGoClient(testDir, ic, "", 1, strcase.ToLowerCamel) 139 }, ShouldPanic) 140 }) 141 }