SwiftParse.swift 2.12 KB
Newer Older
Drew's avatar
Drew committed
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2016 Drew Crawford.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Drew's avatar
Drew committed
14

Drew's avatar
Drew committed
15 16
import StandBack

17
func findTests(_ sourceFiles: [String]) -> [String] {
Drew's avatar
Drew committed
18 19
    var tests: [String] = []
    for file in sourceFiles {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      //load source text

      let sourceText: String
      do {
        sourceText = try String(file: file)
      }
      catch {
        switch error {

          //if (and only if) the missing file is CarolineXCTest, we continue
          //this is because a missing file is generally a build error
          //however CarolineXCTest is often deliberately not checked in, to avoid conflicts between
          //atllbuild `**` behavior and Xcode's generation behavior
          //if we die here without CarolineXCTest, we will fail to regenerate the file as expected
          case caroline_static_tool.FileUtilsError.OpenError where file.hasSuffix("CarolineXCTest.swift"):
          continue
          default:
          fatalError("\(error)")
        }
      }
         
Drew's avatar
Drew committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        for match in try! Regex(pattern: "class[[:space:]]+([[:alnum:]]+)[[:space:]]*:[[:space:]]*CarolineTest[[:space:]]*\\{").findAll(inString: sourceText) {
            let className = match.groups[0]!
            tests.append(className.description)
        }
    }

    return tests
}

func allTestsDeclaration(tests: [String], indentation: Int) -> String {

  var s = ""
  for _ in 0..<indentation {s += " "}
  s += "let allTests: [CarolineTest] = [\n"
  var i = 0
  for test in tests {
    if i != 0 { s += ",\n" }
    i += 1
    for _ in 0..<indentation+4 {s += " "}
    s += "\(test)()"
  }
  s += "\n"
  for _ in 0..<indentation {s += " "}
  s += "]"
  return s
}