0001    // This source file is part of the Swift.org open source project
0002    //
0003    // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
0004    // Licensed under Apache License v2.0 with Runtime Library Exception
0005    //
0006    // See http://swift.org/LICENSE.txt for license information
0007    // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
0008    //
0009    //
0010    //  XCTestMain.swift
0011    //  This is the main file for the framework. It provides the entry point function
0012    //  for running tests and some infrastructure for running them.
0013    //
0014    
0015    #if os(Linux) || os(FreeBSD)
0016    import Glibc
0017    #else
0018    import Darwin
0019    #endif
0020    
0021    internal func XCTPrint
XCTestCase.swift:49
                XCTPrint("Test Case '\(method)' started.")
XCTestCase.swift:76
                XCTPrint("Test Case '\(method)' \(result) (\(printableStringForTimeInterval(duration)) seconds).")
XCTestCase.swift:91
        XCTPrint("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
XCTestMain.swift:34
        XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
XCTestMain.swift:70
    XCTPrint("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
(items: Any..., separator: String = " ", terminator: String = "\n") { 0022 print(items, separator: separator, terminator: terminator) 0023 fflush(stdout) 0024 } 0025 0026 struct XCTFailure
XCTAssert.swift:98
            handler(XCTFailure(message: message(), failureDescription: result.failureDescription(assertion), expected: result.expected, file: file, line: line))
XCTestCase.swift:39
                var failures = [XCTFailure]()
XCTestCase.swift:57
                        let unexpectedFailure = XCTFailure(message: "", failureDescription: "threw error \"\(error)\"", expected: false, file: "<EXPR>", line: 0)
XCTestMain.swift:42
    var failures: [XCTFailure]
XCTestMain.swift:43
    var unexpectedFailures: [XCTFailure] {
XCTestMain.swift:74
internal var XCTFailureHandler: (XCTFailure -> Void)?
{ 0027 var message
XCTestMain.swift:34
        XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
: String 0028 var failureDescription
XCTestMain.swift:34
        XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
: String 0029 var expected
XCTestCase.swift:70
                    if !failure.expected {
XCTestMain.swift:34
        XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
XCTestMain.swift:44
        get { return failures.filter({ failure -> Bool in failure.expected == false }) }
: Bool 0030 var file
XCTestCase.swift:43
                        fatalError("Terminating execution due to test failure", file: failure.file, line: failure.line)
XCTestMain.swift:34
        XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
: StaticString 0031 var line
XCTestCase.swift:43
                        fatalError("Terminating execution due to test failure", file: failure.file, line: failure.line)
XCTestMain.swift:34
        XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
: UInt 0032 0033 func emit
XCTestCase.swift:42
                        failure.emit(method)
XCTestCase.swift:68
                    failure.emit(method)
(method: String) { 0034 XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)") 0035 } 0036 } 0037 0038 internal struct XCTRun
XCTestCase.swift:77
                XCTAllRuns.append(XCTRun(duration: duration, method: method, passed: failures.count == 0, failures: failures))
XCTestMain.swift:75
internal var XCTAllRuns = [XCTRun]()
{ 0039 var duration
XCTestMain.swift:59
    let (totalDuration, totalFailures, totalUnexpectedFailures) = XCTAllRuns.reduce((0.0, 0, 0)) { totals, run in (totals.0 + run.duration, totals.1 + run.failures.count, totals.2 + run.unexpectedFailures.count) }
: TimeInterval 0040 var method: String 0041 var passed: Bool 0042 var failures
XCTestMain.swift:44
        get { return failures.filter({ failure -> Bool in failure.expected == false }) }
XCTestMain.swift:59
    let (totalDuration, totalFailures, totalUnexpectedFailures) = XCTAllRuns.reduce((0.0, 0, 0)) { totals, run in (totals.0 + run.duration, totals.1 + run.failures.count, totals.2 + run.unexpectedFailures.count) }
: [XCTFailure] 0043 var unexpectedFailures
XCTestMain.swift:59
    let (totalDuration, totalFailures, totalUnexpectedFailures) = XCTAllRuns.reduce((0.0, 0, 0)) { totals, run in (totals.0 + run.duration, totals.1 + run.failures.count, totals.2 + run.unexpectedFailures.count) }
: [XCTFailure] { 0044 get { return failures.filter({ failure -> Bool in failure.expected == false }) } 0045 } 0046 } 0047 0048 /// Starts a test run for the specified test cases. 0049 /// 0050 /// This function will not return. If the test cases pass, then it will call `exit(0)`. If there is a failure, then it will call `exit(1)`. 0051 /// - Parameter testCases: An array of test cases to run. 0052 @noreturn public func XCTMain(testCases: [XCTestCase]) { 0053 let overallDuration = measureTimeExecutingBlock { 0054 for testCase in testCases { 0055 testCase.invokeTest() 0056 } 0057 } 0058 0059 let (totalDuration, totalFailures, totalUnexpectedFailures) = XCTAllRuns.reduce((0.0, 0, 0)) { totals, run in (totals.0 + run.duration, totals.1 + run.failures.count, totals.2 + run.unexpectedFailures.count) } 0060 0061 var testCountSuffix = "s" 0062 if XCTAllRuns.count == 1 { 0063 testCountSuffix = "" 0064 } 0065 var failureSuffix = "s" 0066 if totalFailures == 1 { 0067 failureSuffix = "" 0068 } 0069 0070 XCTPrint("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds") 0071 exit(totalFailures > 0 ? 1 : 0) 0072 } 0073 0074 internal var XCTFailureHandler
XCTAssert.swift:97
        if let handler = XCTFailureHandler {
XCTestCase.swift:40
                XCTFailureHandler = { failure in
XCTestCase.swift:58
                        XCTFailureHandler!(unexpectedFailure)
XCTestCase.swift:78
                XCTFailureHandler = nil
: (XCTFailure -> Void)? 0075 internal var XCTAllRuns
XCTestCase.swift:77
                XCTAllRuns.append(XCTRun(duration: duration, method: method, passed: failures.count == 0, failures: failures))
XCTestMain.swift:59
    let (totalDuration, totalFailures, totalUnexpectedFailures) = XCTAllRuns.reduce((0.0, 0, 0)) { totals, run in (totals.0 + run.duration, totals.1 + run.failures.count, totals.2 + run.unexpectedFailures.count) }
XCTestMain.swift:62
    if XCTAllRuns.count == 1 {
XCTestMain.swift:70
    XCTPrint("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
= [XCTRun]() 0076