Browse Source

Convert event tests to use mockk

tags/v0.10.0
Chris Smith 5 years ago
parent
commit
a1aad4dbc7

+ 35
- 19
src/test/kotlin/com/dmdirc/ktirc/events/EventUtilsTest.kt View File

@@ -5,24 +5,24 @@ import com.dmdirc.ktirc.TestConstants
5 5
 import com.dmdirc.ktirc.io.CaseMapping
6 6
 import com.dmdirc.ktirc.messages.tagMap
7 7
 import com.dmdirc.ktirc.model.*
8
-import com.nhaarman.mockitokotlin2.doReturn
9
-import com.nhaarman.mockitokotlin2.mock
10
-import com.nhaarman.mockitokotlin2.verify
8
+import io.mockk.every
9
+import io.mockk.mockk
10
+import io.mockk.verify
11 11
 import org.junit.jupiter.api.Assertions.assertEquals
12 12
 import org.junit.jupiter.api.BeforeEach
13 13
 import org.junit.jupiter.api.Test
14 14
 
15 15
 internal class EventUtilsTest {
16 16
 
17
-    private val serverState = ServerState("", "")
18
-    private val ircClient = mock<IrcClient> {
19
-        on { serverState } doReturn serverState
20
-        on { caseMapping } doReturn CaseMapping.Ascii
17
+    private val fakeServerState = ServerState("", "")
18
+    private val ircClient = mockk<IrcClient> {
19
+        every { serverState } returns fakeServerState
20
+        every { caseMapping } returns CaseMapping.Ascii
21 21
     }
22 22
 
23 23
     @BeforeEach
24 24
     fun setUp() {
25
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
25
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
26 26
     }
27 27
 
28 28
     @Test
@@ -66,11 +66,13 @@ internal class EventUtilsTest {
66 66
 
67 67
     @Test
68 68
     fun `reply sends response to user when message is private`() {
69
-        serverState.localNickname = "zeroCool"
69
+        fakeServerState.localNickname = "zeroCool"
70 70
         val message = MessageReceived(EventMetadata(TestConstants.time), User("acidBurn"), "Zerocool", "Hack the planet!")
71 71
 
72 72
         ircClient.reply(message, "OK")
73
-        verify(ircClient).send(tagMap(), "PRIVMSG", "acidBurn", "OK")
73
+        verify {
74
+            ircClient.send(tagMap(), "PRIVMSG", "acidBurn", "OK")
75
+        }
74 76
     }
75 77
 
76 78
     @Test
@@ -78,7 +80,9 @@ internal class EventUtilsTest {
78 80
         val message = MessageReceived(EventMetadata(TestConstants.time), User("acidBurn"), "#TheGibson", "Hack the planet!")
79 81
 
80 82
         ircClient.reply(message, "OK")
81
-        verify(ircClient).send(tagMap(), "PRIVMSG", "#TheGibson", "OK")
83
+        verify {
84
+            ircClient.send(tagMap(), "PRIVMSG", "#TheGibson", "OK")
85
+        }
82 86
     }
83 87
 
84 88
     @Test
@@ -86,16 +90,20 @@ internal class EventUtilsTest {
86 90
         val message = MessageReceived(EventMetadata(TestConstants.time), User("acidBurn"), "#TheGibson", "Hack the planet!")
87 91
 
88 92
         ircClient.reply(message, "OK", prefixWithNickname = true)
89
-        verify(ircClient).send(tagMap(), "PRIVMSG", "#TheGibson", "acidBurn: OK")
93
+        verify {
94
+            ircClient.send(tagMap(), "PRIVMSG", "#TheGibson", "acidBurn: OK")
95
+        }
90 96
     }
91 97
 
92 98
     @Test
93 99
     fun `reply sends response with message ID to user when message is private`() {
94
-        serverState.localNickname = "zeroCool"
100
+        fakeServerState.localNickname = "zeroCool"
95 101
         val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "abc123"), User("acidBurn"), "Zerocool", "Hack the planet!")
96 102
 
97 103
         ircClient.reply(message, "OK")
98
-        verify(ircClient).send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "acidBurn", "OK")
104
+        verify {
105
+            ircClient.send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "acidBurn", "OK")
106
+        }
99 107
     }
100 108
 
101 109
     @Test
@@ -103,7 +111,9 @@ internal class EventUtilsTest {
103 111
         val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "abc123"), User("acidBurn"), "#TheGibson", "Hack the planet!")
104 112
 
105 113
         ircClient.reply(message, "OK")
106
-        verify(ircClient).send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "#TheGibson", "OK")
114
+        verify {
115
+            ircClient.send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "#TheGibson", "OK")
116
+        }
107 117
     }
108 118
 
109 119
     @Test
@@ -111,17 +121,21 @@ internal class EventUtilsTest {
111 121
         val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "abc123"), User("acidBurn"), "#TheGibson", "Hack the planet!")
112 122
 
113 123
         ircClient.reply(message, "OK", prefixWithNickname = true)
114
-        verify(ircClient).send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "#TheGibson", "acidBurn: OK")
124
+        verify {
125
+            ircClient.send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "#TheGibson", "acidBurn: OK")
126
+        }
115 127
     }
116 128
 
117 129
 
118 130
     @Test
119 131
     fun `react sends response to user when message is private`() {
120
-        serverState.localNickname = "zeroCool"
132
+        fakeServerState.localNickname = "zeroCool"
121 133
         val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "msgId"), User("acidBurn"), "Zerocool", "Hack the planet!")
122 134
 
123 135
         ircClient.react(message, ":P")
124
-        verify(ircClient).send(tagMap(MessageTag.React to ":P", MessageTag.Reply to "msgId"), "TAGMSG", "acidBurn")
136
+        verify {
137
+            ircClient.send(tagMap(MessageTag.React to ":P", MessageTag.Reply to "msgId"), "TAGMSG", "acidBurn")
138
+        }
125 139
     }
126 140
 
127 141
     @Test
@@ -129,7 +143,9 @@ internal class EventUtilsTest {
129 143
         val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "msgId"), User("acidBurn"), "#TheGibson", "Hack the planet!")
130 144
 
131 145
         ircClient.react(message, ":P")
132
-        verify(ircClient).send(tagMap(MessageTag.React to ":P", MessageTag.Reply to "msgId"), "TAGMSG", "#TheGibson")
146
+        verify {
147
+            ircClient.send(tagMap(MessageTag.React to ":P", MessageTag.Reply to "msgId"), "TAGMSG", "#TheGibson")
148
+        }
133 149
     }
134 150
 
135 151
 }

+ 107
- 79
src/test/kotlin/com/dmdirc/ktirc/events/handlers/CapabilitiesHandlerTest.kt View File

@@ -9,31 +9,31 @@ import com.dmdirc.ktirc.model.ServerState
9 9
 import com.dmdirc.ktirc.sasl.SaslMechanism
10 10
 import com.dmdirc.ktirc.sasl.fromBase64
11 11
 import com.dmdirc.ktirc.sasl.toBase64
12
-import com.nhaarman.mockitokotlin2.*
12
+import io.mockk.*
13 13
 import org.junit.jupiter.api.Assertions.*
14 14
 import org.junit.jupiter.api.Test
15 15
 
16 16
 internal class CapabilitiesHandlerTest {
17 17
 
18
-    private val saslMech1 = mock<SaslMechanism> {
19
-        on { priority } doReturn 1
20
-        on { ircName } doReturn "mech1"
18
+    private val saslMech1 = mockk<SaslMechanism> {
19
+        every { priority } returns 1
20
+        every { ircName } returns "mech1"
21 21
     }
22 22
 
23
-    private val saslMech2 = mock<SaslMechanism> {
24
-        on { priority } doReturn 2
25
-        on { ircName } doReturn "mech2"
23
+    private val saslMech2 = mockk<SaslMechanism> {
24
+        every { priority } returns 2
25
+        every { ircName } returns "mech2"
26 26
     }
27 27
 
28
-    private val saslMech3 = mock<SaslMechanism> {
29
-        on { priority } doReturn 3
30
-        on { ircName } doReturn "mech3"
28
+    private val saslMech3 = mockk<SaslMechanism> {
29
+        every { priority } returns 3
30
+        every { ircName } returns "mech3"
31 31
     }
32 32
 
33 33
     private val handler = CapabilitiesHandler()
34
-    private val serverState = ServerState("", "", null)
35
-    private val ircClient = mock<IrcClient> {
36
-        on { serverState } doReturn serverState
34
+    private val fakeServerState = ServerState("", "", null)
35
+    private val ircClient = mockk<IrcClient> {
36
+        every { serverState } returns fakeServerState
37 37
     }
38 38
 
39 39
     @Test
@@ -43,42 +43,46 @@ internal class CapabilitiesHandlerTest {
43 43
                 Capability.HostsInNamesReply.names[0] to "123"
44 44
         )))
45 45
 
46
-        assertEquals(2, serverState.capabilities.advertisedCapabilities.size)
47
-        assertEquals("", serverState.capabilities.advertisedCapabilities[Capability.EchoMessages.names[0]])
48
-        assertEquals("123", serverState.capabilities.advertisedCapabilities[Capability.HostsInNamesReply.names[0]])
46
+        assertEquals(2, fakeServerState.capabilities.advertisedCapabilities.size)
47
+        assertEquals("", fakeServerState.capabilities.advertisedCapabilities[Capability.EchoMessages.names[0]])
48
+        assertEquals("123", fakeServerState.capabilities.advertisedCapabilities[Capability.HostsInNamesReply.names[0]])
49 49
     }
50 50
 
51 51
     @Test
52 52
     fun `updates negotiation state when capabilities finished`() {
53
-        serverState.capabilities.advertisedCapabilities[Capability.EchoMessages.names[0]] = ""
53
+        fakeServerState.capabilities.advertisedCapabilities[Capability.EchoMessages.names[0]] = ""
54 54
 
55 55
         handler.processEvent(ircClient, ServerCapabilitiesFinished(EventMetadata(TestConstants.time)))
56 56
 
57
-        assertEquals(CapabilitiesNegotiationState.AWAITING_ACK, serverState.capabilities.negotiationState)
57
+        assertEquals(CapabilitiesNegotiationState.AWAITING_ACK, fakeServerState.capabilities.negotiationState)
58 58
     }
59 59
 
60 60
     @Test
61 61
     fun `sends REQ when capabilities received`() {
62
-        serverState.capabilities.advertisedCapabilities[Capability.EchoMessages.names[0]] = ""
63
-        serverState.capabilities.advertisedCapabilities[Capability.AccountChangeMessages.names[0]] = ""
62
+        fakeServerState.capabilities.advertisedCapabilities[Capability.EchoMessages.names[0]] = ""
63
+        fakeServerState.capabilities.advertisedCapabilities[Capability.AccountChangeMessages.names[0]] = ""
64 64
 
65 65
         handler.processEvent(ircClient, ServerCapabilitiesFinished(EventMetadata(TestConstants.time)))
66 66
 
67
-        verify(ircClient).send(eq("CAP"), eq("REQ"), argThat { equals("echo-message account-notify") || equals("account-notify echo-message") })
67
+        verify {
68
+            ircClient.send(eq("CAP"), eq("REQ"), or(eq("echo-message account-notify"), eq("account-notify echo-message")))
69
+        }
68 70
     }
69 71
 
70 72
     @Test
71 73
     fun `sends END when blank capabilities received`() {
72 74
         handler.processEvent(ircClient, ServerCapabilitiesFinished(EventMetadata(TestConstants.time)))
73 75
 
74
-        verify(ircClient).send("CAP", "END")
76
+        verify {
77
+            ircClient.send("CAP", "END")
78
+        }
75 79
     }
76 80
 
77 81
     @Test
78 82
     fun `updates negotiation when blank capabilities received`() {
79 83
         handler.processEvent(ircClient, ServerCapabilitiesFinished(EventMetadata(TestConstants.time)))
80 84
 
81
-        assertEquals(CapabilitiesNegotiationState.FINISHED, serverState.capabilities.negotiationState)
85
+        assertEquals(CapabilitiesNegotiationState.FINISHED, fakeServerState.capabilities.negotiationState)
82 86
     }
83 87
 
84 88
     @Test
@@ -88,90 +92,100 @@ internal class CapabilitiesHandlerTest {
88 92
                 Capability.HostsInNamesReply.names[0] to ""
89 93
         )))
