1
+ using System ;
2
+ using System . Linq ;
3
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
4
+ using PubSub . Core ;
5
+
6
+ namespace PubSub . Tests
7
+ {
8
+ [ TestClass ]
9
+ public class CoreHubTests
10
+ {
11
+ private Hub hub ;
12
+ private object sender ;
13
+ private object condemnedSender ;
14
+ private object preservedSender ;
15
+
16
+
17
+ [ TestInitialize ]
18
+ public void Setup ( )
19
+ {
20
+ hub = new Hub ( ) ;
21
+ sender = new object ( ) ;
22
+ condemnedSender = new object ( ) ;
23
+ preservedSender = new object ( ) ;
24
+ }
25
+
26
+ [ TestMethod ]
27
+ public void Publish_CallsAllRegisteredActions ( )
28
+ {
29
+ // arrange
30
+ var callCount = 0 ;
31
+ hub . Subscribe ( new object ( ) , new Action < string > ( a => callCount ++ ) ) ;
32
+ hub . Subscribe ( new object ( ) , new Action < string > ( a => callCount ++ ) ) ;
33
+
34
+ // act
35
+ hub . Publish ( null , default ( string ) ) ;
36
+
37
+ // assert
38
+ Assert . AreEqual ( 2 , callCount ) ;
39
+ }
40
+
41
+ [ TestMethod ]
42
+ public void Publish_SpecialEvent_CaughtByBase ( )
43
+ {
44
+ // arrange
45
+ var callCount = 0 ;
46
+ hub . Subscribe < Event > ( sender , a => callCount ++ ) ;
47
+ hub . Subscribe ( sender , new Action < Event > ( a => callCount ++ ) ) ;
48
+
49
+ // act
50
+ hub . Publish ( sender , new SpecialEvent ( ) ) ;
51
+
52
+ // assert
53
+ Assert . AreEqual ( 2 , callCount ) ;
54
+ }
55
+
56
+ [ TestMethod ]
57
+ public void Publish_BaseEvent_NotCaughtBySpecial ( )
58
+ {
59
+ // arrange
60
+ var callCount = 0 ;
61
+ hub . Subscribe ( sender , new Action < SpecialEvent > ( a => callCount ++ ) ) ;
62
+ hub . Subscribe ( sender , new Action < Event > ( a => callCount ++ ) ) ;
63
+
64
+ // act
65
+ hub . Publish ( sender , new Event ( ) ) ;
66
+
67
+ // assert
68
+ Assert . AreEqual ( 1 , callCount ) ;
69
+ }
70
+
71
+
72
+ [ TestMethod ]
73
+ public void Publish_CleansUpBeforeSending ( )
74
+ {
75
+ // arrange
76
+ var liveSender = new object ( ) ;
77
+
78
+ // act
79
+ hub . Subscribe ( condemnedSender , new Action < string > ( a => { } ) ) ;
80
+ hub . Subscribe ( liveSender , new Action < string > ( a => { } ) ) ;
81
+
82
+ condemnedSender = null ;
83
+ GC . Collect ( ) ;
84
+
85
+ hub . Publish ( null , default ( string ) ) ;
86
+
87
+ // assert
88
+ Assert . AreEqual ( 1 , hub . handlers . Count ) ;
89
+ GC . KeepAlive ( liveSender ) ;
90
+ }
91
+
92
+
93
+ [ TestMethod ]
94
+ public void Subscribe_AddsHandlerToList ( )
95
+ {
96
+ // arrange
97
+ var action = new Action < string > ( a => { } ) ;
98
+
99
+ // act
100
+ hub . Subscribe ( sender , action ) ;
101
+
102
+ // assert
103
+ var h = hub . handlers . First ( ) ;
104
+ Assert . AreEqual ( sender , h . Sender . Target ) ;
105
+ Assert . AreEqual ( action , h . Action ) ;
106
+ Assert . AreEqual ( action . Method . GetParameters ( ) . First ( ) . ParameterType , h . Type ) ;
107
+ }
108
+
109
+
110
+ [ TestMethod ]
111
+ public void Unsubscribe_RemovesAllHandlers_OfAnyType_ForSender ( )
112
+ {
113
+ // act
114
+ hub . Subscribe ( preservedSender , new Action < string > ( a => { } ) ) ;
115
+ hub . Subscribe ( sender , new Action < string > ( a => { } ) ) ;
116
+ hub . Unsubscribe ( sender ) ;
117
+
118
+ // assert
119
+ Assert . IsTrue ( hub . handlers . Any ( a => a . Sender . Target == preservedSender ) ) ;
120
+ Assert . IsFalse ( hub . handlers . Any ( a => a . Sender . Target == sender ) ) ;
121
+ }
122
+
123
+ [ TestMethod ]
124
+ public void Unsubscribe_RemovesAllHandlers_OfSpecificType_ForSender ( )
125
+ {
126
+ // arrange
127
+ hub . Subscribe ( sender , new Action < string > ( a => { } ) ) ;
128
+ hub . Subscribe ( sender , new Action < string > ( a => { } ) ) ;
129
+ hub . Subscribe ( preservedSender , new Action < string > ( a => { } ) ) ;
130
+
131
+ // act
132
+ hub . Unsubscribe < string > ( sender ) ;
133
+
134
+ // assert
135
+ Assert . IsFalse ( hub . handlers . Any ( a => a . Sender . Target == sender ) ) ;
136
+ }
137
+
138
+ [ TestMethod ]
139
+ public void Unsubscribe_RemovesSpecificHandler_ForSender ( )
140
+ {
141
+ var actionToDie = new Action < string > ( a => { } ) ;
142
+ hub . Subscribe ( sender , actionToDie ) ;
143
+ hub . Subscribe ( sender , new Action < string > ( a => { } ) ) ;
144
+ hub . Subscribe ( preservedSender , new Action < string > ( a => { } ) ) ;
145
+
146
+ // act
147
+ hub . Unsubscribe ( sender , actionToDie ) ;
148
+
149
+ // assert
150
+ Assert . IsFalse ( hub . handlers . Any ( a => a . Action . Equals ( actionToDie ) ) ) ;
151
+ }
152
+
153
+
154
+
155
+ [ TestMethod ]
156
+ public void Exists_EventDoesExist ( )
157
+ {
158
+ var action = new Action < string > ( a => { } ) ;
159
+
160
+ hub . Subscribe ( sender , action ) ;
161
+
162
+ Assert . IsTrue ( hub . Exists ( sender , action ) ) ;
163
+ }
164
+
165
+
166
+ [ TestMethod ]
167
+ public void Unsubscribe_CleanUps ( )
168
+ {
169
+ // arrange
170
+ var actionToDie = new Action < string > ( a => { } ) ;
171
+ hub . Subscribe ( sender , actionToDie ) ;
172
+ hub . Subscribe ( sender , new Action < string > ( a => { } ) ) ;
173
+ hub . Subscribe ( condemnedSender , new Action < string > ( a => { } ) ) ;
174
+
175
+ condemnedSender = null ;
176
+
177
+ GC . Collect ( ) ;
178
+
179
+ // act
180
+ hub . Unsubscribe < string > ( sender ) ;
181
+
182
+ // assert
183
+ Assert . AreEqual ( 0 , hub . handlers . Count ) ;
184
+ }
185
+
186
+
187
+
188
+ [ TestMethod ]
189
+ public void PubSubUnsubDirectlyToHub ( )
190
+ {
191
+ // arrange
192
+ var callCount = 0 ;
193
+ var action = new Action < Event > ( a => callCount ++ ) ;
194
+ var myhub = new Hub ( ) ;
195
+
196
+ // before change, this lies and subscribes to the static hub instead.
197
+ myhub . Subscribe ( new Action < Event > ( a => callCount ++ ) ) ;
198
+ myhub . Subscribe ( new Action < SpecialEvent > ( a => callCount ++ ) ) ;
199
+ myhub . Subscribe ( action ) ;
200
+
201
+ // act
202
+
203
+ // before change, this uses the static hub in the Extensions.
204
+ myhub . Publish ( new Event ( ) ) ;
205
+ // before change, this uses myhub which has no listeners.
206
+ myhub . Publish ( new SpecialEvent ( ) ) ;
207
+ // before change, this uses the static hub in the Extensions.
208
+ myhub . Publish < Event > ( ) ;
209
+
210
+ // assert
211
+ Assert . AreEqual ( 7 , callCount ) ;
212
+
213
+ // unsubscribe
214
+ // before change, this lies and unsubscribes from the static hub instead.
215
+ myhub . Unsubscribe < SpecialEvent > ( ) ;
216
+
217
+ // act
218
+ myhub . Publish ( new SpecialEvent ( ) ) ;
219
+
220
+ // assert
221
+ Assert . AreEqual ( 9 , callCount ) ;
222
+
223
+ // unsubscribe specific action
224
+ myhub . Unsubscribe ( action ) ;
225
+
226
+ // act
227
+ myhub . Publish ( new SpecialEvent ( ) ) ;
228
+
229
+ // assert
230
+ Assert . AreEqual ( 10 , callCount ) ;
231
+
232
+ // unsubscribe to all
233
+ myhub . Unsubscribe ( ) ;
234
+
235
+ // act
236
+ myhub . Publish ( new SpecialEvent ( ) ) ;
237
+
238
+ // assert
239
+ Assert . AreEqual ( 10 , callCount ) ;
240
+ }
241
+ }
242
+
243
+ public class Event
244
+ {
245
+ }
246
+
247
+ public class SpecialEvent : Event
248
+ {
249
+ }
250
+ }
0 commit comments