ProtobufCodeGenParserTests.swift 8.1 KB

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