90 94
 
91
-        verify(ircClient).send("CAP", "END")
95
+        verify {
96
+            ircClient.send("CAP", "END")
97
+        }
92 98
     }
93 99
 
94 100
     @Test
95 101
     fun `sends END when capabilities acknowledged and no sasl state`() {
96
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
102
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
97 103
         handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(EventMetadata(TestConstants.time), hashMapOf(
98 104
                 Capability.EchoMessages.names[0] to "",
99 105
                 Capability.HostsInNamesReply.names[0] to ""
100 106
         )))
101 107
 
102
-        verify(ircClient).send("CAP", "END")
108
+        verify {
109
+            ircClient.send("CAP", "END")
110
+        }
103 111
     }
104 112
 
105 113
     @Test
106 114
     fun `sends END when capabilities acknowledged and no shared mechanism`() {
107
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
108
-        serverState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "fake1,fake2"
115
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
116
+        fakeServerState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "fake1,fake2"
109 117
         handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(EventMetadata(TestConstants.time), hashMapOf(
110 118
                 Capability.SaslAuthentication.names[0] to "",
111 119
                 Capability.HostsInNamesReply.names[0] to ""
112 120
         )))
113 121
 
114
-        verify(ircClient).send("CAP", "END")
122
+        verify {
123
+            ircClient.send("CAP", "END")
124
+        }
115 125
     }
116 126
 
117 127
     @Test
118 128
     fun `sets current SASL mechanism when capabilities acknowledged with shared mechanism`() {
119
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
120
-        serverState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "mech1,fake2"
129
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
130
+        fakeServerState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "mech1,fake2"
121 131
         handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(EventMetadata(TestConstants.time), hashMapOf(
122 132
                 Capability.SaslAuthentication.names[0] to "",
123 133
                 Capability.HostsInNamesReply.names[0] to ""
124 134
         )))
125 135
 
126
-        assertSame(saslMech1, serverState.sasl.currentMechanism)
136
+        assertSame(saslMech1, fakeServerState.sasl.currentMechanism)
127 137
     }
128 138
 
129 139
     @Test
130 140
     fun `sets current SASL mechanism when capabilities acknowledged with no declared mechanisms`() {
131
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
132
-        serverState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = ""
141
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
142
+        fakeServerState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = ""
133 143
         handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(EventMetadata(TestConstants.time), hashMapOf(
134 144
                 Capability.SaslAuthentication.names[0] to "",
135 145
                 Capability.HostsInNamesReply.names[0] to ""
136 146
         )))
137 147
 
138
-        assertSame(saslMech3, serverState.sasl.currentMechanism)
148
+        assertSame(saslMech3, fakeServerState.sasl.currentMechanism)
139 149
     }
140 150
 
141 151
     @Test
142 152
     fun `sends authenticate when capabilities acknowledged with shared mechanism`() {
143
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
144
-        serverState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "mech1,fake2"
153
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
154
+        fakeServerState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "mech1,fake2"
145 155
         handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(EventMetadata(TestConstants.time), hashMapOf(
146 156
                 Capability.SaslAuthentication.names[0] to "",
147 157
                 Capability.HostsInNamesReply.names[0] to ""
148 158
         )))
149 159
 
150
-        verify(ircClient).send("AUTHENTICATE", "mech1")
160
+        verify {
161
+            ircClient.send("AUTHENTICATE", "mech1")
162
+        }
151 163
     }
152 164
 
153 165
     @Test
154 166
     fun `sends authenticate when capabilities acknowledged with no declared mechanisms`() {
155
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
156
-        serverState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = ""
167
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
168
+        fakeServerState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = ""
157 169
         handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(EventMetadata(TestConstants.time), hashMapOf(
158 170
                 Capability.SaslAuthentication.names[0] to "",
159 171
                 Capability.HostsInNamesReply.names[0] to ""
160 172
         )))
161 173
 
162
-        verify(ircClient).send("AUTHENTICATE", "mech3")
174
+        verify {
175
+            ircClient.send("AUTHENTICATE", "mech3")
176
+        }
163 177
     }
164 178
 
165 179
     @Test
166 180
     fun `updates negotiation state when capabilities acknowledged with shared mechanism`() {
167
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
168
-        serverState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "mech1,fake2"
181
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
182
+        fakeServerState.capabilities.advertisedCapabilities[Capability.SaslAuthentication.names[0]] = "mech1,fake2"
169 183
         handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(EventMetadata(TestConstants.time), hashMapOf(
170 184
                 Capability.SaslAuthentication.names[0] to "",
171 185
                 Capability.HostsInNamesReply.names[0] to ""
172 186
         )))
173 187
 
174
-        assertEquals(CapabilitiesNegotiationState.AUTHENTICATING, serverState.capabilities.negotiationState)
188
+        assertEquals(CapabilitiesNegotiationState.AUTHENTICATING, fakeServerState.capabilities.negotiationState)
175 189
     }
176 190
 
177 191
     @Test
@@ -181,7 +195,7 @@ internal class CapabilitiesHandlerTest {
181 195
                 Capability.HostsInNamesReply.names[0] to "123"
182 196
         )))
183 197
 
184
-        assertEquals(CapabilitiesNegotiationState.FINISHED, serverState.capabilities.negotiationState)
198
+        assertEquals(CapabilitiesNegotiationState.FINISHED, fakeServerState.capabilities.negotiationState)
185 199
     }
186 200
 
187 201
     @Test
@@ -191,95 +205,105 @@ internal class CapabilitiesHandlerTest {
191 205
                 Capability.HostsInNamesReply.names[0] to "123"
192 206
         )))
193 207
 
194
-        assertEquals(2, serverState.capabilities.enabledCapabilities.size)
195
-        assertEquals("", serverState.capabilities.enabledCapabilities[Capability.EchoMessages])
196
-        assertEquals("123", serverState.capabilities.enabledCapabilities[Capability.HostsInNamesReply])
208
+        assertEquals(2, fakeServerState.capabilities.enabledCapabilities.size)
209
+        assertEquals("", fakeServerState.capabilities.enabledCapabilities[Capability.EchoMessages])
210
+        assertEquals("123", fakeServerState.capabilities.enabledCapabilities[Capability.HostsInNamesReply])
197 211
     }
198 212
 
199 213
     @Test
200 214
     fun `aborts authentication attempt if not expecting one`() {
201
-        serverState.sasl.currentMechanism = null
215
+        fakeServerState.sasl.currentMechanism = null
202 216
         handler.processEvent(ircClient, AuthenticationMessage(EventMetadata(TestConstants.time), "+"))
203 217
 
204
-        verify(ircClient).send("AUTHENTICATE", "*")
218
+        verify {
219
+            ircClient.send("AUTHENTICATE", "*")
220
+        }
205 221
     }
206 222
 
207 223
     @Test
208 224
     fun `passes authentication message to mechanism if in auth process`() {
209
-        serverState.sasl.currentMechanism = saslMech1
225
+        fakeServerState.sasl.currentMechanism = saslMech1
210 226
 
211 227
         val argument = "ABC"
212 228
         handler.processEvent(ircClient, AuthenticationMessage(EventMetadata(TestConstants.time), argument))
213 229
 
214
-        verify(saslMech1).handleAuthenticationEvent(ircClient, argument.fromBase64())
230
+        verify {
231
+            saslMech1.handleAuthenticationEvent(ircClient, argument.fromBase64())
232
+        }
215 233
     }
216 234
 
217 235
     @Test
