ProtobufCodeGenParserTests.swift 8.0 KB

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