forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessengerUnitTest.cs
159 lines (127 loc) · 4.96 KB
/
MessengerUnitTest.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma warning disable 0168
// MessengerUnitTest.cs v1.0 by Magnus Wolffelt, magnus.wolffelt@gmail.com
//
// Some functionality testing of the classes in Messenger.cs.
// A lot of attention is paid to proper exception throwing from the Messenger.
using System;
namespace UnityLibrary
{
class MessengerUnitTest
{
private readonly string eventType1 = "__testEvent1";
private readonly string eventType2 = "__testEvent2";
bool wasCalled = false;
public void RunTest()
{
RunAddTests();
RunBroadcastTests();
RunRemoveTests();
Console.Out.WriteLine("All Messenger tests passed.");
}
private void RunAddTests()
{
Messenger.AddListener(eventType1, TestCallback);
try
{
// This should fail because we're adding a new event listener for same event type but a different delegate signature
Messenger<float>.AddListener(eventType1, TestCallbackFloat);
throw new Exception("Unit test failure - expected a ListenerException");
}
catch (MessengerInternal.ListenerException e)
{
// All good
}
Messenger<float>.AddListener(eventType2, TestCallbackFloat);
}
private void RunBroadcastTests()
{
wasCalled = false;
Messenger.Broadcast(eventType1);
if (!wasCalled) { throw new Exception("Unit test failure - event handler appears to have not been called."); }
wasCalled = false;
Messenger<float>.Broadcast(eventType2, 1.0f);
if (!wasCalled) { throw new Exception("Unit test failure - event handler appears to have not been called."); }
// No listener should exist for this event, but we don't require a listener so it should pass
Messenger<float>.Broadcast(eventType2 + "_", 1.0f, MessengerMode.DONT_REQUIRE_LISTENER);
try
{
// Broadcasting for an event there exists listeners for, but using wrong signature
Messenger<float>.Broadcast(eventType1, 1.0f, MessengerMode.DONT_REQUIRE_LISTENER);
throw new Exception("Unit test failure - expected a BroadcastException");
}
catch (MessengerInternal.BroadcastException e)
{
// All good
}
try
{
// Same thing, but now we (implicitly) require at least one listener
Messenger<float>.Broadcast(eventType2 + "_", 1.0f);
throw new Exception("Unit test failure - expected a BroadcastException");
}
catch (MessengerInternal.BroadcastException e)
{
// All good
}
try
{
// Wrong generic type for this broadcast, and we implicitly require a listener
Messenger<double>.Broadcast(eventType2, 1.0);
throw new Exception("Unit test failure - expected a BroadcastException");
}
catch (MessengerInternal.BroadcastException e)
{
// All good
}
}
private void RunRemoveTests()
{
try
{
// Removal with wrong signature should fail
Messenger<float>.RemoveListener(eventType1, TestCallbackFloat);
throw new Exception("Unit test failure - expected a ListenerException");
}
catch (MessengerInternal.ListenerException e)
{
// All good
}
Messenger.RemoveListener(eventType1, TestCallback);
try
{
// Repeated removal should fail
Messenger.RemoveListener(eventType1, TestCallback);
throw new Exception("Unit test failure - expected a ListenerException");
}
catch (MessengerInternal.ListenerException e)
{
// All good
}
Messenger<float>.RemoveListener(eventType2, TestCallbackFloat);
try
{
// Repeated removal should fail
Messenger<float>.RemoveListener(eventType2, TestCallbackFloat);
throw new Exception("Unit test failure - expected a ListenerException");
}
catch (MessengerInternal.ListenerException e)
{
// All good
}
}
void TestCallback()
{
wasCalled = true;
Console.Out.WriteLine("TestCallback() was called.");
}
void TestCallbackFloat(float f)
{
wasCalled = true;
Console.Out.WriteLine("TestCallbackFloat(float) was called.");
if (f != 1.0f)
{
throw new Exception("Unit test failure - wrong value on float argument");
}
}
}
}