ProtobufCodeGenParserTests.swift 7.6 KB

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