| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- /*
- * Copyright 2023, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)
- import XCTest
- @testable import GRPCCodeGen
- final class ServerCodeTranslatorSnippetBasedTests: XCTestCase {
- typealias MethodDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
- typealias ServiceDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor
- func testServerCodeTranslatorUnaryMethod() throws {
- let method = MethodDescriptor(
- documentation: "Documentation for unaryMethod",
- name: "unaryMethod",
- isInputStreaming: false,
- isOutputStreaming: false,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let service = ServiceDescriptor(
- documentation: "Documentation for ServiceA",
- name: "ServiceA",
- namespace: "namespaceA",
- methods: [method]
- )
- let expectedSwift =
- """
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// Documentation for unaryMethod
- func unaryMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.unaryMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.unaryMethod.Output>
- }
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension namespaceA.ServiceA.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {
- router.registerHandler(
- for: namespaceA.ServiceA.Methods.unaryMethod.descriptor,
- deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.unaryMethod.Input>(),
- serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.unaryMethod.Output>(),
- handler: { request in
- try await self.unaryMethod(request: request)
- }
- )
- }
- }
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAServiceProtocol: namespaceA.ServiceA.StreamingServiceProtocol {
- /// Documentation for unaryMethod
- func unaryMethod(request: ServerRequest.Single<namespaceA.ServiceA.Methods.unaryMethod.Input>) async throws -> ServerResponse.Single<namespaceA.ServiceA.Methods.unaryMethod.Output>
- }
- /// Partial conformance to `namespaceA_ServiceAStreamingServiceProtocol`.
- public extension namespaceA.ServiceA.ServiceProtocol {
- func unaryMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.unaryMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.unaryMethod.Output> {
- let response = try await self.unaryMethod(request: ServerRequest.Single(stream: request))
- return ServerResponse.Stream(single: response)
- }
- }
- """
- try self.assertServerCodeTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
- expectedSwift: expectedSwift
- )
- }
- func testServerCodeTranslatorInputStreamingMethod() throws {
- let method = MethodDescriptor(
- documentation: "Documentation for inputStreamingMethod",
- name: "inputStreamingMethod",
- isInputStreaming: true,
- isOutputStreaming: false,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let service = ServiceDescriptor(
- documentation: "Documentation for ServiceA",
- name: "ServiceA",
- namespace: "namespaceA",
- methods: [method]
- )
- let expectedSwift =
- """
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// Documentation for inputStreamingMethod
- func inputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Output>
- }
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension namespaceA.ServiceA.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {
- router.registerHandler(
- for: namespaceA.ServiceA.Methods.inputStreamingMethod.descriptor,
- deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>(),
- serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.inputStreamingMethod.Output>(),
- handler: { request in
- try await self.inputStreamingMethod(request: request)
- }
- )
- }
- }
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAServiceProtocol: namespaceA.ServiceA.StreamingServiceProtocol {
- /// Documentation for inputStreamingMethod
- func inputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>) async throws -> ServerResponse.Single<namespaceA.ServiceA.Methods.inputStreamingMethod.Output>
- }
- /// Partial conformance to `namespaceA_ServiceAStreamingServiceProtocol`.
- public extension namespaceA.ServiceA.ServiceProtocol {
- func inputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Output> {
- let response = try await self.inputStreamingMethod(request: request)
- return ServerResponse.Stream(single: response)
- }
- }
- """
- try self.assertServerCodeTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
- expectedSwift: expectedSwift
- )
- }
- func testServerCodeTranslatorOutputStreamingMethod() throws {
- let method = MethodDescriptor(
- documentation: "Documentation for outputStreamingMethod",
- name: "outputStreamingMethod",
- isInputStreaming: false,
- isOutputStreaming: true,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let service = ServiceDescriptor(
- documentation: "Documentation for ServiceA",
- name: "ServiceA",
- namespace: "namespaceA",
- methods: [method]
- )
- let expectedSwift =
- """
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// Documentation for outputStreamingMethod
- func outputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Output>
- }
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension namespaceA.ServiceA.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {
- router.registerHandler(
- for: namespaceA.ServiceA.Methods.outputStreamingMethod.descriptor,
- deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>(),
- serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.outputStreamingMethod.Output>(),
- handler: { request in
- try await self.outputStreamingMethod(request: request)
- }
- )
- }
- }
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAServiceProtocol: namespaceA.ServiceA.StreamingServiceProtocol {
- /// Documentation for outputStreamingMethod
- func outputStreamingMethod(request: ServerRequest.Single<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Output>
- }
- /// Partial conformance to `namespaceA_ServiceAStreamingServiceProtocol`.
- public extension namespaceA.ServiceA.ServiceProtocol {
- func outputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Output> {
- let response = try await self.outputStreamingMethod(request: ServerRequest.Single(stream: request))
- return response
- }
- }
- """
- try self.assertServerCodeTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
- expectedSwift: expectedSwift
- )
- }
- func testServerCodeTranslatorBidirectionalStreamingMethod() throws {
- let method = MethodDescriptor(
- documentation: "Documentation for bidirectionalStreamingMethod",
- name: "bidirectionalStreamingMethod",
- isInputStreaming: true,
- isOutputStreaming: true,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let service = ServiceDescriptor(
- documentation: "Documentation for ServiceA",
- name: "ServiceA",
- namespace: "namespaceA",
- methods: [method]
- )
- let expectedSwift =
- """
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// Documentation for bidirectionalStreamingMethod
- func bidirectionalStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.bidirectionalStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.bidirectionalStreamingMethod.Output>
- }
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension namespaceA.ServiceA.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {
- router.registerHandler(
- for: namespaceA.ServiceA.Methods.bidirectionalStreamingMethod.descriptor,
- deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.bidirectionalStreamingMethod.Input>(),
- serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.bidirectionalStreamingMethod.Output>(),
- handler: { request in
- try await self.bidirectionalStreamingMethod(request: request)
- }
- )
- }
- }
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAServiceProtocol: namespaceA.ServiceA.StreamingServiceProtocol {
- /// Documentation for bidirectionalStreamingMethod
- func bidirectionalStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.bidirectionalStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.bidirectionalStreamingMethod.Output>
- }
- /// Partial conformance to `namespaceA_ServiceAStreamingServiceProtocol`.
- public extension namespaceA.ServiceA.ServiceProtocol {
- }
- """
- try self.assertServerCodeTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
- expectedSwift: expectedSwift
- )
- }
- func testServerCodeTranslatorMultipleMethods() throws {
- let inputStreamingMethod = MethodDescriptor(
- documentation: "Documentation for inputStreamingMethod",
- name: "inputStreamingMethod",
- isInputStreaming: true,
- isOutputStreaming: false,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let outputStreamingMethod = MethodDescriptor(
- documentation: "Documentation for outputStreamingMethod",
- name: "outputStreamingMethod",
- isInputStreaming: false,
- isOutputStreaming: true,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let service = ServiceDescriptor(
- documentation: "Documentation for ServiceA",
- name: "ServiceA",
- namespace: "namespaceA",
- methods: [inputStreamingMethod, outputStreamingMethod]
- )
- let expectedSwift =
- """
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// Documentation for inputStreamingMethod
- func inputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Output>
- /// Documentation for outputStreamingMethod
- func outputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Output>
- }
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension namespaceA.ServiceA.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {
- router.registerHandler(
- for: namespaceA.ServiceA.Methods.inputStreamingMethod.descriptor,
- deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>(),
- serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.inputStreamingMethod.Output>(),
- handler: { request in
- try await self.inputStreamingMethod(request: request)
- }
- )
- router.registerHandler(
- for: namespaceA.ServiceA.Methods.outputStreamingMethod.descriptor,
- deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>(),
- serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.outputStreamingMethod.Output>(),
- handler: { request in
- try await self.outputStreamingMethod(request: request)
- }
- )
- }
- }
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAServiceProtocol: namespaceA.ServiceA.StreamingServiceProtocol {
- /// Documentation for inputStreamingMethod
- func inputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>) async throws -> ServerResponse.Single<namespaceA.ServiceA.Methods.inputStreamingMethod.Output>
- /// Documentation for outputStreamingMethod
- func outputStreamingMethod(request: ServerRequest.Single<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Output>
- }
- /// Partial conformance to `namespaceA_ServiceAStreamingServiceProtocol`.
- public extension namespaceA.ServiceA.ServiceProtocol {
- func inputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.inputStreamingMethod.Output> {
- let response = try await self.inputStreamingMethod(request: request)
- return ServerResponse.Stream(single: response)
- }
- func outputStreamingMethod(request: ServerRequest.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Input>) async throws -> ServerResponse.Stream<namespaceA.ServiceA.Methods.outputStreamingMethod.Output> {
- let response = try await self.outputStreamingMethod(request: ServerRequest.Single(stream: request))
- return response
- }
- }
- """
- try assertServerCodeTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
- expectedSwift: expectedSwift
- )
- }
- func testServerCodeTranslatorNoNamespaceService() throws {
- let method = MethodDescriptor(
- documentation: "Documentation for MethodA",
- name: "methodA",
- isInputStreaming: false,
- isOutputStreaming: false,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let service = ServiceDescriptor(
- documentation: "Documentation for ServiceA",
- name: "ServiceA",
- namespace: "",
- methods: [method]
- )
- let expectedSwift =
- """
- /// Documentation for ServiceA
- protocol ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// Documentation for MethodA
- func methodA(request: ServerRequest.Stream<ServiceA.Methods.methodA.Input>) async throws -> ServerResponse.Stream<ServiceA.Methods.methodA.Output>
- }
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension ServiceA.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {
- router.registerHandler(
- for: ServiceA.Methods.methodA.descriptor,
- deserializer: ProtobufDeserializer<ServiceA.Methods.methodA.Input>(),
- serializer: ProtobufSerializer<ServiceA.Methods.methodA.Output>(),
- handler: { request in
- try await self.methodA(request: request)
- }
- )
- }
- }
- /// Documentation for ServiceA
- protocol ServiceAServiceProtocol: ServiceA.StreamingServiceProtocol {
- /// Documentation for MethodA
- func methodA(request: ServerRequest.Single<ServiceA.Methods.methodA.Input>) async throws -> ServerResponse.Single<ServiceA.Methods.methodA.Output>
- }
- /// Partial conformance to `ServiceAStreamingServiceProtocol`.
- public extension ServiceA.ServiceProtocol {
- func methodA(request: ServerRequest.Stream<ServiceA.Methods.methodA.Input>) async throws -> ServerResponse.Stream<ServiceA.Methods.methodA.Output> {
- let response = try await self.methodA(request: ServerRequest.Single(stream: request))
- return ServerResponse.Stream(single: response)
- }
- }
- """
- try self.assertServerCodeTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
- expectedSwift: expectedSwift
- )
- }
- func testServerCodeTranslatorMoreServicesOrder() throws {
- let serviceA = ServiceDescriptor(
- documentation: "Documentation for ServiceA",
- name: "ServiceA",
- namespace: "namespaceA",
- methods: []
- )
- let serviceB = ServiceDescriptor(
- documentation: "Documentation for ServiceB",
- name: "ServiceB",
- namespace: "namespaceA",
- methods: []
- )
- let expectedSwift =
- """
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {}
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension namespaceA.ServiceA.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {}
- }
- /// Documentation for ServiceA
- protocol namespaceA_ServiceAServiceProtocol: namespaceA.ServiceA.StreamingServiceProtocol {}
- /// Partial conformance to `namespaceA_ServiceAStreamingServiceProtocol`.
- public extension namespaceA.ServiceA.ServiceProtocol {
- }
- /// Documentation for ServiceB
- protocol namespaceA_ServiceBStreamingServiceProtocol: GRPCCore.RegistrableRPCService {}
- /// Conformance to `GRPCCore.RegistrableRPCService`.
- public extension namespaceA.ServiceB.StreamingServiceProtocol {
- func registerRPCs(with router: inout GRPCCore.RPCRouter) {}
- }
- /// Documentation for ServiceB
- protocol namespaceA_ServiceBServiceProtocol: namespaceA.ServiceB.StreamingServiceProtocol {}
- /// Partial conformance to `namespaceA_ServiceBStreamingServiceProtocol`.
- public extension namespaceA.ServiceB.ServiceProtocol {
- }
- """
- try self.assertServerCodeTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(services: [serviceA, serviceB]),
- expectedSwift: expectedSwift
- )
- }
- private func assertServerCodeTranslation(
- codeGenerationRequest: CodeGenerationRequest,
- expectedSwift: String
- ) throws {
- let translator = ServerCodeTranslator()
- let codeBlocks = try translator.translate(from: codeGenerationRequest)
- let renderer = TextBasedRenderer.default
- renderer.renderCodeBlocks(codeBlocks)
- let contents = renderer.renderedContents()
- try XCTAssertEqualWithDiff(contents, expectedSwift)
- }
- }
- #endif // os(macOS) || os(Linux)
|