/
MatcherProtocols.swift
157 lines (132 loc) · 4.72 KB
/
MatcherProtocols.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Foundation
// `CGFloat` is in Foundation (swift-corelibs-foundation) on Linux.
#if canImport(Darwin)
import CoreGraphics
#endif
/// Implement this protocol to implement a custom matcher for Swift
@available(*, deprecated, message: "Use Predicate instead")
public protocol Matcher {
associatedtype ValueType
func matches(_ actualExpression: Expression<ValueType>, failureMessage: FailureMessage) throws -> Bool
func doesNotMatch(_ actualExpression: Expression<ValueType>, failureMessage: FailureMessage) throws -> Bool
}
extension Matcher {
var predicate: Predicate<ValueType> {
return Predicate.fromDeprecatedMatcher(self)
}
var toClosure: (Expression<ValueType>, FailureMessage, Bool) throws -> Bool {
return ({ expr, msg, expectedResult in
if expectedResult {
return try self.matches(expr, failureMessage: msg)
} else {
return try self.doesNotMatch(expr, failureMessage: msg)
}
})
}
}
#if canImport(Darwin)
/// Objective-C interface to the Swift variant of Matcher.
@objc public protocol NMBMatcher {
func matches(_ actualBlock: @escaping () -> NSObject?, failureMessage: FailureMessage, location: SourceLocation) -> Bool
func doesNotMatch(_ actualBlock: @escaping () -> NSObject?, failureMessage: FailureMessage, location: SourceLocation) -> Bool
}
#endif
/// Protocol for types that support contain() matcher.
public protocol NMBContainer {
func contains(_ anObject: Any) -> Bool
}
#if canImport(Darwin)
// swiftlint:disable:next todo
// FIXME: NSHashTable can not conform to NMBContainer since swift-DEVELOPMENT-SNAPSHOT-2016-04-25-a
//extension NSHashTable : NMBContainer {} // Corelibs Foundation does not include this class yet
#endif
extension NSArray: NMBContainer {}
extension NSSet: NMBContainer {}
/// Protocol for types that support only beEmpty(), haveCount() matchers
public protocol NMBCollection {
var count: Int { get }
}
#if canImport(Darwin)
extension NSHashTable: NMBCollection {} // Corelibs Foundation does not include these classes yet
extension NSMapTable: NMBCollection {}
#endif
extension NSSet: NMBCollection {}
extension NSIndexSet: NMBCollection {}
extension NSDictionary: NMBCollection {}
/// Protocol for types that support beginWith(), endWith(), beEmpty() matchers
public protocol NMBOrderedCollection: NMBCollection {
func object(at index: Int) -> Any
}
extension NSArray: NMBOrderedCollection {}
public protocol NMBDoubleConvertible {
var doubleValue: CDouble { get }
}
extension Double: NMBDoubleConvertible {
public var doubleValue: CDouble {
return self
}
}
extension Float: NMBDoubleConvertible {
public var doubleValue: CDouble {
return CDouble(self)
}
}
extension CGFloat: NMBDoubleConvertible {
public var doubleValue: CDouble {
return CDouble(self)
}
}
extension NSNumber: NMBDoubleConvertible {
}
private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSS"
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
extension Date: NMBDoubleConvertible {
public var doubleValue: CDouble {
return self.timeIntervalSinceReferenceDate
}
}
extension NSDate: NMBDoubleConvertible {
public var doubleValue: CDouble {
return self.timeIntervalSinceReferenceDate
}
}
extension Date: TestOutputStringConvertible {
public var testDescription: String {
return dateFormatter.string(from: self)
}
}
extension NSDate: TestOutputStringConvertible {
public var testDescription: String {
return dateFormatter.string(from: Date(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate))
}
}
/// Protocol for types to support beLessThan(), beLessThanOrEqualTo(),
/// beGreaterThan(), beGreaterThanOrEqualTo(), and equal() matchers.
///
/// Types that conform to Swift's Comparable protocol will work implicitly too
#if canImport(Darwin)
@objc public protocol NMBComparable {
func NMB_compare(_ otherObject: NMBComparable!) -> ComparisonResult
}
#else
// This should become obsolete once Corelibs Foundation adds Comparable conformance to NSNumber
public protocol NMBComparable {
func NMB_compare(_ otherObject: NMBComparable!) -> ComparisonResult
}
#endif
extension NSNumber: NMBComparable {
public func NMB_compare(_ otherObject: NMBComparable!) -> ComparisonResult {
// swiftlint:disable:next force_cast
return compare(otherObject as! NSNumber)
}
}
extension NSString: NMBComparable {
public func NMB_compare(_ otherObject: NMBComparable!) -> ComparisonResult {
// swiftlint:disable:next force_cast
return compare(otherObject as! String)
}
}