-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathScriptableEventTest.cs
172 lines (135 loc) · 4.99 KB
/
ScriptableEventTest.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
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.TestTools;
namespace CHARK.ScriptableEvents.Tests.Runtime
{
[TestFixtureSource(typeof(ScriptableEventTestSource))]
internal class ScriptableEventTest<
TScriptableEvent,
TScriptableEventListener,
TArg
>
where TScriptableEvent : ScriptableEvent<TArg>
where TScriptableEventListener : ScriptableEventListener<TArg>
{
#region Private Fields
private readonly TArg arg;
private List<TArg> capturedArgs;
private TScriptableEvent scriptableEvent;
private TScriptableEvent scriptableEvent2; // Not used in most tests.
private UnityEvent<TArg> unityEvent;
private TScriptableEventListener scriptableEventListener;
#endregion
#region Public Methods
public ScriptableEventTest(TArg arg)
{
this.arg = arg;
}
[SetUp]
public void SetUp()
{
SetupRaisedArgs();
SetupScriptableEvents();
SetupUnityEvent();
SetupScriptableEventListener();
}
[Test]
public void ShouldRaiseEvent()
{
scriptableEvent.Raise(arg);
Assert.AreEqual(1, capturedArgs.Count);
Assert.AreEqual(arg, capturedArgs.First());
}
[Test]
public void ShouldAddActionListenerAndRaiseEvent()
{
var capturedActionArgs = new List<TArg>();
scriptableEvent.AddListener(capturedActionArgs.Add);
scriptableEvent.Raise(arg);
Assert.AreEqual(1, capturedArgs.Count);
Assert.AreEqual(arg, capturedArgs.First());
}
[Test]
public void ShouldRaiseEventAndContinueListenerChain()
{
LogAssert.ignoreFailingMessages = true;
scriptableEvent.SetField("isSuppressExceptions", true);
scriptableEvent.AddListener(new MockScriptableEventListener<TArg>
{
Action = capturedArg => throw new Exception()
});
scriptableEvent.Raise(arg);
Assert.AreEqual(1, capturedArgs.Count);
}
[Test]
public void ShouldListenToMultipleEvents()
{
scriptableEvent.Raise(arg);
scriptableEvent2.Raise(arg);
Assert.AreEqual(2, capturedArgs.Count);
Assert.AreEqual(arg, capturedArgs[0]);
Assert.AreEqual(arg, capturedArgs[1]);
}
[Test]
public void ShouldRemoveListenerAndRaiseEvent()
{
scriptableEvent.RemoveListeners();
scriptableEvent.AddListener(scriptableEventListener);
scriptableEvent.RemoveListener(scriptableEventListener);
scriptableEvent.Raise(arg);
Assert.AreEqual(0, scriptableEvent.ListenerCount);
Assert.AreEqual(0, capturedArgs.Count);
}
[Test]
public void ShouldAddAndRemoveActionListenerAndRaiseEvent()
{
scriptableEvent.RemoveListeners();
scriptableEvent.AddListener(capturedArgs.Add);
scriptableEvent.RemoveListener(capturedArgs.Add);
scriptableEvent.Raise(arg);
Assert.AreEqual(0, scriptableEvent.ListenerCount);
Assert.AreEqual(0, capturedArgs.Count);
}
[Test]
public void ShouldRemoveListenersAndRaiseEvent()
{
scriptableEvent.RemoveListeners();
scriptableEvent.Raise(arg);
Assert.AreEqual(0, capturedArgs.Count);
}
#endregion
#region Private Methods
private void SetupRaisedArgs()
{
capturedArgs = new List<TArg>();
}
private void SetupScriptableEvents()
{
scriptableEvent = ScriptableObject.CreateInstance<TScriptableEvent>();
scriptableEvent2 = ScriptableObject.CreateInstance<TScriptableEvent>();
}
private void SetupUnityEvent()
{
unityEvent = new UnityEvent<TArg>();
unityEvent.AddListener(capturedArgs.Add);
}
private void SetupScriptableEventListener()
{
var gameObject = new GameObject();
gameObject.SetActive(false);
scriptableEventListener = gameObject.AddComponent<TScriptableEventListener>();
// Note: the type for scriptableEvents is NOT TScriptableEvent, it is BaseScriptableEvent<TArg>
// If treated as TScriptableEvent, casting fails.
scriptableEventListener.AddToListField<ScriptableEvent<TArg>>("scriptableEvents", scriptableEvent);
scriptableEventListener.AddToListField<ScriptableEvent<TArg>>("scriptableEvents", scriptableEvent2);
scriptableEventListener.SetField("onRaised", unityEvent);
// Add listener by triggering OnEnabled.
gameObject.SetActive(true);
}
#endregion
}
}