CollectTests.awk 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/awk -f
  2. # ProtobufRuntime/Sources/Protobuf/ProtobufBinaryEncoding.swift - Binary encoding support
  3. #
  4. # This source file is part of the Swift.org open source project
  5. #
  6. # Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
  7. # Licensed under Apache License v2.0 with Runtime Library Exception
  8. #
  9. # See http://swift.org/LICENSE.txt for license information
  10. # See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  11. #
  12. # -----------------------------------------------------------------------------
  13. #
  14. # The Linux version of XCTest cannot automatically
  15. # discover tests at run-time. So you can either manually
  16. # maintain lists of tests or collect them at build time.
  17. #
  18. # This script is used by the Makefile to do the latter:
  19. # = Scans each XCTest source file for 'class Foo: XCTestCase'
  20. # = Looks for 'func test*' within those classes
  21. # = Emits a class extension with the necessary 'allTests' property
  22. # = Emits an XCTMain invocation to run all the classes
  23. #
  24. # The macOS version of XCTest has another mechanism for
  25. # finding tests at run-time, so this is not needed there.
  26. #
  27. # -----------------------------------------------------------------------------
  28. BEGIN {
  29. CLASS=""
  30. TESTCASES=""
  31. TESTCASE_separator=""
  32. printf("import XCTest\n")
  33. printf("@testable import ProtobufTestSuite\n")
  34. printf("\n")
  35. printf("private func run_test(test:() -> ()) throws {\n")
  36. printf(" test()\n")
  37. printf("}\n")
  38. printf("\n")
  39. printf("private func run_test(test:() throws -> ()) throws {\n")
  40. printf(" try test()\n")
  41. printf("}\n")
  42. printf("\n")
  43. printf("\n")
  44. }
  45. /class .*:.* XCTestCase/ {
  46. if (CLASS != "") {
  47. printf(" ]\n")
  48. printf(" }\n")
  49. printf("}\n")
  50. }
  51. split($0, a, ":")
  52. split(a[1], words, " ")
  53. CLASS=words[2]
  54. TESTCASES = TESTCASES TESTCASE_separator "\n" " (testCaseClass: " CLASS ".self, allTests: " CLASS ".allTests)"
  55. TESTCASE_separator = ","
  56. printf("\n")
  57. printf("extension %s {\n", CLASS)
  58. printf(" static var allTests: [(String, (XCTestCase) throws -> ())] {\n")
  59. printf(" return [")
  60. FUNC_separator=""
  61. }
  62. / *func *test.*/ {
  63. split($0, a, "(")
  64. split(a[1], words, " ")
  65. FUNC=words[2]
  66. printf("")
  67. printf("%s\n (\"%s\", {try run_test(test:($0 as! %s).%s)})", FUNC_separator, FUNC, CLASS, FUNC)
  68. FUNC_separator = ","
  69. }
  70. END {
  71. if (CLASS != "") {
  72. printf("\n ]\n")
  73. printf(" }\n")
  74. printf("}\n")
  75. }
  76. printf("\n")
  77. printf("XCTMain(\n")
  78. printf(" [")
  79. printf(TESTCASES)
  80. printf("\n ]\n)\n")
  81. }