Skip to content

Commit 83f9b25

Browse files
authored
Merge pull request #16 from Bebu1985/master
Refactoring Cleanups and Interfaces for usage with Ioc Container
2 parents affd651 + 4612cad commit 83f9b25

File tree

15 files changed

+748
-552
lines changed

15 files changed

+748
-552
lines changed

PubSub.Tests/CoreHubTests.cs

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
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+
}

PubSub.Tests/ExtensionTests.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using PubSub.Extension;
4+
5+
namespace PubSub.Tests
6+
{
7+
[TestClass]
8+
public class ExtensionTests
9+
{
10+
[TestMethod]
11+
public void Exists_Static()
12+
{
13+
// arrange
14+
var action = new Action<string>(a => { });
15+
this.Subscribe(action);
16+
17+
// act
18+
var exists = this.Exists<string>();
19+
20+
// assert
21+
Assert.IsTrue(exists);
22+
23+
this.Unsubscribe(action);
24+
}
25+
26+
[TestMethod]
27+
public void NotExists_Static()
28+
{
29+
// arrange
30+
var action = new Action<bool>(a => { });
31+
this.Subscribe(action);
32+
33+
// act
34+
var exists = this.Exists<string>();
35+
36+
// assert
37+
Assert.IsFalse(exists);
38+
39+
this.Unsubscribe(action);
40+
}
41+
42+
[TestMethod]
43+
public void PublishExtensions()
44+
{
45+
// arrange
46+
var callCount = 0;
47+
48+
this.Subscribe(new Action<Event>(a => callCount++));
49+
this.Subscribe(new Action<Event>(a => callCount++));
50+
51+
// act
52+
this.Publish(new Event());
53+
this.Publish(new SpecialEvent());
54+
this.Publish<Event>();
55+
56+
// assert
57+
Assert.AreEqual(6, callCount);
58+
}
59+
60+
[TestMethod]
61+
public void UnsubscribeExtensions()
62+
{
63+
// arrange
64+
var callCount = 0;
65+
var action = new Action<Event>(a => callCount++);
66+
67+
this.Subscribe(new Action<Event>(a => callCount++));
68+
this.Subscribe(new Action<SpecialEvent>(a => callCount++));
69+
this.Subscribe(action);
70+
71+
// act
72+
this.Publish(new Event());
73+
this.Publish(new SpecialEvent());
74+
this.Publish<Event>();
75+
76+
// assert
77+
Assert.AreEqual(7, callCount);
78+
79+
// unsubscribe
80+
this.Unsubscribe<SpecialEvent>();
81+
82+
// act
83+
this.Publish<SpecialEvent>();
84+
85+
// assert
86+
Assert.AreEqual(9, callCount);
87+
88+
// unsubscribe specific action
89+
this.Unsubscribe(action);
90+
91+
// act
92+
this.Publish<SpecialEvent>();
93+
94+
// assert
95+
Assert.AreEqual(10, callCount);
96+
97+
// unsubscribe from all
98+
this.Unsubscribe();
99+
100+
// act
101+
this.Publish<SpecialEvent>();
102+
103+
// assert
104+
Assert.AreEqual(10, callCount);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)