@@ -15,40 +15,19 @@ extension AsyncDefaults {
15
15
public static var PollInterval : TimeInterval = 0.01
16
16
}
17
17
18
- private func async < T> ( style: ExpectationStyle , predicate: Predicate < T > , timeout: DispatchTimeInterval , poll: DispatchTimeInterval , fnName: String ) -> Predicate < T > {
19
- return Predicate { actualExpression in
20
- let uncachedExpression = actualExpression. withoutCaching ( )
21
- let fnName = " expect(...). \( fnName) (...) "
22
- var lastPredicateResult : PredicateResult ?
23
- let result = pollBlock (
24
- pollInterval: poll,
25
- timeoutInterval: timeout,
26
- file: actualExpression. location. file,
27
- line: actualExpression. location. line,
28
- fnName: fnName) {
29
- lastPredicateResult = try predicate. satisfies ( uncachedExpression)
30
- return lastPredicateResult!. toBoolean ( expectation: style)
31
- }
32
- switch result {
33
- case . completed: return lastPredicateResult!
34
- case . timedOut:
35
- let message = lastPredicateResult? . message ?? . fail( " timed out before returning a value " )
36
- return PredicateResult ( status: . fail, message: message)
37
- case let . errorThrown( error) :
38
- return PredicateResult ( status: . fail, message: . fail( " unexpected error thrown: < \( error) > " ) )
39
- case let . raisedException( exception) :
40
- return PredicateResult ( status: . fail, message: . fail( " unexpected exception raised: \( exception) " ) )
41
- case . blockedRunLoop:
42
- let message = lastPredicateResult? . message. appended ( message: " (timed out, but main run loop was unresponsive). " ) ??
43
- . fail( " main run loop was unresponsive " )
44
- return PredicateResult ( status: . fail, message: message)
45
- case . incomplete:
46
- internalError ( " Reached .incomplete state for \( fnName) (...). " )
47
- }
48
- }
18
+ private enum AsyncMatchStyle {
19
+ case eventually, never
49
20
}
50
21
51
- private func toNeverPredicate< T> ( predicate: Predicate < T > , timeout: DispatchTimeInterval , poll: DispatchTimeInterval , fnName: String ) -> Predicate < T > {
22
+ // swiftlint:disable:next function_parameter_count
23
+ private func async < T> (
24
+ style: ExpectationStyle ,
25
+ matchStyle: AsyncMatchStyle ,
26
+ predicate: Predicate < T > ,
27
+ timeout: DispatchTimeInterval ,
28
+ poll: DispatchTimeInterval ,
29
+ fnName: String
30
+ ) -> Predicate < T > {
52
31
return Predicate { actualExpression in
53
32
let uncachedExpression = actualExpression. withoutCaching ( )
54
33
let fnName = " expect(...). \( fnName) (...) "
@@ -60,22 +39,32 @@ private func toNeverPredicate<T>(predicate: Predicate<T>, timeout: DispatchTimeI
60
39
line: actualExpression. location. line,
61
40
fnName: fnName) {
62
41
lastPredicateResult = try predicate. satisfies ( uncachedExpression)
63
- return lastPredicateResult!. toBoolean ( expectation: . toMatch )
42
+ return lastPredicateResult!. toBoolean ( expectation: style )
64
43
}
65
44
switch result {
66
45
case . completed:
67
- return PredicateResult (
68
- status: . fail,
69
- message: lastPredicateResult? . message ?? . fail( " matched the predicate when it shouldn't have " )
70
- )
46
+ switch matchStyle {
47
+ case . eventually:
48
+ return lastPredicateResult!
49
+ case . never:
50
+ return PredicateResult (
51
+ status: . fail,
52
+ message: lastPredicateResult? . message ?? . fail( " matched the predicate when it shouldn't have " )
53
+ )
54
+ }
71
55
case . timedOut:
72
- return PredicateResult ( status: . doesNotMatch, message: . expectedTo( " never match the predicate " ) )
56
+ switch matchStyle {
57
+ case . eventually:
58
+ let message = lastPredicateResult? . message ?? . fail( " timed out before returning a value " )
59
+ return PredicateResult ( status: . fail, message: message)
60
+ case . never:
61
+ return PredicateResult ( status: . doesNotMatch, message: . expectedTo( " never match the predicate " ) )
62
+ }
73
63
case let . errorThrown( error) :
74
64
return PredicateResult ( status: . fail, message: . fail( " unexpected error thrown: < \( error) > " ) )
75
65
case let . raisedException( exception) :
76
66
return PredicateResult ( status: . fail, message: . fail( " unexpected exception raised: \( exception) " ) )
77
67
case . blockedRunLoop:
78
- // swiftlint:disable:next line_length
79
68
let message = lastPredicateResult? . message. appended ( message: " (timed out, but main run loop was unresponsive). " ) ??
80
69
. fail( " main run loop was unresponsive " )
81
70
return PredicateResult ( status: . fail, message: message)
@@ -105,7 +94,14 @@ extension Expectation {
105
94
let ( pass, msg) = execute (
106
95
expression,
107
96
. toMatch,
108
- async ( style: . toMatch, predicate: predicate, timeout: timeout, poll: pollInterval, fnName: " toEventually " ) ,
97
+ async (
98
+ style: . toMatch,
99
+ matchStyle: . eventually,
100
+ predicate: predicate,
101
+ timeout: timeout,
102
+ poll: pollInterval,
103
+ fnName: " toEventually "
104
+ ) ,
109
105
to: " to eventually " ,
110
106
description: description,
111
107
captureExceptions: false
@@ -127,6 +123,7 @@ extension Expectation {
127
123
. toNotMatch,
128
124
async (
129
125
style: . toNotMatch,
126
+ matchStyle: . eventually,
130
127
predicate: predicate,
131
128
timeout: timeout,
132
129
poll: pollInterval,
@@ -163,7 +160,14 @@ extension Expectation {
163
160
let ( pass, msg) = execute (
164
161
expression,
165
162
. toNotMatch,
166
- toNeverPredicate ( predicate: predicate, timeout: until, poll: pollInterval, fnName: " toNever " ) ,
163
+ async (
164
+ style: . toMatch,
165
+ matchStyle: . never,
166
+ predicate: predicate,
167
+ timeout: until,
168
+ poll: pollInterval,
169
+ fnName: " toNever "
170
+ ) ,
167
171
to: " to never " ,
168
172
description: description,
169
173
captureExceptions: false
0 commit comments