ProtobufCodeGenParserTests.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 GRPCProtobufCodeGen
  18. import SwiftProtobuf
  19. import SwiftProtobufPluginLibrary
  20. import Testing
  21. @Suite
  22. struct ProtobufCodeGenParserTests {
  23. @Suite("Self-contained service (test-service.proto)")
  24. struct TestService: UsesDescriptorSet {
  25. static let descriptorSetName = "test-service"
  26. static let fileDescriptorName = "test-service"
  27. let codeGen: CodeGenerationRequest
  28. init() throws {
  29. let descriptor = try #require(try Self.fileDescriptor)
  30. self.codeGen = try parseDescriptor(descriptor)
  31. }
  32. @Test("Filename")
  33. func fileName() {
  34. #expect(self.codeGen.fileName == "test-service.proto")
  35. }
  36. @Test("Leading trivia")
  37. func leadingTrivia() {
  38. let expected = """
  39. /// Leading trivia.
  40. // DO NOT EDIT.
  41. // swift-format-ignore-file
  42. //
  43. // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
  44. // Source: test-service.proto
  45. //
  46. // For information on using the generated types, please see the documentation:
  47. // https://github.com/grpc/grpc-swift
  48. """
  49. #expect(self.codeGen.leadingTrivia == expected)
  50. }
  51. @Test("Dependencies")
  52. func dependencies() {
  53. let expected: [GRPCCodeGen.Dependency] = [
  54. .init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
  55. ]
  56. #expect(self.codeGen.dependencies == expected)
  57. }
  58. @Suite("Service")
  59. struct Service {
  60. let service: GRPCCodeGen.ServiceDescriptor
  61. init() throws {
  62. let request = try parseDescriptor(try #require(try TestService.fileDescriptor))
  63. try #require(request.services.count == 1)
  64. self.service = try #require(request.services.first)
  65. }
  66. @Test("Name")
  67. func name() {
  68. #expect(self.service.name.base == "TestService")
  69. }
  70. @Test("Namespace")
  71. func namespace() {
  72. #expect(self.service.namespace.base == "test")
  73. }
  74. @Suite("Methods")
  75. struct Methods {
  76. let unary: GRPCCodeGen.MethodDescriptor
  77. let clientStreaming: GRPCCodeGen.MethodDescriptor
  78. let serverStreaming: GRPCCodeGen.MethodDescriptor
  79. let bidiStreaming: GRPCCodeGen.MethodDescriptor
  80. init() throws {
  81. let request = try parseDescriptor(try #require(try TestService.fileDescriptor))
  82. #expect(request.services.count == 1)
  83. let service = try #require(request.services.first)
  84. self.unary = service.methods[0]
  85. self.clientStreaming = service.methods[1]
  86. self.serverStreaming = service.methods[2]
  87. self.bidiStreaming = service.methods[3]
  88. }
  89. @Test("Documentation")
  90. func documentation() {
  91. #expect(self.unary.documentation == "/// Unary docs.\n")
  92. #expect(self.clientStreaming.documentation == "/// Client streaming docs.\n")
  93. #expect(self.serverStreaming.documentation == "/// Server streaming docs.\n")
  94. #expect(self.bidiStreaming.documentation == "/// Bidirectional streaming docs.\n")
  95. }
  96. @Test("Name")
  97. func name() {
  98. #expect(self.unary.name.base == "Unary")
  99. #expect(self.clientStreaming.name.base == "ClientStreaming")
  100. #expect(self.serverStreaming.name.base == "ServerStreaming")
  101. #expect(self.bidiStreaming.name.base == "BidirectionalStreaming")
  102. }
  103. @Test("Input")
  104. func input() {
  105. #expect(self.unary.inputType == "Test_TestInput")
  106. #expect(!self.unary.isInputStreaming)
  107. #expect(self.clientStreaming.inputType == "Test_TestInput")
  108. #expect(self.clientStreaming.isInputStreaming)
  109. #expect(self.serverStreaming.inputType == "Test_TestInput")
  110. #expect(!self.serverStreaming.isInputStreaming)
  111. #expect(self.bidiStreaming.inputType == "Test_TestInput")
  112. #expect(self.bidiStreaming.isInputStreaming)
  113. }
  114. @Test("Output")
  115. func output() {
  116. #expect(self.unary.outputType == "Test_TestOutput")
  117. #expect(!self.unary.isOutputStreaming)
  118. #expect(self.clientStreaming.outputType == "Test_TestOutput")
  119. #expect(!self.clientStreaming.isOutputStreaming)
  120. #expect(self.serverStreaming.outputType == "Test_TestOutput")
  121. #expect(self.serverStreaming.isOutputStreaming)
  122. #expect(self.bidiStreaming.outputType == "Test_TestOutput")
  123. #expect(self.bidiStreaming.isOutputStreaming)
  124. }
  125. }
  126. }
  127. }
  128. @Suite("Multi-service file (foo-service.proto)")
  129. struct FooService: UsesDescriptorSet {
  130. static let descriptorSetName = "foo-service"
  131. static let fileDescriptorName = "foo-service"
  132. let codeGen: CodeGenerationRequest
  133. init() throws {
  134. let descriptor = try #require(try Self.fileDescriptor)
  135. self.codeGen = try parseDescriptor(descriptor)
  136. }
  137. @Test("Name")
  138. func name() {
  139. #expect(self.codeGen.fileName == "foo-service.proto")
  140. }
  141. @Test("Dependencies")
  142. func dependencies() {
  143. let expected: [GRPCCodeGen.Dependency] = [
  144. .init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
  145. ]
  146. #expect(self.codeGen.dependencies == expected)
  147. }
  148. @Test("Service1")
  149. func service1() throws {
  150. let service = self.codeGen.services[0]
  151. #expect(service.name.base == "FooService1")
  152. #expect(service.namespace.base == "foo")
  153. #expect(service.methods.count == 1)
  154. }
  155. @Test("Service1.Method")
  156. func service1Method() throws {
  157. let method = self.codeGen.services[0].methods[0]
  158. #expect(method.name.base == "Foo")
  159. #expect(method.inputType == "Foo_FooInput")
  160. #expect(method.outputType == "Foo_FooOutput")
  161. }
  162. @Test("Service2")
  163. func service2() throws {
  164. let service = self.codeGen.services[1]
  165. #expect(service.name.base == "FooService2")
  166. #expect(service.namespace.base == "foo")
  167. #expect(service.methods.count == 1)
  168. }
  169. @Test("Service2.Method")
  170. func service2Method() throws {
  171. let method = self.codeGen.services[1].methods[0]
  172. #expect(method.name.base == "Foo")
  173. #expect(method.inputType == "Foo_FooInput")
  174. #expect(method.outputType == "Foo_FooOutput")
  175. }
  176. }
  177. @Suite("Service with no package (bar-service.proto)")
  178. struct BarService: UsesDescriptorSet {
  179. static var descriptorSetName: String { "bar-service" }
  180. static var fileDescriptorName: String { "bar-service" }
  181. let codeGen: CodeGenerationRequest
  182. let service: GRPCCodeGen.ServiceDescriptor
  183. init() throws {
  184. let descriptor = try #require(try Self.fileDescriptor)
  185. self.codeGen = try parseDescriptor(descriptor)
  186. self.service = try #require(self.codeGen.services.first)
  187. }
  188. @Test("Service name")
  189. func serviceName() {
  190. #expect(self.service.name.base == "BarService")
  191. }
  192. @Test("Service namespace")
  193. func serviceNamespace() {
  194. #expect(self.service.namespace.base == "")
  195. }
  196. }
  197. @Suite("Service using 'well-known types' (wkt-service.proto)")
  198. struct WKTService: UsesDescriptorSet {
  199. static let descriptorSetName = "wkt-service"
  200. static let fileDescriptorName = "wkt-service"
  201. let codeGen: CodeGenerationRequest
  202. init() throws {
  203. let descriptor = try #require(try Self.fileDescriptor)
  204. self.codeGen = try parseDescriptor(descriptor)
  205. }
  206. @Test("Dependencies")
  207. func dependencies() {
  208. let expected: [Dependency] = [
  209. Dependency(module: "GRPCProtobuf", accessLevel: .internal),
  210. Dependency(module: "SwiftProtobuf", accessLevel: .internal),
  211. ]
  212. #expect(self.codeGen.dependencies == expected)
  213. }
  214. }
  215. }