ProtobufCodeGenParserTests.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 descriptorSet = DescriptorSet(
  24. protos: [
  25. Google_Protobuf_FileDescriptorProto(
  26. name: "same-module.proto",
  27. package: "same-package"
  28. ),
  29. Google_Protobuf_FileDescriptorProto(
  30. name: "different-module.proto",
  31. package: "different-package"
  32. ),
  33. Google_Protobuf_FileDescriptorProto.helloWorld,
  34. ]
  35. )
  36. guard let fileDescriptor = descriptorSet.fileDescriptor(named: "helloworld.proto") else {
  37. return XCTFail(
  38. """
  39. Could not find the file descriptor of "helloworld.proto".
  40. """
  41. )
  42. }
  43. let moduleMappings = SwiftProtobuf_GenSwift_ModuleMappings.with {
  44. $0.mapping = [
  45. SwiftProtobuf_GenSwift_ModuleMappings.Entry.with {
  46. $0.protoFilePath = ["different-module.proto"]
  47. $0.moduleName = "DifferentModule"
  48. }
  49. ]
  50. }
  51. let parsedCodeGenRequest = try ProtobufCodeGenParser(
  52. input: fileDescriptor,
  53. protoFileModuleMappings: ProtoFileToModuleMappings(moduleMappingsProto: moduleMappings),
  54. extraModuleImports: ["ExtraModule"]
  55. ).parse()
  56. XCTAssertEqual(parsedCodeGenRequest.fileName, "helloworld.proto")
  57. XCTAssertEqual(
  58. parsedCodeGenRequest.leadingTrivia,
  59. """
  60. // Copyright 2015 gRPC authors.
  61. //
  62. // Licensed under the Apache License, Version 2.0 (the "License");
  63. // you may not use this file except in compliance with the License.
  64. // You may obtain a copy of the License at
  65. //
  66. // http://www.apache.org/licenses/LICENSE-2.0
  67. //
  68. // Unless required by applicable law or agreed to in writing, software
  69. // distributed under the License is distributed on an "AS IS" BASIS,
  70. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  71. // See the License for the specific language governing permissions and
  72. // limitations under the License.
  73. // DO NOT EDIT.
  74. // swift-format-ignore-file
  75. //
  76. // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
  77. // Source: helloworld.proto
  78. //
  79. // For information on using the generated types, please see the documentation:
  80. // https://github.com/grpc/grpc-swift
  81. """
  82. )
  83. XCTAssertEqual(parsedCodeGenRequest.dependencies.count, 3)
  84. let expectedDependencyNames = ["GRPCProtobuf", "DifferentModule", "ExtraModule"]
  85. let parsedDependencyNames = parsedCodeGenRequest.dependencies.map { $0.module }
  86. XCTAssertEqual(parsedDependencyNames, expectedDependencyNames)
  87. XCTAssertEqual(parsedCodeGenRequest.services.count, 1)
  88. let expectedMethod = CodeGenerationRequest.ServiceDescriptor.MethodDescriptor(
  89. documentation: "/// Sends a greeting.\n",
  90. name: CodeGenerationRequest.Name(
  91. base: "SayHello",
  92. generatedUpperCase: "SayHello",
  93. generatedLowerCase: "sayHello"
  94. ),
  95. isInputStreaming: false,
  96. isOutputStreaming: false,
  97. inputType: "Helloworld_HelloRequest",
  98. outputType: "Helloworld_HelloReply"
  99. )
  100. guard let method = parsedCodeGenRequest.services.first?.methods.first else { return XCTFail() }
  101. XCTAssertEqual(method, expectedMethod)
  102. let expectedService = CodeGenerationRequest.ServiceDescriptor(
  103. documentation: "/// The greeting service definition.\n",
  104. name: CodeGenerationRequest.Name(
  105. base: "Greeter",
  106. generatedUpperCase: "Greeter",
  107. generatedLowerCase: "greeter"
  108. ),
  109. namespace: CodeGenerationRequest.Name(
  110. base: "helloworld",
  111. generatedUpperCase: "Helloworld",
  112. generatedLowerCase: "helloworld"
  113. ),
  114. methods: [expectedMethod]
  115. )
  116. guard let service = parsedCodeGenRequest.services.first else { return XCTFail() }
  117. XCTAssertEqual(service, expectedService)
  118. XCTAssertEqual(service.methods.count, 1)
  119. XCTAssertEqual(
  120. parsedCodeGenRequest.lookupSerializer("HelloRequest"),
  121. "ProtobufSerializer<HelloRequest>()"
  122. )
  123. XCTAssertEqual(
  124. parsedCodeGenRequest.lookupDeserializer("HelloRequest"),
  125. "ProtobufDeserializer<HelloRequest>()"
  126. )
  127. }
  128. }
  129. extension Google_Protobuf_FileDescriptorProto {
  130. static var helloWorld: Google_Protobuf_FileDescriptorProto {
  131. let requestType = Google_Protobuf_DescriptorProto.with {
  132. $0.name = "HelloRequest"
  133. $0.field = [
  134. Google_Protobuf_FieldDescriptorProto.with {
  135. $0.name = "name"
  136. $0.number = 1
  137. $0.label = .optional
  138. $0.type = .string
  139. $0.jsonName = "name"
  140. }
  141. ]
  142. }
  143. let responseType = Google_Protobuf_DescriptorProto.with {
  144. $0.name = "HelloReply"
  145. $0.field = [
  146. Google_Protobuf_FieldDescriptorProto.with {
  147. $0.name = "message"
  148. $0.number = 1
  149. $0.label = .optional
  150. $0.type = .string
  151. $0.jsonName = "message"
  152. }
  153. ]
  154. }
  155. let service = Google_Protobuf_ServiceDescriptorProto.with {
  156. $0.name = "Greeter"
  157. $0.method = [
  158. Google_Protobuf_MethodDescriptorProto.with {
  159. $0.name = "SayHello"
  160. $0.inputType = ".helloworld.HelloRequest"
  161. $0.outputType = ".helloworld.HelloReply"
  162. $0.clientStreaming = false
  163. $0.serverStreaming = false
  164. }
  165. ]
  166. }
  167. return Google_Protobuf_FileDescriptorProto.with {
  168. $0.name = "helloworld.proto"
  169. $0.package = "helloworld"
  170. $0.dependency = ["same-module.proto", "different-module.proto"]
  171. $0.publicDependency = [1, 2]
  172. $0.messageType = [requestType, responseType]
  173. $0.service = [service]
  174. $0.sourceCodeInfo = Google_Protobuf_SourceCodeInfo.with {
  175. $0.location = [
  176. Google_Protobuf_SourceCodeInfo.Location.with {
  177. $0.path = [12]
  178. $0.span = [14, 0, 18]
  179. $0.leadingDetachedComments = [
  180. """
  181. Copyright 2015 gRPC authors.
  182. Licensed under the Apache License, Version 2.0 (the \"License\");
  183. you may not use this file except in compliance with the License.
  184. You may obtain a copy of the License at
  185. http://www.apache.org/licenses/LICENSE-2.0
  186. Unless required by applicable law or agreed to in writing, software
  187. distributed under the License is distributed on an \"AS IS\" BASIS,
  188. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  189. See the License for the specific language governing permissions and
  190. limitations under the License.
  191. """
  192. ]
  193. },
  194. Google_Protobuf_SourceCodeInfo.Location.with {
  195. $0.path = [6, 0]
  196. $0.span = [19, 0, 22, 1]
  197. $0.leadingComments = " The greeting service definition.\n"
  198. },
  199. Google_Protobuf_SourceCodeInfo.Location.with {
  200. $0.path = [6, 0, 2, 0]
  201. $0.span = [21, 2, 53]
  202. $0.leadingComments = " Sends a greeting.\n"
  203. },
  204. ]
  205. }
  206. $0.syntax = "proto3"
  207. }
  208. }
  209. internal init(name: String, package: String) {
  210. self.init()
  211. self.name = name
  212. self.package = package
  213. }
  214. }