ProtobufCodeGenParserTests.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. .init(module: "SwiftProtobuf", accessLevel: .internal),
  56. ]
  57. #expect(self.codeGen.dependencies == expected)
  58. }
  59. @Suite("Service")
  60. struct Service {
  61. let service: GRPCCodeGen.ServiceDescriptor
  62. init() throws {
  63. let request = try parseDescriptor(try #require(try TestService.fileDescriptor))
  64. try #require(request.services.count == 1)
  65. self.service = try #require(request.services.first)
  66. }
  67. @Test("Name")
  68. func name() {
  69. #expect(self.service.name.identifyingName == "test.TestService")
  70. }
  71. @Suite("Methods")
  72. struct Methods {
  73. let unary: GRPCCodeGen.MethodDescriptor
  74. let clientStreaming: GRPCCodeGen.MethodDescriptor
  75. let serverStreaming: GRPCCodeGen.MethodDescriptor
  76. let bidiStreaming: GRPCCodeGen.MethodDescriptor
  77. init() throws {
  78. let request = try parseDescriptor(try #require(try TestService.fileDescriptor))
  79. #expect(request.services.count == 1)
  80. let service = try #require(request.services.first)
  81. self.unary = service.methods[0]
  82. self.clientStreaming = service.methods[1]
  83. self.serverStreaming = service.methods[2]
  84. self.bidiStreaming = service.methods[3]
  85. }
  86. @Test("Documentation")
  87. func documentation() {
  88. #expect(self.unary.documentation == "/// Unary docs.\n")
  89. #expect(self.clientStreaming.documentation == "/// Client streaming docs.\n")
  90. #expect(self.serverStreaming.documentation == "/// Server streaming docs.\n")
  91. #expect(self.bidiStreaming.documentation == "/// Bidirectional streaming docs.\n")
  92. }
  93. @Test("Name")
  94. func name() {
  95. #expect(self.unary.name.identifyingName == "Unary")
  96. #expect(self.clientStreaming.name.identifyingName == "ClientStreaming")
  97. #expect(self.serverStreaming.name.identifyingName == "ServerStreaming")
  98. #expect(self.bidiStreaming.name.identifyingName == "BidirectionalStreaming")
  99. }
  100. @Test("Input")
  101. func input() {
  102. #expect(self.unary.inputType == "Test_TestInput")
  103. #expect(!self.unary.isInputStreaming)
  104. #expect(self.clientStreaming.inputType == "Test_TestInput")
  105. #expect(self.clientStreaming.isInputStreaming)
  106. #expect(self.serverStreaming.inputType == "Test_TestInput")
  107. #expect(!self.serverStreaming.isInputStreaming)
  108. #expect(self.bidiStreaming.inputType == "Test_TestInput")
  109. #expect(self.bidiStreaming.isInputStreaming)
  110. }
  111. @Test("Output")
  112. func output() {
  113. #expect(self.unary.outputType == "Test_TestOutput")
  114. #expect(!self.unary.isOutputStreaming)
  115. #expect(self.clientStreaming.outputType == "Test_TestOutput")
  116. #expect(!self.clientStreaming.isOutputStreaming)
  117. #expect(self.serverStreaming.outputType == "Test_TestOutput")
  118. #expect(self.serverStreaming.isOutputStreaming)
  119. #expect(self.bidiStreaming.outputType == "Test_TestOutput")
  120. #expect(self.bidiStreaming.isOutputStreaming)
  121. }
  122. }
  123. }
  124. }
  125. @Suite("Multi-service file (foo-service.proto)")
  126. struct FooService: UsesDescriptorSet {
  127. static let descriptorSetName = "foo-service"
  128. static let fileDescriptorName = "foo-service"
  129. let codeGen: CodeGenerationRequest
  130. init() throws {
  131. let descriptor = try #require(try Self.fileDescriptor)
  132. self.codeGen = try parseDescriptor(descriptor)
  133. }
  134. @Test("Name")
  135. func name() {
  136. #expect(self.codeGen.fileName == "foo-service.proto")
  137. }
  138. @Test("Dependencies")
  139. func dependencies() {
  140. let expected: [GRPCCodeGen.Dependency] = [
  141. .init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
  142. ]
  143. #expect(self.codeGen.dependencies == expected)
  144. }
  145. @Test("Service1")
  146. func service1() throws {
  147. let service = self.codeGen.services[0]
  148. #expect(service.name.identifyingName == "foo.FooService1")
  149. #expect(service.methods.count == 1)
  150. }
  151. @Test("Service1.Method")
  152. func service1Method() throws {
  153. let method = self.codeGen.services[0].methods[0]
  154. #expect(method.name.identifyingName == "Foo")
  155. #expect(method.inputType == "Foo_FooInput")
  156. #expect(method.outputType == "Foo_FooOutput")
  157. }
  158. @Test("Service2")
  159. func service2() throws {
  160. let service = self.codeGen.services[1]
  161. #expect(service.name.identifyingName == "foo.FooService2")
  162. #expect(service.methods.count == 1)
  163. }
  164. @Test("Service2.Method")
  165. func service2Method() throws {
  166. let method = self.codeGen.services[1].methods[0]
  167. #expect(method.name.identifyingName == "Foo")
  168. #expect(method.inputType == "Foo_FooInput")
  169. #expect(method.outputType == "Foo_FooOutput")
  170. }
  171. }
  172. @Suite("Service with no package (bar-service.proto)")
  173. struct BarService: UsesDescriptorSet {
  174. static var descriptorSetName: String { "bar-service" }
  175. static var fileDescriptorName: String { "bar-service" }
  176. let codeGen: CodeGenerationRequest
  177. let service: GRPCCodeGen.ServiceDescriptor
  178. init() throws {
  179. let descriptor = try #require(try Self.fileDescriptor)
  180. self.codeGen = try parseDescriptor(descriptor)
  181. self.service = try #require(self.codeGen.services.first)
  182. }
  183. @Test("Service name")
  184. func serviceName() {
  185. #expect(self.service.name.identifyingName == "BarService")
  186. }
  187. }
  188. @Suite("Service using 'well-known types' (wkt-service.proto)")
  189. struct WKTService: UsesDescriptorSet {
  190. static let descriptorSetName = "wkt-service"
  191. static let fileDescriptorName = "wkt-service"
  192. let codeGen: CodeGenerationRequest
  193. init() throws {
  194. let descriptor = try #require(try Self.fileDescriptor)
  195. self.codeGen = try parseDescriptor(descriptor)
  196. }
  197. @Test("Dependencies")
  198. func dependencies() {
  199. let expected: [Dependency] = [
  200. Dependency(module: "GRPCProtobuf", accessLevel: .internal),
  201. Dependency(module: "SwiftProtobuf", accessLevel: .internal),
  202. ]
  203. #expect(self.codeGen.dependencies == expected)
  204. }
  205. }
  206. }