218 236
     fun `stores partial authentication message if it's 400 bytes long`() {
219
-        serverState.sasl.currentMechanism = saslMech1
237
+        fakeServerState.sasl.currentMechanism = saslMech1
220 238
 
221 239
         val argument = "A".repeat(400)
222 240
         handler.processEvent(ircClient, AuthenticationMessage(EventMetadata(TestConstants.time), argument))
223 241
 
224
-        assertEquals(argument, serverState.sasl.saslBuffer)
225
-        verify(saslMech1, never()).handleAuthenticationEvent(any(), any())
242
+        assertEquals(argument, fakeServerState.sasl.saslBuffer)
243
+        verify(inverse = true) {
244
+            saslMech1.handleAuthenticationEvent(any(), any())
245
+        }
226 246
     }
227 247
 
228 248
     @Test
229 249
     fun `appends authentication messages if it's 400 bytes long and data already exists`() {
230
-        serverState.sasl.currentMechanism = saslMech1
250
+        fakeServerState.sasl.currentMechanism = saslMech1
231 251
 
232
-        serverState.sasl.saslBuffer = "A".repeat(400)
252
+        fakeServerState.sasl.saslBuffer = "A".repeat(400)
233 253
         handler.processEvent(ircClient, AuthenticationMessage(EventMetadata(TestConstants.time), "B".repeat(400)))
234 254
 
235
-        assertEquals("A".repeat(400) + "B".repeat(400), serverState.sasl.saslBuffer)
236
-        verify(saslMech1, never()).handleAuthenticationEvent(any(), any())
255
+        assertEquals("A".repeat(400) + "B".repeat(400), fakeServerState.sasl.saslBuffer)
256
+        verify(inverse = true) {
257
+            saslMech1.handleAuthenticationEvent(any(), any())
258
+        }
237 259
     }
238 260
 
239 261
     @Test
240 262
     fun `reconstructs partial authentication message to mechanism if data stored and partial received`() {
241
-        serverState.sasl.currentMechanism = saslMech1
263
+        fakeServerState.sasl.currentMechanism = saslMech1
264
+        fakeServerState.sasl.saslBuffer = "A".repeat(400)
242 265
 
243
-        serverState.sasl.saslBuffer = "A".repeat(400)
266
+        val slot = slot<ByteArray>()
267
+        every { saslMech1.handleAuthenticationEvent(eq(ircClient), capture(slot)) } just Runs
244 268
 
245 269
         val argument = "ABCD"
246 270
         handler.processEvent(ircClient, AuthenticationMessage(EventMetadata(TestConstants.time), argument))
247 271
 
248
-        val captor = argumentCaptor<ByteArray>()
249
-        verify(saslMech1).handleAuthenticationEvent(same(ircClient), captor.capture())
250
-        assertEquals("A".repeat(400) + "ABCD", captor.firstValue.toBase64())
272
+        assertEquals("A".repeat(400) + "ABCD", slot.captured.toBase64())
251 273
     }
252 274
 
253 275
     @Test
254 276
     fun `reconstructs partial authentication message to mechanism if data stored and null received`() {
255
-        serverState.sasl.currentMechanism = saslMech1
277
+        fakeServerState.sasl.currentMechanism = saslMech1
278
+        fakeServerState.sasl.saslBuffer = "A".repeat(400)
256 279
 
257
-        serverState.sasl.saslBuffer = "A".repeat(400)
280
+        val slot = slot<ByteArray>()
281
+        every { saslMech1.handleAuthenticationEvent(eq(ircClient), capture(slot)) } just Runs
258 282
 
259 283
         handler.processEvent(ircClient, AuthenticationMessage(EventMetadata(TestConstants.time), null))
260 284
 
261
-        val captor = argumentCaptor<ByteArray>()
262
-        verify(saslMech1).handleAuthenticationEvent(same(ircClient), captor.capture())
263
-        assertEquals("A".repeat(400), captor.firstValue.toBase64())
285
+        assertEquals("A".repeat(400), slot.captured.toBase64())
264 286
     }
265 287
 
266 288
     @Test
267 289
     fun `sends END when SASL auth finished`() {
268 290
         handler.processEvent(ircClient, SaslFinished(EventMetadata(TestConstants.time), true))
269 291
 
270
-        verify(ircClient).send("CAP", "END")
292
+        verify {
293
+            ircClient.send("CAP", "END")
294
+        }
271 295
     }
272 296
 
273 297
     @Test
274 298
     fun `sets negotiation state when SASL auth finished`() {
275 299
         handler.processEvent(ircClient, SaslFinished(EventMetadata(TestConstants.time), true))
276 300
 
277
-        assertEquals(CapabilitiesNegotiationState.FINISHED, serverState.capabilities.negotiationState)
301
+        assertEquals(CapabilitiesNegotiationState.FINISHED, fakeServerState.capabilities.negotiationState)
278 302
     }
279 303
 
280 304
     @Test
