@@ -5,87 +5,135 @@ import Nimble
5
5
import Quick
6
6
@testable import ReactiveSwift
7
7
8
+ private class Object {
9
+ var value : Int = 0
10
+ }
11
+
8
12
class UnidirectionalBindingSpec : QuickSpec {
9
13
override func spec( ) {
10
14
describe ( " BindingTarget " ) {
11
15
var token : Lifetime . Token !
12
16
var lifetime : Lifetime !
13
- var target : BindingTarget < Int > !
14
- var optionalTarget : BindingTarget < Int ? > !
15
- var value : Int ?
16
17
17
18
beforeEach {
18
19
token = Lifetime . Token ( )
19
20
lifetime = Lifetime ( token)
20
- target = BindingTarget ( lifetime: lifetime, action: { value = $0 } )
21
- optionalTarget = BindingTarget ( lifetime: lifetime, action: { value = $0 } )
22
- value = nil
23
21
}
24
22
25
- describe ( " non-optional target " ) {
26
- it ( " should pass through the lifetime " ) {
27
- expect ( target. lifetime) . to ( beIdenticalTo ( lifetime) )
23
+ describe ( " closure binding target " ) {
24
+ var target : BindingTarget < Int > !
25
+ var optionalTarget : BindingTarget < Int ? > !
26
+ var value : Int ?
27
+
28
+ beforeEach {
29
+ target = BindingTarget ( lifetime: lifetime, action: { value = $0 } )
30
+ optionalTarget = BindingTarget ( lifetime: lifetime, action: { value = $0 } )
31
+ value = nil
28
32
}
29
33
30
- it ( " should trigger the supplied setter " ) {
31
- expect ( value) . to ( beNil ( ) )
34
+ describe ( " non-optional target " ) {
35
+ it ( " should pass through the lifetime " ) {
36
+ expect ( target. lifetime) . to ( beIdenticalTo ( lifetime) )
37
+ }
32
38
33
- target. action ( 1 )
34
- expect ( value) == 1
39
+ it ( " should trigger the supplied setter " ) {
40
+ expect ( value) . to ( beNil ( ) )
41
+
42
+ target. action ( 1 )
43
+ expect ( value) == 1
44
+ }
45
+
46
+ it ( " should accept bindings from properties " ) {
47
+ expect ( value) . to ( beNil ( ) )
48
+
49
+ let property = MutableProperty ( 1 )
50
+ target <~ property
51
+ expect ( value) == 1
52
+
53
+ property. value = 2
54
+ expect ( value) == 2
55
+ }
35
56
}
36
57
37
- it ( " should accept bindings from properties " ) {
38
- expect ( value) . to ( beNil ( ) )
58
+ describe ( " optional target " ) {
59
+ it ( " should pass through the lifetime " ) {
60
+ expect ( optionalTarget. lifetime) . to ( beIdenticalTo ( lifetime) )
61
+ }
39
62
40
- let property = MutableProperty ( 1 )
41
- target <~ property
42
- expect ( value) == 1
63
+ it ( " should trigger the supplied setter " ) {
64
+ expect ( value) . to ( beNil ( ) )
43
65
44
- property. value = 2
45
- expect ( value) == 2
66
+ optionalTarget. action ( 1 )
67
+ expect ( value) == 1
68
+ }
69
+
70
+ it ( " should accept bindings from properties " ) {
71
+ expect ( value) . to ( beNil ( ) )
72
+
73
+ let property = MutableProperty ( 1 )
74
+ optionalTarget <~ property
75
+ expect ( value) == 1
76
+
77
+ property. value = 2
78
+ expect ( value) == 2
79
+ }
46
80
}
47
81
}
48
82
49
- describe ( " optional target " ) {
83
+ #if swift(>=3.2)
84
+ describe ( " key path binding target " ) {
85
+ var target : BindingTarget < Int > !
86
+ var object : Object !
87
+
88
+ beforeEach {
89
+ object = Object ( )
90
+ target = BindingTarget ( lifetime: lifetime, object: object, keyPath: \. value)
91
+ }
92
+
50
93
it ( " should pass through the lifetime " ) {
51
- expect ( optionalTarget . lifetime) . to ( beIdenticalTo ( lifetime) )
94
+ expect ( target . lifetime) . to ( beIdenticalTo ( lifetime) )
52
95
}
53
96
54
97
it ( " should trigger the supplied setter " ) {
55
- expect ( value) . to ( beNil ( ) )
98
+ expect ( object . value) == 0
56
99
57
- optionalTarget . action ( 1 )
58
- expect ( value) == 1
100
+ target . action ( 1 )
101
+ expect ( object . value) == 1
59
102
}
60
103
61
104
it ( " should accept bindings from properties " ) {
62
- expect ( value) . to ( beNil ( ) )
105
+ expect ( object . value) == 0
63
106
64
107
let property = MutableProperty ( 1 )
65
- optionalTarget <~ property
66
- expect ( value) == 1
108
+ target <~ property
109
+ expect ( object . value) == 1
67
110
68
111
property. value = 2
69
- expect ( value) == 2
112
+ expect ( object . value) == 2
70
113
}
71
114
}
115
+ #endif
72
116
73
117
it ( " should not deadlock on the same queue " ) {
74
- target = BindingTarget ( on: UIScheduler ( ) ,
75
- lifetime: lifetime,
76
- action: { value = $0 } )
118
+ var value : Int ?
119
+
120
+ let target = BindingTarget ( on: UIScheduler ( ) ,
121
+ lifetime: lifetime,
122
+ action: { value = $0 } )
77
123
78
124
let property = MutableProperty ( 1 )
79
125
target <~ property
80
126
expect ( value) == 1
81
127
}
82
128
83
129
it ( " should not deadlock on the main thread even if the context was switched to a different queue " ) {
130
+ var value : Int ?
131
+
84
132
let queue = DispatchQueue ( label: #file)
85
133
86
- target = BindingTarget ( on: UIScheduler ( ) ,
87
- lifetime: lifetime,
88
- action: { value = $0 } )
134
+ let target = BindingTarget ( on: UIScheduler ( ) ,
135
+ lifetime: lifetime,
136
+ action: { value = $0 } )
89
137
90
138
let property = MutableProperty ( 1 )
91
139
@@ -97,6 +145,8 @@ class UnidirectionalBindingSpec: QuickSpec {
97
145
}
98
146
99
147
it ( " should not deadlock even if the value is originated from the same queue indirectly " ) {
148
+ var value : Int ?
149
+
100
150
let key = DispatchSpecificKey < Void > ( )
101
151
DispatchQueue . main. setSpecific ( key: key, value: ( ) )
102
152
@@ -107,9 +157,9 @@ class UnidirectionalBindingSpec: QuickSpec {
107
157
mainQueueCounter. modify { $0 += DispatchQueue . getSpecific ( key: key) != nil ? 1 : 0 }
108
158
}
109
159
110
- target = BindingTarget ( on: UIScheduler ( ) ,
111
- lifetime: lifetime,
112
- action: setter)
160
+ let target = BindingTarget ( on: UIScheduler ( ) ,
161
+ lifetime: lifetime,
162
+ action: setter)
113
163
114
164
let scheduler : QueueScheduler
115
165
if #available( OSX 10 . 10 , * ) {
0 commit comments