ProtobufCodeGeneratorTests.swift 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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 SwiftProtobufPluginLibrary
  19. import Testing
  20. struct ProtobufCodeGeneratorTests {
  21. @Suite("Self-contained service (test-service.proto)")
  22. struct TestService: UsesDescriptorSet {
  23. static let descriptorSetName = "test-service"
  24. static let fileDescriptorName = "test-service"
  25. @Test("Generate", arguments: [CodeGenerator.Config.AccessLevel.internal])
  26. func generate(accessLevel: GRPCCodeGen.CodeGenerator.Config.AccessLevel) throws {
  27. var config = ProtobufCodeGenerator.Config.defaults
  28. config.accessLevel = accessLevel
  29. config.indentation = 2
  30. let generator = ProtobufCodeGenerator(config: config)
  31. let access: String
  32. switch accessLevel {
  33. case .public:
  34. access = "public"
  35. case .internal:
  36. access = "internal"
  37. case .package:
  38. access = "package"
  39. default:
  40. fatalError()
  41. }
  42. let generated = try generator.generateCode(
  43. fileDescriptor: Self.fileDescriptor,
  44. protoFileModuleMappings: ProtoFileToModuleMappings(),
  45. extraModuleImports: []
  46. )
  47. let expected = """
  48. /// Leading trivia.
  49. // DO NOT EDIT.
  50. // swift-format-ignore-file
  51. // swiftlint:disable all
  52. //
  53. // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
  54. // Source: test-service.proto
  55. //
  56. // For information on using the generated types, please see the documentation:
  57. // https://github.com/grpc/grpc-swift
  58. import GRPCCore
  59. import GRPCProtobuf
  60. import SwiftProtobuf
  61. // MARK: - test.TestService
  62. /// Namespace containing generated types for the "test.TestService" service.
  63. \(access) enum Test_TestService {
  64. /// Service descriptor for the "test.TestService" service.
  65. \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService")
  66. /// Namespace for method metadata.
  67. \(access) enum Method {
  68. /// Namespace for "Unary" metadata.
  69. \(access) enum Unary {
  70. /// Request type for "Unary".
  71. \(access) typealias Input = Test_TestInput
  72. /// Response type for "Unary".
  73. \(access) typealias Output = Test_TestOutput
  74. /// Descriptor for "Unary".
  75. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  76. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  77. method: "Unary"
  78. )
  79. }
  80. /// Namespace for "ClientStreaming" metadata.
  81. \(access) enum ClientStreaming {
  82. /// Request type for "ClientStreaming".
  83. \(access) typealias Input = Test_TestInput
  84. /// Response type for "ClientStreaming".
  85. \(access) typealias Output = Test_TestOutput
  86. /// Descriptor for "ClientStreaming".
  87. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  88. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  89. method: "ClientStreaming"
  90. )
  91. }
  92. /// Namespace for "ServerStreaming" metadata.
  93. \(access) enum ServerStreaming {
  94. /// Request type for "ServerStreaming".
  95. \(access) typealias Input = Test_TestInput
  96. /// Response type for "ServerStreaming".
  97. \(access) typealias Output = Test_TestOutput
  98. /// Descriptor for "ServerStreaming".
  99. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  100. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  101. method: "ServerStreaming"
  102. )
  103. }
  104. /// Namespace for "BidirectionalStreaming" metadata.
  105. \(access) enum BidirectionalStreaming {
  106. /// Request type for "BidirectionalStreaming".
  107. \(access) typealias Input = Test_TestInput
  108. /// Response type for "BidirectionalStreaming".
  109. \(access) typealias Output = Test_TestOutput
  110. /// Descriptor for "BidirectionalStreaming".
  111. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  112. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  113. method: "BidirectionalStreaming"
  114. )
  115. }
  116. /// Descriptors for all methods in the "test.TestService" service.
  117. \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
  118. Unary.descriptor,
  119. ClientStreaming.descriptor,
  120. ServerStreaming.descriptor,
  121. BidirectionalStreaming.descriptor
  122. ]
  123. }
  124. }
  125. extension GRPCCore.ServiceDescriptor {
  126. /// Service descriptor for the "test.TestService" service.
  127. \(access) static let test_TestService = GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService")
  128. }
  129. // MARK: test.TestService (server)
  130. extension Test_TestService {
  131. /// Streaming variant of the service protocol for the "test.TestService" service.
  132. ///
  133. /// This protocol is the lowest-level of the service protocols generated for this service
  134. /// giving you the most flexibility over the implementation of your service. This comes at
  135. /// the cost of more verbose and less strict APIs. Each RPC requires you to implement it in
  136. /// terms of a request stream and response stream. Where only a single request or response
  137. /// message is expected, you are responsible for enforcing this invariant is maintained.
  138. ///
  139. /// Where possible, prefer using the stricter, less-verbose ``ServiceProtocol``
  140. /// or ``SimpleServiceProtocol`` instead.
  141. ///
  142. /// > Source IDL Documentation:
  143. /// >
  144. /// > Service docs.
  145. \(access) protocol StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  146. /// Handle the "Unary" method.
  147. ///
  148. /// > Source IDL Documentation:
  149. /// >
  150. /// > Unary docs.
  151. ///
  152. /// - Parameters:
  153. /// - request: A streaming request of `Test_TestInput` messages.
  154. /// - context: Context providing information about the RPC.
  155. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  156. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  157. /// to an internal error.
  158. /// - Returns: A streaming response of `Test_TestOutput` messages.
  159. func unary(
  160. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  161. context: GRPCCore.ServerContext
  162. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  163. /// Handle the "ClientStreaming" method.
  164. ///
  165. /// > Source IDL Documentation:
  166. /// >
  167. /// > Client streaming docs.
  168. ///
  169. /// - Parameters:
  170. /// - request: A streaming request of `Test_TestInput` messages.
  171. /// - context: Context providing information about the RPC.
  172. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  173. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  174. /// to an internal error.
  175. /// - Returns: A streaming response of `Test_TestOutput` messages.
  176. func clientStreaming(
  177. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  178. context: GRPCCore.ServerContext
  179. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  180. /// Handle the "ServerStreaming" method.
  181. ///
  182. /// > Source IDL Documentation:
  183. /// >
  184. /// > Server streaming docs.
  185. ///
  186. /// - Parameters:
  187. /// - request: A streaming request of `Test_TestInput` messages.
  188. /// - context: Context providing information about the RPC.
  189. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  190. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  191. /// to an internal error.
  192. /// - Returns: A streaming response of `Test_TestOutput` messages.
  193. func serverStreaming(
  194. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  195. context: GRPCCore.ServerContext
  196. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  197. /// Handle the "BidirectionalStreaming" method.
  198. ///
  199. /// > Source IDL Documentation:
  200. /// >
  201. /// > Bidirectional streaming docs.
  202. ///
  203. /// - Parameters:
  204. /// - request: A streaming request of `Test_TestInput` messages.
  205. /// - context: Context providing information about the RPC.
  206. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  207. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  208. /// to an internal error.
  209. /// - Returns: A streaming response of `Test_TestOutput` messages.
  210. func bidirectionalStreaming(
  211. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  212. context: GRPCCore.ServerContext
  213. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  214. }
  215. /// Service protocol for the "test.TestService" service.
  216. ///
  217. /// This protocol is higher level than ``StreamingServiceProtocol`` but lower level than
  218. /// the ``SimpleServiceProtocol``, it provides access to request and response metadata and
  219. /// trailing response metadata. If you don't need these then consider using
  220. /// the ``SimpleServiceProtocol``. If you need fine grained control over your RPCs then
  221. /// use ``StreamingServiceProtocol``.
  222. ///
  223. /// > Source IDL Documentation:
  224. /// >
  225. /// > Service docs.
  226. \(access) protocol ServiceProtocol: Test_TestService.StreamingServiceProtocol {
  227. /// Handle the "Unary" method.
  228. ///
  229. /// > Source IDL Documentation:
  230. /// >
  231. /// > Unary docs.
  232. ///
  233. /// - Parameters:
  234. /// - request: A request containing a single `Test_TestInput` message.
  235. /// - context: Context providing information about the RPC.
  236. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  237. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  238. /// to an internal error.
  239. /// - Returns: A response containing a single `Test_TestOutput` message.
  240. func unary(
  241. request: GRPCCore.ServerRequest<Test_TestInput>,
  242. context: GRPCCore.ServerContext
  243. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput>
  244. /// Handle the "ClientStreaming" method.
  245. ///
  246. /// > Source IDL Documentation:
  247. /// >
  248. /// > Client streaming docs.
  249. ///
  250. /// - Parameters:
  251. /// - request: A streaming request of `Test_TestInput` messages.
  252. /// - context: Context providing information about the RPC.
  253. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  254. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  255. /// to an internal error.
  256. /// - Returns: A response containing a single `Test_TestOutput` message.
  257. func clientStreaming(
  258. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  259. context: GRPCCore.ServerContext
  260. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput>
  261. /// Handle the "ServerStreaming" method.
  262. ///
  263. /// > Source IDL Documentation:
  264. /// >
  265. /// > Server streaming docs.
  266. ///
  267. /// - Parameters:
  268. /// - request: A request containing a single `Test_TestInput` message.
  269. /// - context: Context providing information about the RPC.
  270. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  271. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  272. /// to an internal error.
  273. /// - Returns: A streaming response of `Test_TestOutput` messages.
  274. func serverStreaming(
  275. request: GRPCCore.ServerRequest<Test_TestInput>,
  276. context: GRPCCore.ServerContext
  277. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  278. /// Handle the "BidirectionalStreaming" method.
  279. ///
  280. /// > Source IDL Documentation:
  281. /// >
  282. /// > Bidirectional streaming docs.
  283. ///
  284. /// - Parameters:
  285. /// - request: A streaming request of `Test_TestInput` messages.
  286. /// - context: Context providing information about the RPC.
  287. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  288. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  289. /// to an internal error.
  290. /// - Returns: A streaming response of `Test_TestOutput` messages.
  291. func bidirectionalStreaming(
  292. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  293. context: GRPCCore.ServerContext
  294. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  295. }
  296. /// Simple service protocol for the "test.TestService" service.
  297. ///
  298. /// This is the highest level protocol for the service. The API is the easiest to use but
  299. /// doesn't provide access to request or response metadata. If you need access to these
  300. /// then use ``ServiceProtocol`` instead.
  301. ///
  302. /// > Source IDL Documentation:
  303. /// >
  304. /// > Service docs.
  305. \(access) protocol SimpleServiceProtocol: Test_TestService.ServiceProtocol {
  306. /// Handle the "Unary" method.
  307. ///
  308. /// > Source IDL Documentation:
  309. /// >
  310. /// > Unary docs.
  311. ///
  312. /// - Parameters:
  313. /// - request: A `Test_TestInput` message.
  314. /// - context: Context providing information about the RPC.
  315. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  316. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  317. /// to an internal error.
  318. /// - Returns: A `Test_TestOutput` to respond with.
  319. func unary(
  320. request: Test_TestInput,
  321. context: GRPCCore.ServerContext
  322. ) async throws -> Test_TestOutput
  323. /// Handle the "ClientStreaming" method.
  324. ///
  325. /// > Source IDL Documentation:
  326. /// >
  327. /// > Client streaming docs.
  328. ///
  329. /// - Parameters:
  330. /// - request: A stream of `Test_TestInput` messages.
  331. /// - context: Context providing information about the RPC.
  332. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  333. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  334. /// to an internal error.
  335. /// - Returns: A `Test_TestOutput` to respond with.
  336. func clientStreaming(
  337. request: GRPCCore.RPCAsyncSequence<Test_TestInput, any Swift.Error>,
  338. context: GRPCCore.ServerContext
  339. ) async throws -> Test_TestOutput
  340. /// Handle the "ServerStreaming" method.
  341. ///
  342. /// > Source IDL Documentation:
  343. /// >
  344. /// > Server streaming docs.
  345. ///
  346. /// - Parameters:
  347. /// - request: A `Test_TestInput` message.
  348. /// - response: A response stream of `Test_TestOutput` messages.
  349. /// - context: Context providing information about the RPC.
  350. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  351. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  352. /// to an internal error.
  353. func serverStreaming(
  354. request: Test_TestInput,
  355. response: GRPCCore.RPCWriter<Test_TestOutput>,
  356. context: GRPCCore.ServerContext
  357. ) async throws
  358. /// Handle the "BidirectionalStreaming" method.
  359. ///
  360. /// > Source IDL Documentation:
  361. /// >
  362. /// > Bidirectional streaming docs.
  363. ///
  364. /// - Parameters:
  365. /// - request: A stream of `Test_TestInput` messages.
  366. /// - response: A response stream of `Test_TestOutput` messages.
  367. /// - context: Context providing information about the RPC.
  368. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  369. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  370. /// to an internal error.
  371. func bidirectionalStreaming(
  372. request: GRPCCore.RPCAsyncSequence<Test_TestInput, any Swift.Error>,
  373. response: GRPCCore.RPCWriter<Test_TestOutput>,
  374. context: GRPCCore.ServerContext
  375. ) async throws
  376. }
  377. }
  378. // Default implementation of 'registerMethods(with:)'.
  379. extension Test_TestService.StreamingServiceProtocol {
  380. \(access) func registerMethods<Transport>(with router: inout GRPCCore.RPCRouter<Transport>) where Transport: GRPCCore.ServerTransport {
  381. router.registerHandler(
  382. forMethod: Test_TestService.Method.Unary.descriptor,
  383. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  384. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  385. handler: { request, context in
  386. try await self.unary(
  387. request: request,
  388. context: context
  389. )
  390. }
  391. )
  392. router.registerHandler(
  393. forMethod: Test_TestService.Method.ClientStreaming.descriptor,
  394. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  395. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  396. handler: { request, context in
  397. try await self.clientStreaming(
  398. request: request,
  399. context: context
  400. )
  401. }
  402. )
  403. router.registerHandler(
  404. forMethod: Test_TestService.Method.ServerStreaming.descriptor,
  405. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  406. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  407. handler: { request, context in
  408. try await self.serverStreaming(
  409. request: request,
  410. context: context
  411. )
  412. }
  413. )
  414. router.registerHandler(
  415. forMethod: Test_TestService.Method.BidirectionalStreaming.descriptor,
  416. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  417. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  418. handler: { request, context in
  419. try await self.bidirectionalStreaming(
  420. request: request,
  421. context: context
  422. )
  423. }
  424. )
  425. }
  426. }
  427. // Default implementation of streaming methods from 'StreamingServiceProtocol'.
  428. extension Test_TestService.ServiceProtocol {
  429. \(access) func unary(
  430. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  431. context: GRPCCore.ServerContext
  432. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  433. let response = try await self.unary(
  434. request: GRPCCore.ServerRequest(stream: request),
  435. context: context
  436. )
  437. return GRPCCore.StreamingServerResponse(single: response)
  438. }
  439. \(access) func clientStreaming(
  440. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  441. context: GRPCCore.ServerContext
  442. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  443. let response = try await self.clientStreaming(
  444. request: request,
  445. context: context
  446. )
  447. return GRPCCore.StreamingServerResponse(single: response)
  448. }
  449. \(access) func serverStreaming(
  450. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  451. context: GRPCCore.ServerContext
  452. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  453. let response = try await self.serverStreaming(
  454. request: GRPCCore.ServerRequest(stream: request),
  455. context: context
  456. )
  457. return response
  458. }
  459. }
  460. // Default implementation of methods from 'ServiceProtocol'.
  461. extension Test_TestService.SimpleServiceProtocol {
  462. \(access) func unary(
  463. request: GRPCCore.ServerRequest<Test_TestInput>,
  464. context: GRPCCore.ServerContext
  465. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput> {
  466. return GRPCCore.ServerResponse<Test_TestOutput>(
  467. message: try await self.unary(
  468. request: request.message,
  469. context: context
  470. ),
  471. metadata: [:]
  472. )
  473. }
  474. \(access) func clientStreaming(
  475. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  476. context: GRPCCore.ServerContext
  477. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput> {
  478. return GRPCCore.ServerResponse<Test_TestOutput>(
  479. message: try await self.clientStreaming(
  480. request: request.messages,
  481. context: context
  482. ),
  483. metadata: [:]
  484. )
  485. }
  486. \(access) func serverStreaming(
  487. request: GRPCCore.ServerRequest<Test_TestInput>,
  488. context: GRPCCore.ServerContext
  489. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  490. return GRPCCore.StreamingServerResponse<Test_TestOutput>(
  491. metadata: [:],
  492. producer: { writer in
  493. try await self.serverStreaming(
  494. request: request.message,
  495. response: writer,
  496. context: context
  497. )
  498. return [:]
  499. }
  500. )
  501. }
  502. \(access) func bidirectionalStreaming(
  503. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  504. context: GRPCCore.ServerContext
  505. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  506. return GRPCCore.StreamingServerResponse<Test_TestOutput>(
  507. metadata: [:],
  508. producer: { writer in
  509. try await self.bidirectionalStreaming(
  510. request: request.messages,
  511. response: writer,
  512. context: context
  513. )
  514. return [:]
  515. }
  516. )
  517. }
  518. }
  519. // MARK: test.TestService (client)
  520. extension Test_TestService {
  521. /// Generated client protocol for the "test.TestService" service.
  522. ///
  523. /// You don't need to implement this protocol directly, use the generated
  524. /// implementation, ``Client``.
  525. ///
  526. /// > Source IDL Documentation:
  527. /// >
  528. /// > Service docs.
  529. \(access) protocol ClientProtocol: Sendable {
  530. /// Call the "Unary" method.
  531. ///
  532. /// > Source IDL Documentation:
  533. /// >
  534. /// > Unary docs.
  535. ///
  536. /// - Parameters:
  537. /// - request: A request containing a single `Test_TestInput` message.
  538. /// - serializer: A serializer for `Test_TestInput` messages.
  539. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  540. /// - options: Options to apply to this RPC.
  541. /// - handleResponse: A closure which handles the response, the result of which is
  542. /// returned to the caller. Returning from the closure will cancel the RPC if it
  543. /// hasn't already finished.
  544. /// - Returns: The result of `handleResponse`.
  545. func unary<Result>(
  546. request: GRPCCore.ClientRequest<Test_TestInput>,
  547. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  548. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  549. options: GRPCCore.CallOptions,
  550. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result
  551. ) async throws -> Result where Result: Sendable
  552. /// Call the "ClientStreaming" method.
  553. ///
  554. /// > Source IDL Documentation:
  555. /// >
  556. /// > Client streaming docs.
  557. ///
  558. /// - Parameters:
  559. /// - request: A streaming request producing `Test_TestInput` messages.
  560. /// - serializer: A serializer for `Test_TestInput` messages.
  561. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  562. /// - options: Options to apply to this RPC.
  563. /// - handleResponse: A closure which handles the response, the result of which is
  564. /// returned to the caller. Returning from the closure will cancel the RPC if it
  565. /// hasn't already finished.
  566. /// - Returns: The result of `handleResponse`.
  567. func clientStreaming<Result>(
  568. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  569. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  570. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  571. options: GRPCCore.CallOptions,
  572. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result
  573. ) async throws -> Result where Result: Sendable
  574. /// Call the "ServerStreaming" method.
  575. ///
  576. /// > Source IDL Documentation:
  577. /// >
  578. /// > Server streaming docs.
  579. ///
  580. /// - Parameters:
  581. /// - request: A request containing a single `Test_TestInput` message.
  582. /// - serializer: A serializer for `Test_TestInput` messages.
  583. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  584. /// - options: Options to apply to this RPC.
  585. /// - handleResponse: A closure which handles the response, the result of which is
  586. /// returned to the caller. Returning from the closure will cancel the RPC if it
  587. /// hasn't already finished.
  588. /// - Returns: The result of `handleResponse`.
  589. func serverStreaming<Result>(
  590. request: GRPCCore.ClientRequest<Test_TestInput>,
  591. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  592. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  593. options: GRPCCore.CallOptions,
  594. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  595. ) async throws -> Result where Result: Sendable
  596. /// Call the "BidirectionalStreaming" method.
  597. ///
  598. /// > Source IDL Documentation:
  599. /// >
  600. /// > Bidirectional streaming docs.
  601. ///
  602. /// - Parameters:
  603. /// - request: A streaming request producing `Test_TestInput` messages.
  604. /// - serializer: A serializer for `Test_TestInput` messages.
  605. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  606. /// - options: Options to apply to this RPC.
  607. /// - handleResponse: A closure which handles the response, the result of which is
  608. /// returned to the caller. Returning from the closure will cancel the RPC if it
  609. /// hasn't already finished.
  610. /// - Returns: The result of `handleResponse`.
  611. func bidirectionalStreaming<Result>(
  612. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  613. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  614. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  615. options: GRPCCore.CallOptions,
  616. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  617. ) async throws -> Result where Result: Sendable
  618. }
  619. /// Generated client for the "test.TestService" service.
  620. ///
  621. /// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
  622. /// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
  623. /// means of communication with the remote peer.
  624. ///
  625. /// > Source IDL Documentation:
  626. /// >
  627. /// > Service docs.
  628. \(access) struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
  629. private let client: GRPCCore.GRPCClient<Transport>
  630. /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
  631. ///
  632. /// - Parameters:
  633. /// - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
  634. \(access) init(wrapping client: GRPCCore.GRPCClient<Transport>) {
  635. self.client = client
  636. }
  637. /// Call the "Unary" method.
  638. ///
  639. /// > Source IDL Documentation:
  640. /// >
  641. /// > Unary docs.
  642. ///
  643. /// - Parameters:
  644. /// - request: A request containing a single `Test_TestInput` message.
  645. /// - serializer: A serializer for `Test_TestInput` messages.
  646. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  647. /// - options: Options to apply to this RPC.
  648. /// - handleResponse: A closure which handles the response, the result of which is
  649. /// returned to the caller. Returning from the closure will cancel the RPC if it
  650. /// hasn't already finished.
  651. /// - Returns: The result of `handleResponse`.
  652. \(access) func unary<Result>(
  653. request: GRPCCore.ClientRequest<Test_TestInput>,
  654. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  655. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  656. options: GRPCCore.CallOptions = .defaults,
  657. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  658. try response.message
  659. }
  660. ) async throws -> Result where Result: Sendable {
  661. try await self.client.unary(
  662. request: request,
  663. descriptor: Test_TestService.Method.Unary.descriptor,
  664. serializer: serializer,
  665. deserializer: deserializer,
  666. options: options,
  667. onResponse: handleResponse
  668. )
  669. }
  670. /// Call the "ClientStreaming" method.
  671. ///
  672. /// > Source IDL Documentation:
  673. /// >
  674. /// > Client streaming docs.
  675. ///
  676. /// - Parameters:
  677. /// - request: A streaming request producing `Test_TestInput` messages.
  678. /// - serializer: A serializer for `Test_TestInput` messages.
  679. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  680. /// - options: Options to apply to this RPC.
  681. /// - handleResponse: A closure which handles the response, the result of which is
  682. /// returned to the caller. Returning from the closure will cancel the RPC if it
  683. /// hasn't already finished.
  684. /// - Returns: The result of `handleResponse`.
  685. \(access) func clientStreaming<Result>(
  686. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  687. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  688. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  689. options: GRPCCore.CallOptions = .defaults,
  690. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  691. try response.message
  692. }
  693. ) async throws -> Result where Result: Sendable {
  694. try await self.client.clientStreaming(
  695. request: request,
  696. descriptor: Test_TestService.Method.ClientStreaming.descriptor,
  697. serializer: serializer,
  698. deserializer: deserializer,
  699. options: options,
  700. onResponse: handleResponse
  701. )
  702. }
  703. /// Call the "ServerStreaming" method.
  704. ///
  705. /// > Source IDL Documentation:
  706. /// >
  707. /// > Server streaming docs.
  708. ///
  709. /// - Parameters:
  710. /// - request: A request containing a single `Test_TestInput` message.
  711. /// - serializer: A serializer for `Test_TestInput` messages.
  712. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  713. /// - options: Options to apply to this RPC.
  714. /// - handleResponse: A closure which handles the response, the result of which is
  715. /// returned to the caller. Returning from the closure will cancel the RPC if it
  716. /// hasn't already finished.
  717. /// - Returns: The result of `handleResponse`.
  718. \(access) func serverStreaming<Result>(
  719. request: GRPCCore.ClientRequest<Test_TestInput>,
  720. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  721. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  722. options: GRPCCore.CallOptions = .defaults,
  723. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  724. ) async throws -> Result where Result: Sendable {
  725. try await self.client.serverStreaming(
  726. request: request,
  727. descriptor: Test_TestService.Method.ServerStreaming.descriptor,
  728. serializer: serializer,
  729. deserializer: deserializer,
  730. options: options,
  731. onResponse: handleResponse
  732. )
  733. }
  734. /// Call the "BidirectionalStreaming" method.
  735. ///
  736. /// > Source IDL Documentation:
  737. /// >
  738. /// > Bidirectional streaming docs.
  739. ///
  740. /// - Parameters:
  741. /// - request: A streaming request producing `Test_TestInput` messages.
  742. /// - serializer: A serializer for `Test_TestInput` messages.
  743. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  744. /// - options: Options to apply to this RPC.
  745. /// - handleResponse: A closure which handles the response, the result of which is
  746. /// returned to the caller. Returning from the closure will cancel the RPC if it
  747. /// hasn't already finished.
  748. /// - Returns: The result of `handleResponse`.
  749. \(access) func bidirectionalStreaming<Result>(
  750. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  751. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  752. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  753. options: GRPCCore.CallOptions = .defaults,
  754. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  755. ) async throws -> Result where Result: Sendable {
  756. try await self.client.bidirectionalStreaming(
  757. request: request,
  758. descriptor: Test_TestService.Method.BidirectionalStreaming.descriptor,
  759. serializer: serializer,
  760. deserializer: deserializer,
  761. options: options,
  762. onResponse: handleResponse
  763. )
  764. }
  765. }
  766. }
  767. // Helpers providing default arguments to 'ClientProtocol' methods.
  768. extension Test_TestService.ClientProtocol {
  769. /// Call the "Unary" method.
  770. ///
  771. /// > Source IDL Documentation:
  772. /// >
  773. /// > Unary docs.
  774. ///
  775. /// - Parameters:
  776. /// - request: A request containing a single `Test_TestInput` message.
  777. /// - options: Options to apply to this RPC.
  778. /// - handleResponse: A closure which handles the response, the result of which is
  779. /// returned to the caller. Returning from the closure will cancel the RPC if it
  780. /// hasn't already finished.
  781. /// - Returns: The result of `handleResponse`.
  782. \(access) func unary<Result>(
  783. request: GRPCCore.ClientRequest<Test_TestInput>,
  784. options: GRPCCore.CallOptions = .defaults,
  785. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  786. try response.message
  787. }
  788. ) async throws -> Result where Result: Sendable {
  789. try await self.unary(
  790. request: request,
  791. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  792. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  793. options: options,
  794. onResponse: handleResponse
  795. )
  796. }
  797. /// Call the "ClientStreaming" method.
  798. ///
  799. /// > Source IDL Documentation:
  800. /// >
  801. /// > Client streaming docs.
  802. ///
  803. /// - Parameters:
  804. /// - request: A streaming request producing `Test_TestInput` messages.
  805. /// - options: Options to apply to this RPC.
  806. /// - handleResponse: A closure which handles the response, the result of which is
  807. /// returned to the caller. Returning from the closure will cancel the RPC if it
  808. /// hasn't already finished.
  809. /// - Returns: The result of `handleResponse`.
  810. \(access) func clientStreaming<Result>(
  811. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  812. options: GRPCCore.CallOptions = .defaults,
  813. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  814. try response.message
  815. }
  816. ) async throws -> Result where Result: Sendable {
  817. try await self.clientStreaming(
  818. request: request,
  819. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  820. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  821. options: options,
  822. onResponse: handleResponse
  823. )
  824. }
  825. /// Call the "ServerStreaming" method.
  826. ///
  827. /// > Source IDL Documentation:
  828. /// >
  829. /// > Server streaming docs.
  830. ///
  831. /// - Parameters:
  832. /// - request: A request containing a single `Test_TestInput` message.
  833. /// - options: Options to apply to this RPC.
  834. /// - handleResponse: A closure which handles the response, the result of which is
  835. /// returned to the caller. Returning from the closure will cancel the RPC if it
  836. /// hasn't already finished.
  837. /// - Returns: The result of `handleResponse`.
  838. \(access) func serverStreaming<Result>(
  839. request: GRPCCore.ClientRequest<Test_TestInput>,
  840. options: GRPCCore.CallOptions = .defaults,
  841. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  842. ) async throws -> Result where Result: Sendable {
  843. try await self.serverStreaming(
  844. request: request,
  845. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  846. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  847. options: options,
  848. onResponse: handleResponse
  849. )
  850. }
  851. /// Call the "BidirectionalStreaming" method.
  852. ///
  853. /// > Source IDL Documentation:
  854. /// >
  855. /// > Bidirectional streaming docs.
  856. ///
  857. /// - Parameters:
  858. /// - request: A streaming request producing `Test_TestInput` messages.
  859. /// - options: Options to apply to this RPC.
  860. /// - handleResponse: A closure which handles the response, the result of which is
  861. /// returned to the caller. Returning from the closure will cancel the RPC if it
  862. /// hasn't already finished.
  863. /// - Returns: The result of `handleResponse`.
  864. \(access) func bidirectionalStreaming<Result>(
  865. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  866. options: GRPCCore.CallOptions = .defaults,
  867. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  868. ) async throws -> Result where Result: Sendable {
  869. try await self.bidirectionalStreaming(
  870. request: request,
  871. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  872. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  873. options: options,
  874. onResponse: handleResponse
  875. )
  876. }
  877. }
  878. // Helpers providing sugared APIs for 'ClientProtocol' methods.
  879. extension Test_TestService.ClientProtocol {
  880. /// Call the "Unary" method.
  881. ///
  882. /// > Source IDL Documentation:
  883. /// >
  884. /// > Unary docs.
  885. ///
  886. /// - Parameters:
  887. /// - message: request message to send.
  888. /// - metadata: Additional metadata to send, defaults to empty.
  889. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  890. /// - handleResponse: A closure which handles the response, the result of which is
  891. /// returned to the caller. Returning from the closure will cancel the RPC if it
  892. /// hasn't already finished.
  893. /// - Returns: The result of `handleResponse`.
  894. \(access) func unary<Result>(
  895. _ message: Test_TestInput,
  896. metadata: GRPCCore.Metadata = [:],
  897. options: GRPCCore.CallOptions = .defaults,
  898. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  899. try response.message
  900. }
  901. ) async throws -> Result where Result: Sendable {
  902. let request = GRPCCore.ClientRequest<Test_TestInput>(
  903. message: message,
  904. metadata: metadata
  905. )
  906. return try await self.unary(
  907. request: request,
  908. options: options,
  909. onResponse: handleResponse
  910. )
  911. }
  912. /// Call the "ClientStreaming" method.
  913. ///
  914. /// > Source IDL Documentation:
  915. /// >
  916. /// > Client streaming docs.
  917. ///
  918. /// - Parameters:
  919. /// - metadata: Additional metadata to send, defaults to empty.
  920. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  921. /// - producer: A closure producing request messages to send to the server. The request
  922. /// stream is closed when the closure returns.
  923. /// - handleResponse: A closure which handles the response, the result of which is
  924. /// returned to the caller. Returning from the closure will cancel the RPC if it
  925. /// hasn't already finished.
  926. /// - Returns: The result of `handleResponse`.
  927. \(access) func clientStreaming<Result>(
  928. metadata: GRPCCore.Metadata = [:],
  929. options: GRPCCore.CallOptions = .defaults,
  930. requestProducer producer: @Sendable @escaping (GRPCCore.RPCWriter<Test_TestInput>) async throws -> Void,
  931. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  932. try response.message
  933. }
  934. ) async throws -> Result where Result: Sendable {
  935. let request = GRPCCore.StreamingClientRequest<Test_TestInput>(
  936. metadata: metadata,
  937. producer: producer
  938. )
  939. return try await self.clientStreaming(
  940. request: request,
  941. options: options,
  942. onResponse: handleResponse
  943. )
  944. }
  945. /// Call the "ServerStreaming" method.
  946. ///
  947. /// > Source IDL Documentation:
  948. /// >
  949. /// > Server streaming docs.
  950. ///
  951. /// - Parameters:
  952. /// - message: request message to send.
  953. /// - metadata: Additional metadata to send, defaults to empty.
  954. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  955. /// - handleResponse: A closure which handles the response, the result of which is
  956. /// returned to the caller. Returning from the closure will cancel the RPC if it
  957. /// hasn't already finished.
  958. /// - Returns: The result of `handleResponse`.
  959. \(access) func serverStreaming<Result>(
  960. _ message: Test_TestInput,
  961. metadata: GRPCCore.Metadata = [:],
  962. options: GRPCCore.CallOptions = .defaults,
  963. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  964. ) async throws -> Result where Result: Sendable {
  965. let request = GRPCCore.ClientRequest<Test_TestInput>(
  966. message: message,
  967. metadata: metadata
  968. )
  969. return try await self.serverStreaming(
  970. request: request,
  971. options: options,
  972. onResponse: handleResponse
  973. )
  974. }
  975. /// Call the "BidirectionalStreaming" method.
  976. ///
  977. /// > Source IDL Documentation:
  978. /// >
  979. /// > Bidirectional streaming docs.
  980. ///
  981. /// - Parameters:
  982. /// - metadata: Additional metadata to send, defaults to empty.
  983. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  984. /// - producer: A closure producing request messages to send to the server. The request
  985. /// stream is closed when the closure returns.
  986. /// - handleResponse: A closure which handles the response, the result of which is
  987. /// returned to the caller. Returning from the closure will cancel the RPC if it
  988. /// hasn't already finished.
  989. /// - Returns: The result of `handleResponse`.
  990. \(access) func bidirectionalStreaming<Result>(
  991. metadata: GRPCCore.Metadata = [:],
  992. options: GRPCCore.CallOptions = .defaults,
  993. requestProducer producer: @Sendable @escaping (GRPCCore.RPCWriter<Test_TestInput>) async throws -> Void,
  994. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  995. ) async throws -> Result where Result: Sendable {
  996. let request = GRPCCore.StreamingClientRequest<Test_TestInput>(
  997. metadata: metadata,
  998. producer: producer
  999. )
  1000. return try await self.bidirectionalStreaming(
  1001. request: request,
  1002. options: options,
  1003. onResponse: handleResponse
  1004. )
  1005. }
  1006. }
  1007. """
  1008. #expect(generated == expected)
  1009. }
  1010. @Test("Generate with different module names")
  1011. func generateWithDifferentModuleNames() throws {
  1012. var config = ProtobufCodeGenerator.Config.defaults
  1013. let defaultNames = config.moduleNames
  1014. config.accessLevel = .public
  1015. config.indentation = 2
  1016. config.moduleNames.grpcCore = String(config.moduleNames.grpcCore.reversed())
  1017. config.moduleNames.grpcProtobuf = String(config.moduleNames.grpcProtobuf.reversed())
  1018. config.moduleNames.swiftProtobuf = String(config.moduleNames.swiftProtobuf.reversed())
  1019. let generator = ProtobufCodeGenerator(config: config)
  1020. let generated = try generator.generateCode(
  1021. fileDescriptor: Self.fileDescriptor,
  1022. protoFileModuleMappings: ProtoFileToModuleMappings(),
  1023. extraModuleImports: []
  1024. )
  1025. // Mustn't contain the default names.
  1026. #expect(!generated.contains(defaultNames.grpcCore))
  1027. #expect(!generated.contains(defaultNames.grpcProtobuf))
  1028. #expect(!generated.contains(defaultNames.swiftProtobuf))
  1029. // Must contain the configured names.
  1030. #expect(generated.contains(config.moduleNames.grpcCore))
  1031. #expect(generated.contains(config.moduleNames.grpcProtobuf))
  1032. #expect(generated.contains(config.moduleNames.swiftProtobuf))
  1033. }
  1034. }
  1035. @Suite("File-without-services (foo-messages.proto)")
  1036. struct NoServices: UsesDescriptorSet {
  1037. static let descriptorSetName = "foo-messages"
  1038. static let fileDescriptorName = "foo-messages"
  1039. @Test("Generate")
  1040. func generate() throws {
  1041. var config: ProtobufCodeGenerator.Config = .defaults
  1042. config.accessLevel = .public
  1043. config.indentation = 2
  1044. let generator = ProtobufCodeGenerator(config: config)
  1045. let generated = try generator.generateCode(
  1046. fileDescriptor: Self.fileDescriptor,
  1047. protoFileModuleMappings: ProtoFileToModuleMappings(),
  1048. extraModuleImports: []
  1049. )
  1050. let expected = """
  1051. /// Leading trivia.
  1052. // DO NOT EDIT.
  1053. // swift-format-ignore-file
  1054. // swiftlint:disable all
  1055. //
  1056. // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
  1057. // Source: foo-messages.proto
  1058. //
  1059. // For information on using the generated types, please see the documentation:
  1060. // https://github.com/grpc/grpc-swift
  1061. // This file contained no services.
  1062. """
  1063. #expect(generated == expected)
  1064. }
  1065. }
  1066. }