From c0478aae0a967f5ce6f7c8696ad33cd510ad6af8 Mon Sep 17 00:00:00 2001 From: Martin Petrov <357181+mpetrov@users.noreply.github.com> Date: Mon, 11 Mar 2019 10:36:11 -0400 Subject: [PATCH] Generate ClientCallUnary test stubs. (#398) * Generate ClientCallUnary test stubs. This simplifies testing for async calls, e.g. a failing request could be simulated with something like: ``` override func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall { completion(nil, .error(statusCode: .notFound)) return Echo_EchoGetCallTestStub() } ``` * Remove generics from unary call stub. --- Sources/Examples/Echo/Generated/echo.grpc.swift | 4 ++++ Sources/SwiftGRPC/Runtime/ClientCallUnary.swift | 7 +++++++ Sources/protoc-gen-swiftgrpc/Generator-Client.swift | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/Sources/Examples/Echo/Generated/echo.grpc.swift b/Sources/Examples/Echo/Generated/echo.grpc.swift index 87ee13fc3..03eb7c362 100644 --- a/Sources/Examples/Echo/Generated/echo.grpc.swift +++ b/Sources/Examples/Echo/Generated/echo.grpc.swift @@ -31,6 +31,10 @@ fileprivate final class Echo_EchoGetCallBase: ClientCallUnaryBase Echo_EchoResponse? diff --git a/Sources/SwiftGRPC/Runtime/ClientCallUnary.swift b/Sources/SwiftGRPC/Runtime/ClientCallUnary.swift index b70441f5c..1cb3823e2 100644 --- a/Sources/SwiftGRPC/Runtime/ClientCallUnary.swift +++ b/Sources/SwiftGRPC/Runtime/ClientCallUnary.swift @@ -56,3 +56,10 @@ open class ClientCallUnaryBase: ClientC return self } } + +/// Simple fake implementation of `ClientCallUnary`. +open class ClientCallUnaryTestStub: ClientCallUnary { + open class var method: String { fatalError("needs to be overridden") } + + open func cancel() {} +} diff --git a/Sources/protoc-gen-swiftgrpc/Generator-Client.swift b/Sources/protoc-gen-swiftgrpc/Generator-Client.swift index d3cf3e269..10f21ae94 100644 --- a/Sources/protoc-gen-swiftgrpc/Generator-Client.swift +++ b/Sources/protoc-gen-swiftgrpc/Generator-Client.swift @@ -66,6 +66,14 @@ 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() }