ProtobufCodeGenParserTests.swift 6.8 KB

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