ClientCodeTranslatorSnippetBasedTests.swift 24 KB

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