mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-24 14:30:09 +08:00
Introduce AbstractEventTest
This commit is contained in:
parent
684b844b44
commit
6b442b251f
28
mirai-core-api/src/jvmTest/kotlin/event/AbstractEventTest.kt
Normal file
28
mirai-core-api/src/jvmTest/kotlin/event/AbstractEventTest.kt
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.event
|
||||
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
|
||||
internal abstract class AbstractEventTest {
|
||||
@BeforeEach
|
||||
fun loadEventBroadcast() {
|
||||
_EventBroadcast.implementation = object : _EventBroadcast() {
|
||||
override suspend fun <E : Event> broadcastPublic(event: E): E =
|
||||
broadcastImpl(event) // do not call MiraiImpl
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun unloadEventBroadcast() {
|
||||
_EventBroadcast.implementation = _EventBroadcast() // restore
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
@ -9,13 +9,11 @@
|
||||
package net.mamoe.mirai.event
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import net.mamoe.mirai.event.broadcast
|
||||
import net.mamoe.mirai.event.subscribeAlways
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertFalse
|
||||
|
||||
|
||||
internal class CancelScopeTest {
|
||||
internal class CancelScopeTest : AbstractEventTest() {
|
||||
@Test
|
||||
fun testCancelScope() {
|
||||
val scope = CoroutineScope(SupervisorJob())
|
||||
|
@ -30,7 +30,7 @@ import kotlin.coroutines.suspendCoroutine
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
internal class EventChannelTest {
|
||||
internal class EventChannelTest : AbstractEventTest() {
|
||||
suspend fun suspendCall() {
|
||||
|
||||
}
|
||||
|
@ -22,8 +22,9 @@ class TestEvent : AbstractEvent() {
|
||||
var triggered = false
|
||||
}
|
||||
|
||||
class EventTests {
|
||||
internal class EventTests : AbstractEventTest() {
|
||||
var scope = CoroutineScope(EmptyCoroutineContext)
|
||||
|
||||
@AfterEach
|
||||
fun finiallyReset() {
|
||||
resetEventListeners()
|
||||
|
@ -13,7 +13,6 @@ package net.mamoe.mirai.event
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.mamoe.mirai.internal.event.GlobalEventListeners
|
||||
import org.jetbrains.annotations.NotNull
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
@ -22,7 +21,7 @@ import kotlin.coroutines.EmptyCoroutineContext
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
|
||||
internal class JvmMethodEventsTest {
|
||||
internal class JvmMethodEventsTest : AbstractEventTest() {
|
||||
|
||||
@Test
|
||||
fun testMethodListener() {
|
||||
|
@ -10,18 +10,31 @@
|
||||
package net.mamoe.mirai.event
|
||||
|
||||
import kotlinx.coroutines.cancel
|
||||
import net.mamoe.mirai.utils.JavaFriendlyAPI
|
||||
import net.mamoe.mirai.utils.EventListenerLikeJava
|
||||
import net.mamoe.mirai.utils.JavaFriendlyAPI
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@EventListenerLikeJava
|
||||
@JavaFriendlyAPI
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal class JvmMethodEventsTestJava : SimpleListenerHost() {
|
||||
internal class JvmMethodEventsTestJava : AbstractEventTest() {
|
||||
private val called = AtomicInteger(0)
|
||||
|
||||
@OptIn(JavaFriendlyAPI::class)
|
||||
@Test
|
||||
fun test() {
|
||||
val host = TestHost(called)
|
||||
GlobalEventChannel.registerListenerHost(host)
|
||||
TestEvent().__broadcastJava()
|
||||
assertEquals(3, called.get(), null)
|
||||
host.cancel() // reset listeners
|
||||
}
|
||||
}
|
||||
|
||||
@EventListenerLikeJava
|
||||
@Suppress("UNUSED_PARAMETER", "RedundantVisibilityModifier", "RedundantNullableReturnType")
|
||||
internal class TestHost(
|
||||
private val called: AtomicInteger
|
||||
) : SimpleListenerHost() {
|
||||
@EventHandler
|
||||
public fun ev(event: TestEvent?) {
|
||||
called.incrementAndGet()
|
||||
@ -39,11 +52,4 @@ internal class JvmMethodEventsTestJava : SimpleListenerHost() {
|
||||
return ListeningStatus.LISTENING
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
this.globalEventChannel().registerListenerHost(this)
|
||||
TestEvent().__broadcastJava()
|
||||
assertEquals(3, called.get(), null)
|
||||
cancel() // reset listeners
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,15 +10,15 @@
|
||||
package net.mamoe.mirai.event
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import net.mamoe.mirai.utils.JavaFriendlyAPI
|
||||
import net.mamoe.mirai.utils.EventListenerLikeJava
|
||||
import net.mamoe.mirai.utils.JavaFriendlyAPI
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
@JavaFriendlyAPI
|
||||
@EventListenerLikeJava
|
||||
class SimpleListenerHostTestJava {
|
||||
internal class SimpleListenerHostTestJava : AbstractEventTest() {
|
||||
@Test
|
||||
fun testJavaSimpleListenerHostWork() {
|
||||
val called = AtomicBoolean()
|
||||
|
Loading…
Reference in New Issue
Block a user