IDLToStructuredSwiftTranslatorSnippetBasedTests.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)
  17. import XCTest
  18. @testable import GRPCCodeGen
  19. final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
  20. typealias MethodDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
  21. typealias ServiceDescriptor = GRPCCodeGen.CodeGenerationRequest.ServiceDescriptor
  22. func testSameNameServicesNoNamespaceError() throws {
  23. let serviceA = ServiceDescriptor(
  24. documentation: "Documentation for AService",
  25. name: "AService",
  26. namespace: "",
  27. methods: []
  28. )
  29. let codeGenerationRequest = makeCodeGenerationRequest(services: [serviceA, serviceA])
  30. let translator = IDLToStructuredSwiftTranslator()
  31. XCTAssertThrowsError(
  32. ofType: CodeGenError.self,
  33. try translator.translate(
  34. codeGenerationRequest: codeGenerationRequest,
  35. client: true,
  36. server: true
  37. )
  38. ) {
  39. error in
  40. XCTAssertEqual(
  41. error as CodeGenError,
  42. CodeGenError(
  43. code: .nonUniqueServiceName,
  44. message: """
  45. Services in an empty namespace must have unique names. \
  46. AService is used as a name for multiple services without namespaces.
  47. """
  48. )
  49. )
  50. }
  51. }
  52. func testSameNameServicesSameNamespaceError() throws {
  53. let serviceA = ServiceDescriptor(
  54. documentation: "Documentation for AService",
  55. name: "AService",
  56. namespace: "namespacea",
  57. methods: []
  58. )
  59. let codeGenerationRequest = makeCodeGenerationRequest(services: [serviceA, serviceA])
  60. let translator = IDLToStructuredSwiftTranslator()
  61. XCTAssertThrowsError(
  62. ofType: CodeGenError.self,
  63. try translator.translate(
  64. codeGenerationRequest: codeGenerationRequest,
  65. client: true,
  66. server: true
  67. )
  68. ) {
  69. error in
  70. XCTAssertEqual(
  71. error as CodeGenError,
  72. CodeGenError(
  73. code: .nonUniqueServiceName,
  74. message: """
  75. Services within the same namespace must have unique names. \
  76. AService is used as a name for multiple services in the namespacea namespace.
  77. """
  78. )
  79. )
  80. }
  81. }
  82. func testSameNameMethodsSameServiceError() throws {
  83. let methodA = MethodDescriptor(
  84. documentation: "Documentation for MethodA",
  85. name: "MethodA",
  86. isInputStreaming: false,
  87. isOutputStreaming: false,
  88. inputType: "NamespaceA_ServiceARequest",
  89. outputType: "NamespaceA_ServiceAResponse"
  90. )
  91. let service = ServiceDescriptor(
  92. documentation: "Documentation for AService",
  93. name: "AService",
  94. namespace: "namespacea",
  95. methods: [methodA, methodA]
  96. )
  97. let codeGenerationRequest = makeCodeGenerationRequest(services: [service])
  98. let translator = IDLToStructuredSwiftTranslator()
  99. XCTAssertThrowsError(
  100. ofType: CodeGenError.self,
  101. try translator.translate(
  102. codeGenerationRequest: codeGenerationRequest,
  103. client: true,
  104. server: true
  105. )
  106. ) {
  107. error in
  108. XCTAssertEqual(
  109. error as CodeGenError,
  110. CodeGenError(
  111. code: .nonUniqueMethodName,
  112. message: """
  113. Methods of a service must have unique names. \
  114. MethodA is used as a name for multiple methods of the AService service.
  115. """
  116. )
  117. )
  118. }
  119. }
  120. func testSameNameNoNamespaceServiceAndNamespaceError() throws {
  121. let serviceA = ServiceDescriptor(
  122. documentation: "Documentation for SameName service with no namespace",
  123. name: "SameName",
  124. namespace: "",
  125. methods: []
  126. )
  127. let serviceB = ServiceDescriptor(
  128. documentation: "Documentation for BService",
  129. name: "BService",
  130. namespace: "SameName",
  131. methods: []
  132. )
  133. let codeGenerationRequest = makeCodeGenerationRequest(services: [serviceA, serviceB])
  134. let translator = IDLToStructuredSwiftTranslator()
  135. XCTAssertThrowsError(
  136. ofType: CodeGenError.self,
  137. try translator.translate(
  138. codeGenerationRequest: codeGenerationRequest,
  139. client: true,
  140. server: true
  141. )
  142. ) {
  143. error in
  144. XCTAssertEqual(
  145. error as CodeGenError,
  146. CodeGenError(
  147. code: .nonUniqueServiceName,
  148. message: """
  149. Services with no namespace must not have the same names as the namespaces. \
  150. SameName is used as a name for a service with no namespace and a namespace.
  151. """
  152. )
  153. )
  154. }
  155. }
  156. }
  157. #endif // os(macOS) || os(Linux)