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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
import me.bush.illnamethislater.Event
import me.bush.illnamethislater.EventBus
import me.bush.illnamethislater.listener
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.core.config.Configurator
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle
import kotlin.random.Random
/**
* I don't know how to do these....
*
* @author bush
* @since 1.0.0
*/
@TestInstance(Lifecycle.PER_CLASS)
class Test {
private lateinit var eventBus: EventBus
private val logger = LogManager.getLogger()
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@BeforeAll
fun setup() {
Configurator.setRootLevel(Level.ALL)
// Test that init works
logger.info("Initializing")
eventBus = EventBus()
// Ensure no npe
logger.info("Logging empty debug info")
eventBus.debugInfo()
// Test that basic subscribing reflection works
logger.info("Subscribing")
eventBus.subscribe(this)
logger.info("Testing Events")
}
@AfterAll
fun unsubscribe() {
logger.info("Unsubscribing")
eventBus.unsubscribe(this)
eventBus.debugInfo()
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests debug info format
@Test
fun debugInfoTest() {
eventBus.debugInfo()
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests autoboxing and internal data structure against primitives.
@Test
fun primitiveListenerTest() {
val random = Random.nextInt()
eventBus.post(random)
Assertions.assertEquals(random, primitiveTestValue)
}
private var primitiveTestValue = 0
val primitiveListener = listener<Int> {
primitiveTestValue = it
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests unsubscribing of listeners which don't belong to a subscriber.
@Test
fun freeListenerTest() {
// Register listener and keep the value
val listener = eventBus.register(listener<String> {
freeListenerTestValue = it
})
val valueOne = "i love bush's eventbus <3"
val valueTwo = "sdklasdjsakdsadlksadlksdl"
// Will change the value
eventBus.post(valueOne)
Assertions.assertEquals(valueOne, freeListenerTestValue)
// Remove the listener
eventBus.unregister(listener)
// No effect
eventBus.post(valueTwo)
// Value will not change
Assertions.assertEquals(valueOne, freeListenerTestValue)
}
private var freeListenerTestValue: String? = null
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests priority and receiveCancelled functionality.
@Test
fun myEventListenerTest() {
val event = MyEvent()
eventBus.post(event)
Assertions.assertEquals(event.lastListener, "myEventListener3")
}
// First to be called; highest priority.
val myEventListener0 = listener<MyEvent>(priority = 10) {
Assertions.assertEquals(it.lastListener, "")
it.lastListener = "myEventListener0"
it.cancel()
}
// Will not be called; second-highest priority, no receiveCancelled.
val myEventListener1
get() = listener<MyEvent>(priority = 0) {
Assertions.assertTrue(false)
}
// Second to be called; has receiveCancelled and can un-cancel the event.
fun myEventListener2() = listener<MyEvent>(priority = Int.MIN_VALUE + 100, receiveCancelled = true) {
Assertions.assertEquals(it.lastListener, "myEventListener0")
it.lastListener = "myEventListener2"
it.cancelled = false
}
// Last to be called; does not have receiveCancelled, but the last listener un-cancelled the event.
fun myEventListener3() = listener<MyEvent>(priority = Int.MIN_VALUE) {
Assertions.assertEquals(it.lastListener, "myEventListener2")
it.lastListener = "myEventListener3"
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests external event cancel state functionality.
@Test
fun externalEventListenerTest() {
var unchanged = "this will not change"
// Cancels the event
eventBus.register(listener<ExternalEvent>(200) {
it.canceled = true
})
// This shouldn't be called
eventBus.register(listener<ExternalEvent>(100) {
unchanged = "changed"
})
eventBus.post(ExternalEvent())
Assertions.assertEquals(unchanged, "this will not change")
// Tests that duplicates are detected, and that both
// "canceled" and "cancelled" are detected as valid fields
eventBus.register(listener<ExternalDuplicates> {})
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests parallel invocation functionality.
@Test
fun parallelListenerTest() {
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests reflection against singleton object classes.
@Test
fun objectSubscriberTest() {
eventBus.subscribe(ObjectSubscriber)
eventBus.post(Unit)
Assertions.assertTrue(ObjectSubscriber.willChange)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// todo test thread safety
// todo ensure boolean functions return proper value (subscribe, unsubscribe, etc)
}
object ObjectSubscriber {
var willChange = false
fun listener() = listener<Unit> {
willChange = true
}
}
class MyEvent : Event() {
override val cancellable = true
var lastListener = ""
var someString = "donda"
}
class ExternalEvent {
var canceled = false
}
// Should give us a warning about duplicates
class ExternalDuplicates {
var canceled = false
var cancelled = false
}
|