ServerCodeTranslatorSnippetBasedTests.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 ServerCodeTranslatorSnippetBasedTests: XCTestCase {
  20. func testServerCodeTranslatorUnaryMethod() throws {
  21. let method = MethodDescriptor(
  22. documentation: "/// Documentation for unaryMethod",
  23. name: Name(base: "UnaryMethod", generatedUpperCase: "Unary", generatedLowerCase: "unary"),
  24. isInputStreaming: false,
  25. isOutputStreaming: false,
  26. inputType: "NamespaceA_ServiceARequest",
  27. outputType: "NamespaceA_ServiceAResponse"
  28. )
  29. let service = ServiceDescriptor(
  30. documentation: "/// Documentation for ServiceA",
  31. name: Name(
  32. base: "AlongNameForServiceA",
  33. generatedUpperCase: "ServiceA",
  34. generatedLowerCase: "serviceA"
  35. ),
  36. namespace: Name(
  37. base: "namespaceA",
  38. generatedUpperCase: "NamespaceA",
  39. generatedLowerCase: "namespaceA"
  40. ),
  41. methods: [method]
  42. )
  43. let expectedSwift =
  44. """
  45. /// Documentation for ServiceA
  46. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  47. public protocol NamespaceA_ServiceA_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  48. /// Documentation for unaryMethod
  49. func unary(
  50. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  51. context: GRPCCore.ServerContext
  52. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  53. }
  54. /// Conformance to `GRPCCore.RegistrableRPCService`.
  55. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  56. extension NamespaceA_ServiceA.StreamingServiceProtocol {
  57. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  58. public func registerMethods(with router: inout GRPCCore.RPCRouter) {
  59. router.registerHandler(
  60. forMethod: NamespaceA_ServiceA.Method.Unary.descriptor,
  61. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  62. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  63. handler: { request, context in
  64. try await self.unary(
  65. request: request,
  66. context: context
  67. )
  68. }
  69. )
  70. }
  71. }
  72. /// Documentation for ServiceA
  73. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  74. public protocol NamespaceA_ServiceA_ServiceProtocol: NamespaceA_ServiceA.StreamingServiceProtocol {
  75. /// Documentation for unaryMethod
  76. func unary(
  77. request: GRPCCore.ServerRequest<NamespaceA_ServiceARequest>,
  78. context: GRPCCore.ServerContext
  79. ) async throws -> GRPCCore.ServerResponse<NamespaceA_ServiceAResponse>
  80. }
  81. /// Partial conformance to `NamespaceA_ServiceA_StreamingServiceProtocol`.
  82. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  83. extension NamespaceA_ServiceA.ServiceProtocol {
  84. public func unary(
  85. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  86. context: GRPCCore.ServerContext
  87. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse> {
  88. let response = try await self.unary(
  89. request: GRPCCore.ServerRequest(stream: request),
  90. context: context
  91. )
  92. return GRPCCore.StreamingServerResponse(single: response)
  93. }
  94. }
  95. """
  96. try self.assertServerCodeTranslation(
  97. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  98. expectedSwift: expectedSwift,
  99. accessLevel: .public
  100. )
  101. }
  102. func testServerCodeTranslatorInputStreamingMethod() throws {
  103. let method = MethodDescriptor(
  104. documentation: "/// Documentation for inputStreamingMethod",
  105. name: Name(
  106. base: "InputStreamingMethod",
  107. generatedUpperCase: "InputStreaming",
  108. generatedLowerCase: "inputStreaming"
  109. ),
  110. isInputStreaming: true,
  111. isOutputStreaming: false,
  112. inputType: "NamespaceA_ServiceARequest",
  113. outputType: "NamespaceA_ServiceAResponse"
  114. )
  115. let service = ServiceDescriptor(
  116. documentation: "/// Documentation for ServiceA",
  117. name: Name(base: "ServiceA", generatedUpperCase: "ServiceA", generatedLowerCase: "serviceA"),
  118. namespace: Name(
  119. base: "namespaceA",
  120. generatedUpperCase: "NamespaceA",
  121. generatedLowerCase: "namespaceA"
  122. ),
  123. methods: [method]
  124. )
  125. let expectedSwift =
  126. """
  127. /// Documentation for ServiceA
  128. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  129. package protocol NamespaceA_ServiceA_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  130. /// Documentation for inputStreamingMethod
  131. func inputStreaming(
  132. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  133. context: GRPCCore.ServerContext
  134. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  135. }
  136. /// Conformance to `GRPCCore.RegistrableRPCService`.
  137. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  138. extension NamespaceA_ServiceA.StreamingServiceProtocol {
  139. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  140. package func registerMethods(with router: inout GRPCCore.RPCRouter) {
  141. router.registerHandler(
  142. forMethod: NamespaceA_ServiceA.Method.InputStreaming.descriptor,
  143. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  144. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  145. handler: { request, context in
  146. try await self.inputStreaming(
  147. request: request,
  148. context: context
  149. )
  150. }
  151. )
  152. }
  153. }
  154. /// Documentation for ServiceA
  155. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  156. package protocol NamespaceA_ServiceA_ServiceProtocol: NamespaceA_ServiceA.StreamingServiceProtocol {
  157. /// Documentation for inputStreamingMethod
  158. func inputStreaming(
  159. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  160. context: GRPCCore.ServerContext
  161. ) async throws -> GRPCCore.ServerResponse<NamespaceA_ServiceAResponse>
  162. }
  163. /// Partial conformance to `NamespaceA_ServiceA_StreamingServiceProtocol`.
  164. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  165. extension NamespaceA_ServiceA.ServiceProtocol {
  166. package func inputStreaming(
  167. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  168. context: GRPCCore.ServerContext
  169. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse> {
  170. let response = try await self.inputStreaming(
  171. request: request,
  172. context: context
  173. )
  174. return GRPCCore.StreamingServerResponse(single: response)
  175. }
  176. }
  177. """
  178. try self.assertServerCodeTranslation(
  179. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  180. expectedSwift: expectedSwift,
  181. accessLevel: .package
  182. )
  183. }
  184. func testServerCodeTranslatorOutputStreamingMethod() throws {
  185. let method = MethodDescriptor(
  186. documentation: "/// Documentation for outputStreamingMethod",
  187. name: Name(
  188. base: "OutputStreamingMethod",
  189. generatedUpperCase: "OutputStreaming",
  190. generatedLowerCase: "outputStreaming"
  191. ),
  192. isInputStreaming: false,
  193. isOutputStreaming: true,
  194. inputType: "NamespaceA_ServiceARequest",
  195. outputType: "NamespaceA_ServiceAResponse"
  196. )
  197. let service = ServiceDescriptor(
  198. documentation: "/// Documentation for ServiceA",
  199. name: Name(
  200. base: "ServiceATest",
  201. generatedUpperCase: "ServiceA",
  202. generatedLowerCase: "serviceA"
  203. ),
  204. namespace: Name(
  205. base: "namespaceA",
  206. generatedUpperCase: "NamespaceA",
  207. generatedLowerCase: "namespaceA"
  208. ),
  209. methods: [method]
  210. )
  211. let expectedSwift =
  212. """
  213. /// Documentation for ServiceA
  214. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  215. public protocol NamespaceA_ServiceA_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  216. /// Documentation for outputStreamingMethod
  217. func outputStreaming(
  218. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  219. context: GRPCCore.ServerContext
  220. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  221. }
  222. /// Conformance to `GRPCCore.RegistrableRPCService`.
  223. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  224. extension NamespaceA_ServiceA.StreamingServiceProtocol {
  225. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  226. public func registerMethods(with router: inout GRPCCore.RPCRouter) {
  227. router.registerHandler(
  228. forMethod: NamespaceA_ServiceA.Method.OutputStreaming.descriptor,
  229. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  230. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  231. handler: { request, context in
  232. try await self.outputStreaming(
  233. request: request,
  234. context: context
  235. )
  236. }
  237. )
  238. }
  239. }
  240. /// Documentation for ServiceA
  241. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  242. public protocol NamespaceA_ServiceA_ServiceProtocol: NamespaceA_ServiceA.StreamingServiceProtocol {
  243. /// Documentation for outputStreamingMethod
  244. func outputStreaming(
  245. request: GRPCCore.ServerRequest<NamespaceA_ServiceARequest>,
  246. context: GRPCCore.ServerContext
  247. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  248. }
  249. /// Partial conformance to `NamespaceA_ServiceA_StreamingServiceProtocol`.
  250. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  251. extension NamespaceA_ServiceA.ServiceProtocol {
  252. public func outputStreaming(
  253. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  254. context: GRPCCore.ServerContext
  255. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse> {
  256. let response = try await self.outputStreaming(
  257. request: GRPCCore.ServerRequest(stream: request),
  258. context: context
  259. )
  260. return response
  261. }
  262. }
  263. """
  264. try self.assertServerCodeTranslation(
  265. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  266. expectedSwift: expectedSwift,
  267. accessLevel: .public
  268. )
  269. }
  270. func testServerCodeTranslatorBidirectionalStreamingMethod() throws {
  271. let method = MethodDescriptor(
  272. documentation: "/// Documentation for bidirectionalStreamingMethod",
  273. name: Name(
  274. base: "BidirectionalStreamingMethod",
  275. generatedUpperCase: "BidirectionalStreaming",
  276. generatedLowerCase: "bidirectionalStreaming"
  277. ),
  278. isInputStreaming: true,
  279. isOutputStreaming: true,
  280. inputType: "NamespaceA_ServiceARequest",
  281. outputType: "NamespaceA_ServiceAResponse"
  282. )
  283. let service = ServiceDescriptor(
  284. documentation: "/// Documentation for ServiceA",
  285. name: Name(
  286. base: "ServiceATest",
  287. generatedUpperCase: "ServiceA",
  288. generatedLowerCase: "serviceA"
  289. ),
  290. namespace: Name(
  291. base: "namespaceA",
  292. generatedUpperCase: "NamespaceA",
  293. generatedLowerCase: "namespaceA"
  294. ),
  295. methods: [method]
  296. )
  297. let expectedSwift =
  298. """
  299. /// Documentation for ServiceA
  300. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  301. package protocol NamespaceA_ServiceA_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  302. /// Documentation for bidirectionalStreamingMethod
  303. func bidirectionalStreaming(
  304. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  305. context: GRPCCore.ServerContext
  306. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  307. }
  308. /// Conformance to `GRPCCore.RegistrableRPCService`.
  309. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  310. extension NamespaceA_ServiceA.StreamingServiceProtocol {
  311. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  312. package func registerMethods(with router: inout GRPCCore.RPCRouter) {
  313. router.registerHandler(
  314. forMethod: NamespaceA_ServiceA.Method.BidirectionalStreaming.descriptor,
  315. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  316. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  317. handler: { request, context in
  318. try await self.bidirectionalStreaming(
  319. request: request,
  320. context: context
  321. )
  322. }
  323. )
  324. }
  325. }
  326. /// Documentation for ServiceA
  327. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  328. package protocol NamespaceA_ServiceA_ServiceProtocol: NamespaceA_ServiceA.StreamingServiceProtocol {
  329. /// Documentation for bidirectionalStreamingMethod
  330. func bidirectionalStreaming(
  331. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  332. context: GRPCCore.ServerContext
  333. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  334. }
  335. /// Partial conformance to `NamespaceA_ServiceA_StreamingServiceProtocol`.
  336. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  337. extension NamespaceA_ServiceA.ServiceProtocol {
  338. }
  339. """
  340. try self.assertServerCodeTranslation(
  341. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  342. expectedSwift: expectedSwift,
  343. accessLevel: .package
  344. )
  345. }
  346. func testServerCodeTranslatorMultipleMethods() throws {
  347. let inputStreamingMethod = MethodDescriptor(
  348. documentation: "/// Documentation for inputStreamingMethod",
  349. name: Name(
  350. base: "InputStreamingMethod",
  351. generatedUpperCase: "InputStreaming",
  352. generatedLowerCase: "inputStreaming"
  353. ),
  354. isInputStreaming: true,
  355. isOutputStreaming: false,
  356. inputType: "NamespaceA_ServiceARequest",
  357. outputType: "NamespaceA_ServiceAResponse"
  358. )
  359. let outputStreamingMethod = MethodDescriptor(
  360. documentation: "/// Documentation for outputStreamingMethod",
  361. name: Name(
  362. base: "outputStreamingMethod",
  363. generatedUpperCase: "OutputStreaming",
  364. generatedLowerCase: "outputStreaming"
  365. ),
  366. isInputStreaming: false,
  367. isOutputStreaming: true,
  368. inputType: "NamespaceA_ServiceARequest",
  369. outputType: "NamespaceA_ServiceAResponse"
  370. )
  371. let service = ServiceDescriptor(
  372. documentation: "/// Documentation for ServiceA",
  373. name: Name(
  374. base: "ServiceATest",
  375. generatedUpperCase: "ServiceA",
  376. generatedLowerCase: "serviceA"
  377. ),
  378. namespace: Name(
  379. base: "namespaceA",
  380. generatedUpperCase: "NamespaceA",
  381. generatedLowerCase: "namespaceA"
  382. ),
  383. methods: [inputStreamingMethod, outputStreamingMethod]
  384. )
  385. let expectedSwift =
  386. """
  387. /// Documentation for ServiceA
  388. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  389. internal protocol NamespaceA_ServiceA_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  390. /// Documentation for inputStreamingMethod
  391. func inputStreaming(
  392. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  393. context: GRPCCore.ServerContext
  394. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  395. /// Documentation for outputStreamingMethod
  396. func outputStreaming(
  397. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  398. context: GRPCCore.ServerContext
  399. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  400. }
  401. /// Conformance to `GRPCCore.RegistrableRPCService`.
  402. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  403. extension NamespaceA_ServiceA.StreamingServiceProtocol {
  404. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  405. internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
  406. router.registerHandler(
  407. forMethod: NamespaceA_ServiceA.Method.InputStreaming.descriptor,
  408. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  409. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  410. handler: { request, context in
  411. try await self.inputStreaming(
  412. request: request,
  413. context: context
  414. )
  415. }
  416. )
  417. router.registerHandler(
  418. forMethod: NamespaceA_ServiceA.Method.OutputStreaming.descriptor,
  419. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  420. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  421. handler: { request, context in
  422. try await self.outputStreaming(
  423. request: request,
  424. context: context
  425. )
  426. }
  427. )
  428. }
  429. }
  430. /// Documentation for ServiceA
  431. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  432. internal protocol NamespaceA_ServiceA_ServiceProtocol: NamespaceA_ServiceA.StreamingServiceProtocol {
  433. /// Documentation for inputStreamingMethod
  434. func inputStreaming(
  435. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  436. context: GRPCCore.ServerContext
  437. ) async throws -> GRPCCore.ServerResponse<NamespaceA_ServiceAResponse>
  438. /// Documentation for outputStreamingMethod
  439. func outputStreaming(
  440. request: GRPCCore.ServerRequest<NamespaceA_ServiceARequest>,
  441. context: GRPCCore.ServerContext
  442. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  443. }
  444. /// Partial conformance to `NamespaceA_ServiceA_StreamingServiceProtocol`.
  445. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  446. extension NamespaceA_ServiceA.ServiceProtocol {
  447. internal func inputStreaming(
  448. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  449. context: GRPCCore.ServerContext
  450. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse> {
  451. let response = try await self.inputStreaming(
  452. request: request,
  453. context: context
  454. )
  455. return GRPCCore.StreamingServerResponse(single: response)
  456. }
  457. internal func outputStreaming(
  458. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  459. context: GRPCCore.ServerContext
  460. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse> {
  461. let response = try await self.outputStreaming(
  462. request: GRPCCore.ServerRequest(stream: request),
  463. context: context
  464. )
  465. return response
  466. }
  467. }
  468. """
  469. try assertServerCodeTranslation(
  470. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  471. expectedSwift: expectedSwift,
  472. accessLevel: .internal
  473. )
  474. }
  475. func testServerCodeTranslatorNoNamespaceService() throws {
  476. let method = MethodDescriptor(
  477. documentation: "/// Documentation for MethodA",
  478. name: Name(base: "methodA", generatedUpperCase: "MethodA", generatedLowerCase: "methodA"),
  479. isInputStreaming: false,
  480. isOutputStreaming: false,
  481. inputType: "NamespaceA_ServiceARequest",
  482. outputType: "NamespaceA_ServiceAResponse"
  483. )
  484. let service = ServiceDescriptor(
  485. documentation: "/// Documentation for ServiceA",
  486. name: Name(
  487. base: "ServiceATest",
  488. generatedUpperCase: "ServiceA",
  489. generatedLowerCase: "serviceA"
  490. ),
  491. namespace: Name(base: "", generatedUpperCase: "", generatedLowerCase: ""),
  492. methods: [method]
  493. )
  494. let expectedSwift =
  495. """
  496. /// Documentation for ServiceA
  497. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  498. internal protocol ServiceA_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  499. /// Documentation for MethodA
  500. func methodA(
  501. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  502. context: GRPCCore.ServerContext
  503. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
  504. }
  505. /// Conformance to `GRPCCore.RegistrableRPCService`.
  506. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  507. extension ServiceA.StreamingServiceProtocol {
  508. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  509. internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
  510. router.registerHandler(
  511. forMethod: ServiceA.Method.MethodA.descriptor,
  512. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceARequest>(),
  513. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceAResponse>(),
  514. handler: { request, context in
  515. try await self.methodA(
  516. request: request,
  517. context: context
  518. )
  519. }
  520. )
  521. }
  522. }
  523. /// Documentation for ServiceA
  524. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  525. internal protocol ServiceA_ServiceProtocol: ServiceA.StreamingServiceProtocol {
  526. /// Documentation for MethodA
  527. func methodA(
  528. request: GRPCCore.ServerRequest<NamespaceA_ServiceARequest>,
  529. context: GRPCCore.ServerContext
  530. ) async throws -> GRPCCore.ServerResponse<NamespaceA_ServiceAResponse>
  531. }
  532. /// Partial conformance to `ServiceA_StreamingServiceProtocol`.
  533. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  534. extension ServiceA.ServiceProtocol {
  535. internal func methodA(
  536. request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
  537. context: GRPCCore.ServerContext
  538. ) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse> {
  539. let response = try await self.methodA(
  540. request: GRPCCore.ServerRequest(stream: request),
  541. context: context
  542. )
  543. return GRPCCore.StreamingServerResponse(single: response)
  544. }
  545. }
  546. """
  547. try self.assertServerCodeTranslation(
  548. codeGenerationRequest: makeCodeGenerationRequest(services: [service]),
  549. expectedSwift: expectedSwift,
  550. accessLevel: .internal
  551. )
  552. }
  553. func testServerCodeTranslatorMoreServicesOrder() throws {
  554. let serviceA = ServiceDescriptor(
  555. documentation: "/// Documentation for ServiceA",
  556. name: Name(base: "ServiceA", generatedUpperCase: "ServiceA", generatedLowerCase: "serviceA"),
  557. namespace: Name(
  558. base: "namespaceA",
  559. generatedUpperCase: "NamespaceA",
  560. generatedLowerCase: "namespaceA"
  561. ),
  562. methods: []
  563. )
  564. let serviceB = ServiceDescriptor(
  565. documentation: "/// Documentation for ServiceB",
  566. name: Name(base: "ServiceB", generatedUpperCase: "ServiceB", generatedLowerCase: "serviceB"),
  567. namespace: Name(
  568. base: "namespaceA",
  569. generatedUpperCase: "NamespaceA",
  570. generatedLowerCase: "namespaceA"
  571. ),
  572. methods: []
  573. )
  574. let expectedSwift =
  575. """
  576. /// Documentation for ServiceA
  577. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  578. public protocol NamespaceA_ServiceA_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {}
  579. /// Conformance to `GRPCCore.RegistrableRPCService`.
  580. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  581. extension NamespaceA_ServiceA.StreamingServiceProtocol {
  582. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  583. public func registerMethods(with router: inout GRPCCore.RPCRouter) {}
  584. }
  585. /// Documentation for ServiceA
  586. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  587. public protocol NamespaceA_ServiceA_ServiceProtocol: NamespaceA_ServiceA.StreamingServiceProtocol {}
  588. /// Partial conformance to `NamespaceA_ServiceA_StreamingServiceProtocol`.
  589. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  590. extension NamespaceA_ServiceA.ServiceProtocol {
  591. }
  592. /// Documentation for ServiceB
  593. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  594. public protocol NamespaceA_ServiceB_StreamingServiceProtocol: GRPCCore.RegistrableRPCService {}
  595. /// Conformance to `GRPCCore.RegistrableRPCService`.
  596. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  597. extension NamespaceA_ServiceB.StreamingServiceProtocol {
  598. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  599. public func registerMethods(with router: inout GRPCCore.RPCRouter) {}
  600. }
  601. /// Documentation for ServiceB
  602. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  603. public protocol NamespaceA_ServiceB_ServiceProtocol: NamespaceA_ServiceB.StreamingServiceProtocol {}
  604. /// Partial conformance to `NamespaceA_ServiceB_StreamingServiceProtocol`.
  605. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  606. extension NamespaceA_ServiceB.ServiceProtocol {
  607. }
  608. """
  609. try self.assertServerCodeTranslation(
  610. codeGenerationRequest: makeCodeGenerationRequest(services: [serviceA, serviceB]),
  611. expectedSwift: expectedSwift,
  612. accessLevel: .public
  613. )
  614. }
  615. private func assertServerCodeTranslation(
  616. codeGenerationRequest: CodeGenerationRequest,
  617. expectedSwift: String,
  618. accessLevel: SourceGenerator.Config.AccessLevel
  619. ) throws {
  620. let translator = ServerCodeTranslator(accessLevel: accessLevel)
  621. let codeBlocks = try translator.translate(from: codeGenerationRequest)
  622. let renderer = TextBasedRenderer.default
  623. renderer.renderCodeBlocks(codeBlocks)
  624. let contents = renderer.renderedContents()
  625. try XCTAssertEqualWithDiff(contents, expectedSwift)
  626. }
  627. }
  628. #endif // os(macOS) || os(Linux)