CarolineCore.swift 2.65 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
#if os(OSX) || os(iOS)
Drew's avatar
Drew committed
16
import Darwin
Drew's avatar
Drew committed
17 18 19
#elseif os(Linux)
import Glibc
#endif
Drew's avatar
Drew committed
20 21

protocol CarolineEngine {
Drew's avatar
Drew committed
22 23
    @warn_unused_result
    func testAll(_ tests: [CarolineTest]) -> Bool
Drew's avatar
Drew committed
24 25 26
}

public class CarolineCoreEngine : CarolineEngine {
Drew's avatar
Drew committed
27 28
    @warn_unused_result
    public func testAll(_ tests: [CarolineTest]) -> Bool {
Drew's avatar
Drew committed
29 30 31 32 33 34 35 36 37 38 39
        setvbuf(stdout, nil, _IOLBF, 0)
        for test in tests {
          if test.skip {
            var state = CarolineState(test: test)
            state.outcome = .Skipped
            state.commit()
            continue
          }
            print("test \(test) started.")
            test.test()
            print("test \(test) ended.")
Drew's avatar
Drew committed
40 41
            var state = CarolineState(test: test)
            if state.outcome == .Initial { state.outcome = .Passed; state.commit() }
Drew's avatar
Drew committed
42 43 44
        }

        //count failures
Drew's avatar
Drew committed
45 46
        print("Test report:")
        print("-----------------------")
Drew's avatar
Drew committed
47 48 49 50
        var failures = 0
        var skipped = 0
        for test in tests {
          let state = CarolineState(test: test)
Drew's avatar
Drew committed
51 52 53 54 55 56 57 58
          if state.outcome == .Failed && !test.expectFailure {
            failures += 1
            print("\(test) failed")
          }
          if test.expectFailure && state.outcome != .XFailed {
            failures += 1 
            print("\(test) didn't fail as expected – \(state.outcome)")
          }
Drew's avatar
Drew committed
59
          if state.outcome == .Skipped { skipped += 1}
Drew's avatar
Drew committed
60 61 62

        }
        print("-----------------------")
Drew's avatar
Drew committed
63
        #if os(Linux)
Drew's avatar
Drew committed
64
        srand(UInt32(time(nil)))
Drew's avatar
Drew committed
65 66 67 68 69
        let rand_int = Int(rand())
        #elseif arch(arm) //32-bit platforms
        let rand_int = Int(arc4random() % UInt32(Int32.max))
        #else //64-bit platforms
        let rand_int = Int(arc4random())
Drew's avatar
Drew committed
70
        #endif
Drew's avatar
Drew committed
71
        if failures == 0 {
Drew's avatar
Drew committed
72
            print(passSnark[Int(rand_int) % passSnark.count])
Drew's avatar
Drew committed
73 74
        }
        else {
Drew's avatar
Drew committed
75
            print(failSnark[Int(rand_int) % failSnark.count])
Drew's avatar
Drew committed
76
        }
Drew's avatar
Drew committed
77
        print("Test execution finished with \(tests.count) tests, \(failures) failures, \(skipped) skipped.")
Drew's avatar
Drew committed
78 79
        if failures > 0 { return false }
        return true
Drew's avatar
Drew committed
80 81
    }
    public init() { }
Drew's avatar
Drew committed
82

Drew's avatar
Drew committed
83
}