| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /*
- * 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.
- */
- import Testing
- @testable import GRPCCodeGen
- extension StructuredSwiftTests {
- @Suite("Metadata")
- struct Metadata {
- @Test("typealias Input = <Name>", arguments: AccessModifier.allCases)
- func methodInputTypealias(access: AccessModifier) {
- let decl: TypealiasDescription = .methodInput(accessModifier: access, name: "Foo")
- let expected = "\(access) typealias Input = Foo"
- #expect(render(.typealias(decl)) == expected)
- }
- @Test("typealias Output = <Name>", arguments: AccessModifier.allCases)
- func methodOutputTypealias(access: AccessModifier) {
- let decl: TypealiasDescription = .methodOutput(accessModifier: access, name: "Foo")
- let expected = "\(access) typealias Output = Foo"
- #expect(render(.typealias(decl)) == expected)
- }
- @Test(
- "static let descriptor = GRPCCore.MethodDescriptor(...)",
- arguments: AccessModifier.allCases
- )
- func staticMethodDescriptorProperty(access: AccessModifier) {
- let decl: VariableDescription = .methodDescriptor(
- accessModifier: access,
- literalFullyQualifiedService: "foo.Foo",
- literalMethodName: "Bar"
- )
- let expected = """
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "foo.Foo"),
- method: "Bar"
- )
- """
- #expect(render(.variable(decl)) == expected)
- }
- @Test(
- "static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService:)",
- arguments: AccessModifier.allCases
- )
- func staticServiceDescriptorProperty(access: AccessModifier) {
- let decl: VariableDescription = .serviceDescriptor(
- accessModifier: access,
- literalFullyQualifiedService: "foo.Bar"
- )
- let expected = """
- \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "foo.Bar")
- """
- #expect(render(.variable(decl)) == expected)
- }
- @Test("extension GRPCCore.ServiceDescriptor { ... }", arguments: AccessModifier.allCases)
- func staticServiceDescriptorPropertyExtension(access: AccessModifier) {
- let decl: ExtensionDescription = .serviceDescriptor(
- accessModifier: access,
- propertyName: "foo",
- literalFullyQualifiedService: "echo.EchoService"
- )
- let expected = """
- extension GRPCCore.ServiceDescriptor {
- /// Service descriptor for the "echo.EchoService" service.
- \(access) static let foo = GRPCCore.ServiceDescriptor(fullyQualifiedService: "echo.EchoService")
- }
- """
- #expect(render(.extension(decl)) == expected)
- }
- @Test(
- "static let descriptors: [GRPCCore.MethodDescriptor] = [...]",
- arguments: AccessModifier.allCases
- )
- func staticMethodDescriptorsArray(access: AccessModifier) {
- let decl: VariableDescription = .methodDescriptorsArray(
- accessModifier: access,
- methodNamespaceNames: ["Foo", "Bar", "Baz"]
- )
- let expected = """
- \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
- Foo.descriptor,
- Bar.descriptor,
- Baz.descriptor
- ]
- """
- #expect(render(.variable(decl)) == expected)
- }
- @Test("enum <Method> { ... }", arguments: AccessModifier.allCases)
- func methodNamespaceEnum(access: AccessModifier) {
- let decl: EnumDescription = .methodNamespace(
- accessModifier: access,
- name: "Foo",
- literalMethod: "Foo",
- literalFullyQualifiedService: "bar.Bar",
- inputType: "FooInput",
- outputType: "FooOutput"
- )
- let expected = """
- \(access) enum Foo {
- /// Request type for "Foo".
- \(access) typealias Input = FooInput
- /// Response type for "Foo".
- \(access) typealias Output = FooOutput
- /// Descriptor for "Foo".
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "bar.Bar"),
- method: "Foo"
- )
- }
- """
- #expect(render(.enum(decl)) == expected)
- }
- @Test("enum Method { ... }", arguments: AccessModifier.allCases)
- func methodsNamespaceEnum(access: AccessModifier) {
- let decl: EnumDescription = .methodsNamespace(
- accessModifier: access,
- literalFullyQualifiedService: "bar.Bar",
- methods: [
- .init(
- documentation: "",
- name: .init(base: "Foo", generatedUpperCase: "Foo", generatedLowerCase: "foo"),
- isInputStreaming: false,
- isOutputStreaming: false,
- inputType: "FooInput",
- outputType: "FooOutput"
- )
- ]
- )
- let expected = """
- \(access) enum Method {
- /// Namespace for "Foo" metadata.
- \(access) enum Foo {
- /// Request type for "Foo".
- \(access) typealias Input = FooInput
- /// Response type for "Foo".
- \(access) typealias Output = FooOutput
- /// Descriptor for "Foo".
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "bar.Bar"),
- method: "Foo"
- )
- }
- /// Descriptors for all methods in the "bar.Bar" service.
- \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
- Foo.descriptor
- ]
- }
- """
- #expect(render(.enum(decl)) == expected)
- }
- @Test("enum Method { ... } (no methods)", arguments: AccessModifier.allCases)
- func methodsNamespaceEnumNoMethods(access: AccessModifier) {
- let decl: EnumDescription = .methodsNamespace(
- accessModifier: access,
- literalFullyQualifiedService: "bar.Bar",
- methods: []
- )
- let expected = """
- \(access) enum Method {
- /// Descriptors for all methods in the "bar.Bar" service.
- \(access) static let descriptors: [GRPCCore.MethodDescriptor] = []
- }
- """
- #expect(render(.enum(decl)) == expected)
- }
- @Test("enum <Service> { ... }", arguments: AccessModifier.allCases)
- func serviceNamespaceEnum(access: AccessModifier) {
- let decl: EnumDescription = .serviceNamespace(
- accessModifier: access,
- name: "Foo",
- literalFullyQualifiedService: "Foo",
- methods: [
- .init(
- documentation: "",
- name: .init(base: "Bar", generatedUpperCase: "Bar", generatedLowerCase: "bar"),
- isInputStreaming: false,
- isOutputStreaming: false,
- inputType: "BarInput",
- outputType: "BarOutput"
- )
- ]
- )
- let expected = """
- \(access) enum Foo {
- /// Service descriptor for the "Foo" service.
- \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "Foo")
- /// Namespace for method metadata.
- \(access) enum Method {
- /// Namespace for "Bar" metadata.
- \(access) enum Bar {
- /// Request type for "Bar".
- \(access) typealias Input = BarInput
- /// Response type for "Bar".
- \(access) typealias Output = BarOutput
- /// Descriptor for "Bar".
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "Foo"),
- method: "Bar"
- )
- }
- /// Descriptors for all methods in the "Foo" service.
- \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
- Bar.descriptor
- ]
- }
- }
- """
- #expect(render(.enum(decl)) == expected)
- }
- @Test("enum <Service> { ... } (no methods)", arguments: AccessModifier.allCases)
- func serviceNamespaceEnumNoMethods(access: AccessModifier) {
- let decl: EnumDescription = .serviceNamespace(
- accessModifier: access,
- name: "Foo",
- literalFullyQualifiedService: "Foo",
- methods: []
- )
- let expected = """
- \(access) enum Foo {
- /// Service descriptor for the "Foo" service.
- \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "Foo")
- /// Namespace for method metadata.
- \(access) enum Method {
- /// Descriptors for all methods in the "Foo" service.
- \(access) static let descriptors: [GRPCCore.MethodDescriptor] = []
- }
- }
- """
- #expect(render(.enum(decl)) == expected)
- }
- }
- }
|