ProtobufCodeGenParserTests.swift 6.9 KB

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