| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*
- * 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 GRPCCodeGen
- import GRPCProtobufCodeGen
- import SwiftProtobuf
- import SwiftProtobufPluginLibrary
- import Testing
- @Suite
- struct ProtobufCodeGenParserTests {
- @Suite("Self-contained service (test-service.proto)")
- struct TestService: UsesDescriptorSet {
- static let descriptorSetName = "test-service"
- static let fileDescriptorName = "test-service"
- let codeGen: CodeGenerationRequest
- init() throws {
- let descriptor = try #require(try Self.fileDescriptor)
- self.codeGen = try parseDescriptor(descriptor)
- }
- @Test("Filename")
- func fileName() {
- #expect(self.codeGen.fileName == "test-service.proto")
- }
- @Test("Leading trivia")
- func leadingTrivia() {
- let expected = """
- /// Leading trivia.
- // DO NOT EDIT.
- // swift-format-ignore-file
- // swiftlint:disable all
- //
- // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
- // Source: test-service.proto
- //
- // For information on using the generated types, please see the documentation:
- // https://github.com/grpc/grpc-swift
- """
- #expect(self.codeGen.leadingTrivia == expected)
- }
- @Test("Dependencies")
- func dependencies() {
- let expected: [GRPCCodeGen.Dependency] = [
- .init(module: "GRPCProtobuf", accessLevel: .internal), // Always an internal import
- .init(module: "SwiftProtobuf", accessLevel: .internal),
- ]
- #expect(self.codeGen.dependencies == expected)
- }
- @Suite("Service")
- struct Service {
- let service: GRPCCodeGen.ServiceDescriptor
- init() throws {
- let request = try parseDescriptor(try #require(try TestService.fileDescriptor))
- try #require(request.services.count == 1)
- self.service = try #require(request.services.first)
- }
- @Test("Name")
- func name() {
- #expect(self.service.name.identifyingName == "test.TestService")
- }
- @Suite("Methods")
- struct Methods {
- let unary: GRPCCodeGen.MethodDescriptor
- let clientStreaming: GRPCCodeGen.MethodDescriptor
- let serverStreaming: GRPCCodeGen.MethodDescriptor
- let bidiStreaming: GRPCCodeGen.MethodDescriptor
- init() throws {
- let request = try parseDescriptor(try #require(try TestService.fileDescriptor))
- #expect(request.services.count == 1)
- let service = try #require(request.services.first)
- self.unary = service.methods[0]
- self.clientStreaming = service.methods[1]
- self.serverStreaming = service.methods[2]
- self.bidiStreaming = service.methods[3]
- }
- @Test("Documentation")
- func documentation() {
- #expect(self.unary.documentation == "/// Unary docs.\n")
- #expect(self.clientStreaming.documentation == "/// Client streaming docs.\n")
- #expect(self.serverStreaming.documentation == "/// Server streaming docs.\n")
- #expect(self.bidiStreaming.documentation == "/// Bidirectional streaming docs.\n")
- }
- @Test("Name")
- func name() {
- #expect(self.unary.name.identifyingName == "Unary")
- #expect(self.clientStreaming.name.identifyingName == "ClientStreaming")
- #expect(self.serverStreaming.name.identifyingName == "ServerStreaming")
- #expect(self.bidiStreaming.name.identifyingName == "BidirectionalStreaming")
- }
- @Test("Input")
- func input() {
- #expect(self.unary.inputType == "Test_TestInput")
- #expect(!self.unary.isInputStreaming)
- #expect(self.clientStreaming.inputType == "Test_TestInput")
- #expect(self.clientStreaming.isInputStreaming)
- #expect(self.serverStreaming.inputType == "Test_TestInput")
- #expect(!self.serverStreaming.isInputStreaming)
- #expect(self.bidiStreaming.inputType == "Test_TestInput")
- #expect(self.bidiStreaming.isInputStreaming)
- }
- @Test("Output")
- func output() {
- #expect(self.unary.outputType == "Test_TestOutput")
- #expect(!self.unary.isOutputStreaming)
- #expect(self.clientStreaming.outputType == "Test_TestOutput")
- #expect(!self.clientStreaming.isOutputStreaming)
- #expect(self.serverStreaming.outputType == "Test_TestOutput")
- #expect(self.serverStreaming.isOutputStreaming)
- #expect(self.bidiStreaming.outputType == "Test_TestOutput")
- #expect(self.bidiStreaming.isOutputStreaming)
- }
- }
- }
- }
- @Suite("Multi-service file (foo-service.proto)")
- struct FooService: UsesDescriptorSet {
- static let descriptorSetName = "foo-service"
- static let fileDescriptorName = "foo-service"
- let codeGen: CodeGenerationRequest
- init() throws {
- let descriptor = try #require(try Self.fileDescriptor)
- self.codeGen = try parseDescriptor(descriptor)
- }
- @Test("Name")
- func name() {
- #expect(self.codeGen.fileName == "foo-service.proto")
- }
- @Test("Dependencies")
- func dependencies() {
- let expected: [GRPCCodeGen.Dependency] = [
- .init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
- ]
- #expect(self.codeGen.dependencies == expected)
- }
- @Test("Service1")
- func service1() throws {
- let service = self.codeGen.services[0]
- #expect(service.name.identifyingName == "foo.FooService1")
- #expect(service.methods.count == 1)
- }
- @Test("Service1.Method")
- func service1Method() throws {
- let method = self.codeGen.services[0].methods[0]
- #expect(method.name.identifyingName == "Foo")
- #expect(method.inputType == "Foo_FooInput")
- #expect(method.outputType == "Foo_FooOutput")
- }
- @Test("Service2")
- func service2() throws {
- let service = self.codeGen.services[1]
- #expect(service.name.identifyingName == "foo.FooService2")
- #expect(service.methods.count == 1)
- }
- @Test("Service2.Method")
- func service2Method() throws {
- let method = self.codeGen.services[1].methods[0]
- #expect(method.name.identifyingName == "Foo")
- #expect(method.inputType == "Foo_FooInput")
- #expect(method.outputType == "Foo_FooOutput")
- }
- }
- @Suite("Service with no package (bar-service.proto)")
- struct BarService: UsesDescriptorSet {
- static var descriptorSetName: String { "bar-service" }
- static var fileDescriptorName: String { "bar-service" }
- let codeGen: CodeGenerationRequest
- let service: GRPCCodeGen.ServiceDescriptor
- init() throws {
- let descriptor = try #require(try Self.fileDescriptor)
- self.codeGen = try parseDescriptor(descriptor)
- self.service = try #require(self.codeGen.services.first)
- }
- @Test("Service name")
- func serviceName() {
- #expect(self.service.name.identifyingName == "BarService")
- }
- }
- @Suite("Service using 'well-known types' (wkt-service.proto)")
- struct WKTService: UsesDescriptorSet {
- static let descriptorSetName = "wkt-service"
- static let fileDescriptorName = "wkt-service"
- let codeGen: CodeGenerationRequest
- init() throws {
- let descriptor = try #require(try Self.fileDescriptor)
- self.codeGen = try parseDescriptor(descriptor)
- }
- @Test("Dependencies")
- func dependencies() {
- let expected: [Dependency] = [
- Dependency(module: "GRPCProtobuf", accessLevel: .internal),
- Dependency(module: "SwiftProtobuf", accessLevel: .internal),
- ]
- #expect(self.codeGen.dependencies == expected)
- }
- }
- }
|