@@ -127,10 +127,11 @@ func TestGenerate(t *testing.T) {
127127 t .Parallel ()
128128
129129 tests := []struct {
130- name string
131- req * pluginpb.CodeGeneratorRequest
132- wantStrings []string
133- wantErr bool
130+ name string
131+ req * pluginpb.CodeGeneratorRequest
132+ wantStrings []string
133+ dontWantStrings []string
134+ wantErr bool
134135 }{
135136 {
136137 name : "empty request" ,
@@ -195,7 +196,127 @@ func TestGenerate(t *testing.T) {
195196 },
196197 },
197198 wantErr : false ,
198- wantStrings : []string {"def try_(self" },
199+ wantStrings : []string {"class TestServiceASGIApplication" , "class TestServiceWSGIApplication" },
200+ },
201+ {
202+ name : "async only" ,
203+ req : & pluginpb.CodeGeneratorRequest {
204+ FileToGenerate : []string {"test.proto" },
205+ Parameter : proto .String ("async=true" ),
206+ ProtoFile : []* descriptorpb.FileDescriptorProto {
207+ {
208+ Name : proto .String ("test.proto" ),
209+ Package : proto .String ("test" ),
210+ Dependency : []string {"other.proto" },
211+ Service : []* descriptorpb.ServiceDescriptorProto {
212+ {
213+ Name : proto .String ("TestService" ),
214+ Method : []* descriptorpb.MethodDescriptorProto {
215+ {
216+ Name : proto .String ("TestMethod" ),
217+ InputType : proto .String (".test.TestRequest" ),
218+ OutputType : proto .String (".test.TestResponse" ),
219+ },
220+ {
221+ Name : proto .String ("TestMethod2" ),
222+ InputType : proto .String (".otherpackage.OtherRequest" ),
223+ OutputType : proto .String (".otherpackage.OtherResponse" ),
224+ },
225+ // Reserved keyword
226+ {
227+ Name : proto .String ("Try" ),
228+ InputType : proto .String (".otherpackage.OtherRequest" ),
229+ OutputType : proto .String (".otherpackage.OtherResponse" ),
230+ },
231+ },
232+ },
233+ },
234+ MessageType : []* descriptorpb.DescriptorProto {
235+ {
236+ Name : proto .String ("TestRequest" ),
237+ },
238+ {
239+ Name : proto .String ("TestResponse" ),
240+ },
241+ },
242+ },
243+ {
244+ Name : proto .String ("other.proto" ),
245+ Package : proto .String ("otherpackage" ),
246+ MessageType : []* descriptorpb.DescriptorProto {
247+ {
248+ Name : proto .String ("OtherRequest" ),
249+ },
250+ {
251+ Name : proto .String ("OtherResponse" ),
252+ },
253+ },
254+ },
255+ },
256+ },
257+ wantErr : false ,
258+ wantStrings : []string {"class TestServiceASGIApplication" },
259+ dontWantStrings : []string {"class TestServiceWSGIApplication" },
260+ },
261+ {
262+ name : "sync only" ,
263+ req : & pluginpb.CodeGeneratorRequest {
264+ FileToGenerate : []string {"test.proto" },
265+ Parameter : proto .String ("async=false" ),
266+ ProtoFile : []* descriptorpb.FileDescriptorProto {
267+ {
268+ Name : proto .String ("test.proto" ),
269+ Package : proto .String ("test" ),
270+ Dependency : []string {"other.proto" },
271+ Service : []* descriptorpb.ServiceDescriptorProto {
272+ {
273+ Name : proto .String ("TestService" ),
274+ Method : []* descriptorpb.MethodDescriptorProto {
275+ {
276+ Name : proto .String ("TestMethod" ),
277+ InputType : proto .String (".test.TestRequest" ),
278+ OutputType : proto .String (".test.TestResponse" ),
279+ },
280+ {
281+ Name : proto .String ("TestMethod2" ),
282+ InputType : proto .String (".otherpackage.OtherRequest" ),
283+ OutputType : proto .String (".otherpackage.OtherResponse" ),
284+ },
285+ // Reserved keyword
286+ {
287+ Name : proto .String ("Try" ),
288+ InputType : proto .String (".otherpackage.OtherRequest" ),
289+ OutputType : proto .String (".otherpackage.OtherResponse" ),
290+ },
291+ },
292+ },
293+ },
294+ MessageType : []* descriptorpb.DescriptorProto {
295+ {
296+ Name : proto .String ("TestRequest" ),
297+ },
298+ {
299+ Name : proto .String ("TestResponse" ),
300+ },
301+ },
302+ },
303+ {
304+ Name : proto .String ("other.proto" ),
305+ Package : proto .String ("otherpackage" ),
306+ MessageType : []* descriptorpb.DescriptorProto {
307+ {
308+ Name : proto .String ("OtherRequest" ),
309+ },
310+ {
311+ Name : proto .String ("OtherResponse" ),
312+ },
313+ },
314+ },
315+ },
316+ },
317+ wantErr : false ,
318+ wantStrings : []string {"class TestServiceWSGIApplication" },
319+ dontWantStrings : []string {"class TestServiceASGIApplication" },
199320 },
200321 }
201322
@@ -219,6 +340,11 @@ func TestGenerate(t *testing.T) {
219340 t .Errorf ("generate() missing expected string: %v" , s )
220341 }
221342 }
343+ for _ , s := range tt .dontWantStrings {
344+ if strings .Contains (resp .GetFile ()[0 ].GetContent (), s ) {
345+ t .Errorf ("generate() contains unexpected string: %v" , s )
346+ }
347+ }
222348 }
223349 })
224350 }
0 commit comments