Browse Source

Make the plugin's tests also generate test stubs, so we can ensure that those compile as well, and fix compilation of those stubs.

Daniel Alm 7 years ago
parent
commit
5ea856389f

+ 162 - 0
Examples/Echo/Generated/echo.grpc.swift

@@ -169,6 +169,21 @@ fileprivate final class Echo_EchoExpandCallImpl: Echo_EchoExpandCall {
   }
   }
 }
 }
 
 
+/// Simple fake implementation of Echo_EchoExpandCall that returns a previously-defined set of results.
+class Echo_EchoExpandCallTestStub: Echo_EchoExpandCall {
+  var outputs: [Echo_EchoResponse] = []
+  
+  func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
+    if let output = outputs.first {
+      outputs.removeFirst()
+      completion(output, nil)
+    } else {
+      completion(nil, Echo_EchoClientError.endOfStream)
+    }
+  }
+
+  func cancel() { }
+}
 
 
 /// Collect (Client Streaming)
 /// Collect (Client Streaming)
 internal protocol Echo_EchoCollectCall {
 internal protocol Echo_EchoCollectCall {
@@ -247,6 +262,22 @@ fileprivate final class Echo_EchoCollectCallImpl: Echo_EchoCollectCall {
   }
   }
 }
 }
 
 
+/// Simple fake implementation of Echo_EchoCollectCall
+/// stores sent values for later verification and finall returns a previously-defined result.
+class Echo_EchoCollectCallTestStub: Echo_EchoCollectCall {
+  var inputs: [Echo_EchoRequest] = []
+  var output: Echo_EchoResponse?
+
+  func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
+    inputs.append(message)
+  }
+  
+  func closeAndReceive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
+    completion(output!, nil)
+  }
+
+  func cancel() { }
+}
 
 
 /// Update (Bidirectional Streaming)
 /// Update (Bidirectional Streaming)
 internal protocol Echo_EchoUpdateCall {
 internal protocol Echo_EchoUpdateCall {
@@ -340,6 +371,29 @@ fileprivate final class Echo_EchoUpdateCallImpl: Echo_EchoUpdateCall {
   }
   }
 }
 }
 
 
+/// Simple fake implementation of Echo_EchoUpdateCall that returns a previously-defined set of results
+/// and stores sent values for later verification.
+class Echo_EchoUpdateCallTestStub: Echo_EchoUpdateCall {
+  var inputs: [Echo_EchoRequest] = []
+  var outputs: [Echo_EchoResponse] = []
+  
+  func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
+    if let output = outputs.first {
+      outputs.removeFirst()
+      completion(output, nil)
+    } else {
+      completion(nil, Echo_EchoClientError.endOfStream)
+    }
+  }
+
+  func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
+    inputs.append(message)
+  }
+
+  func closeSend(completion: (()->())?) throws { completion?() }
+
+  func cancel() { }
+}
 
 
 
 
 /// Instantiate Echo_EchoServiceImpl, then call methods of this protocol to make API calls.
 /// Instantiate Echo_EchoServiceImpl, then call methods of this protocol to make API calls.
@@ -465,6 +519,51 @@ internal final class Echo_EchoServiceClient: Echo_EchoService {
 
 
 }
 }
 
 
+/// Simple fake implementation of Echo_EchoService that returns a previously-defined set of results
+/// and stores request values passed into it for later verification.
+/// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
+class Echo_EchoServiceTestStub: Echo_EchoService {
+  var channel: Channel { fatalError("not implemented") }
+  var metadata = Metadata()
+  var host = ""
+  var timeout: TimeInterval = 0
+  
+  var getRequests: [Echo_EchoRequest] = []
+  var getResponses: [Echo_EchoResponse] = []
+  func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
+    getRequests.append(request)
+    defer { getResponses.removeFirst() }
+    return getResponses.first!
+  }
+  func get(_ request: Echo_EchoRequest,
+                  completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall {
+    fatalError("not implemented")
+  }
+
+  var expandRequests: [Echo_EchoRequest] = []
+  var expandCalls: [Echo_EchoExpandCall] = []
+  func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
+    throws -> Echo_EchoExpandCall {
+      expandRequests.append(request)
+      defer { expandCalls.removeFirst() }
+      return expandCalls.first!
+  }
+
+  var collectCalls: [Echo_EchoCollectCall] = []
+  func collect(completion: ((CallResult)->())?)
+    throws -> Echo_EchoCollectCall {
+      defer { collectCalls.removeFirst() }
+      return collectCalls.first!
+  }
+
+  var updateCalls: [Echo_EchoUpdateCall] = []
+  func update(completion: ((CallResult)->())?)
+    throws -> Echo_EchoUpdateCall {
+      defer { updateCalls.removeFirst() }
+      return updateCalls.first!
+  }
+
+}
 
 
 
 
 
 
@@ -505,6 +604,14 @@ fileprivate class Echo_EchoSessionImpl: Echo_EchoSession {
   }
   }
 }
 }
 
 
+class Echo_EchoSessionTestStub: Echo_EchoSession {
+  var requestMetadata = Metadata()
+
+  var statusCode = StatusCode.ok
+  var statusMessage = "OK"
+  var initialMetadata = Metadata()
+  var trailingMetadata = Metadata()
+}
 
 
 // Get (Unary Streaming)
 // Get (Unary Streaming)
 internal protocol Echo_EchoGetSession : Echo_EchoSession { }
 internal protocol Echo_EchoGetSession : Echo_EchoSession { }
@@ -533,6 +640,8 @@ fileprivate final class Echo_EchoGetSessionImpl : Echo_EchoSessionImpl, Echo_Ech
   }
   }
 }
 }
 
 
