2
0

TestFunctions.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2023, 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. //
  18. // This source file is part of the SwiftOpenAPIGenerator open source project
  19. //
  20. // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
  21. // Licensed under Apache License v2.0
  22. //
  23. // See LICENSE.txt for license information
  24. // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
  25. //
  26. // SPDX-License-Identifier: Apache-2.0
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)
  30. import XCTest
  31. import GRPCCodeGen
  32. private func diff(expected: String, actual: String) throws -> String {
  33. let process = Process()
  34. process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
  35. process.arguments = [
  36. "bash", "-c",
  37. "diff -U5 --label=expected <(echo '\(expected)') --label=actual <(echo '\(actual)')",
  38. ]
  39. let pipe = Pipe()
  40. process.standardOutput = pipe
  41. try process.run()
  42. process.waitUntilExit()
  43. let pipeData = try XCTUnwrap(
  44. pipe.fileHandleForReading.readToEnd(),
  45. """
  46. No output from command:
  47. \(process.executableURL!.path) \(process.arguments!.joined(separator: " "))
  48. """
  49. )
  50. return String(decoding: pipeData, as: UTF8.self)
  51. }
  52. internal func XCTAssertEqualWithDiff(
  53. _ actual: String,
  54. _ expected: String,
  55. file: StaticString = #filePath,
  56. line: UInt = #line
  57. ) throws {
  58. if actual == expected { return }
  59. XCTFail(
  60. """
  61. XCTAssertEqualWithDiff failed (click for diff)
  62. \(try diff(expected: expected, actual: actual))
  63. """,
  64. file: file,
  65. line: line
  66. )
  67. }
  68. internal func makeCodeGenerationRequest(
  69. services: [CodeGenerationRequest.ServiceDescriptor] = [],
  70. dependencies: [CodeGenerationRequest.Dependency] = []
  71. ) -> CodeGenerationRequest {
  72. return CodeGenerationRequest(
  73. fileName: "test.grpc",
  74. leadingTrivia: "/// Some really exciting license header 2023.\n",
  75. dependencies: dependencies,
  76. services: services,
  77. lookupSerializer: {
  78. "ProtobufSerializer<\($0)>()"
  79. },
  80. lookupDeserializer: {
  81. "ProtobufDeserializer<\($0)>()"
  82. }
  83. )
  84. }
  85. internal func XCTAssertThrowsError<T, E: Error>(
  86. ofType: E.Type,
  87. _ expression: @autoclosure () throws -> T,
  88. _ errorHandler: (E) -> Void
  89. ) {
  90. XCTAssertThrowsError(try expression()) { error in
  91. guard let error = error as? E else {
  92. return XCTFail("Error had unexpected type '\(type(of: error))'")
  93. }
  94. errorHandler(error)
  95. }
  96. }
  97. #endif // os(macOS) || os(Linux)