Skip to content

Commit

Permalink
Generate ClientCallUnary test stubs. (#398)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
mpetrov authored and MrMage committed Mar 11, 2019
1 parent 9745a76 commit c0478aa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/Examples/Echo/Generated/echo.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftGRPC/Runtime/ClientCallUnary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 8 additions & 0 deletions Sources/protoc-gen-swiftgrpc/Generator-Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit c0478aa

Please sign in to comment.