ClientCodeTranslatorSnippetBasedTests.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright 2023, 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. #if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)
  17. import XCTest
  18. @testable import GRPCCodeGen
  19. final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
  20. typealias MethodDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
  21. typealias ServiceDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor
  22. func testClientCodeTranslatorUnaryMethod() throws {
  23. let method = MethodDescriptor(
  24. documentation: "Documentation for MethodA",
  25. name: "methodA",
  26. isInputStreaming: false,
  27. isOutputStreaming: false,
  28. inputType: "NamespaceA_ServiceARequest",
  29. outputType: "NamespaceA_ServiceAResponse"
  30. )
  31. let service = ServiceDescriptor(
  32. documentation: "Documentation for ServiceA",
  33. name: "ServiceA",
  34. namespace: "namespaceA",
  35. methods: [method]
  36. )
  37. let expectedSwift =
  38. """
  39. /// Documentation for ServiceA
  40. protocol namespaceA_ServiceAClientProtocol: Sendable {
  41. /// Documentation for MethodA
  42. func methodA<R>(
  43. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodA.Input>,
  44. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  45. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  46. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  47. ) async throws -> R where R: Sendable
  48. }
  49. extension namespaceA.ServiceA.ClientProtocol {
  50. func methodA<R>(
  51. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodA.Input>,
  52. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  53. ) async throws -> R where R: Sendable {
  54. try await self.methodA(
  55. request: request,
  56. serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.methodA.Input>(),
  57. deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.methodA.Output>(),
  58. body
  59. )
  60. }
  61. }
  62. /// Documentation for ServiceA
  63. struct namespaceA_ServiceAClient: namespaceA.ServiceA.ClientProtocol {
  64. let client: GRPCCore.GRPCClient
  65. init(client: GRPCCore.GRPCClient) {
  66. self.client = client
  67. }
  68. /// Documentation for MethodA
  69. func methodA<R>(
  70. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodA.Input>,
  71. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  72. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  73. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  74. ) async throws -> R where R: Sendable {
  75. try await self.client.unary(
  76. request: request,
  77. descriptor: namespaceA.ServiceA.Methods.methodA.descriptor,
  78. serializer: serializer,
  79. deserializer: deserializer,
  80. handler: body
  81. )
  82. }
  83. }
  84. """
  85. try self.assertClientCodeTranslation(
  86. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  87. expectedSwift: expectedSwift
  88. )
  89. }
  90. func testClientCodeTranslatorClientStreamingMethod() throws {
  91. let method = MethodDescriptor(
  92. documentation: "Documentation for MethodA",
  93. name: "methodA",
  94. isInputStreaming: true,
  95. isOutputStreaming: false,
  96. inputType: "NamespaceA_ServiceARequest",
  97. outputType: "NamespaceA_ServiceAResponse"
  98. )
  99. let service = ServiceDescriptor(
  100. documentation: "Documentation for ServiceA",
  101. name: "ServiceA",
  102. namespace: "namespaceA",
  103. methods: [method]
  104. )
  105. let expectedSwift =
  106. """
  107. /// Documentation for ServiceA
  108. protocol namespaceA_ServiceAClientProtocol: Sendable {
  109. /// Documentation for MethodA
  110. func methodA<R>(
  111. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  112. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  113. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  114. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  115. ) async throws -> R where R: Sendable
  116. }
  117. extension namespaceA.ServiceA.ClientProtocol {
  118. func methodA<R>(
  119. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  120. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  121. ) async throws -> R where R: Sendable {
  122. try await self.methodA(
  123. request: request,
  124. serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.methodA.Input>(),
  125. deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.methodA.Output>(),
  126. body
  127. )
  128. }
  129. }
  130. /// Documentation for ServiceA
  131. struct namespaceA_ServiceAClient: namespaceA.ServiceA.ClientProtocol {
  132. let client: GRPCCore.GRPCClient
  133. init(client: GRPCCore.GRPCClient) {
  134. self.client = client
  135. }
  136. /// Documentation for MethodA
  137. func methodA<R>(
  138. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  139. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  140. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  141. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  142. ) async throws -> R where R: Sendable {
  143. try await self.client.clientStreaming(
  144. request: request,
  145. descriptor: namespaceA.ServiceA.Methods.methodA.descriptor,
  146. serializer: serializer,
  147. deserializer: deserializer,
  148. handler: body
  149. )
  150. }
  151. }
  152. """
  153. try self.assertClientCodeTranslation(
  154. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  155. expectedSwift: expectedSwift
  156. )
  157. }
  158. func testClientCodeTranslatorServerStreamingMethod() throws {
  159. let method = MethodDescriptor(
  160. documentation: "Documentation for MethodA",
  161. name: "methodA",
  162. isInputStreaming: false,
  163. isOutputStreaming: true,
  164. inputType: "NamespaceA_ServiceARequest",
  165. outputType: "NamespaceA_ServiceAResponse"
  166. )
  167. let service = ServiceDescriptor(
  168. documentation: "Documentation for ServiceA",
  169. name: "ServiceA",
  170. namespace: "namespaceA",
  171. methods: [method]
  172. )
  173. let expectedSwift =
  174. """
  175. /// Documentation for ServiceA
  176. protocol namespaceA_ServiceAClientProtocol: Sendable {
  177. /// Documentation for MethodA
  178. func methodA<R>(
  179. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodA.Input>,
  180. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  181. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  182. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  183. ) async throws -> R where R: Sendable
  184. }
  185. extension namespaceA.ServiceA.ClientProtocol {
  186. func methodA<R>(
  187. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodA.Input>,
  188. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  189. ) async throws -> R where R: Sendable {
  190. try await self.methodA(
  191. request: request,
  192. serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.methodA.Input>(),
  193. deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.methodA.Output>(),
  194. body
  195. )
  196. }
  197. }
  198. /// Documentation for ServiceA
  199. struct namespaceA_ServiceAClient: namespaceA.ServiceA.ClientProtocol {
  200. let client: GRPCCore.GRPCClient
  201. init(client: GRPCCore.GRPCClient) {
  202. self.client = client
  203. }
  204. /// Documentation for MethodA
  205. func methodA<R>(
  206. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodA.Input>,
  207. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  208. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  209. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  210. ) async throws -> R where R: Sendable {
  211. try await self.client.serverStreaming(
  212. request: request,
  213. descriptor: namespaceA.ServiceA.Methods.methodA.descriptor,
  214. serializer: serializer,
  215. deserializer: deserializer,
  216. handler: body
  217. )
  218. }
  219. }
  220. """
  221. try self.assertClientCodeTranslation(
  222. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  223. expectedSwift: expectedSwift
  224. )
  225. }
  226. func testClientCodeTranslatorBidirectionalStreamingMethod() throws {
  227. let method = MethodDescriptor(
  228. documentation: "Documentation for MethodA",
  229. name: "methodA",
  230. isInputStreaming: true,
  231. isOutputStreaming: true,
  232. inputType: "NamespaceA_ServiceARequest",
  233. outputType: "NamespaceA_ServiceAResponse"
  234. )
  235. let service = ServiceDescriptor(
  236. documentation: "Documentation for ServiceA",
  237. name: "ServiceA",
  238. namespace: "namespaceA",
  239. methods: [method]
  240. )
  241. let expectedSwift =
  242. """
  243. /// Documentation for ServiceA
  244. protocol namespaceA_ServiceAClientProtocol: Sendable {
  245. /// Documentation for MethodA
  246. func methodA<R>(
  247. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  248. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  249. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  250. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  251. ) async throws -> R where R: Sendable
  252. }
  253. extension namespaceA.ServiceA.ClientProtocol {
  254. func methodA<R>(
  255. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  256. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  257. ) async throws -> R where R: Sendable {
  258. try await self.methodA(
  259. request: request,
  260. serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.methodA.Input>(),
  261. deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.methodA.Output>(),
  262. body
  263. )
  264. }
  265. }
  266. /// Documentation for ServiceA
  267. struct namespaceA_ServiceAClient: namespaceA.ServiceA.ClientProtocol {
  268. let client: GRPCCore.GRPCClient
  269. init(client: GRPCCore.GRPCClient) {
  270. self.client = client
  271. }
  272. /// Documentation for MethodA
  273. func methodA<R>(
  274. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  275. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  276. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  277. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  278. ) async throws -> R where R: Sendable {
  279. try await self.client.bidirectionalStreaming(
  280. request: request,
  281. descriptor: namespaceA.ServiceA.Methods.methodA.descriptor,
  282. serializer: serializer,
  283. deserializer: deserializer,
  284. handler: body
  285. )
  286. }
  287. }
  288. """
  289. try self.assertClientCodeTranslation(
  290. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  291. expectedSwift: expectedSwift
  292. )
  293. }
  294. func testClientCodeTranslatorMultipleMethods() throws {
  295. let methodA = MethodDescriptor(
  296. documentation: "Documentation for MethodA",
  297. name: "methodA",
  298. isInputStreaming: true,
  299. isOutputStreaming: false,
  300. inputType: "NamespaceA_ServiceARequest",
  301. outputType: "NamespaceA_ServiceAResponse"
  302. )
  303. let methodB = MethodDescriptor(
  304. documentation: "Documentation for MethodB",
  305. name: "methodB",
  306. isInputStreaming: false,
  307. isOutputStreaming: true,
  308. inputType: "NamespaceA_ServiceARequest",
  309. outputType: "NamespaceA_ServiceAResponse"
  310. )
  311. let service = ServiceDescriptor(
  312. documentation: "Documentation for ServiceA",
  313. name: "ServiceA",
  314. namespace: "namespaceA",
  315. methods: [methodA, methodB]
  316. )
  317. let expectedSwift =
  318. """
  319. /// Documentation for ServiceA
  320. protocol namespaceA_ServiceAClientProtocol: Sendable {
  321. /// Documentation for MethodA
  322. func methodA<R>(
  323. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  324. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  325. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  326. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  327. ) async throws -> R where R: Sendable
  328. /// Documentation for MethodB
  329. func methodB<R>(
  330. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodB.Input>,
  331. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodB.Input>,
  332. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodB.Output>,
  333. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodB.Output>) async throws -> R
  334. ) async throws -> R where R: Sendable
  335. }
  336. extension namespaceA.ServiceA.ClientProtocol {
  337. func methodA<R>(
  338. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  339. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  340. ) async throws -> R where R: Sendable {
  341. try await self.methodA(
  342. request: request,
  343. serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.methodA.Input>(),
  344. deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.methodA.Output>(),
  345. body
  346. )
  347. }
  348. func methodB<R>(
  349. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodB.Input>,
  350. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodB.Output>) async throws -> R
  351. ) async throws -> R where R: Sendable {
  352. try await self.methodB(
  353. request: request,
  354. serializer: ProtobufSerializer<namespaceA.ServiceA.Methods.methodB.Input>(),
  355. deserializer: ProtobufDeserializer<namespaceA.ServiceA.Methods.methodB.Output>(),
  356. body
  357. )
  358. }
  359. }
  360. /// Documentation for ServiceA
  361. struct namespaceA_ServiceAClient: namespaceA.ServiceA.ClientProtocol {
  362. let client: GRPCCore.GRPCClient
  363. init(client: GRPCCore.GRPCClient) {
  364. self.client = client
  365. }
  366. /// Documentation for MethodA
  367. func methodA<R>(
  368. request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
  369. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
  370. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
  371. _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
  372. ) async throws -> R where R: Sendable {
  373. try await self.client.clientStreaming(
  374. request: request,
  375. descriptor: namespaceA.ServiceA.Methods.methodA.descriptor,
  376. serializer: serializer,
  377. deserializer: deserializer,
  378. handler: body
  379. )
  380. }
  381. /// Documentation for MethodB
  382. func methodB<R>(
  383. request: ClientRequest.Single<namespaceA.ServiceA.Methods.methodB.Input>,
  384. serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodB.Input>,
  385. deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodB.Output>,
  386. _ body: @Sendable @escaping (ClientResponse.Stream<namespaceA.ServiceA.Methods.methodB.Output>) async throws -> R
  387. ) async throws -> R where R: Sendable {
  388. try await self.client.serverStreaming(
  389. request: request,
  390. descriptor: namespaceA.ServiceA.Methods.methodB.descriptor,
  391. serializer: serializer,
  392. deserializer: deserializer,
  393. handler: body
  394. )
  395. }
  396. }
  397. """
  398. try self.assertClientCodeTranslation(
  399. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  400. expectedSwift: expectedSwift
  401. )
  402. }
  403. func testClientCodeTranslatorNoNamespaceService() throws {
  404. let method = MethodDescriptor(
  405. documentation: "Documentation for MethodA",
  406. name: "methodA",
  407. isInputStreaming: false,
  408. isOutputStreaming: false,
  409. inputType: "ServiceARequest",
  410. outputType: "ServiceAResponse"
  411. )
  412. let service = ServiceDescriptor(
  413. documentation: "Documentation for ServiceA",
  414. name: "ServiceA",
  415. namespace: "",
  416. methods: [method]
  417. )
  418. let expectedSwift =
  419. """
  420. /// Documentation for ServiceA
  421. protocol ServiceAClientProtocol: Sendable {
  422. /// Documentation for MethodA
  423. func methodA<R>(
  424. request: ClientRequest.Single<ServiceA.Methods.methodA.Input>,
  425. serializer: some MessageSerializer<ServiceA.Methods.methodA.Input>,
  426. deserializer: some MessageDeserializer<ServiceA.Methods.methodA.Output>,
  427. _ body: @Sendable @escaping (ClientResponse.Single<ServiceA.Methods.methodA.Output>) async throws -> R
  428. ) async throws -> R where R: Sendable
  429. }
  430. extension ServiceA.ClientProtocol {
  431. func methodA<R>(
  432. request: ClientRequest.Single<ServiceA.Methods.methodA.Input>,
  433. _ body: @Sendable @escaping (ClientResponse.Single<ServiceA.Methods.methodA.Output>) async throws -> R
  434. ) async throws -> R where R: Sendable {
  435. try await self.methodA(
  436. request: request,
  437. serializer: ProtobufSerializer<ServiceA.Methods.methodA.Input>(),
  438. deserializer: ProtobufDeserializer<ServiceA.Methods.methodA.Output>(),
  439. body
  440. )
  441. }
  442. }
  443. /// Documentation for ServiceA
  444. struct ServiceAClient: ServiceA.ClientProtocol {
  445. let client: GRPCCore.GRPCClient
  446. init(client: GRPCCore.GRPCClient) {
  447. self.client = client
  448. }
  449. /// Documentation for MethodA
  450. func methodA<R>(
  451. request: ClientRequest.Single<ServiceA.Methods.methodA.Input>,
  452. serializer: some MessageSerializer<ServiceA.Methods.methodA.Input>,
  453. deserializer: some MessageDeserializer<ServiceA.Methods.methodA.Output>,
  454. _ body: @Sendable @escaping (ClientResponse.Single<ServiceA.Methods.methodA.Output>) async throws -> R
  455. ) async throws -> R where R: Sendable {
  456. try await self.client.unary(
  457. request: request,
  458. descriptor: ServiceA.Methods.methodA.descriptor,
  459. serializer: serializer,
  460. deserializer: deserializer,
  461. handler: body
  462. )
  463. }
  464. }
  465. """
  466. try self.assertClientCodeTranslation(
  467. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  468. expectedSwift: expectedSwift
  469. )
  470. }
  471. func testClientCodeTranslatorMultipleServices() throws {
  472. let serviceA = ServiceDescriptor(
  473. documentation: "Documentation for ServiceA",
  474. name: "ServiceA",
  475. namespace: "namespaceA",
  476. methods: []
  477. )
  478. let serviceB = ServiceDescriptor(
  479. documentation: "Documentation for ServiceB",
  480. name: "ServiceB",
  481. namespace: "",
  482. methods: []
  483. )
  484. let expectedSwift =
  485. """
  486. /// Documentation for ServiceA
  487. protocol namespaceA_ServiceAClientProtocol: Sendable {}
  488. extension namespaceA.ServiceA.ClientProtocol {
  489. }
  490. /// Documentation for ServiceA
  491. struct namespaceA_ServiceAClient: namespaceA.ServiceA.ClientProtocol {
  492. let client: GRPCCore.GRPCClient
  493. init(client: GRPCCore.GRPCClient) {
  494. self.client = client
  495. }
  496. }
  497. /// Documentation for ServiceB
  498. protocol ServiceBClientProtocol: Sendable {}
  499. extension ServiceB.ClientProtocol {
  500. }
  501. /// Documentation for ServiceB
  502. struct ServiceBClient: ServiceB.ClientProtocol {
  503. let client: GRPCCore.GRPCClient
  504. init(client: GRPCCore.GRPCClient) {
  505. self.client = client
  506. }
  507. }
  508. """
  509. try self.assertClientCodeTranslation(
  510. codeGenerationRequest: makeCodeGenerationRequest(services: [serviceA, serviceB]),
  511. expectedSwift: expectedSwift
  512. )
  513. }
  514. private func assertClientCodeTranslation(
  515. codeGenerationRequest: CodeGenerationRequest,
  516. expectedSwift: String
  517. ) throws {
  518. let translator = ClientCodeTranslator()
  519. let codeBlocks = try translator.translate(from: codeGenerationRequest)
  520. let renderer = TextBasedRenderer.default
  521. renderer.renderCodeBlocks(codeBlocks)
  522. let contents = renderer.renderedContents()
  523. try XCTAssertEqualWithDiff(contents, expectedSwift)
  524. }
  525. }
  526. #endif // os(macOS) || os(Linux)