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

Drew's avatar
Drew committed
45 46
    ///Fail the test.
    ///- parameter message: A reason for failure
Drew's avatar
Drew committed
47 48 49 50 51 52 53 54 55 56 57 58
    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
59
  #if swift(>=3.0)
Drew's avatar
Drew committed
60 61 62
    ///Assert a boolean expression
    ///- parameter condition: A condition to assert
    ///- message: A reason for failure
63
    public final func assert(_ condition: @autoclosure () -> Bool,  _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
Drew's avatar
Drew committed
64
        if !condition() {
Drew's avatar
Drew committed
65
            self.fail(message(), file: file, line: line)
Drew's avatar
Drew committed
66 67
        }
    }
Drew's avatar
Drew committed
68
    #else
Drew's avatar
Drew committed
69
    ///- noindex: true
70
    public final func assert(@autoclosure _ condition: () -> Bool,  @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
Drew's avatar
Drew committed
71
        if !condition() {
Drew's avatar
Drew committed
72
            self.fail(message(), file: file, line: line)
Drew's avatar
Drew committed
73 74 75
        }
    }
    #endif
76 77 78 79

    //Equatable and SequenceType<Equatable>

    #if swift(>=3.0)
Drew's avatar
Drew committed
80 81
    ///Assert that two objects are equal
    ///- parameter message: A reason for failure
82 83
    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 {
84
            self.fail("\(message()) - \(a) != \(b)", file: file, line: line)
Drew's avatar
Drew committed
85 86
        }
    }
Drew's avatar
Drew committed
87 88
    ///Assert that two objects aren't equal
    ///- parameter message: A reason for failure
Drew's avatar
Drew committed
89 90
    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 {
91
            self.fail("\(message()) - \(a) == \(b)", file: file, line: line)
92 93
        }
    }
Drew's avatar
Drew committed
94 95
    ///Assert that two sequences are equal
    ///- parameter message: A reason for failure
96
    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
97
        if !compare(a,b) {
98
            self.fail("\(message()) - \(a) != \(b)", file: file, line: line)
Drew's avatar
Drew committed
99 100
        }
    }
Drew's avatar
Drew committed
101 102
    ///Assert that two sequences aren't equal
    ///- parameter message: A reason for failure
Drew's avatar
Drew committed
103 104
    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) {
105
            self.fail("\(message()) - \(a) == \(b)", file: file, line: line)
106 107 108
        }
    }
    #else
Drew's avatar
Drew committed
109
    ///- noindex: true
110 111
    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 {
112
            self.fail("\(message()) - \(a) != \(b)", file: file, line: line)
Drew's avatar
Drew committed
113
        }
Drew's avatar
Drew committed
114 115
    }    
    ///- noindex: true
Drew's avatar
Drew committed
116 117
    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 {
118
            self.fail("\(message()) - \(a) == \(b)", file: file, line: line)
119 120
        }
    }
Drew's avatar
Drew committed
121
    ///- noindex: true
122
    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
123
        if !compare(a,b) {
124
            self.fail("\(message()) - \(a) != \(b)", file: file, line: line)
Drew's avatar
Drew committed
125 126
        }
    }
Drew's avatar
Drew committed
127
    ///- noindex: true
Drew's avatar
Drew committed
128 129
    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) {
130
            self.fail("\(message()) - \(a) == \(b)", file: file, line: line)
131 132 133 134 135 136
        }
    }
    #endif

}