|
|
@@ -25,6 +25,10 @@ extension Generator {
|
|
|
} else {
|
|
|
printCGRPCClient(asynchronousCode: asynchronousCode,
|
|
|
synchronousCode: synchronousCode)
|
|
|
+ if options.generateTestStubs {
|
|
|
+ printCGRPCClientTestStubs(asynchronousCode: asynchronousCode,
|
|
|
+ synchronousCode: synchronousCode)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -52,10 +56,25 @@ extension Generator {
|
|
|
println()
|
|
|
printServiceClientImplementation(asynchronousCode: asynchronousCode,
|
|
|
synchronousCode: synchronousCode)
|
|
|
- if options.generateTestStubs {
|
|
|
- println()
|
|
|
- printServiceClientTestStubs()
|
|
|
+ }
|
|
|
+
|
|
|
+ private func printCGRPCClientTestStubs(asynchronousCode: Bool,
|
|
|
+ synchronousCode: Bool) {
|
|
|
+ for method in service.methods {
|
|
|
+ self.method = method
|
|
|
+ switch streamingType(method) {
|
|
|
+ case .unary:
|
|
|
+ printServiceClientMethodCallUnaryTestStub()
|
|
|
+ case .serverStreaming:
|
|
|
+ printServiceClientMethodCallServerStreamingTestStub()
|
|
|
+ case .clientStreaming:
|
|
|
+ printServiceClientMethodCallClientStreamingTestStub()
|
|
|
+ case .bidirectionalStreaming:
|
|
|
+ printServiceClientMethodCallBidiStreamingTestStub()
|
|
|
+ }
|
|
|
}
|
|
|
+ println()
|
|
|
+ printServiceClientTestStubs(asynchronousCode: asynchronousCode, synchronousCode: synchronousCode)
|
|
|
}
|
|
|
|
|
|
private func printServiceClientMethodCallUnary() {
|
|
|
@@ -66,17 +85,18 @@ extension Generator {
|
|
|
println("override class var method: String { return \(methodPath) }")
|
|
|
outdent()
|
|
|
println("}")
|
|
|
- if options.generateTestStubs {
|
|
|
- println()
|
|
|
- println("class \(callName)TestStub: ClientCallUnaryTestStub, \(callName) {")
|
|
|
- indent()
|
|
|
- println("override class var method: String { return \(methodPath) }")
|
|
|
- outdent()
|
|
|
- println("}")
|
|
|
- }
|
|
|
println()
|
|
|
}
|
|
|
|
|
|
+ private func printServiceClientMethodCallUnaryTestStub() {
|
|
|
+ println()
|
|
|
+ println("class \(callName)TestStub: ClientCallUnaryTestStub, \(callName) {")
|
|
|
+ indent()
|
|
|
+ println("override class var method: String { return \(methodPath) }")
|
|
|
+ outdent()
|
|
|
+ println("}")
|
|
|
+ }
|
|
|
+
|
|
|
private func printServiceClientMethodCallServerStreaming() {
|
|
|
println("\(access) protocol \(callName): ClientCallServerStreaming {")
|
|
|
indent()
|
|
|
@@ -91,17 +111,18 @@ extension Generator {
|
|
|
println("override class var method: String { return \(methodPath) }")
|
|
|
outdent()
|
|
|
println("}")
|
|
|
- if options.generateTestStubs {
|
|
|
- println()
|
|
|
- println("class \(callName)TestStub: ClientCallServerStreamingTestStub<\(methodOutputName)>, \(callName) {")
|
|
|
- indent()
|
|
|
- println("override class var method: String { return \(methodPath) }")
|
|
|
- outdent()
|
|
|
- println("}")
|
|
|
- }
|
|
|
println()
|
|
|
}
|
|
|
|
|
|
+ private func printServiceClientMethodCallServerStreamingTestStub() {
|
|
|
+ println()
|
|
|
+ println("class \(callName)TestStub: ClientCallServerStreamingTestStub<\(methodOutputName)>, \(callName) {")
|
|
|
+ indent()
|
|
|
+ println("override class var method: String { return \(methodPath) }")
|
|
|
+ outdent()
|
|
|
+ println("}")
|
|
|
+ }
|
|
|
+
|
|
|
private func printServiceClientMethodCallClientStreaming() {
|
|
|
println("\(options.visibility.sourceSnippet) protocol \(callName): ClientCallClientStreaming {")
|
|
|
indent()
|
|
|
@@ -121,19 +142,20 @@ extension Generator {
|
|
|
println("override class var method: String { return \(methodPath) }")
|
|
|
outdent()
|
|
|
println("}")
|
|
|
- if options.generateTestStubs {
|
|
|
- println()
|
|
|
- println("/// Simple fake implementation of \(callName)")
|
|
|
- println("/// stores sent values for later verification and finall returns a previously-defined result.")
|
|
|
- println("class \(callName)TestStub: ClientCallClientStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
|
|
|
- indent()
|
|
|
- println("override class var method: String { return \(methodPath) }")
|
|
|
- outdent()
|
|
|
- println("}")
|
|
|
- }
|
|
|
println()
|
|
|
}
|
|
|
|
|
|
+ private func printServiceClientMethodCallClientStreamingTestStub() {
|
|
|
+ println()
|
|
|
+ println("/// Simple fake implementation of \(callName)")
|
|
|
+ println("/// stores sent values for later verification and finall returns a previously-defined result.")
|
|
|
+ println("class \(callName)TestStub: ClientCallClientStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
|
|
|
+ indent()
|
|
|
+ println("override class var method: String { return \(methodPath) }")
|
|
|
+ outdent()
|
|
|
+ println("}")
|
|
|
+ }
|
|
|
+
|
|
|
private func printServiceClientMethodCallBidiStreaming() {
|
|
|
println("\(access) protocol \(callName): ClientCallBidirectionalStreaming {")
|
|
|
indent()
|
|
|
@@ -157,17 +179,18 @@ extension Generator {
|
|
|
println("override class var method: String { return \(methodPath) }")
|
|
|
outdent()
|
|
|
println("}")
|
|
|
- if options.generateTestStubs {
|
|
|
- println()
|
|
|
- println("class \(callName)TestStub: ClientCallBidirectionalStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
|
|
|
- indent()
|
|
|
- println("override class var method: String { return \(methodPath) }")
|
|
|
- outdent()
|
|
|
- println("}")
|
|
|
- }
|
|
|
println()
|
|
|
}
|
|
|
|
|
|
+ private func printServiceClientMethodCallBidiStreamingTestStub() {
|
|
|
+ println()
|
|
|
+ println("class \(callName)TestStub: ClientCallBidirectionalStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
|
|
|
+ indent()
|
|
|
+ println("override class var method: String { return \(methodPath) }")
|
|
|
+ outdent()
|
|
|
+ println("}")
|
|
|
+ }
|
|
|
+
|
|
|
private func printServiceClientProtocol(asynchronousCode: Bool,
|
|
|
synchronousCode: Bool) {
|
|
|
println("/// Instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
|
|
|
@@ -335,7 +358,8 @@ extension Generator {
|
|
|
println("}")
|
|
|
}
|
|
|
|
|
|
- private func printServiceClientTestStubs() {
|
|
|
+ private func printServiceClientTestStubs(asynchronousCode: Bool,
|
|
|
+ synchronousCode: Bool) {
|
|
|
println("class \(serviceClassName)TestStub: ServiceClientTestStubBase, \(serviceClassName) {")
|
|
|
indent()
|
|
|
for method in service.methods {
|
|
|
@@ -344,19 +368,23 @@ extension Generator {
|
|
|
case .unary:
|
|
|
println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
|
|
|
println("var \(methodFunctionName)Responses: [\(methodOutputName)] = []")
|
|
|
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
|
|
|
- indent()
|
|
|
- println("\(methodFunctionName)Requests.append(request)")
|
|
|
- println("defer { \(methodFunctionName)Responses.removeFirst() }")
|
|
|
- println("return \(methodFunctionName)Responses.first!")
|
|
|
- outdent()
|
|
|
- println("}")
|
|
|
- println("@discardableResult")
|
|
|
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
|
|
|
- indent()
|
|
|
- println("fatalError(\"not implemented\")")
|
|
|
- outdent()
|
|
|
- println("}")
|
|
|
+ if synchronousCode {
|
|
|
+ println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
|
|
|
+ indent()
|
|
|
+ println("\(methodFunctionName)Requests.append(request)")
|
|
|
+ println("defer { \(methodFunctionName)Responses.removeFirst() }")
|
|
|
+ println("return \(methodFunctionName)Responses.first!")
|
|
|
+ outdent()
|
|
|
+ println("}")
|
|
|
+ }
|
|
|
+ if asynchronousCode {
|
|
|
+ println("@discardableResult")
|
|
|
+ println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
|
|
|
+ indent()
|
|
|
+ println("fatalError(\"not implemented\")")
|
|
|
+ outdent()
|
|
|
+ println("}")
|
|
|
+ }
|
|
|
case .serverStreaming:
|
|
|
println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
|
|
|
println("var \(methodFunctionName)Calls: [\(callName)] = []")
|