github.com/cloudwego/kitex@v0.9.0/tool/internal_pkg/generator/generator_test.go (about) 1 // Copyright 2021 CloudWeGo Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package generator 16 17 import ( 18 "reflect" 19 "testing" 20 "time" 21 22 "github.com/cloudwego/thriftgo/generator/golang/streaming" 23 24 "github.com/cloudwego/kitex/internal/test" 25 "github.com/cloudwego/kitex/tool/internal_pkg/util" 26 ) 27 28 func TestConfig_Pack(t *testing.T) { 29 var s feature 30 var i int64 31 val := reflect.ValueOf(&s) 32 val.Elem().SetInt(i) 33 type fields struct { 34 Verbose bool 35 GenerateMain bool 36 GenerateInvoker bool 37 Version string 38 NoFastAPI bool 39 ModuleName string 40 ServiceName string 41 Use string 42 IDLType string 43 Includes util.StringSlice 44 ThriftOptions util.StringSlice 45 ProtobufOptions util.StringSlice 46 IDL string 47 OutputPath string 48 PackagePrefix string 49 CombineService bool 50 CopyIDL bool 51 ThriftPlugins util.StringSlice 52 ProtobufPlugins util.StringSlice 53 Features []feature 54 FrugalPretouch bool 55 Record bool 56 RecordCmd string 57 ThriftPluginTimeLimit time.Duration 58 TemplateDir string 59 Protocol string 60 HandlerReturnKeepResp bool 61 } 62 tests := []struct { 63 name string 64 fields fields 65 wantRes []string 66 }{ 67 // TODO: Add test cases. 68 { 69 name: "some", 70 fields: fields{Features: []feature{feature(999)}, ThriftPluginTimeLimit: 30 * time.Second}, 71 wantRes: []string{"Verbose=false", "GenerateMain=false", "GenerateInvoker=false", "Version=", "NoFastAPI=false", "ModuleName=", "ServiceName=", "Use=", "IDLType=", "Includes=", "ThriftOptions=", "ProtobufOptions=", "Hessian2Options=", "IDL=", "OutputPath=", "PackagePrefix=", "CombineService=false", "CopyIDL=false", "ProtobufPlugins=", "Features=999", "FrugalPretouch=false", "ThriftPluginTimeLimit=30s", "CompilerPath=", "ExtensionFile=", "Record=false", "RecordCmd=", "TemplateDir=", "GenPath=", "DeepCopyAPI=false", "Protocol=", "HandlerReturnKeepResp=false"}, 72 }, 73 } 74 for _, tt := range tests { 75 t.Run(tt.name, func(t *testing.T) { 76 c := &Config{ 77 Verbose: tt.fields.Verbose, 78 GenerateMain: tt.fields.GenerateMain, 79 GenerateInvoker: tt.fields.GenerateInvoker, 80 Version: tt.fields.Version, 81 NoFastAPI: tt.fields.NoFastAPI, 82 ModuleName: tt.fields.ModuleName, 83 ServiceName: tt.fields.ServiceName, 84 Use: tt.fields.Use, 85 IDLType: tt.fields.IDLType, 86 Includes: tt.fields.Includes, 87 ThriftOptions: tt.fields.ThriftOptions, 88 ProtobufOptions: tt.fields.ProtobufOptions, 89 IDL: tt.fields.IDL, 90 OutputPath: tt.fields.OutputPath, 91 PackagePrefix: tt.fields.PackagePrefix, 92 CombineService: tt.fields.CombineService, 93 CopyIDL: tt.fields.CopyIDL, 94 ThriftPlugins: tt.fields.ThriftPlugins, 95 Features: tt.fields.Features, 96 FrugalPretouch: tt.fields.FrugalPretouch, 97 ThriftPluginTimeLimit: tt.fields.ThriftPluginTimeLimit, 98 TemplateDir: tt.fields.TemplateDir, 99 Protocol: tt.fields.Protocol, 100 } 101 if gotRes := c.Pack(); !reflect.DeepEqual(gotRes, tt.wantRes) { 102 t.Errorf("Config.Pack() = \n%v\nwant\n%v", gotRes, tt.wantRes) 103 } 104 }) 105 } 106 } 107 108 func TestConfig_Unpack(t *testing.T) { 109 type fields struct { 110 Verbose bool 111 GenerateMain bool 112 GenerateInvoker bool 113 Version string 114 NoFastAPI bool 115 ModuleName string 116 ServiceName string 117 Use string 118 IDLType string 119 Includes util.StringSlice 120 ThriftOptions util.StringSlice 121 ProtobufOptions util.StringSlice 122 IDL string 123 OutputPath string 124 PackagePrefix string 125 CombineService bool 126 CopyIDL bool 127 Features []feature 128 FrugalPretouch bool 129 TemplateDir string 130 Protocol string 131 HandlerReturnKeepResp bool 132 } 133 type args struct { 134 args []string 135 } 136 tests := []struct { 137 name string 138 fields fields 139 args args 140 wantErr bool 141 }{ 142 // TODO: Add test cases. 143 { 144 name: "some", 145 fields: fields{}, 146 args: args{args: []string{"Features=0"}}, 147 wantErr: false, 148 }, 149 } 150 for _, tt := range tests { 151 t.Run(tt.name, func(t *testing.T) { 152 c := &Config{ 153 Verbose: tt.fields.Verbose, 154 GenerateMain: tt.fields.GenerateMain, 155 GenerateInvoker: tt.fields.GenerateInvoker, 156 Version: tt.fields.Version, 157 NoFastAPI: tt.fields.NoFastAPI, 158 ModuleName: tt.fields.ModuleName, 159 ServiceName: tt.fields.ServiceName, 160 Use: tt.fields.Use, 161 IDLType: tt.fields.IDLType, 162 Includes: tt.fields.Includes, 163 ThriftOptions: tt.fields.ThriftOptions, 164 ProtobufOptions: tt.fields.ProtobufOptions, 165 IDL: tt.fields.IDL, 166 OutputPath: tt.fields.OutputPath, 167 PackagePrefix: tt.fields.PackagePrefix, 168 CombineService: tt.fields.CombineService, 169 CopyIDL: tt.fields.CopyIDL, 170 Features: tt.fields.Features, 171 FrugalPretouch: tt.fields.FrugalPretouch, 172 TemplateDir: tt.fields.TemplateDir, 173 Protocol: tt.fields.Protocol, 174 HandlerReturnKeepResp: tt.fields.HandlerReturnKeepResp, 175 } 176 if err := c.Unpack(tt.args.args); (err != nil) != tt.wantErr { 177 t.Errorf("Config.Unpack() error = %v, wantErr %v", err, tt.wantErr) 178 } 179 }) 180 } 181 } 182 183 func Test_needCallOpt(t *testing.T) { 184 t.Run("protobuf", func(t *testing.T) { 185 pkg := &PackageInfo{ 186 Codec: "protobuf", 187 } 188 test.Assert(t, needCallOpt(pkg)) 189 }) 190 t.Run("thrift-no-methods", func(t *testing.T) { 191 pkg := &PackageInfo{ 192 Codec: "thrift", 193 ServiceInfo: &ServiceInfo{ 194 Methods: nil, 195 }, 196 } 197 test.Assert(t, !needCallOpt(pkg)) 198 }) 199 t.Run("thrift-with-only-streaming-methods", func(t *testing.T) { 200 pkg := &PackageInfo{ 201 Codec: "thrift", 202 ServiceInfo: &ServiceInfo{ 203 Methods: []*MethodInfo{{ 204 Streaming: &streaming.Streaming{ 205 IsStreaming: true, 206 }, 207 }}, 208 }, 209 } 210 test.Assert(t, !needCallOpt(pkg)) 211 }) 212 t.Run("thrift-with-ping-pong-methods", func(t *testing.T) { 213 pkg := &PackageInfo{ 214 Codec: "thrift", 215 ServiceInfo: &ServiceInfo{ 216 Methods: []*MethodInfo{ 217 { 218 Streaming: &streaming.Streaming{ 219 IsStreaming: true, 220 }, 221 }, 222 { 223 Streaming: &streaming.Streaming{ 224 IsStreaming: false, 225 }, 226 }, 227 }, 228 }, 229 } 230 test.Assert(t, needCallOpt(pkg)) 231 }) 232 }