|
| 1 | +/* |
| 2 | + * Copyright 2024, 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 | + |
| 17 | +import Testing |
| 18 | + |
| 19 | +@testable import GRPCCodeGen |
| 20 | + |
| 21 | +extension StructuedSwiftTests { |
| 22 | + @Suite("Metadata") |
| 23 | + struct Metadata { |
| 24 | + @Test("@available(...)") |
| 25 | + func grpcAvailability() async throws { |
| 26 | + let availability: AvailabilityDescription = .grpc |
| 27 | + let structDecl = StructDescription(name: "Ignored") |
| 28 | + let expected = """ |
| 29 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 30 | + struct Ignored {} |
| 31 | + """ |
| 32 | + |
| 33 | + #expect(render(.guarded(availability, .struct(structDecl))) == expected) |
| 34 | + } |
| 35 | + |
| 36 | + @Test("typealias Input = <Name>", arguments: AccessModifier.allCases) |
| 37 | + func methodInputTypealias(access: AccessModifier) { |
| 38 | + let decl: TypealiasDescription = .methodInput(accessModifier: access, name: "Foo") |
| 39 | + let expected = "\(access) typealias Input = Foo" |
| 40 | + #expect(render(.typealias(decl)) == expected) |
| 41 | + } |
| 42 | + |
| 43 | + @Test("typealias Output = <Name>", arguments: AccessModifier.allCases) |
| 44 | + func methodOutputTypealias(access: AccessModifier) { |
| 45 | + let decl: TypealiasDescription = .methodOutput(accessModifier: access, name: "Foo") |
| 46 | + let expected = "\(access) typealias Output = Foo" |
| 47 | + #expect(render(.typealias(decl)) == expected) |
| 48 | + } |
| 49 | + |
| 50 | + @Test( |
| 51 | + "static let descriptor = GRPCCore.MethodDescriptor(...)", |
| 52 | + arguments: AccessModifier.allCases |
| 53 | + ) |
| 54 | + func staticMethodDescriptorProperty(access: AccessModifier) { |
| 55 | + let decl: VariableDescription = .methodDescriptor( |
| 56 | + accessModifier: access, |
| 57 | + serviceNamespace: "FooService", |
| 58 | + literalMethodName: "Bar" |
| 59 | + ) |
| 60 | + |
| 61 | + let expected = """ |
| 62 | + \(access) static let descriptor = GRPCCore.MethodDescriptor( |
| 63 | + service: FooService.descriptor.fullyQualifiedService, |
| 64 | + method: "Bar" |
| 65 | + ) |
| 66 | + """ |
| 67 | + #expect(render(.variable(decl)) == expected) |
| 68 | + } |
| 69 | + |
| 70 | + @Test( |
| 71 | + "static let descriptor = GRPCCore.ServiceDescriptor.<Name>", |
| 72 | + arguments: AccessModifier.allCases |
| 73 | + ) |
| 74 | + func staticServiceDescriptorProperty(access: AccessModifier) { |
| 75 | + let decl: VariableDescription = .serviceDescriptor( |
| 76 | + accessModifier: access, |
| 77 | + namespacedProperty: "foo" |
| 78 | + ) |
| 79 | + |
| 80 | + let expected = "\(access) static let descriptor = GRPCCore.ServiceDescriptor.foo" |
| 81 | + #expect(render(.variable(decl)) == expected) |
| 82 | + } |
| 83 | + |
| 84 | + @Test("extension GRPCCore.ServiceDescriptor { ... }", arguments: AccessModifier.allCases) |
| 85 | + func staticServiceDescriptorPropertyExtension(access: AccessModifier) { |
| 86 | + let decl: ExtensionDescription = .serviceDescriptor( |
| 87 | + accessModifier: access, |
| 88 | + propertyName: "foo", |
| 89 | + literalNamespace: "echo", |
| 90 | + literalService: "EchoService" |
| 91 | + ) |
| 92 | + |
| 93 | + let expected = """ |
| 94 | + extension GRPCCore.ServiceDescriptor { |
| 95 | + \(access) static let foo = Self( |
| 96 | + package: "echo", |
| 97 | + service: "EchoService" |
| 98 | + ) |
| 99 | + } |
| 100 | + """ |
| 101 | + #expect(render(.extension(decl)) == expected) |
| 102 | + } |
| 103 | + |
| 104 | + @Test( |
| 105 | + "static let descriptors: [GRPCCore.MethodDescriptor] = [...]", |
| 106 | + arguments: AccessModifier.allCases |
| 107 | + ) |
| 108 | + func staticMethodDescriptorsArray(access: AccessModifier) { |
| 109 | + let decl: VariableDescription = .methodDescriptorsArray( |
| 110 | + accessModifier: access, |
| 111 | + methodNamespaceNames: ["Foo", "Bar", "Baz"] |
| 112 | + ) |
| 113 | + |
| 114 | + let expected = """ |
| 115 | + \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [ |
| 116 | + Foo.descriptor, |
| 117 | + Bar.descriptor, |
| 118 | + Baz.descriptor |
| 119 | + ] |
| 120 | + """ |
| 121 | + #expect(render(.variable(decl)) == expected) |
| 122 | + } |
| 123 | + |
| 124 | + @Test("enum <Method> { ... }", arguments: AccessModifier.allCases) |
| 125 | + func methodNamespaceEnum(access: AccessModifier) { |
| 126 | + let decl: EnumDescription = .methodNamespace( |
| 127 | + accessModifier: access, |
| 128 | + name: "Foo", |
| 129 | + literalMethod: "Foo", |
| 130 | + serviceNamespace: "Bar_Baz", |
| 131 | + inputType: "FooInput", |
| 132 | + outputType: "FooOutput" |
| 133 | + ) |
| 134 | + |
| 135 | + let expected = """ |
| 136 | + \(access) enum Foo { |
| 137 | + \(access) typealias Input = FooInput |
| 138 | + \(access) typealias Output = FooOutput |
| 139 | + \(access) static let descriptor = GRPCCore.MethodDescriptor( |
| 140 | + service: Bar_Baz.descriptor.fullyQualifiedService, |
| 141 | + method: "Foo" |
| 142 | + ) |
| 143 | + } |
| 144 | + """ |
| 145 | + #expect(render(.enum(decl)) == expected) |
| 146 | + } |
| 147 | + |
| 148 | + @Test("enum Method { ... }", arguments: AccessModifier.allCases) |
| 149 | + func methodsNamespaceEnum(access: AccessModifier) { |
| 150 | + let decl: EnumDescription = .methodsNamespace( |
| 151 | + accessModifier: access, |
| 152 | + serviceNamespace: "Bar_Baz", |
| 153 | + methods: [ |
| 154 | + .init( |
| 155 | + documentation: "", |
| 156 | + name: .init(base: "Foo", generatedUpperCase: "Foo", generatedLowerCase: "foo"), |
| 157 | + isInputStreaming: false, |
| 158 | + isOutputStreaming: false, |
| 159 | + inputType: "FooInput", |
| 160 | + outputType: "FooOutput" |
| 161 | + ) |
| 162 | + ] |
| 163 | + ) |
| 164 | + |
| 165 | + let expected = """ |
| 166 | + \(access) enum Method { |
| 167 | + \(access) enum Foo { |
| 168 | + \(access) typealias Input = FooInput |
| 169 | + \(access) typealias Output = FooOutput |
| 170 | + \(access) static let descriptor = GRPCCore.MethodDescriptor( |
| 171 | + service: Bar_Baz.descriptor.fullyQualifiedService, |
| 172 | + method: "Foo" |
| 173 | + ) |
| 174 | + } |
| 175 | + \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [ |
| 176 | + Foo.descriptor |
| 177 | + ] |
| 178 | + } |
| 179 | + """ |
| 180 | + #expect(render(.enum(decl)) == expected) |
| 181 | + } |
| 182 | + |
| 183 | + @Test("enum Method { ... } (no methods)", arguments: AccessModifier.allCases) |
| 184 | + func methodsNamespaceEnumNoMethods(access: AccessModifier) { |
| 185 | + let decl: EnumDescription = .methodsNamespace( |
| 186 | + accessModifier: access, |
| 187 | + serviceNamespace: "Bar_Baz", |
| 188 | + methods: [] |
| 189 | + ) |
| 190 | + |
| 191 | + let expected = """ |
| 192 | + \(access) enum Method { |
| 193 | + \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [] |
| 194 | + } |
| 195 | + """ |
| 196 | + #expect(render(.enum(decl)) == expected) |
| 197 | + } |
| 198 | + |
| 199 | + @Test("enum <Service> { ... }", arguments: AccessModifier.allCases) |
| 200 | + func serviceNamespaceEnum(access: AccessModifier) { |
| 201 | + let decl: EnumDescription = .serviceNamespace( |
| 202 | + accessModifier: access, |
| 203 | + name: "Foo", |
| 204 | + serviceDescriptorProperty: "foo", |
| 205 | + client: false, |
| 206 | + server: false, |
| 207 | + methods: [ |
| 208 | + .init( |
| 209 | + documentation: "", |
| 210 | + name: .init(base: "Bar", generatedUpperCase: "Bar", generatedLowerCase: "bar"), |
| 211 | + isInputStreaming: false, |
| 212 | + isOutputStreaming: false, |
| 213 | + inputType: "BarInput", |
| 214 | + outputType: "BarOutput" |
| 215 | + ) |
| 216 | + ] |
| 217 | + ) |
| 218 | + |
| 219 | + let expected = """ |
| 220 | + \(access) enum Foo { |
| 221 | + \(access) static let descriptor = GRPCCore.ServiceDescriptor.foo |
| 222 | + \(access) enum Method { |
| 223 | + \(access) enum Bar { |
| 224 | + \(access) typealias Input = BarInput |
| 225 | + \(access) typealias Output = BarOutput |
| 226 | + \(access) static let descriptor = GRPCCore.MethodDescriptor( |
| 227 | + service: Foo.descriptor.fullyQualifiedService, |
| 228 | + method: "Bar" |
| 229 | + ) |
| 230 | + } |
| 231 | + \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [ |
| 232 | + Bar.descriptor |
| 233 | + ] |
| 234 | + } |
| 235 | + } |
| 236 | + """ |
| 237 | + #expect(render(.enum(decl)) == expected) |
| 238 | + } |
| 239 | + |
| 240 | + @Test( |
| 241 | + "enum <Service> { ... } (no methods)", |
| 242 | + arguments: AccessModifier.allCases, |
| 243 | + [(true, true), (false, false), (true, false), (false, true)] |
| 244 | + ) |
| 245 | + func serviceNamespaceEnumNoMethods(access: AccessModifier, config: (client: Bool, server: Bool)) |
| 246 | + { |
| 247 | + let decl: EnumDescription = .serviceNamespace( |
| 248 | + accessModifier: access, |
| 249 | + name: "Foo", |
| 250 | + serviceDescriptorProperty: "foo", |
| 251 | + client: config.client, |
| 252 | + server: config.server, |
| 253 | + methods: [] |
| 254 | + ) |
| 255 | + |
| 256 | + var expected = """ |
| 257 | + \(access) enum Foo { |
| 258 | + \(access) static let descriptor = GRPCCore.ServiceDescriptor.foo |
| 259 | + \(access) enum Method { |
| 260 | + \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [] |
| 261 | + }\n |
| 262 | + """ |
| 263 | + |
| 264 | + if config.server { |
| 265 | + expected += """ |
| 266 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 267 | + \(access) typealias StreamingServiceProtocol = Foo_StreamingServiceProtocol |
| 268 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 269 | + \(access) typealias ServiceProtocol = Foo_ServiceProtocol |
| 270 | + """ |
| 271 | + } |
| 272 | + |
| 273 | + if config.client { |
| 274 | + if config.server { |
| 275 | + expected += "\n" |
| 276 | + } |
| 277 | + |
| 278 | + expected += """ |
| 279 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 280 | + \(access) typealias ClientProtocol = Foo_ClientProtocol |
| 281 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 282 | + \(access) typealias Client = Foo_Client |
| 283 | + """ |
| 284 | + } |
| 285 | + |
| 286 | + if config.client || config.server { |
| 287 | + expected += "\n}" |
| 288 | + } else { |
| 289 | + expected += "}" |
| 290 | + } |
| 291 | + |
| 292 | + #expect(render(.enum(decl)) == expected) |
| 293 | + } |
| 294 | + } |
| 295 | +} |
0 commit comments