| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /*
- * Copyright 2024, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)
- import XCTest
- @testable import GRPCCodeGen
- final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
- typealias MethodDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
- typealias ServiceDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor
- func testImports() throws {
- var dependencies = [CodeGenerationRequest.Dependency]()
- dependencies.append(CodeGenerationRequest.Dependency(module: "Foo"))
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .typealias, name: "Bar"), module: "Foo")
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .struct, name: "Baz"), module: "Foo")
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .class, name: "Bac"), module: "Foo")
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .enum, name: "Bap"), module: "Foo")
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .protocol, name: "Bat"), module: "Foo")
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .let, name: "Baq"), module: "Foo")
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .var, name: "Bag"), module: "Foo")
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(item: .init(kind: .func, name: "Bak"), module: "Foo")
- )
- let expectedSwift =
- """
- /// Some really exciting license header 2023.
- import GRPCCore
- import Foo
- import typealias Foo.Bar
- import struct Foo.Baz
- import class Foo.Bac
- import enum Foo.Bap
- import protocol Foo.Bat
- import let Foo.Baq
- import var Foo.Bag
- import func Foo.Bak
- """
- try self.assertIDLToStructuredSwiftTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(dependencies: dependencies),
- expectedSwift: expectedSwift
- )
- }
- func testPreconcurrencyImports() throws {
- var dependencies = [CodeGenerationRequest.Dependency]()
- dependencies.append(CodeGenerationRequest.Dependency(module: "Foo", preconcurrency: .required))
- dependencies.append(
- CodeGenerationRequest.Dependency(
- item: .init(kind: .enum, name: "Bar"),
- module: "Foo",
- preconcurrency: .required
- )
- )
- dependencies.append(
- CodeGenerationRequest.Dependency(
- module: "Baz",
- preconcurrency: .requiredOnOS(["Deq", "Der"])
- )
- )
- let expectedSwift =
- """
- /// Some really exciting license header 2023.
- import GRPCCore
- @preconcurrency import Foo
- @preconcurrency import enum Foo.Bar
- #if os(Deq) || os(Der)
- @preconcurrency import Baz
- #else
- import Baz
- #endif
- """
- try self.assertIDLToStructuredSwiftTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(dependencies: dependencies),
- expectedSwift: expectedSwift
- )
- }
- func testSPIImports() throws {
- var dependencies = [CodeGenerationRequest.Dependency]()
- dependencies.append(CodeGenerationRequest.Dependency(module: "Foo", spi: "Secret"))
- dependencies.append(
- CodeGenerationRequest.Dependency(
- item: .init(kind: .enum, name: "Bar"),
- module: "Foo",
- spi: "Secret"
- )
- )
- let expectedSwift =
- """
- /// Some really exciting license header 2023.
- import GRPCCore
- @_spi(Secret) import Foo
- @_spi(Secret) import enum Foo.Bar
- """
- try self.assertIDLToStructuredSwiftTranslation(
- codeGenerationRequest: makeCodeGenerationRequest(dependencies: dependencies),
- expectedSwift: expectedSwift
- )
- }
- private func assertIDLToStructuredSwiftTranslation(
- codeGenerationRequest: CodeGenerationRequest,
- expectedSwift: String
- ) throws {
- let translator = IDLToStructuredSwiftTranslator()
- let structuredSwift = try translator.translate(
- codeGenerationRequest: codeGenerationRequest,
- client: false,
- server: false
- )
- let renderer = TextBasedRenderer.default
- let sourceFile = try renderer.render(structured: structuredSwift)
- let contents = sourceFile.contents
- try XCTAssertEqualWithDiff(contents, expectedSwift)
- }
- func testSameNameServicesNoNamespaceError() throws {
- let serviceA = ServiceDescriptor(
- documentation: "Documentation for AService",
- name: "AService",
- namespace: "",
- methods: []
- )
- let codeGenerationRequest = makeCodeGenerationRequest(services: [serviceA, serviceA])
- let translator = IDLToStructuredSwiftTranslator()
- XCTAssertThrowsError(
- ofType: CodeGenError.self,
- try translator.translate(
- codeGenerationRequest: codeGenerationRequest,
- client: true,
- server: true
- )
- ) {
- error in
- XCTAssertEqual(
- error as CodeGenError,
- CodeGenError(
- code: .nonUniqueServiceName,
- message: """
- Services in an empty namespace must have unique names. \
- AService is used as a name for multiple services without namespaces.
- """
- )
- )
- }
- }
- func testSameNameServicesSameNamespaceError() throws {
- let serviceA = ServiceDescriptor(
- documentation: "Documentation for AService",
- name: "AService",
- namespace: "namespacea",
- methods: []
- )
- let codeGenerationRequest = makeCodeGenerationRequest(services: [serviceA, serviceA])
- let translator = IDLToStructuredSwiftTranslator()
- XCTAssertThrowsError(
- ofType: CodeGenError.self,
- try translator.translate(
- codeGenerationRequest: codeGenerationRequest,
- client: true,
- server: true
- )
- ) {
- error in
- XCTAssertEqual(
- error as CodeGenError,
- CodeGenError(
- code: .nonUniqueServiceName,
- message: """
- Services within the same namespace must have unique names. \
- AService is used as a name for multiple services in the namespacea namespace.
- """
- )
- )
- }
- }
- func testSameNameMethodsSameServiceError() throws {
- let methodA = MethodDescriptor(
- documentation: "Documentation for MethodA",
- name: "MethodA",
- isInputStreaming: false,
- isOutputStreaming: false,
- inputType: "NamespaceA_ServiceARequest",
- outputType: "NamespaceA_ServiceAResponse"
- )
- let service = ServiceDescriptor(
- documentation: "Documentation for AService",
- name: "AService",
- namespace: "namespacea",
- methods: [methodA, methodA]
- )
- let codeGenerationRequest = makeCodeGenerationRequest(services: [service])
- let translator = IDLToStructuredSwiftTranslator()
- XCTAssertThrowsError(
- ofType: CodeGenError.self,
- try translator.translate(
- codeGenerationRequest: codeGenerationRequest,
- client: true,
- server: true
- )
- ) {
- error in
- XCTAssertEqual(
- error as CodeGenError,
- CodeGenError(
- code: .nonUniqueMethodName,
- message: """
- Methods of a service must have unique names. \
- MethodA is used as a name for multiple methods of the AService service.
- """
- )
- )
- }
- }
- func testSameNameNoNamespaceServiceAndNamespaceError() throws {
- let serviceA = ServiceDescriptor(
- documentation: "Documentation for SameName service with no namespace",
- name: "SameName",
- namespace: "",
- methods: []
- )
- let serviceB = ServiceDescriptor(
- documentation: "Documentation for BService",
- name: "BService",
- namespace: "SameName",
- methods: []
- )
- let codeGenerationRequest = makeCodeGenerationRequest(services: [serviceA, serviceB])
- let translator = IDLToStructuredSwiftTranslator()
- XCTAssertThrowsError(
- ofType: CodeGenError.self,
- try translator.translate(
- codeGenerationRequest: codeGenerationRequest,
- client: true,
- server: true
- )
- ) {
- error in
- XCTAssertEqual(
- error as CodeGenError,
- CodeGenError(
- code: .nonUniqueServiceName,
- message: """
- Services with no namespace must not have the same names as the namespaces. \
- SameName is used as a name for a service with no namespace and a namespace.
- """
- )
- )
- }
- }
- }
- #endif // os(macOS) || os(Linux)
|