Functions.swift 4.62 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

//MARK: Functions
Drew's avatar
Drew committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#if swift(>=3.0)
private func compare<T: Sequence where T.Iterator.Element: Equatable>(_ a: T,  _ b: T) -> Bool {
    var iteratorB = b.makeIterator()
    for itemA in a {
        let itemB = iteratorB.next()
        if itemB != itemA {
            return false
        }
    }
    //We've concluded iterating A.  Did B have more elements?
    if let _ = iteratorB.next() { return false }
    return true
}
#else
private func compare<T: SequenceType where T.Generator.Element: Equatable>(_ a: T,  _ b: T) -> Bool {
    var iteratorB = b.generate()
    for itemA in a {
        let itemB = iteratorB.next()
        if itemB != itemA {
            return false
        }
    }
    //We've concluded iterating A.  Did B have more elements?
    if let _ = iteratorB.next() { return false }
    return true
}
#endif
Drew's avatar
Drew committed
43
extension CarolineTest {
Drew's avatar
Drew committed
44 45 46 47 48 49 50 51 52 53 54 55 56

    public final func fail(_ message: String = "Test failed", file: String = #file, line: Int = #line) {
        var state = CarolineState(test: self)
        if self.expectFailure {
            state.outcome = .XFailed
        }
        else {
            state.outcome = .Failed
            print("Test failed at \(file):\(line) - \(message)")
        }
        state.commit()
    }

Drew's avatar
Drew committed
57
  #if swift(>=3.0)
58
    public final func assert(_ condition: @autoclosure () -> Bool,  _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
Drew's avatar
Drew committed
59
        if !condition() {
Drew's avatar
Drew committed
60
            self.fail(message(), file: file, line: line)
Drew's avatar
Drew committed
61 62
        }
    }
Drew's avatar
Drew committed
63
    #else
64
    public final func assert(@autoclosure _ condition: () -> Bool,  @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
Drew's avatar
Drew committed
65
        if !condition() {
Drew's avatar
Drew committed
66
            self.fail(message(), file: file, line: line)
Drew's avatar
Drew committed
67 68 69
        }
    }
    #endif
70 71 72 73 74 75

    //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 {
Drew's avatar
Drew committed
76 77 78 79 80 81
            self.fail("\(message()) - \(a) != \(b)")
        }
    }
    public final func assert<T: Equatable>(_ a: T,  notEqual b: T, _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        if a == b {
            self.fail("\(message()) - \(a) == \(b)")
82 83 84
        }
    }
    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) {
Drew's avatar
Drew committed
85 86 87 88 89 90 91
        if !compare(a,b) {
            self.fail("\(message()) - \(a) != \(b)")
        }
    }
    public final func assert<T:Sequence where T.Iterator.Element: Equatable>(_ a: T,  notEqual b: T, _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        if compare(a,b) {
            self.fail("\(message()) - \(a) == \(b)")
92 93 94 95 96
        }
    }
    #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 {
Drew's avatar
Drew committed
97 98 99 100 101 102
            self.fail("\(message()) - \(a) != \(b)")
        }
    }
    public final func assert<T: Equatable>(_ a: T,  notEqual b: T, @autoclosure _ message:  () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        if a == b {
            self.fail("\(message()) - \(a) == \(b)")
103 104 105
        }
    }
    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) {
Drew's avatar
Drew committed
106 107 108 109 110 111 112
        if !compare(a,b) {
            self.fail("\(message()) - \(a) != \(b)")
        }
    }
    public final func assert<T:SequenceType where T.Generator.Element: Equatable>(_ a: T,  notEqual b: T, @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
        if compare(a,b) {
            self.fail("\(message()) - \(a) == \(b)")
113 114 115 116 117 118
        }
    }
    #endif

}