-
-
Notifications
You must be signed in to change notification settings - Fork 592
/
NMBExpectation.swift
131 lines (109 loc) · 4.05 KB
/
NMBExpectation.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
import Foundation
#if _runtime(_ObjC)
internal struct ObjCMatcherWrapper: Matcher {
let matcher: NMBMatcher
func matches(_ actualExpression: Expression<NSObject>, failureMessage: FailureMessage) -> Bool {
return matcher.matches(
({ try! actualExpression.evaluate() }),
failureMessage: failureMessage,
location: actualExpression.location)
}
func doesNotMatch(_ actualExpression: Expression<NSObject>, failureMessage: FailureMessage) -> Bool {
return matcher.doesNotMatch(
({ try! actualExpression.evaluate() }),
failureMessage: failureMessage,
location: actualExpression.location)
}
}
// Equivalent to Expectation, but for Nimble's Objective-C interface
public class NMBExpectation: NSObject {
internal let _actualBlock: () -> NSObject!
internal var _negative: Bool
internal let _file: FileString
internal let _line: UInt
internal var _timeout: TimeInterval = 1.0
public init(actualBlock: @escaping () -> NSObject!, negative: Bool, file: FileString, line: UInt) {
self._actualBlock = actualBlock
self._negative = negative
self._file = file
self._line = line
}
private var expectValue: Expectation<NSObject> {
return expect(_file, line: _line) {
self._actualBlock() as NSObject?
}
}
public var withTimeout: (TimeInterval) -> NMBExpectation {
return ({ timeout in self._timeout = timeout
return self
})
}
public var to: (NMBMatcher) -> Void {
return ({ matcher in
self.expectValue.to(ObjCMatcherWrapper(matcher: matcher))
})
}
public var toWithDescription: (NMBMatcher, String) -> Void {
return ({ matcher, description in
self.expectValue.to(ObjCMatcherWrapper(matcher: matcher), description: description)
})
}
public var toNot: (NMBMatcher) -> Void {
return ({ matcher in
self.expectValue.toNot(
ObjCMatcherWrapper(matcher: matcher)
)
})
}
public var toNotWithDescription: (NMBMatcher, String) -> Void {
return ({ matcher, description in
self.expectValue.toNot(
ObjCMatcherWrapper(matcher: matcher), description: description
)
})
}
public var notTo: (NMBMatcher) -> Void { return toNot }
public var notToWithDescription: (NMBMatcher, String) -> Void { return toNotWithDescription }
public var toEventually: (NMBMatcher) -> Void {
return ({ matcher in
self.expectValue.toEventually(
ObjCMatcherWrapper(matcher: matcher),
timeout: self._timeout,
description: nil
)
})
}
public var toEventuallyWithDescription: (NMBMatcher, String) -> Void {
return ({ matcher, description in
self.expectValue.toEventually(
ObjCMatcherWrapper(matcher: matcher),
timeout: self._timeout,
description: description
)
})
}
public var toEventuallyNot: (NMBMatcher) -> Void {
return ({ matcher in
self.expectValue.toEventuallyNot(
ObjCMatcherWrapper(matcher: matcher),
timeout: self._timeout,
description: nil
)
})
}
public var toEventuallyNotWithDescription: (NMBMatcher, String) -> Void {
return ({ matcher, description in
self.expectValue.toEventuallyNot(
ObjCMatcherWrapper(matcher: matcher),
timeout: self._timeout,
description: description
)
})
}
public var toNotEventually: (NMBMatcher) -> Void { return toEventuallyNot }
public var toNotEventuallyWithDescription: (NMBMatcher, String) -> Void { return toEventuallyNotWithDescription }
public class func failWithMessage(_ message: String, file: FileString, line: UInt) {
fail(message, location: SourceLocation(file: file, line: line))
}
}
#endif