ServerCodeTranslatorSnippetBasedTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Testing
  17. @testable import GRPCCodeGen
  18. @Suite
  19. final class ServerCodeTranslatorSnippetBasedTests {
  20. @Test
  21. func translate() {
  22. let method = MethodDescriptor(
  23. documentation: "/// Documentation for unaryMethod",
  24. name: Name(base: "UnaryMethod", generatedUpperCase: "Unary", generatedLowerCase: "unary"),
  25. isInputStreaming: false,
  26. isOutputStreaming: false,
  27. inputType: "NamespaceA_ServiceARequest",
  28. outputType: "NamespaceA_ServiceAResponse"
  29. )
  30. let service = ServiceDescriptor(
  31. documentation: "/// Documentation for ServiceA",
  32. name: Name(
  33. base: "AlongNameForServiceA",
  34. generatedUpperCase: "ServiceA",
  35. generatedLowerCase: "serviceA"
  36. ),
  37. namespace: Name(
  38. base: "namespaceA",
  39. generatedUpperCase: "NamespaceA",
  40. generatedLowerCase: "namespaceA"
  41. ),
  42. methods: [method]
  43. )
  44. let expectedSwift = """
  45. extension NamespaceA_ServiceA {
  46. /// Documentation for ServiceA
  47. public protocol StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  48. /// Documentation for unaryMethod
  49. func unary(
  50. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  51. context: GRPCCore.ServerContext
  52. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  53. }
  54. /// Documentation for ServiceA
  55. public protocol ServiceProtocol: NamespaceA_ServiceA.StreamingServiceProtocol {
  56. /// Documentation for unaryMethod
  57. func unary(
  58. request: GRPCCore.ServerRequest<NamespaceA_ServiceARequest>,
  59. context: GRPCCore.ServerContext
  60. ) async throws -> GRPCCore.ServerResponse<NamespaceA_ServiceAResponse>
  61. }
  62. }
  63. /// Conformance to `GRPCCore.RegistrableRPCService`.
  64. extension NamespaceA_ServiceA.StreamingServiceProtocol {
  65. public func registerMethods(with router: inout GRPCCore.RPCRouter) {
  66. router.registerHandler(
  67. forMethod: NamespaceA_ServiceA.Method.Unary.descriptor,
  68. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  69. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  70. handler: { request, context in
  71. try await self.unary(
  72. request: request,
  73. context: context
  74. )
  75. }
  76. )
  77. }
  78. }
  79. extension NamespaceA_ServiceA.ServiceProtocol {
  80. public func unary(
  81. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  82. context: GRPCCore.ServerContext
  83. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse> {
  84. let response = try await self.unary(
  85. request: GRPCCore.ServerRequest(stream: request),
  86. context: context
  87. )
  88. return GRPCCore.StreamingServerResponse(single: response)
  89. }
  90. }
  91. """
  92. let rendered = self.render(accessLevel: .public, service: service)
  93. #expect(rendered == expectedSwift)
  94. }
  95. private func render(
  96. accessLevel: AccessModifier,
  97. service: ServiceDescriptor
  98. ) -> String {
  99. let translator = ServerCodeTranslator()
  100. let codeBlocks = translator.translate(accessModifier: accessLevel, service: service) {
  101. "GRPCProtobuf.ProtobufSerializer<\($0)>()"
  102. } deserializer: {
  103. "GRPCProtobuf.ProtobufDeserializer<\($0)>()"
  104. }
  105. let renderer = TextBasedRenderer.default
  106. renderer.renderCodeBlocks(codeBlocks)
  107. return renderer.renderedContents()
  108. }
  109. }