ProtobufCodeGenParserTests.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2024, 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 GRPCCodeGen
  17. import SwiftProtobuf
  18. import SwiftProtobufPluginLibrary
  19. import XCTest
  20. @testable import GRPCProtobufCodeGen
  21. final class ProtobufCodeGenParserTests: XCTestCase {
  22. func testParser() throws {
  23. let parsedCodeGenRequest = try ProtobufCodeGenParser().parse(
  24. input: self.helloWorldFileDescriptor
  25. )
  26. XCTAssertEqual(parsedCodeGenRequest.fileName, "helloworld.proto")
  27. XCTAssertEqual(
  28. parsedCodeGenRequest.leadingTrivia,
  29. """
  30. // Copyright 2015 gRPC authors.
  31. //
  32. // Licensed under the Apache License, Version 2.0 (the "License");
  33. // you may not use this file except in compliance with the License.
  34. // You may obtain a copy of the License at
  35. //
  36. // http://www.apache.org/licenses/LICENSE-2.0
  37. //
  38. // Unless required by applicable law or agreed to in writing, software
  39. // distributed under the License is distributed on an "AS IS" BASIS,
  40. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41. // See the License for the specific language governing permissions and
  42. // limitations under the License.
  43. // DO NOT EDIT.
  44. // swift-format-ignore-file
  45. //
  46. // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
  47. // Source: helloworld.proto
  48. //
  49. // For information on using the generated types, please see the documentation:
  50. // https://github.com/grpc/grpc-swift
  51. """
  52. )
  53. XCTAssertEqual(parsedCodeGenRequest.services.count, 1)
  54. let expectedMethod = CodeGenerationRequest.ServiceDescriptor.MethodDescriptor(
  55. documentation: "/// Sends a greeting.\n",
  56. name: CodeGenerationRequest.Name(
  57. base: "SayHello",
  58. generatedUpperCase: "SayHello",
  59. generatedLowerCase: "sayHello"
  60. ),
  61. isInputStreaming: false,
  62. isOutputStreaming: false,
  63. inputType: "HelloRequest",
  64. outputType: "HelloReply"
  65. )
  66. guard let method = parsedCodeGenRequest.services.first?.methods.first else { return XCTFail() }
  67. XCTAssertEqual(method, expectedMethod)
  68. let expectedService = CodeGenerationRequest.ServiceDescriptor(
  69. documentation: "/// The greeting service definition.\n",
  70. name: CodeGenerationRequest.Name(
  71. base: "Greeter",
  72. generatedUpperCase: "Greeter",
  73. generatedLowerCase: "greeter"
  74. ),
  75. namespace: CodeGenerationRequest.Name(
  76. base: "helloworld",
  77. generatedUpperCase: "Helloworld",
  78. generatedLowerCase: "helloworld"
  79. ),
  80. methods: [expectedMethod]
  81. )
  82. guard let service = parsedCodeGenRequest.services.first else { return XCTFail() }
  83. XCTAssertEqual(service, expectedService)
  84. XCTAssertEqual(service.methods.count, 1)
  85. XCTAssertEqual(
  86. parsedCodeGenRequest.lookupSerializer("HelloRequest"),
  87. "ProtobufSerializer<HelloRequest>()"
  88. )
  89. XCTAssertEqual(
  90. parsedCodeGenRequest.lookupDeserializer("HelloRequest"),
  91. "ProtobufDeserializer<HelloRequest>()"
  92. )
  93. XCTAssertEqual(parsedCodeGenRequest.dependencies.count, 1)
  94. XCTAssertEqual(
  95. parsedCodeGenRequest.dependencies[0],
  96. CodeGenerationRequest.Dependency(module: "GRPCProtobuf")
  97. )
  98. }
  99. var helloWorldFileDescriptor: FileDescriptor {
  100. let requestType = Google_Protobuf_DescriptorProto.with {
  101. $0.name = "HelloRequest"
  102. $0.field = [
  103. Google_Protobuf_FieldDescriptorProto.with {
  104. $0.name = "name"
  105. $0.number = 1
  106. $0.label = .optional
  107. $0.type = .string
  108. $0.jsonName = "name"
  109. }
  110. ]
  111. }
  112. let responseType = Google_Protobuf_DescriptorProto.with {
  113. $0.name = "HelloReply"
  114. $0.field = [
  115. Google_Protobuf_FieldDescriptorProto.with {
  116. $0.name = "message"
  117. $0.number = 1
  118. $0.label = .optional
  119. $0.type = .string
  120. $0.jsonName = "message"
  121. }
  122. ]
  123. }
  124. let service = Google_Protobuf_ServiceDescriptorProto.with {
  125. $0.name = "Greeter"
  126. $0.method = [
  127. Google_Protobuf_MethodDescriptorProto.with {
  128. $0.name = "SayHello"
  129. $0.inputType = ".helloworld.HelloRequest"
  130. $0.outputType = ".helloworld.HelloReply"
  131. $0.clientStreaming = false
  132. $0.serverStreaming = false
  133. }
  134. ]
  135. }
  136. let protoDescriptor = Google_Protobuf_FileDescriptorProto.with {
  137. $0.name = "helloworld.proto"
  138. $0.package = "helloworld"
  139. $0.messageType = [requestType, responseType]
  140. $0.service = [service]
  141. $0.sourceCodeInfo = Google_Protobuf_SourceCodeInfo.with {
  142. $0.location = [
  143. Google_Protobuf_SourceCodeInfo.Location.with {
  144. $0.path = [12]
  145. $0.span = [14, 0, 18]
  146. $0.leadingDetachedComments = [
  147. """
  148. Copyright 2015 gRPC authors.
  149. Licensed under the Apache License, Version 2.0 (the \"License\");
  150. you may not use this file except in compliance with the License.
  151. You may obtain a copy of the License at
  152. http://www.apache.org/licenses/LICENSE-2.0
  153. Unless required by applicable law or agreed to in writing, software
  154. distributed under the License is distributed on an \"AS IS\" BASIS,
  155. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  156. See the License for the specific language governing permissions and
  157. limitations under the License.
  158. """
  159. ]
  160. },
  161. Google_Protobuf_SourceCodeInfo.Location.with {
  162. $0.path = [6, 0]
  163. $0.span = [19, 0, 22, 1]
  164. $0.leadingComments = " The greeting service definition.\n"
  165. },
  166. Google_Protobuf_SourceCodeInfo.Location.with {
  167. $0.path = [6, 0, 2, 0]
  168. $0.span = [21, 2, 53]
  169. $0.leadingComments = " Sends a greeting.\n"
  170. },
  171. ]
  172. }
  173. $0.syntax = "proto3"
  174. }
  175. let descriptorSet = DescriptorSet(protos: [protoDescriptor])
  176. return descriptorSet.fileDescriptor(named: "helloworld.proto")!
  177. }
  178. }