@@ -25,6 +25,10 @@ extension Generator {
2525 } else {
2626 printCGRPCClient ( asynchronousCode: asynchronousCode,
2727 synchronousCode: synchronousCode)
28+ if options. generateTestStubs {
29+ printCGRPCClientTestStubs ( asynchronousCode: asynchronousCode,
30+ synchronousCode: synchronousCode)
31+ }
2832 }
2933 }
3034
@@ -52,10 +56,25 @@ extension Generator {
5256 println ( )
5357 printServiceClientImplementation ( asynchronousCode: asynchronousCode,
5458 synchronousCode: synchronousCode)
55- if options. generateTestStubs {
56- println ( )
57- printServiceClientTestStubs ( )
59+ }
60+
61+ private func printCGRPCClientTestStubs( asynchronousCode: Bool ,
62+ synchronousCode: Bool ) {
63+ for method in service. methods {
64+ self . method = method
65+ switch streamingType ( method) {
66+ case . unary:
67+ printServiceClientMethodCallUnaryTestStub ( )
68+ case . serverStreaming:
69+ printServiceClientMethodCallServerStreamingTestStub ( )
70+ case . clientStreaming:
71+ printServiceClientMethodCallClientStreamingTestStub ( )
72+ case . bidirectionalStreaming:
73+ printServiceClientMethodCallBidiStreamingTestStub ( )
74+ }
5875 }
76+ println ( )
77+ printServiceClientTestStubs ( asynchronousCode: asynchronousCode, synchronousCode: synchronousCode)
5978 }
6079
6180 private func printServiceClientMethodCallUnary( ) {
@@ -66,17 +85,18 @@ extension Generator {
6685 println ( " override class var method: String { return \( methodPath) } " )
6786 outdent ( )
6887 println ( " } " )
69- if options. generateTestStubs {
70- println ( )
71- println ( " class \( callName) TestStub: ClientCallUnaryTestStub, \( callName) { " )
72- indent ( )
73- println ( " override class var method: String { return \( methodPath) } " )
74- outdent ( )
75- println ( " } " )
76- }
7788 println ( )
7889 }
7990
91+ private func printServiceClientMethodCallUnaryTestStub( ) {
92+ println ( )
93+ println ( " class \( callName) TestStub: ClientCallUnaryTestStub, \( callName) { " )
94+ indent ( )
95+ println ( " override class var method: String { return \( methodPath) } " )
96+ outdent ( )
97+ println ( " } " )
98+ }
99+
80100 private func printServiceClientMethodCallServerStreaming( ) {
81101 println ( " \( access) protocol \( callName) : ClientCallServerStreaming { " )
82102 indent ( )
@@ -91,17 +111,18 @@ extension Generator {
91111 println ( " override class var method: String { return \( methodPath) } " )
92112 outdent ( )
93113 println ( " } " )
94- if options. generateTestStubs {
95- println ( )
96- println ( " class \( callName) TestStub: ClientCallServerStreamingTestStub< \( methodOutputName) >, \( callName) { " )
97- indent ( )
98- println ( " override class var method: String { return \( methodPath) } " )
99- outdent ( )
100- println ( " } " )
101- }
102114 println ( )
103115 }
104116
117+ private func printServiceClientMethodCallServerStreamingTestStub( ) {
118+ println ( )
119+ println ( " class \( callName) TestStub: ClientCallServerStreamingTestStub< \( methodOutputName) >, \( callName) { " )
120+ indent ( )
121+ println ( " override class var method: String { return \( methodPath) } " )
122+ outdent ( )
123+ println ( " } " )
124+ }
125+
105126 private func printServiceClientMethodCallClientStreaming( ) {
106127 println ( " \( options. visibility. sourceSnippet) protocol \( callName) : ClientCallClientStreaming { " )
107128 indent ( )
@@ -121,19 +142,20 @@ extension Generator {
121142 println ( " override class var method: String { return \( methodPath) } " )
122143 outdent ( )
123144 println ( " } " )
124- if options. generateTestStubs {
125- println ( )
126- println ( " /// Simple fake implementation of \( callName) " )
127- println ( " /// stores sent values for later verification and finall returns a previously-defined result. " )
128- println ( " class \( callName) TestStub: ClientCallClientStreamingTestStub< \( methodInputName) , \( methodOutputName) >, \( callName) { " )
129- indent ( )
130- println ( " override class var method: String { return \( methodPath) } " )
131- outdent ( )
132- println ( " } " )
133- }
134145 println ( )
135146 }
136147
148+ private func printServiceClientMethodCallClientStreamingTestStub( ) {
149+ println ( )
150+ println ( " /// Simple fake implementation of \( callName) " )
151+ println ( " /// stores sent values for later verification and finall returns a previously-defined result. " )
152+ println ( " class \( callName) TestStub: ClientCallClientStreamingTestStub< \( methodInputName) , \( methodOutputName) >, \( callName) { " )
153+ indent ( )
154+ println ( " override class var method: String { return \( methodPath) } " )
155+ outdent ( )
156+ println ( " } " )
157+ }
158+
137159 private func printServiceClientMethodCallBidiStreaming( ) {
138160 println ( " \( access) protocol \( callName) : ClientCallBidirectionalStreaming { " )
139161 indent ( )
@@ -157,17 +179,18 @@ extension Generator {
157179 println ( " override class var method: String { return \( methodPath) } " )
158180 outdent ( )
159181 println ( " } " )
160- if options. generateTestStubs {
161- println ( )
162- println ( " class \( callName) TestStub: ClientCallBidirectionalStreamingTestStub< \( methodInputName) , \( methodOutputName) >, \( callName) { " )
163- indent ( )
164- println ( " override class var method: String { return \( methodPath) } " )
165- outdent ( )
166- println ( " } " )
167- }
168182 println ( )
169183 }
170184
185+ private func printServiceClientMethodCallBidiStreamingTestStub( ) {
186+ println ( )
187+ println ( " class \( callName) TestStub: ClientCallBidirectionalStreamingTestStub< \( methodInputName) , \( methodOutputName) >, \( callName) { " )
188+ indent ( )
189+ println ( " override class var method: String { return \( methodPath) } " )
190+ outdent ( )
191+ println ( " } " )
192+ }
193+
171194 private func printServiceClientProtocol( asynchronousCode: Bool ,
172195 synchronousCode: Bool ) {
173196 println ( " /// Instantiate \( serviceClassName) Client, then call methods of this protocol to make API calls. " )
@@ -335,7 +358,8 @@ extension Generator {
335358 println ( " } " )
336359 }
337360
338- private func printServiceClientTestStubs( ) {
361+ private func printServiceClientTestStubs( asynchronousCode: Bool ,
362+ synchronousCode: Bool ) {
339363 println ( " class \( serviceClassName) TestStub: ServiceClientTestStubBase, \( serviceClassName) { " )
340364 indent ( )
341365 for method in service. methods {
@@ -344,19 +368,23 @@ extension Generator {
344368 case . unary:
345369 println ( " var \( methodFunctionName) Requests: [ \( methodInputName) ] = [] " )
346370 println ( " var \( methodFunctionName) Responses: [ \( methodOutputName) ] = [] " )
347- println ( " func \( methodFunctionName) (_ request: \( methodInputName) , metadata customMetadata: Metadata) throws -> \( methodOutputName) { " )
348- indent ( )
349- println ( " \( methodFunctionName) Requests.append(request) " )
350- println ( " defer { \( methodFunctionName) Responses.removeFirst() } " )
351- println ( " return \( methodFunctionName) Responses.first! " )
352- outdent ( )
353- println ( " } " )
354- println ( " @discardableResult " )
355- println ( " func \( methodFunctionName) (_ request: \( methodInputName) , metadata customMetadata: Metadata, completion: @escaping ( \( methodOutputName) ?, CallResult) -> Void) throws -> \( callName) { " )
356- indent ( )
357- println ( " fatalError( \" not implemented \" ) " )
358- outdent ( )
359- println ( " } " )
371+ if synchronousCode {
372+ println ( " func \( methodFunctionName) (_ request: \( methodInputName) , metadata customMetadata: Metadata) throws -> \( methodOutputName) { " )
373+ indent ( )
374+ println ( " \( methodFunctionName) Requests.append(request) " )
375+ println ( " defer { \( methodFunctionName) Responses.removeFirst() } " )
376+ println ( " return \( methodFunctionName) Responses.first! " )
377+ outdent ( )
378+ println ( " } " )
379+ }
380+ if asynchronousCode {
381+ println ( " @discardableResult " )
382+ println ( " func \( methodFunctionName) (_ request: \( methodInputName) , metadata customMetadata: Metadata, completion: @escaping ( \( methodOutputName) ?, CallResult) -> Void) throws -> \( callName) { " )
383+ indent ( )
384+ println ( " fatalError( \" not implemented \" ) " )
385+ outdent ( )
386+ println ( " } " )
387+ }
360388 case . serverStreaming:
361389 println ( " var \( methodFunctionName) Requests: [ \( methodInputName) ] = [] " )
362390 println ( " var \( methodFunctionName) Calls: [ \( callName) ] = [] " )
0 commit comments