Browse Source

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.
Martin Petrov 6 years ago
parent
commit
c0478aae0a

+ 4 - 0
Sources/Examples/Echo/Generated/echo.grpc.swift

@@ -31,6 +31,10 @@ fileprivate final class Echo_EchoGetCallBase: ClientCallUnaryBase<Echo_EchoReque
   override class var method: String { return "/echo.Echo/Get" }
 }
 
+class Echo_EchoGetCallTestStub: ClientCallUnaryTestStub, Echo_EchoGetCall {
+  override class var method: String { return "/echo.Echo/Get" }
+}
+
 internal protocol Echo_EchoExpandCall: ClientCallServerStreaming {
   /// Do not call this directly, call `receive()` in the protocol extension below instead.
   func _receive(timeout: DispatchTime) throws -> Echo_EchoResponse?

+ 7 - 0
Sources/SwiftGRPC/Runtime/ClientCallUnary.swift

@@ -56,3 +56,10 @@ open class ClientCallUnaryBase<InputType: Message, OutputType: Message>: 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() {}
+}

+ 8 - 0
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()
   }