Functions.swift 3.47 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 15 16

//MARK: Functions
extension CarolineTest {
Drew's avatar
Drew committed
17
  #if swift(>=3.0)
18
    public final func assert(_ condition: @autoclosure () -> Bool,  _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
Drew's avatar
Drew committed
19 20 21 22 23 24 25
        if !condition() {
            var state = CarolineState(test: self)
            state.outcome = .Failed
            state.commit()
            print("Test failed at \(file):\(line) - \(message())")
        }
    }
Drew's avatar
Drew committed
26
    #else
27
    public final func assert(@autoclosure _ condition: () -> Bool,  @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
Drew's avatar
Drew committed
28 29 30 31 32 33 34 35
        if !condition() {
            var state = CarolineState(test: self)
            state.outcome = .Failed
            state.commit()
            print("Test failed at \(file):\(line) - \(message())")
        }
    }
    #endif
36 37 38 39 40 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    //Equatable and SequenceType<Equatable>

    #if swift(>=3.0)
    public final func assert<T: Equatable>(_ a: T,  equals b: T, _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        if a != b {
            var state = CarolineState(test: self)
            state.outcome = .Failed
            state.commit()
            print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
        }
    }
    public final func assert<T:Sequence where T.Iterator.Element: Equatable>(_ a: T,  equals b: T, _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        var iteratorB = b.makeIterator()
        for itemA in a {
            let itemB = iteratorB.next()
            if itemB != itemA {
                var state = CarolineState(test: self)
                state.outcome = .Failed
                state.commit()
                print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
                return
            }
        }
    }
    #else
    public final func assert<T: Equatable>(_ a: T,  equals b: T, @autoclosure _ message:  () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        if a != b {
            var state = CarolineState(test: self)
            state.outcome = .Failed
            state.commit()
            print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
        }
    }
    public final func assert<T:SequenceType where T.Generator.Element: Equatable>(_ a: T,  equals b: T, @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        var iteratorB = b.generate()
        for itemA in a {
            let itemB = iteratorB.next()
            if itemB != itemA {
                var state = CarolineState(test: self)
                state.outcome = .Failed
                state.commit()
                print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
                return
            }
        }
    }
    #endif

}