+/// Trivial fake implementation of Echo_EchoGetSession.
+class Echo_EchoGetSessionTestStub : Echo_EchoSessionTestStub, Echo_EchoGetSession { }
 
 
 // Expand (Server Streaming)
 // Expand (Server Streaming)
 internal protocol Echo_EchoExpandSession : Echo_EchoSession {
 internal protocol Echo_EchoExpandSession : Echo_EchoSession {
@@ -580,6 +689,17 @@ fileprivate final class Echo_EchoExpandSessionImpl : Echo_EchoSessionImpl, Echo_
   }
   }
 }
 }
 
 
+/// Simple fake implementation of Echo_EchoExpandSession that returns a previously-defined set of results
+/// and stores sent values for later verification.
+class Echo_EchoExpandSessionTestStub : Echo_EchoSessionTestStub, Echo_EchoExpandSession {
+  var outputs: [Echo_EchoResponse] = []
+
+  func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws {
+    outputs.append(response)
+  }
+
+  func close() throws { }
+}
 
 
 // Collect (Client Streaming)
 // Collect (Client Streaming)
 internal protocol Echo_EchoCollectSession : Echo_EchoSession {
 internal protocol Echo_EchoCollectSession : Echo_EchoSession {
@@ -636,6 +756,27 @@ fileprivate final class Echo_EchoCollectSessionImpl : Echo_EchoSessionImpl, Echo
   }
   }
 }
 }
 
 
+/// Simple fake implementation of Echo_EchoCollectSession that returns a previously-defined set of results
+/// and stores sent values for later verification.
+class Echo_EchoCollectSessionTestStub: Echo_EchoSessionTestStub, Echo_EchoCollectSession {
+  var inputs: [Echo_EchoRequest] = []
+  var output: Echo_EchoResponse?
+
+  func receive() throws -> Echo_EchoRequest {
+    if let input = inputs.first {
+      inputs.removeFirst()
+      return input
+    } else {
+      throw Echo_EchoClientError.endOfStream
+    }
+  }
+
+  func sendAndClose(_ response: Echo_EchoResponse) throws {
+    output = response
+  }
+
+  func close() throws { }
+}
 
 
 // Update (Bidirectional Streaming)
 // Update (Bidirectional Streaming)
 internal protocol Echo_EchoUpdateSession : Echo_EchoSession {
 internal protocol Echo_EchoUpdateSession : Echo_EchoSession {
@@ -705,6 +846,27 @@ fileprivate final class Echo_EchoUpdateSessionImpl : Echo_EchoSessionImpl, Echo_
   }
   }
 }
 }
 
 
+/// Simple fake implementation of Echo_EchoUpdateSession that returns a previously-defined set of results
+/// and stores sent values for later verification.
+class Echo_EchoUpdateSessionTestStub : Echo_EchoSessionTestStub, Echo_EchoUpdateSession {
+  var inputs: [Echo_EchoRequest] = []
+  var outputs: [Echo_EchoResponse] = []
+
+  func receive() throws -> Echo_EchoRequest {
+    if let input = inputs.first {
+      inputs.removeFirst()
+      return input
+    } else {
+      throw Echo_EchoClientError.endOfStream
+    }
+  }
+
+  func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws {
+    outputs.append(response)
+  }
+
+  func close() throws { }
+}
 
 
 
 
 /// Main server for generated service
 /// Main server for generated service

+ 1 - 1
Plugin/Makefile

@@ -9,7 +9,7 @@ build:  clear
 	cp .build/debug/protoc-gen-swift .
 	cp .build/debug/protoc-gen-swift .
 
 
 test:	build
 test:	build
-	protoc ../Examples/Echo/echo.proto --proto_path=../Examples/Echo --plugin=./protoc-gen-swiftgrpc --swiftgrpc_out=. --swift_out=.
+	protoc ../Examples/Echo/echo.proto --proto_path=../Examples/Echo --plugin=./protoc-gen-swiftgrpc --swiftgrpc_out=. --swift_out=. --swiftgrpc_opt=TestStubs=true
 	diff echo.grpc.swift ../Examples/Echo/Generated/echo.grpc.swift
 	diff echo.grpc.swift ../Examples/Echo/Generated/echo.grpc.swift
 
 
 deploy:
 deploy:

+ 1 - 1
Plugin/Templates/server-session-bidistreaming.swift

@@ -82,7 +82,7 @@ class {{ .|session:file,service,method }}TestStub : {{ .|service:file,service }}
     }
     }
   }
   }
 
 
-  func send(_ response: {{ method|output }}, completion: @escaping ()->()) throws {
+  func send(_ response: {{ method|output }}, completion: ((Bool)->())?) throws {
     outputs.append(response)
     outputs.append(response)
   }
   }
 
 

+ 1 - 1
Plugin/Templates/server-session-serverstreaming.swift

@@ -50,7 +50,7 @@ fileprivate final class {{ .|session:file,service,method }}Impl : {{ .|service:f
 class {{ .|session:file,service,method }}TestStub : {{ .|service:file,service }}SessionTestStub, {{ .|session:file,service,method }} {
 class {{ .|session:file,service,method }}TestStub : {{ .|service:file,service }}SessionTestStub, {{ .|session:file,service,method }} {
   var outputs: [{{ method|output }}] = []
   var outputs: [{{ method|output }}] = []
 
 
-  func send(_ response: {{ method|output }}, completion: @escaping ()->()) throws {
+  func send(_ response: {{ method|output }}, completion: ((Bool)->())?) throws {
     outputs.append(response)
     outputs.append(response)
   }
   }