|
| 1 | +// |
| 2 | +// tap.swift |
| 3 | +// tap |
| 4 | +// |
| 5 | +// Created by Dan Kogai on 1/21/16. |
| 6 | +// Copyright © 2016 Dan Kogai. All rights reserved. |
| 7 | +// |
| 8 | +#if os(Linux) |
| 9 | + import Glibc |
| 10 | +#else |
| 11 | + import Darwin |
| 12 | +#endif |
| 13 | + |
| 14 | +public class TAP { |
| 15 | + var tests = 0, runs = [Bool]() |
| 16 | + public init() {} |
| 17 | + public init(tests:Int) { |
| 18 | + self.plan(tests) |
| 19 | + } |
| 20 | + /// sets the number of tests to run. call it before the first test |
| 21 | + public func plan(tests:Int) { |
| 22 | + self.tests = tests |
| 23 | + print("1..\(tests)") |
| 24 | + } |
| 25 | + /// ok if `predicate` is true |
| 26 | + public func ok(@autoclosure predicate:()->Bool, _ message:String = "")->Bool { |
| 27 | + let ok = predicate() |
| 28 | + runs.append(ok) |
| 29 | + let ornot = ok ? "" : "not " |
| 30 | + print("\(ornot)ok \(runs.count) - \(message)") |
| 31 | + return ok |
| 32 | + } |
| 33 | + /// ok if `actual` == `expected` |
| 34 | + public func eq<T:Equatable>(actual:T?, _ expected:T?, _ message:String = "")->Bool { |
| 35 | + if ok(actual == expected, message) { return true } |
| 36 | + print("# got: \(actual)") |
| 37 | + print("# expected: \(expected)") |
| 38 | + return false |
| 39 | + } |
| 40 | + /// ok if arrays are `actual` == `expected` |
| 41 | + public func eq<T:Equatable>(actual:[T], _ expected:[T], _ message:String = "")->Bool { |
| 42 | + if ok(actual == expected, message) { return true } |
| 43 | + print("# got: \(actual)") |
| 44 | + print("# expected: \(expected)") |
| 45 | + return false |
| 46 | + } |
| 47 | + /// ok if dictionaries are `actual` == `expected` |
| 48 | + public func eq<K:Hashable,V:Equatable>(actual:[K:V], _ expected:[K:V], _ message:String = "")->Bool { |
| 49 | + if ok(actual == expected, message) { return true } |
| 50 | + print("# got: \(actual)") |
| 51 | + print("# expected: \(expected)") |
| 52 | + return false |
| 53 | + } |
| 54 | + /// ok if `actual` != `expected` |
| 55 | + public func ne<T:Equatable>(actual:T?, _ expected:T?, _ message:String = "")->Bool { |
| 56 | + if ok(actual != expected, message) { return true } |
| 57 | + print("# got: \(actual)") |
| 58 | + print("# expected: anthing but \(expected)") |
| 59 | + return false |
| 60 | + } |
| 61 | + /// ok if arrays are `actual` == `expected` |
| 62 | + public func ne<T:Equatable>(actual:[T], _ expected:[T], _ message:String = "")->Bool { |
| 63 | + if ok(actual != expected, message) { return true } |
| 64 | + print("# got: \(actual)") |
| 65 | + print("# expected: anthing but \(expected)") |
| 66 | + return false |
| 67 | + } |
| 68 | + /// ok if dictionaries are `actual` == `expected` |
| 69 | + public func ne<K:Hashable,V:Equatable>(actual:[K:V], _ expected:[K:V], _ message:String = "")->Bool { |
| 70 | + if ok(actual != expected, message) { return true } |
| 71 | + print("# got: \(actual)") |
| 72 | + print("# expected: anthing but \(expected)") |
| 73 | + return false |
| 74 | + } |
| 75 | + /// checks the test results, print stuff if neccesary, |
| 76 | + /// and `exit()` with code == number of failures |
| 77 | + public func done(dontExit nx:Bool = false)->[Bool] { |
| 78 | + if runs.count == 0 && nx != true { |
| 79 | + print("# no test run!") |
| 80 | + exit(-1) |
| 81 | + } |
| 82 | + if tests == 0 { |
| 83 | + print("1..\(runs.count)") |
| 84 | + } |
| 85 | + else if runs.count != tests { |
| 86 | + print("not ok \(runs.count + 1) - planned to run \(tests) but actually ran \(runs.count)") |
| 87 | + runs.append(false) |
| 88 | + if nx != true { exit(-1) } |
| 89 | + } |
| 90 | + if nx != true { |
| 91 | + let code = min(254, runs.filter{ $0 == false }.count) |
| 92 | + exit(Int32(code)) |
| 93 | + } |
| 94 | + return runs |
| 95 | + } |
| 96 | + deinit { |
| 97 | + done(dontExit:true) |
| 98 | + } |
| 99 | +} |
0 commit comments