ConditionalInterceptorTests.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 GRPCCore
  17. import Testing
  18. @Suite("ConditionalInterceptor")
  19. struct ConditionalInterceptorTests {
  20. @Test(
  21. "Applies to",
  22. arguments: [
  23. (
  24. .all,
  25. [.fooBar, .fooBaz, .barFoo, .barBaz],
  26. []
  27. ),
  28. (
  29. .services([ServiceDescriptor(package: "pkg", service: "foo")]),
  30. [.fooBar, .fooBaz],
  31. [.barFoo, .barBaz]
  32. ),
  33. (
  34. .methods([.barFoo]),
  35. [.barFoo],
  36. [.fooBar, .fooBaz, .barBaz]
  37. ),
  38. ] as [(ConditionalInterceptor<any Sendable>.Subject, [MethodDescriptor], [MethodDescriptor])]
  39. )
  40. func appliesTo(
  41. target: ConditionalInterceptor<any Sendable>.Subject,
  42. applicableMethods: [MethodDescriptor],
  43. notApplicableMethods: [MethodDescriptor]
  44. ) {
  45. for applicableMethod in applicableMethods {
  46. #expect(target.applies(to: applicableMethod))
  47. }
  48. for notApplicableMethod in notApplicableMethods {
  49. #expect(!target.applies(to: notApplicableMethod))
  50. }
  51. }
  52. }
  53. extension MethodDescriptor {
  54. fileprivate static let fooBar = Self(fullyQualifiedService: "pkg.foo", method: "bar")
  55. fileprivate static let fooBaz = Self(fullyQualifiedService: "pkg.foo", method: "baz")
  56. fileprivate static let barFoo = Self(fullyQualifiedService: "pkg.bar", method: "foo")
  57. fileprivate static let barBaz = Self(fullyQualifiedService: "pkg.bar", method: "Baz")
  58. }