281 305
     fun `resets SASL state when SASL auth finished`() {
282
-        with (serverState.sasl) {
306
+        with (fakeServerState.sasl) {
283 307
             currentMechanism = saslMech1
284 308
             saslBuffer = "HackThePlanet"
285 309
             mechanismState = "root@thegibson"
@@ -287,7 +311,7 @@ internal class CapabilitiesHandlerTest {
287 311
 
288 312
         handler.processEvent(ircClient, SaslFinished(EventMetadata(TestConstants.time), true))
289 313
 
290
-        with (serverState.sasl) {
314
+        with (fakeServerState.sasl) {
291 315
             assertNull(currentMechanism)
292 316
             assertEquals("", saslBuffer)
293 317
             assertNull(mechanismState)
@@ -296,26 +320,30 @@ internal class CapabilitiesHandlerTest {
296 320
 
297 321
     @Test
298 322
     fun `sends a new authenticate request when sasl mechanism rejected and new one is acceptable`() {
299
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
323
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
300 324
         handler.processEvent(ircClient, SaslMechanismNotAvailableError(EventMetadata(TestConstants.time), listOf("mech1", "fake2")))
301 325
 
302
-        verify(ircClient).send("AUTHENTICATE", "mech1")
326
+        verify {
327
+            ircClient.send("AUTHENTICATE", "mech1")
328
+        }
303 329
     }
304 330
 
305 331
     @Test
306 332
     fun `sends cap end when sasl mechanism rejected and no new one is acceptable`() {
307
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
333
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
308 334
         handler.processEvent(ircClient, SaslMechanismNotAvailableError(EventMetadata(TestConstants.time), listOf("fake1", "fake2")))
309 335
 
310
-        verify(ircClient).send("CAP", "END")
336
+        verify {
337
+            ircClient.send("CAP", "END")
338
+        }
311 339
     }
312 340
 
313 341
     @Test
314 342
     fun `sets negotiation state when sasl mechanism rejected and no new one is acceptable`() {
315
-        serverState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
343
+        fakeServerState.sasl.mechanisms.addAll(listOf(saslMech1, saslMech2, saslMech3))
316 344
         handler.processEvent(ircClient, SaslMechanismNotAvailableError(EventMetadata(TestConstants.time), listOf("fake1", "fake2")))
317 345
 
318
-        assertEquals(CapabilitiesNegotiationState.FINISHED, serverState.capabilities.negotiationState)
346
+        assertEquals(CapabilitiesNegotiationState.FINISHED, fakeServerState.capabilities.negotiationState)
319 347
     }
320 348
 
321 349
 }

+ 84
- 79
src/test/kotlin/com/dmdirc/ktirc/events/handlers/ChannelStateHandlerTest.kt View File

@@ -6,46 +6,45 @@ import com.dmdirc.ktirc.TestConstants
6 6
 import com.dmdirc.ktirc.events.*
7 7
 import com.dmdirc.ktirc.io.CaseMapping
8 8
 import com.dmdirc.ktirc.model.*
9
-import com.nhaarman.mockitokotlin2.doReturn
10
-import com.nhaarman.mockitokotlin2.mock
11
-import com.nhaarman.mockitokotlin2.never
12
-import com.nhaarman.mockitokotlin2.verify
9
+import io.mockk.every
10
+import io.mockk.mockk
11
+import io.mockk.verify
13 12
 import org.junit.jupiter.api.Assertions.*
14 13
 import org.junit.jupiter.api.Test
15 14
 
16 15
 internal class ChannelStateHandlerTest {
17 16
 
18 17
     private val handler = ChannelStateHandler()
19
-    private val channelStateMap = ChannelStateMap { CaseMapping.Rfc }
20
-    private val serverState = ServerState("", "")
21
-    private val behaviour = BehaviourConfig()
22
-    private val ircClient = mock<IrcClient> {
23
-        on { serverState } doReturn serverState
24
-        on { channelState } doReturn channelStateMap
25
-        on { behaviour } doReturn behaviour
26
-        on { isLocalUser(User("acidburn", "libby", "root.localhost")) } doReturn true
27
-        on { isLocalUser("acidburn") } doReturn  true
18
+    private val fakeChannelState = ChannelStateMap { CaseMapping.Rfc }
19
+    private val fakeServerState = ServerState("", "")
20
+    private val behaviourConfig = BehaviourConfig()
21
+    private val ircClient = mockk<IrcClient> {
22
+        every { serverState } returns fakeServerState
23
+        every { channelState } returns fakeChannelState
24
+        every { behaviour } returns behaviourConfig
25
+        every { isLocalUser(any<User>()) } answers { arg<User>(0) == User("acidburn", "libby", "root.localhost") }
26
+        every { isLocalUser(any<String>()) } answers { arg<String>(0) == "acidburn" }
28 27
     }
29 28
 
30 29
     @Test
31 30
     fun `creates new state object for local joins`() {
32 31
         handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson"))
33
-        assertTrue("#thegibson" in channelStateMap)
32
+        assertTrue("#thegibson" in fakeChannelState)
34 33
     }
35 34
 
36 35
     @Test
37 36
     fun `does not create new state object for remote joins`() {
38 37
         handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
39
-        assertFalse("#thegibson" in channelStateMap)
38
+        assertFalse("#thegibson" in fakeChannelState)
40 39
     }
41 40
 
42 41
     @Test
43 42
     fun `adds joiners to channel state`() {
44
-        channelStateMap += ChannelState("#thegibson") { CaseMapping.Rfc }
43
+        fakeChannelState += ChannelState("#thegibson") { CaseMapping.Rfc }
45 44
 
46 45
         handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
47 46
 
48
-        assertTrue("zerocool" in channelStateMap["#thegibson"]?.users!!)
47
+        assertTrue("zerocool" in fakeChannelState["#thegibson"]?.users!!)
49 48
     }
50 49
 
51 50
     @Test
@@ -53,7 +52,7 @@ internal class ChannelStateHandlerTest {
53 52
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
54 53
         channel.users += ChannelUser("acidBurn")
55 54
         channel.users += ChannelUser("thePlague")
56
-        channelStateMap += channel
55
+        fakeChannelState += channel
57 56
 
58 57
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("zeroCool")))
59 58
 
@@ -64,7 +63,7 @@ internal class ChannelStateHandlerTest {
64 63
     @Test
65 64
     fun `adds users from multiple name received events`() {
66 65
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
67
-        channelStateMap += channel
66
+        fakeChannelState += channel
68 67
 
69 68
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("zeroCool")))
70 69
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("acidBurn")))
@@ -79,7 +78,7 @@ internal class ChannelStateHandlerTest {
79 78
     @Test
80 79
     fun `clears and readds users on additional names received`() {
81 80
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
82
-        channelStateMap += channel
81
+        fakeChannelState += channel
83 82
 
84 83
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("zeroCool")))
85 84
         handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
@@ -94,8 +93,8 @@ internal class ChannelStateHandlerTest {
94 93
     @Test
95 94
     fun `adds users with mode prefixes`() {
96 95
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
97
-        channelStateMap += channel
98
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
96
+        fakeChannelState += channel
97
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
99 98
 
100 99
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@zeroCool", "@+acidBurn", "+thePlague", "cerealKiller")))
101 100
         handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
@@ -110,8 +109,8 @@ internal class ChannelStateHandlerTest {
110 109
     @Test
111 110
     fun `adds users with full hosts`() {
112 111
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
113
-        channelStateMap += channel
114
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
112
+        fakeChannelState += channel
113
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
115 114
 
116 115
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@zeroCool!dade@root.localhost", "+acidBurn!libby@root.localhost")))
117 116
         handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
@@ -124,8 +123,8 @@ internal class ChannelStateHandlerTest {
124 123
     @Test
125 124
     fun `updates receiving user list state`() {
126 125
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
127
-        channelStateMap += channel
128
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
126
+        fakeChannelState += channel
127
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
129 128
 
130 129
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@zeroCool!dade@root.localhost", "+acidBurn!libby@root.localhost")))
131 130
 
@@ -139,55 +138,61 @@ internal class ChannelStateHandlerTest {
139 138
     @Test
140 139
     fun `requests modes on end of names if configured and undiscovered`() {
141 140
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
142
-        channelStateMap += channel
143
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
144
-        behaviour.requestModesOnJoin = true
141
+        fakeChannelState += channel
142
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
143
+        behaviourConfig.requestModesOnJoin = true
145 144
 
146 145
         handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
147 146
 
148
-        verify(ircClient).send("MODE", "#thegibson")
147
+        verify {
148
+            ircClient.send("MODE", "#thegibson")
149
+        }
149 150
     }
150 151
 
151 152
     @Test
152 153
     fun `does not request modes on end of names if already discovered`() {
153 154
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
154
-        channelStateMap += channel
155
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
156
-        behaviour.requestModesOnJoin = true
155
+        fakeChannelState += channel
156
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
157
+        behaviourConfig.requestModesOnJoin = true
157 158
         channel.modesDiscovered = true
158 159
 
159 160
         handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
160 161
 
161
-        verify(ircClient, never()).send("MODE", "#thegibson")
162
+        verify(inverse = true) {
163
+            ircClient.send("MODE", "#thegibson")
164
+        }
162 165
     }
163 166
 
164 167
     @Test
165 168
     fun `does not request modes on end of names if not configured`() {
166 169
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
167
-        channelStateMap += channel
168
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
169
-        behaviour.requestModesOnJoin = false
170
+        fakeChannelState += channel
171
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
172
+        behaviourConfig.requestModesOnJoin = false
170 173
 
171 174
         handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
172 175
 
173
-        verify(ircClient, never()).send("MODE", "#thegibson")
176
+        verify(inverse = true) {
177
+            ircClient.send("MODE", "#thegibson")
178
+        }
174 179
     }
175 180
 
176 181
     @Test
177 182
     fun `removes state object for local parts`() {
178 183
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
179
-        channelStateMap += channel
184
+        fakeChannelState += channel
180 185
 
181 186
         handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson"))
182 187
 
183
-        assertFalse("#thegibson" in channelStateMap)
188
+        assertFalse("#thegibson" in fakeChannelState)
184 189
     }
185 190
 
186 191
     @Test
187 192
     fun `removes user from channel member list for remote parts`() {
188 193
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
189 194
         channel.users += ChannelUser("ZeroCool")
190
-        channelStateMap += channel
195
+        fakeChannelState += channel
191 196
 
192 197
         handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
193 198
 
@@ -197,18 +202,18 @@ internal class ChannelStateHandlerTest {
197 202
     @Test
198 203
     fun `removes state object for local kicks`() {
199 204
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
200
-        channelStateMap += channel
205
+        fakeChannelState += channel
201 206
 
202 207
         handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson", "acidburn", "Bye!"))
203 208
 
204
-        assertFalse("#thegibson" in channelStateMap)
209
+        assertFalse("#thegibson" in fakeChannelState)
205 210
     }
206 211
 
207 212
     @Test
208 213
     fun `removes user from channel member list for remote kicks`() {
209 214
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
210 215
         channel.users += ChannelUser("ZeroCool")
211
-        channelStateMap += channel
216
+        fakeChannelState += channel
212 217
 
213 218
         handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson", "zerocool", "Bye!"))
214 219
 
@@ -219,33 +224,33 @@ internal class ChannelStateHandlerTest {
219 224
     fun `removes user from channel member lists for quits`() {
220 225
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
221 226
             users += ChannelUser("ZeroCool")
222
-            channelStateMap += this
227
+            fakeChannelState += this
223 228
         }
224 229
 
225 230
         with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
226 231
             users += ChannelUser("ZeroCool")
227
-            channelStateMap += this
232
+            fakeChannelState += this
228 233
         }
229 234
 
230 235
         with (ChannelState("#chat") { CaseMapping.Rfc }) {
231 236
             users += ChannelUser("AcidBurn")
232
-            channelStateMap += this
237
+            fakeChannelState += this
233 238
         }
234 239
 
235 240
         handler.processEvent(ircClient, ChannelQuit(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
236 241
         handler.processEvent(ircClient, ChannelQuit(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#dumpsterdiving"))
237 242
 
238
-        assertFalse("zerocool" in channelStateMap["#thegibson"]!!.users)
239
-        assertFalse("zerocool" in channelStateMap["#dumpsterdiving"]!!.users)
240
-        assertFalse("zerocool" in channelStateMap["#chat"]!!.users)
241
-        assertTrue("acidburn" in channelStateMap["#chat"]!!.users)
243
+        assertFalse("zerocool" in fakeChannelState["#thegibson"]!!.users)
244
+        assertFalse("zerocool" in fakeChannelState["#dumpsterdiving"]!!.users)
245
+        assertFalse("zerocool" in fakeChannelState["#chat"]!!.users)
246
+        assertTrue("acidburn" in fakeChannelState["#chat"]!!.users)
242 247
     }
243 248
 
244 249
     @Test
245 250
     fun `renames user in channel member list for nick changes`() {
246 251
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
247 252
         channel.users += ChannelUser("acidBurn")
248
-        channelStateMap += channel
253
+        fakeChannelState += channel
249 254
 
250 255
         handler.processEvent(ircClient, ChannelNickChanged(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson", "acidB"))
251 256
         handler.processEvent(ircClient, ChannelNickChanged(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#dumpsterdiving", "acidB"))
@@ -258,8 +263,8 @@ internal class ChannelStateHandlerTest {
258 263
     @Test
259 264
     fun `sets mode discovered flag when discovered mode event received`() {
260 265
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
261
-        channelStateMap += channel
262
-        serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
266
+        fakeChannelState += channel
267
+        fakeServerState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
263 268
 
264 269
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+", emptyArray(), true))
265 270
 
@@ -269,8 +274,8 @@ internal class ChannelStateHandlerTest {
269 274
     @Test
270 275
     fun `adds modes when discovered mode event received`() {
271 276
         val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
272
-        channelStateMap += channel
273
-        serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
277
+        fakeChannelState += channel
278
+        fakeServerState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
274 279
 
275 280
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+ceg", arrayOf("CCC", "EEE"), true))
276 281
 
@@ -285,8 +290,8 @@ internal class ChannelStateHandlerTest {
285 290
         channel.modes['c'] = "CCC"
286 291
         channel.modes['e'] = "EEE"
287 292
         channel.modes['h'] = ""
288
-        channelStateMap += channel
289
-        serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
293
+        fakeChannelState += channel
294
+        fakeServerState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
290 295
 
291 296
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "-c+d-eh+fg", arrayOf("CCC", "DDD", "FFF"), true))
292 297
 
@@ -302,81 +307,81 @@ internal class ChannelStateHandlerTest {
302 307
     fun `handles unprivileged user gaining new mode`() {
303 308
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
304 309
             users += ChannelUser("ZeroCool")
305
-            channelStateMap += this
310
+            fakeChannelState += this
306 311
         }
307 312
 
308 313
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+o", arrayOf("zeroCool")))
309 314
 
310
-        assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
315
+        assertEquals("o", fakeChannelState["#thegibson"]?.users?.get("zeroCool")?.modes)
311 316
     }
312 317
 
313 318
     @Test
314 319
     fun `handles privileged user gaining lesser mode`() {
315 320
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
316 321
             users += ChannelUser("ZeroCool", "o")
317
-            channelStateMap += this
322
+            fakeChannelState += this
318 323
         }
319 324
 
320 325
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+v", arrayOf("zeroCool")))
321 326
 
322
-        assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
327
+        assertEquals("ov", fakeChannelState["#thegibson"]?.users?.get("zeroCool")?.modes)
323 328
     }
324 329
 
325 330
     @Test
326 331
     fun `handles privileged user gaining greater mode`() {
327 332
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
328 333
             users += ChannelUser("ZeroCool", "v")
329
-            channelStateMap += this
334
+            fakeChannelState += this
330 335
         }
331 336
 
332 337
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+o", arrayOf("zeroCool")))
333 338
 
334
-        assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
339
+        assertEquals("ov", fakeChannelState["#thegibson"]?.users?.get("zeroCool")?.modes)
335 340
     }
336 341
 
337 342
     @Test
338 343
     fun `handles user gaining multiple modes`() {
339 344
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
340 345
             users += ChannelUser("ZeroCool")
341
-            channelStateMap += this
346
+            fakeChannelState += this
342 347
         }
343 348
 
344 349
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+vo", arrayOf("zeroCool", "zeroCool")))
345 350
 
346
-        assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
351
+        assertEquals("ov", fakeChannelState["#thegibson"]?.users?.get("zeroCool")?.modes)
347 352
     }
348 353
 
349 354
     @Test
350 355
     fun `handles user losing multiple modes`() {
351 356
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
352 357
             users += ChannelUser("ZeroCool", "ov")
353
-            channelStateMap += this
358
+            fakeChannelState += this
354 359
         }
355 360
 
356 361
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "-vo", arrayOf("zeroCool", "zeroCool")))
357 362
 
358
-        assertEquals("", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
363
+        assertEquals("", fakeChannelState["#thegibson"]?.users?.get("zeroCool")?.modes)
359 364
     }
360 365
 
361 366
     @Test
362 367
     fun `handles mixture of user modes and normal modes`() {
363 368
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
364 369
             users += ChannelUser("ZeroCool", "v")
365
-            channelStateMap += this
370
+            fakeChannelState += this
366 371
         }
367
-        serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
372
+        fakeServerState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
368 373
 
369 374
         handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "oa-v+b", arrayOf("zeroCool", "aaa", "zeroCool", "bbb")))
370 375
 
371
-        assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
372
-        assertEquals("aaa", channelStateMap["#thegibson"]?.modes?.get('a'))
373
-        assertEquals("bbb", channelStateMap["#thegibson"]?.modes?.get('b'))
376
+        assertEquals("o", fakeChannelState["#thegibson"]?.users?.get("zeroCool")?.modes)
377
+        assertEquals("aaa", fakeChannelState["#thegibson"]?.modes?.get('a'))
378
+        assertEquals("bbb", fakeChannelState["#thegibson"]?.modes?.get('b'))
374 379
     }
375 380
 
376 381
     @Test
377 382
     fun `updates topic state when it's discovered for the first time`() {
378 383
         val state = ChannelState("#thegibson") { CaseMapping.Rfc }
379
-        channelStateMap += state
384
+        fakeChannelState += state
380 385
 
381 386
         handler.processEvent(ircClient, ChannelTopicDiscovered(EventMetadata(TestConstants.time), "#thegibson", "Hack the planet!"))
382 387
         handler.processEvent(ircClient, ChannelTopicMetadataDiscovered(EventMetadata(TestConstants.time), "#thegibson", User("acidBurn"), TestConstants.otherTime))
@@ -388,7 +393,7 @@ internal class ChannelStateHandlerTest {
388 393
     @Test
389 394
     fun `updates topic state when no topic is discovered for the first time`() {
390 395
         val state = ChannelState("#thegibson") { CaseMapping.Rfc }
391
-        channelStateMap += state
396
+        fakeChannelState += state
392 397
 
393 398
         handler.processEvent(ircClient, ChannelTopicDiscovered(EventMetadata(TestConstants.time), "#thegibson", null))
394 399
 
@@ -401,7 +406,7 @@ internal class ChannelStateHandlerTest {
401 406
         val state = ChannelState("#thegibson") { CaseMapping.Rfc }
402 407
         state.topic = ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.otherTime)
403 408
         state.topicDiscovered = true
404
-        channelStateMap += state
409
+        fakeChannelState += state
405 410
 
406 411
         handler.processEvent(ircClient, ChannelTopicDiscovered(EventMetadata(TestConstants.time), "#thegibson", "Hack the planet"))
407 412
         handler.processEvent(ircClient, ChannelTopicMetadataDiscovered(EventMetadata(TestConstants.time), "#thegibson", User("zeroCool"), TestConstants.time))
@@ -413,7 +418,7 @@ internal class ChannelStateHandlerTest {
413 418
     @Test
414 419
     fun `updates topic state when the topic is changed`() {
415 420
         val state = ChannelState("#thegibson") { CaseMapping.Rfc }
416
-        channelStateMap += state
421
+        fakeChannelState += state
417 422
 
418 423
         handler.processEvent(ircClient, ChannelTopicChanged(EventMetadata(TestConstants.time), User("acidBurn"), "#thegibson", "Hack the planet!"))
419 424
 
@@ -423,7 +428,7 @@ internal class ChannelStateHandlerTest {
423 428
     @Test
424 429
     fun `updates topic state when the topic is unset`() {
425 430
         val state = ChannelState("#thegibson") { CaseMapping.Rfc }
426
-        channelStateMap += state
431
+        fakeChannelState += state
427 432
 
428 433
         handler.processEvent(ircClient, ChannelTopicChanged(EventMetadata(TestConstants.time), User("acidBurn"), "#thegibson", null))
429 434
 
@@ -433,7 +438,7 @@ internal class ChannelStateHandlerTest {
433 438
     @Test
434 439
     fun `ignores topic change when channel doesn't exist`() {
435 440
         val state = ChannelState("#thegibson") { CaseMapping.Rfc }
436
-        channelStateMap += state
441
+        fakeChannelState += state
437 442
 
438 443
         handler.processEvent(ircClient, ChannelTopicChanged(EventMetadata(TestConstants.time), User("acidBurn"), "#dumpsterdiving", "Hack the planet!"))
439 444
 

+ 7
- 6
src/test/kotlin/com/dmdirc/ktirc/events/handlers/PingHandlerTest.kt View File

@@ -4,21 +4,22 @@ import com.dmdirc.ktirc.IrcClient
4 4
 import com.dmdirc.ktirc.TestConstants
5 5
 import com.dmdirc.ktirc.events.EventMetadata
6 6
 import com.dmdirc.ktirc.events.PingReceived
7
-import com.nhaarman.mockitokotlin2.mock
8
-import com.nhaarman.mockitokotlin2.verify
9
-import kotlinx.coroutines.runBlocking
7
+import io.mockk.mockk
8
+import io.mockk.verify
10 9
 import org.junit.jupiter.api.Test
11 10
 
12 11
 internal class PingHandlerTest {
13 12
 
14
-    private val ircClient = mock<IrcClient>()
13
+    private val ircClient = mockk<IrcClient>()
15 14
 
16 15
     private val handler = PingHandler()
17 16
 
18 17
     @Test
19
-    fun `PingHandler responses to pings with a pong`() = runBlocking {
18
+    fun `responses to pings with a pong`() {
20 19
         handler.processEvent(ircClient, PingReceived(EventMetadata(TestConstants.time), "the_plague".toByteArray()))
21
-        verify(ircClient).send("PONG", "the_plague")
20
+        verify {
21
+            ircClient.send("PONG", "the_plague")
22
+        }
22 23
     }
23 24
 
24 25
 }

+ 15
- 15
src/test/kotlin/com/dmdirc/ktirc/events/handlers/ServerStateHandlerTest.kt View File

@@ -7,16 +7,16 @@ import com.dmdirc.ktirc.model.ServerFeature
7 7
 import com.dmdirc.ktirc.model.ServerFeatureMap
8 8
 import com.dmdirc.ktirc.model.ServerState
9 9
 import com.dmdirc.ktirc.model.ServerStatus
10
-import com.nhaarman.mockitokotlin2.doReturn
11
-import com.nhaarman.mockitokotlin2.mock
10
+import io.mockk.every
11
+import io.mockk.mockk
12 12
 import org.junit.jupiter.api.Assertions.*
13 13
 import org.junit.jupiter.api.Test
14 14
 
15 15
 internal class ServerStateHandlerTest {
16 16
 
17
-    private val serverState = ServerState("", "")
18
-    private val ircClient = mock<IrcClient> {
19
-        on { serverState } doReturn serverState
17
+    private val fakeServerState = ServerState("", "")
18
+    private val ircClient = mockk<IrcClient> {
19
+        every { serverState } returns fakeServerState
20 20
     }
21 21
 
22 22
     private val handler = ServerStateHandler()
@@ -24,44 +24,44 @@ internal class ServerStateHandlerTest {
24 24
     @Test
25 25
     fun `sets local nickname on welcome event`() {
26 26
         handler.processEvent(ircClient, ServerWelcome(EventMetadata(TestConstants.time), "the.gibson", "acidBurn"))
27
-        assertEquals("acidBurn", serverState.localNickname)
27
+        assertEquals("acidBurn", fakeServerState.localNickname)
28 28
     }
29 29
 
30 30
     @Test
31 31
     fun `sets server name on welcome event`() {
32 32
         handler.processEvent(ircClient, ServerWelcome(EventMetadata(TestConstants.time), "the.gibson", "acidBurn"))
33
-        assertEquals("the.gibson", serverState.serverName)
33
+        assertEquals("the.gibson", fakeServerState.serverName)
34 34
     }
35 35
 
36 36
     @Test
37 37
     fun `sets receivedWelcome on welcome event`() {
38 38
         handler.processEvent(ircClient, ServerWelcome(EventMetadata(TestConstants.time), "the.gibson", "acidBurn"))
39
-        assertTrue(serverState.receivedWelcome)
39
+        assertTrue(fakeServerState.receivedWelcome)
40 40
     }
41 41
 
42 42
     @Test
43 43
     fun `sets state to connecting on event`() {
44 44
         handler.processEvent(ircClient, ServerConnecting(EventMetadata(TestConstants.time)))
45
-        assertEquals(ServerStatus.Connecting, serverState.status)
45
+        assertEquals(ServerStatus.Connecting, fakeServerState.status)
46 46
     }
47 47
 
48 48
     @Test
49 49
     fun `sets state to disconnected on event`() {
50
-        serverState.status = ServerStatus.Ready
50
+        fakeServerState.status = ServerStatus.Ready
51 51
         handler.processEvent(ircClient, ServerDisconnected(EventMetadata(TestConstants.time)))
52
-        assertEquals(ServerStatus.Disconnected, serverState.status)
52
+        assertEquals(ServerStatus.Disconnected, fakeServerState.status)
53 53
     }
54 54
 
55 55
     @Test
56 56
     fun `sets state to negotiating on connected`() {
57 57
         handler.processEvent(ircClient, ServerConnected(EventMetadata(TestConstants.time)))
58
-        assertEquals(ServerStatus.Negotiating, serverState.status)
58
+        assertEquals(ServerStatus.Negotiating, fakeServerState.status)
59 59
     }
60 60
 
61 61
     @Test
62 62
     fun `sets state to ready on ServerReady`() {
63 63
         handler.processEvent(ircClient, ServerReady(EventMetadata(TestConstants.time)))
64
-        assertEquals(ServerStatus.Ready, serverState.status)
64
+        assertEquals(ServerStatus.Ready, fakeServerState.status)
65 65
     }
66 66
 
67 67
     @Test
@@ -72,8 +72,8 @@ internal class ServerStateHandlerTest {
72 72
 
73 73
         handler.processEvent(ircClient, ServerFeaturesUpdated(EventMetadata(TestConstants.time), features))
74 74
 
75
-        assertArrayEquals(arrayOf("abc", "def"), serverState.features[ServerFeature.ChannelModes])
76
-        assertEquals(true, serverState.features[ServerFeature.WhoxSupport])
75
+        assertArrayEquals(arrayOf("abc", "def"), fakeServerState.features[ServerFeature.ChannelModes])
76
+        assertEquals(true, fakeServerState.features[ServerFeature.WhoxSupport])
77 77
     }
78 78
 
79 79
 }

+ 77
- 80
src/test/kotlin/com/dmdirc/ktirc/events/handlers/UserStateHandlerTest.kt View File

@@ -5,241 +5,238 @@ import com.dmdirc.ktirc.TestConstants
5 5
 import com.dmdirc.ktirc.events.*
6 6
 import com.dmdirc.ktirc.io.CaseMapping
7 7
 import com.dmdirc.ktirc.model.*
8
-import com.nhaarman.mockitokotlin2.argForWhich
9
-import com.nhaarman.mockitokotlin2.doReturn
10
-import com.nhaarman.mockitokotlin2.mock
11
-import com.nhaarman.mockitokotlin2.whenever
8
+import io.mockk.every
9
+import io.mockk.mockk
12 10
 import org.junit.jupiter.api.Assertions.*
13 11
 import org.junit.jupiter.api.BeforeEach
14 12
 import org.junit.jupiter.api.Test
15 13
 
16 14
 internal class UserStateHandlerTest {
17 15
 
18
-    private val serverState = ServerState("", "")
19
-    private val userState = UserState { CaseMapping.Rfc }
16
+    private val fakeServerState = ServerState("", "")
17
+    private val fakeUserState = UserState { CaseMapping.Rfc }
20 18
 
21
-    private val ircClient = mock<IrcClient> {
22
-        on { serverState } doReturn serverState
23
-        on { userState } doReturn userState
24
-        on { isLocalUser(argForWhich<User> { nickname == "zeroCool" }) } doReturn true
25
-        on { isLocalUser("zeroCool") } doReturn true
19
+    private val ircClient = mockk<IrcClient> {
20
+        every { serverState } returns fakeServerState
21
+        every { userState } returns fakeUserState
22
+        every { isLocalUser(any<User>()) } answers { arg<User>(0).nickname == "zeroCool" }
23
+        every { isLocalUser(any<String>()) } answers { arg<String>(0) == "zeroCool" }
26 24
     }
27 25
 
28 26
     private val handler = UserStateHandler()
29 27
 
30 28
     @BeforeEach
31 29
     fun setUp() {
32
-        serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
30
+        fakeServerState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
33 31
     }
34 32
 
35 33
     @Test
36 34
     fun `adds channel to user on join`() {
37
-        userState += User("acidBurn")
35
+        fakeUserState += User("acidBurn")
38 36
 
39 37
         handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("acidBurn", "libby", "root.localhost"), "#thegibson"))
40 38
 
41
-        assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
39
+        assertEquals(listOf("#thegibson"), fakeUserState["acidBurn"]?.channels?.toList())
42 40
     }
43 41
 
44 42
     @Test
45 43
     fun `updates user info on join`() {
46
-        userState += User("acidBurn")
44
+        fakeUserState += User("acidBurn")
47 45
 
48 46
         handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("acidBurn", "libby", "root.localhost"), "#thegibson"))
49 47
 
50
-        val details = userState["acidBurn"]?.details!!
48
+        val details = fakeUserState["acidBurn"]?.details!!
51 49
         assertEquals("libby", details.ident)
52 50
         assertEquals("root.localhost", details.hostname)
53 51
     }
54 52
 
55 53
     @Test
56 54
     fun `removes channel from user on part`() {
57
-        userState += User("acidBurn")
58
-        userState.addToChannel(User("acidBurn"), "#thegibson")
59
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
55
+        fakeUserState += User("acidBurn")
56
+        fakeUserState.addToChannel(User("acidBurn"), "#thegibson")
57
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
60 58
 
61 59
         handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
62 60
 
63
-        assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
61
+        assertEquals(listOf("#thegibson"), fakeUserState["acidBurn"]?.channels?.toList())
64 62
     }
65 63
 
66 64
     @Test
67 65
     fun `removes user on part from last channel`() {
68
-        userState += User("acidBurn")
69
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
66
+        fakeUserState += User("acidBurn")
67
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
70 68
 
71 69
         handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
72 70
 
73
-        assertNull(userState["acidBurn"])
71
+        assertNull(fakeUserState["acidBurn"])
74 72
     }
75 73
 
76 74
     @Test
77 75
     fun `removes channel from all users on local part`() {
78
-        userState += User("acidBurn")
79
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
80
-        userState.addToChannel(User("acidBurn"), "#thegibson")
76
+        fakeUserState += User("acidBurn")
77
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
78
+        fakeUserState.addToChannel(User("acidBurn"), "#thegibson")
81 79
 
82
-        userState += User("zeroCool")
83
-        userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
84
-        userState.addToChannel(User("zeroCool"), "#thegibson")
80
+        fakeUserState += User("zeroCool")
81
+        fakeUserState.addToChannel(User("zeroCool"), "#dumpsterdiving")
82
+        fakeUserState.addToChannel(User("zeroCool"), "#thegibson")
85 83
 
86 84
         handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
87 85
 
88
-        assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
89
-        assertEquals(listOf("#thegibson"), userState["zeroCool"]?.channels?.toList())
86
+        assertEquals(listOf("#thegibson"), fakeUserState["acidBurn"]?.channels?.toList())
87
+        assertEquals(listOf("#thegibson"), fakeUserState["zeroCool"]?.channels?.toList())
90 88
     }
91 89
 
92 90
     @Test
93 91
     fun `removes remote users with no remaining channels on local part`() {
94
-        userState += User("acidBurn")
95
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
92
+        fakeUserState += User("acidBurn")
93
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
96 94
 
97 95
         handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
98 96
 
99
-        assertNull(userState["acidBurn"])
97
+        assertNull(fakeUserState["acidBurn"])
100 98
     }
101 99
 
102 100
     @Test
103 101
     fun `keeps local user with no remaining channels after local part`() {
104
-        userState += User("zeroCool")
105
-        userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
102
+        fakeUserState += User("zeroCool")
103
+        fakeUserState.addToChannel(User("zeroCool"), "#dumpsterdiving")
106 104
 
107 105
         handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
108 106
 
109
-        assertNotNull(userState["zeroCool"])
107
+        assertNotNull(fakeUserState["zeroCool"])
110 108
     }
111 109
 
112 110
 
113 111
     @Test
114 112
     fun `removes channel from user on kick`() {
115
-        userState += User("acidBurn")
116
-        userState.addToChannel(User("acidBurn"), "#thegibson")
117
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
113
+        fakeUserState += User("acidBurn")
114
+        fakeUserState.addToChannel(User("acidBurn"), "#thegibson")
115
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
118 116
 
119 117
         handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("thePlague"), "#dumpsterdiving", "acidBurn"))
120 118
 
121
-        assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
119
+        assertEquals(listOf("#thegibson"), fakeUserState["acidBurn"]?.channels?.toList())
122 120
     }
123 121
 
124 122
     @Test
125 123
     fun `removes user on kick from last channel`() {
126
-        userState += User("acidBurn")
127
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
124
+        fakeUserState += User("acidBurn")
125
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
128 126
 
129 127
         handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("thePlague"), "#dumpsterdiving", "acidBurn"))
130 128
 
131
-        assertNull(userState["acidBurn"])
129
+        assertNull(fakeUserState["acidBurn"])
132 130
     }
133 131
 
134 132
     @Test
135 133
     fun `removes channel from all users on local kick`() {
136
-        userState += User("acidBurn")
137
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
138
-        userState.addToChannel(User("acidBurn"), "#thegibson")
134
+        fakeUserState += User("acidBurn")
135
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
136
+        fakeUserState.addToChannel(User("acidBurn"), "#thegibson")
139 137
 
140
-        userState += User("zeroCool")
141
-        userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
142
-        userState.addToChannel(User("zeroCool"), "#thegibson")
138
+        fakeUserState += User("zeroCool")
139
+        fakeUserState.addToChannel(User("zeroCool"), "#dumpsterdiving")
140
+        fakeUserState.addToChannel(User("zeroCool"), "#thegibson")
143 141
 
144 142
         handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("thePlague"), "#dumpsterdiving", "zeroCool"))
145 143
 
146
-        assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
147
-        assertEquals(listOf("#thegibson"), userState["zeroCool"]?.channels?.toList())
144
+        assertEquals(listOf("#thegibson"), fakeUserState["acidBurn"]?.channels?.toList())
145
+        assertEquals(listOf("#thegibson"), fakeUserState["zeroCool"]?.channels?.toList())
148 146
     }
149 147
 
150 148
     @Test
151 149
     fun `removes remote users with no remaining channels on local kick`() {
152
-        userState += User("acidBurn")
153
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
150
+        fakeUserState += User("acidBurn")
151
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
154 152
 
155 153
         handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("thePlague"), "#dumpsterdiving", "zeroCool"))
156 154
 
157
-        assertNull(userState["acidBurn"])
155
+        assertNull(fakeUserState["acidBurn"])
158 156
     }
159 157
 
160 158
     @Test
161 159
     fun `keeps local user with no remaining channels after local kick`() {
162
-        userState += User("zeroCool")
163
-        userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
160
+        fakeUserState += User("zeroCool")
161
+        fakeUserState.addToChannel(User("zeroCool"), "#dumpsterdiving")
164 162
 
165 163
         handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("thePlague"), "#dumpsterdiving", "zeroCool"))
166 164
 
167
-        assertNotNull(userState["zeroCool"])
165
+        assertNotNull(fakeUserState["zeroCool"])
168 166
     }
169 167
 
170 168
     @Test
171 169
     fun `removes user entirely on quit`() {
172
-        userState += User("acidBurn")
173
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
170
+        fakeUserState += User("acidBurn")
171
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
174 172
 
175 173
         handler.processEvent(ircClient, UserQuit(EventMetadata(TestConstants.time), User("acidBurn", "libby", "root.localhost")))
176 174
 
177
-        assertNull(userState["acidBurn"])
175
+        assertNull(fakeUserState["acidBurn"])
178 176
     }
179 177
 
180 178
     @Test
181 179
     fun `adds users to channels on names received`() {
182
-        userState += User("acidBurn")
183
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
180
+        fakeUserState += User("acidBurn")
181
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
184 182
 
185 183
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@acidBurn")))
186 184
 
187
-        assertEquals(listOf("#dumpsterdiving", "#thegibson"), userState["acidBurn"]?.channels?.toList())
185
+        assertEquals(listOf("#dumpsterdiving", "#thegibson"), fakeUserState["acidBurn"]?.channels?.toList())
188 186
     }
189 187
 
190 188
     @Test
191 189
     fun `updates user details on names received`() {
192
-        userState += User("acidBurn")
193
-        userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
190
+        fakeUserState += User("acidBurn")
191
+        fakeUserState.addToChannel(User("acidBurn"), "#dumpsterdiving")
194 192
 
195 193
         handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@acidBurn!libby@root.localhost")))
196 194
 
197
-        val details = userState["acidBurn"]?.details!!
195
+        val details = fakeUserState["acidBurn"]?.details!!
198 196
         assertEquals("libby", details.ident)
199 197
         assertEquals("root.localhost", details.hostname)
200 198
     }
201 199
 
202 200
     @Test
203 201
     fun `updates user info on account change`() {
204
-        userState += User("acidBurn")
202
+        fakeUserState += User("acidBurn")
205 203
 
206 204
         handler.processEvent(ircClient, UserAccountChanged(EventMetadata(TestConstants.time), User("acidBurn", "libby", "root.localhost"), "acidBurn"))
207 205
 
208
-        val details = userState["acidBurn"]?.details!!
206
+        val details = fakeUserState["acidBurn"]?.details!!
209 207
         assertEquals("acidBurn", details.account)
210 208
     }
211 209
 
212 210
     @Test
213 211
     fun `updates local nickname for local nick changes`() {
214
-        val user = User("acidBurn", "libby", "root.localhost")
215
-        whenever(ircClient.isLocalUser(user)).doReturn(true)
212
+        val user = User("zeroCool", "dade", "root.localhost")
216 213
 
217
-        handler.processEvent(ircClient, UserNickChanged(EventMetadata(TestConstants.time), user, "acid~"))
214
+        handler.processEvent(ircClient, UserNickChanged(EventMetadata(TestConstants.time), user, "crashOverride"))
218 215
 
219
-        assertEquals("acid~", serverState.localNickname)
216
+        assertEquals("crashOverride", fakeServerState.localNickname)
220 217
     }
221 218
 
222 219
     @Test
223 220
     fun `updates nickname for remote nick changes`() {
224 221
         val user = User("acidBurn", "libby", "root.localhost")
225
-        userState += User("AcidBurn")
222
+        fakeUserState += User("AcidBurn")
226 223
 
227 224
         handler.processEvent(ircClient, UserNickChanged(EventMetadata(TestConstants.time), user, "acid~"))
228 225
 
229
-        assertNotNull(userState["acid~"])
230
-        assertNull(userState["AcidBurn"])
231
-        assertEquals("acid~", userState["acid~"]?.details?.nickname)
226
+        assertNotNull(fakeUserState["acid~"])
227
+        assertNull(fakeUserState["AcidBurn"])
228
+        assertEquals("acid~", fakeUserState["acid~"]?.details?.nickname)
232 229
     }
233 230
 
234 231
     @Test
235 232
     fun `updates details for remote host changes`() {
236 233
         val user = User("acidBurn", "libby", "root.localhost")
237
-        userState += User("AcidBurn")
234
+        fakeUserState += User("AcidBurn")
238 235
 
239 236
         handler.processEvent(ircClient, UserHostChanged(EventMetadata(TestConstants.time), user, "burn", "root.gibson"))
240 237
 
241
-        assertEquals("burn", userState["acidBurn"]?.details?.ident)
242
-        assertEquals("root.gibson", userState["acidBurn"]?.details?.hostname)
238
+        assertEquals("burn", fakeUserState["acidBurn"]?.details?.ident)
239
+        assertEquals("root.gibson", fakeUserState["acidBurn"]?.details?.hostname)
243 240
     }
244 241
 
245 242
 }

+ 22
- 18
src/test/kotlin/com/dmdirc/ktirc/events/mutators/BatchMutatorTest.kt View File

@@ -7,7 +7,9 @@ import com.dmdirc.ktirc.io.MessageEmitter
7 7
 import com.dmdirc.ktirc.model.Batch
8 8
 import com.dmdirc.ktirc.model.ServerState
9 9
 import com.dmdirc.ktirc.model.User
10
-import com.nhaarman.mockitokotlin2.*
10
+import io.mockk.every
11
+import io.mockk.mockk
12
+import io.mockk.verify
11 13
 import org.junit.jupiter.api.Assertions.*
12 14
 import org.junit.jupiter.api.Test
13 15
 
@@ -15,10 +17,10 @@ internal class BatchMutatorTest {
15 17
 
16 18
     private val mutator = BatchMutator()
17 19
 
18
-    private val serverState = ServerState("", "")
19
-    private val messageEmitter = mock<MessageEmitter>()
20
-    private val ircClient = mock<IrcClient> {
21
-        on { serverState } doReturn serverState
20
+    private val messageEmitter = mockk<MessageEmitter>()
21
+    private val fakeServerState = ServerState("", "")
22
+    private val ircClient = mockk<IrcClient> {
23
+        every { serverState } returns fakeServerState
22 24
     }
23 25
 
24 26
     @Test
@@ -36,8 +38,8 @@ internal class BatchMutatorTest {
36 38
         val events = mutator.mutateEvent(ircClient, messageEmitter, event)
37 39
 
38 40
         assertTrue(events.isEmpty())
39
-        assertNotNull(serverState.batches["abcdef"])
40
-        serverState.batches["abcdef"]?.let {
41
+        assertNotNull(fakeServerState.batches["abcdef"])
42
+        fakeServerState.batches["abcdef"]?.let {
41 43
             assertEquals(listOf("foo", "bar"), it.arguments)
42 44
             assertEquals("netsplit", it.type)
43 45
             assertTrue(it.events.isEmpty())
@@ -47,17 +49,17 @@ internal class BatchMutatorTest {
47 49
 
48 50
     @Test
49 51
     fun `adds to batch when event has a batch ID`() {
50
-        serverState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time))
52
+        fakeServerState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time))
51 53
 
52 54
         val event = UserNickChanged(EventMetadata(TestConstants.time, "abcdef"), User("zeroCool"), "crashOverride")
53 55
         mutator.mutateEvent(ircClient, messageEmitter, event)
54 56
 
55
-        assertEquals(listOf(event), serverState.batches["abcdef"]!!.events)
57
+        assertEquals(listOf(event), fakeServerState.batches["abcdef"]!!.events)
56 58
     }
57 59
 
58 60
     @Test
59 61
     fun `suppresses event when it has a batch ID`() {
60
-        serverState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time))
62
+        fakeServerState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time))
61 63
 
62 64
         val event = UserNickChanged(EventMetadata(TestConstants.time, "abcdef"), User("zeroCool"), "crashOverride")
63 65
         val events = mutator.mutateEvent(ircClient, messageEmitter, event)
@@ -67,17 +69,19 @@ internal class BatchMutatorTest {
67 69
 
68 70
     @Test
69 71
     fun `passes event for processing only when it has a batch ID`() {
70
-        serverState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time))
72
+        fakeServerState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time))
71 73
 
72 74
         val event = UserNickChanged(EventMetadata(TestConstants.time, "abcdef"), User("zeroCool"), "crashOverride")
73 75
         mutator.mutateEvent(ircClient, messageEmitter, event)
74 76
 
75
-        verify(messageEmitter).handleEvent(any(), same(event), eq(true))
77
+        verify {
78
+            messageEmitter.handleEvent(any(), refEq(event), eq(true))
79
+        }
76 80
     }
77 81
 
78 82
     @Test
79 83
     fun `sends a batch when it finishes and the parent is null`() {
80
-        serverState.batches["abcdef"] = Batch("netsplit", listOf("p1", "p2"), EventMetadata(TestConstants.time), events = mutableListOf(ServerConnected(EventMetadata(TestConstants.time, "abcdef"))))
84
+        fakeServerState.batches["abcdef"] = Batch("netsplit", listOf("p1", "p2"), EventMetadata(TestConstants.time), events = mutableListOf(ServerConnected(EventMetadata(TestConstants.time, "abcdef"))))
81 85
 
82 86
         val events = mutator.mutateEvent(ircClient, messageEmitter, BatchFinished(EventMetadata(TestConstants.time), "abcdef"))
83 87
 
@@ -92,14 +96,14 @@ internal class BatchMutatorTest {
92 96
 
93 97
     @Test
94 98
     fun `adds a batch to its parent when it finishes`() {
95
-        serverState.batches["12345"] = Batch("history", emptyList(), EventMetadata(TestConstants.time))
96
-        serverState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time, batchId = "12345"), mutableListOf(ServerConnected(EventMetadata(TestConstants.time, "abcdef"))))
99
+        fakeServerState.batches["12345"] = Batch("history", emptyList(), EventMetadata(TestConstants.time))
100
+        fakeServerState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time, batchId = "12345"), mutableListOf(ServerConnected(EventMetadata(TestConstants.time, "abcdef"))))
97 101
 
98 102
         val events = mutator.mutateEvent(ircClient, messageEmitter, BatchFinished(EventMetadata(TestConstants.time), "abcdef"))
99 103
 
100 104
         assertEquals(0, events.size)
101 105
 
102
-        val parent = serverState.batches["12345"]?.events
106
+        val parent = fakeServerState.batches["12345"]?.events
103 107
         assertEquals(1, parent?.size)
104 108
 
105 109
         val event = parent?.get(0) as BatchReceived
@@ -109,11 +113,11 @@ internal class BatchMutatorTest {
109 113
 
110 114
     @Test
111 115
     fun `deletes batch when it finishes`() {
112
-        serverState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time), events = mutableListOf(ServerConnected(EventMetadata(TestConstants.time, "abcdef"))))
116
+        fakeServerState.batches["abcdef"] = Batch("netsplit", emptyList(), EventMetadata(TestConstants.time), events = mutableListOf(ServerConnected(EventMetadata(TestConstants.time, "abcdef"))))
113 117
 
114 118
         mutator.mutateEvent(ircClient, messageEmitter, BatchFinished(EventMetadata(TestConstants.time), "abcdef"))
115 119
 
116
-        assertNull(serverState.batches["abcdef"])
120
+        assertNull(fakeServerState.batches["abcdef"])
117 121
     }
118 122
 
119 123
 }

+ 30
- 29
src/test/kotlin/com/dmdirc/ktirc/events/mutators/ChannelFanOutMutatorTest.kt View File

@@ -7,85 +7,86 @@ import com.dmdirc.ktirc.events.*
7 7
 import com.dmdirc.ktirc.io.CaseMapping
8 8
 import com.dmdirc.ktirc.io.MessageEmitter
9 9
 import com.dmdirc.ktirc.model.*
10
-import com.nhaarman.mockitokotlin2.doReturn
11
-import com.nhaarman.mockitokotlin2.mock
10
+import io.mockk.every
11
+import io.mockk.mockk
12 12
 import org.junit.jupiter.api.Assertions
13
+import org.junit.jupiter.api.Assertions.*
13 14
 import org.junit.jupiter.api.Test
14 15
 
15 16
 internal class ChannelFanOutMutatorTest {
16 17
 
17 18
     private val mutator = ChannelFanOutMutator()
18
-    private val channelStateMap = ChannelStateMap { CaseMapping.Rfc }
19
-    private val serverState = ServerState("", "")
20
-    private val behaviour = BehaviourConfig()
21
-    private val messageEmitter = mock<MessageEmitter>()
22
-    private val ircClient = mock<IrcClient> {
23
-        on { serverState } doReturn serverState
24
-        on { channelState } doReturn channelStateMap
25
-        on { behaviour } doReturn behaviour
26
-        on { isLocalUser(User("acidburn", "libby", "root.localhost")) } doReturn true
27
-        on { isLocalUser("acidburn") } doReturn  true
19
+    private val fakeServerState = ServerState("", "")
20
+    private val fakeChannelStateMap = ChannelStateMap { CaseMapping.Rfc }
21
+    private val fakeBehaviour = BehaviourConfig()
22
+    private val messageEmitter = mockk<MessageEmitter>()
23
+    private val ircClient = mockk<IrcClient> {
24
+        every { serverState } returns fakeServerState
25
+        every { channelState } returns fakeChannelStateMap
26
+        every { behaviour } returns fakeBehaviour
27
+        every { isLocalUser(any<User>()) } answers { arg<User>(0) == User("acidburn", "libby", "root.localhost") }
28
+        every { isLocalUser(any<String>()) } answers { arg<String>(0) == "acidburn" }
28 29
     }
29 30
 
30 31
     @Test
31 32
     fun `raises ChannelQuit event for each channel a user quits from`() {
32 33
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
33 34
             users += ChannelUser("ZeroCool")
34
-            channelStateMap += this
35
+            fakeChannelStateMap += this
35 36
         }
36 37
 
37 38
         with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
38 39
             users += ChannelUser("ZeroCool")
39
-            channelStateMap += this
40
+            fakeChannelStateMap += this
40 41
         }
41 42
 
42 43
         with (ChannelState("#chat") { CaseMapping.Rfc }) {
43 44
             users += ChannelUser("AcidBurn")
44
-            channelStateMap += this
45
+            fakeChannelStateMap += this
45 46
         }
46 47
 
47 48
         val quitEvent = UserQuit(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "Hack the planet!")
48 49
         val events = mutator.mutateEvent(ircClient, messageEmitter, quitEvent)
49 50
 
50 51
         val names = mutableListOf<String>()
51
-        Assertions.assertEquals(3, events.size)
52
-        Assertions.assertSame(quitEvent, events[0])
52
+        assertEquals(3, events.size)
53
+        assertSame(quitEvent, events[0])
53 54
         events.subList(1, events.size).forEach { event ->
54 55
             (event as ChannelQuit).let {
55
-                Assertions.assertEquals(TestConstants.time, it.metadata.time)
56
-                Assertions.assertEquals("zerocool", it.user.nickname)
57
-                Assertions.assertEquals("Hack the planet!", it.reason)
56
+                assertEquals(TestConstants.time, it.metadata.time)
57
+                assertEquals("zerocool", it.user.nickname)
58
+                assertEquals("Hack the planet!", it.reason)
58 59
                 names.add(it.target)
59 60
             }
60 61
         }
61 62
 
62
-        Assertions.assertTrue("#thegibson" in names)
63
-        Assertions.assertTrue("#dumpsterdiving" in names)
63
+        assertTrue("#thegibson" in names)
64
+        assertTrue("#dumpsterdiving" in names)
64 65
     }
65 66
 
66 67
     @Test
67 68
     fun `raises ChannelNickChanged event for each channel a user changes nicks in`() {
68 69
         with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
69 70
             users += ChannelUser("ZeroCool")
70
-            channelStateMap += this
71
+            fakeChannelStateMap += this
71 72
         }
72 73
 
73 74
         with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
74 75
             users += ChannelUser("ZeroCool")
75
-            channelStateMap += this
76
+            fakeChannelStateMap += this
76 77
         }
77 78
 
78 79
         with (ChannelState("#chat") { CaseMapping.Rfc }) {
79 80
             users += ChannelUser("AcidBurn")
80
-            channelStateMap += this
81
+            fakeChannelStateMap += this
81 82
         }
82 83
 
83 84
         val nickEvent = UserNickChanged(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "zer0c00l")
84 85
         val events = mutator.mutateEvent(ircClient, messageEmitter, nickEvent)
85 86
 
86 87
         val names = mutableListOf<String>()
87
-        Assertions.assertEquals(3, events.size)
88
-        Assertions.assertSame(nickEvent, events[0])
88
+        assertEquals(3, events.size)
89
+        assertSame(nickEvent, events[0])
89 90
         events.subList(1, events.size).forEach { event ->
90 91
             (event as ChannelNickChanged).let {
91 92
                 Assertions.assertEquals(TestConstants.time, it.metadata.time)
@@ -95,7 +96,7 @@ internal class ChannelFanOutMutatorTest {
95 96
             }
96 97
         }
97 98
 
98
-        Assertions.assertTrue("#thegibson" in names)
99
-        Assertions.assertTrue("#dumpsterdiving" in names)
99
+        assertTrue("#thegibson" in names)
100
+        assertTrue("#dumpsterdiving" in names)
100 101
     }
101 102
 }

+ 6
- 6
src/test/kotlin/com/dmdirc/ktirc/events/mutators/ServerReadyMutatorTest.kt View File

@@ -7,17 +7,17 @@ import com.dmdirc.ktirc.io.MessageEmitter
7 7
 import com.dmdirc.ktirc.model.ServerState
8 8
 import com.dmdirc.ktirc.model.ServerStatus
9 9
 import com.dmdirc.ktirc.model.User
10
-import com.nhaarman.mockitokotlin2.doReturn
11
-import com.nhaarman.mockitokotlin2.mock
10
+import io.mockk.every
11
+import io.mockk.mockk
12 12
 import org.junit.jupiter.api.Assertions.*
13 13
 import org.junit.jupiter.api.Test
14 14
 
15 15
 internal class ServerReadyMutatorTest {
16 16
 
17
-    private val serverState = ServerState("", "")
18
-    private val messageEmitter = mock<MessageEmitter>()
19
-    private val ircClient = mock<IrcClient> {
20
-        on { serverState } doReturn serverState
17
+    private val messageEmitter = mockk<MessageEmitter>()
18
+    private val fakeServerState = ServerState("", "")
19
+    private val ircClient = mockk<IrcClient> {
20
+        every { serverState } returns fakeServerState
21 21
     }
22 22
 
23 23
     private val mutator = ServerReadyMutator()

+ 1
- 0
src/test/resources/io/mockk/settings.properties View File

@@ -0,0 +1 @@
1
+relaxUnitFun=true

Loading…
Cancel
Save