StructuredSwift+MetadataTests.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. import Testing
  17. @testable import GRPCCodeGen
  18. extension StructuredSwiftTests {
  19. @Suite("Metadata")
  20. struct Metadata {
  21. @Test("typealias Input = <Name>", arguments: AccessModifier.allCases)
  22. @available(gRPCSwift 2.0, *)
  23. func methodInputTypealias(access: AccessModifier) {
  24. let decl: TypealiasDescription = .methodInput(accessModifier: access, name: "Foo")
  25. let expected = "\(access) typealias Input = Foo"
  26. #expect(render(.typealias(decl)) == expected)
  27. }
  28. @Test("typealias Output = <Name>", arguments: AccessModifier.allCases)
  29. @available(gRPCSwift 2.0, *)
  30. func methodOutputTypealias(access: AccessModifier) {
  31. let decl: TypealiasDescription = .methodOutput(accessModifier: access, name: "Foo")
  32. let expected = "\(access) typealias Output = Foo"
  33. #expect(render(.typealias(decl)) == expected)
  34. }
  35. @Test(
  36. "static let descriptor = GRPCCore.MethodDescriptor(...)",
  37. arguments: AccessModifier.allCases
  38. )
  39. @available(gRPCSwift 2.0, *)
  40. func staticMethodDescriptorProperty(access: AccessModifier) {
  41. let decl: VariableDescription = .methodDescriptor(
  42. accessModifier: access,
  43. literalFullyQualifiedService: "foo.Foo",
  44. literalMethodName: "Bar"
  45. )
  46. let expected = """
  47. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  48. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "foo.Foo"),
  49. method: "Bar"
  50. )
  51. """
  52. #expect(render(.variable(decl)) == expected)
  53. }
  54. @Test(
  55. "static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService:)",
  56. arguments: AccessModifier.allCases
  57. )
  58. @available(gRPCSwift 2.0, *)
  59. func staticServiceDescriptorProperty(access: AccessModifier) {
  60. let decl: VariableDescription = .serviceDescriptor(
  61. accessModifier: access,
  62. literalFullyQualifiedService: "foo.Bar"
  63. )
  64. let expected = """
  65. \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "foo.Bar")
  66. """
  67. #expect(render(.variable(decl)) == expected)
  68. }
  69. @Test("extension GRPCCore.ServiceDescriptor { ... }", arguments: AccessModifier.allCases)
  70. @available(gRPCSwift 2.0, *)
  71. func staticServiceDescriptorPropertyExtension(access: AccessModifier) {
  72. let decl: ExtensionDescription = .serviceDescriptor(
  73. accessModifier: access,
  74. propertyName: "foo",
  75. literalFullyQualifiedService: "echo.EchoService"
  76. )
  77. let expected = """
  78. extension GRPCCore.ServiceDescriptor {
  79. /// Service descriptor for the "echo.EchoService" service.
  80. \(access) static let foo = GRPCCore.ServiceDescriptor(fullyQualifiedService: "echo.EchoService")
  81. }
  82. """
  83. #expect(render(.extension(decl)) == expected)
  84. }
  85. @Test(
  86. "static let descriptors: [GRPCCore.MethodDescriptor] = [...]",
  87. arguments: AccessModifier.allCases
  88. )
  89. @available(gRPCSwift 2.0, *)
  90. func staticMethodDescriptorsArray(access: AccessModifier) {
  91. let decl: VariableDescription = .methodDescriptorsArray(
  92. accessModifier: access,
  93. methodNamespaceNames: ["Foo", "Bar", "Baz"]
  94. )
  95. let expected = """
  96. \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
  97. Foo.descriptor,
  98. Bar.descriptor,
  99. Baz.descriptor
  100. ]
  101. """
  102. #expect(render(.variable(decl)) == expected)
  103. }
  104. @Test("enum <Method> { ... }", arguments: AccessModifier.allCases)
  105. @available(gRPCSwift 2.0, *)
  106. func methodNamespaceEnum(access: AccessModifier) {
  107. let decl: EnumDescription = .methodNamespace(
  108. accessModifier: access,
  109. name: "Foo",
  110. literalMethod: "Foo",
  111. literalFullyQualifiedService: "bar.Bar",
  112. inputType: "FooInput",
  113. outputType: "FooOutput"
  114. )
  115. let expected = """
  116. \(access) enum Foo {
  117. /// Request type for "Foo".
  118. \(access) typealias Input = FooInput
  119. /// Response type for "Foo".
  120. \(access) typealias Output = FooOutput
  121. /// Descriptor for "Foo".
  122. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  123. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "bar.Bar"),
  124. method: "Foo"
  125. )
  126. }
  127. """
  128. #expect(render(.enum(decl)) == expected)
  129. }
  130. @Test("enum Method { ... }", arguments: AccessModifier.allCases)
  131. @available(gRPCSwift 2.0, *)
  132. func methodsNamespaceEnum(access: AccessModifier) {
  133. let decl: EnumDescription = .methodsNamespace(
  134. accessModifier: access,
  135. literalFullyQualifiedService: "bar.Bar",
  136. methods: [
  137. .init(
  138. documentation: "",
  139. name: MethodName(identifyingName: "Foo", typeName: "Foo", functionName: "foo"),
  140. isInputStreaming: false,
  141. isOutputStreaming: false,
  142. inputType: "FooInput",
  143. outputType: "FooOutput"
  144. )
  145. ]
  146. )
  147. let expected = """
  148. \(access) enum Method {
  149. /// Namespace for "Foo" metadata.
  150. \(access) enum Foo {
  151. /// Request type for "Foo".
  152. \(access) typealias Input = FooInput
  153. /// Response type for "Foo".
  154. \(access) typealias Output = FooOutput
  155. /// Descriptor for "Foo".
  156. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  157. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "bar.Bar"),
  158. method: "Foo"
  159. )
  160. }
  161. /// Descriptors for all methods in the "bar.Bar" service.
  162. \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
  163. Foo.descriptor
  164. ]
  165. }
  166. """
  167. #expect(render(.enum(decl)) == expected)
  168. }
  169. @Test("enum Method { ... } (no methods)", arguments: AccessModifier.allCases)
  170. @available(gRPCSwift 2.0, *)
  171. func methodsNamespaceEnumNoMethods(access: AccessModifier) {
  172. let decl: EnumDescription = .methodsNamespace(
  173. accessModifier: access,
  174. literalFullyQualifiedService: "bar.Bar",
  175. methods: []
  176. )
  177. let expected = """
  178. \(access) enum Method {
  179. /// Descriptors for all methods in the "bar.Bar" service.
  180. \(access) static let descriptors: [GRPCCore.MethodDescriptor] = []
  181. }
  182. """
  183. #expect(render(.enum(decl)) == expected)
  184. }
  185. @Test("enum <Service> { ... }", arguments: AccessModifier.allCases)
  186. @available(gRPCSwift 2.0, *)
  187. func serviceNamespaceEnum(access: AccessModifier) {
  188. let decl: EnumDescription = .serviceNamespace(
  189. accessModifier: access,
  190. name: "Foo",
  191. literalFullyQualifiedService: "Foo",
  192. methods: [
  193. .init(
  194. documentation: "",
  195. name: MethodName(identifyingName: "Bar", typeName: "Bar", functionName: "bar"),
  196. isInputStreaming: false,
  197. isOutputStreaming: false,
  198. inputType: "BarInput",
  199. outputType: "BarOutput"
  200. )
  201. ]
  202. )
  203. let expected = """
  204. \(access) enum Foo {
  205. /// Service descriptor for the "Foo" service.
  206. \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "Foo")
  207. /// Namespace for method metadata.
  208. \(access) enum Method {
  209. /// Namespace for "Bar" metadata.
  210. \(access) enum Bar {
  211. /// Request type for "Bar".
  212. \(access) typealias Input = BarInput
  213. /// Response type for "Bar".
  214. \(access) typealias Output = BarOutput
  215. /// Descriptor for "Bar".
  216. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  217. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "Foo"),
  218. method: "Bar"
  219. )
  220. }
  221. /// Descriptors for all methods in the "Foo" service.
  222. \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
  223. Bar.descriptor
  224. ]
  225. }
  226. }
  227. """
  228. #expect(render(.enum(decl)) == expected)
  229. }
  230. @Test("enum <Service> { ... } (no methods)", arguments: AccessModifier.allCases)
  231. @available(gRPCSwift 2.0, *)
  232. func serviceNamespaceEnumNoMethods(access: AccessModifier) {
  233. let decl: EnumDescription = .serviceNamespace(
  234. accessModifier: access,
  235. name: "Foo",
  236. literalFullyQualifiedService: "Foo",
  237. methods: []
  238. )
  239. let expected = """
  240. \(access) enum Foo {
  241. /// Service descriptor for the "Foo" service.
  242. \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "Foo")
  243. /// Namespace for method metadata.
  244. \(access) enum Method {
  245. /// Descriptors for all methods in the "Foo" service.
  246. \(access) static let descriptors: [GRPCCore.MethodDescriptor] = []
  247. }
  248. }
  249. """
  250. #expect(render(.enum(decl)) == expected)
  251. }
  252. }
  253. }