mirai/backend/mirai-console/test/TestMiraiConosle.kt

113 lines
4.4 KiB
Kotlin
Raw Normal View History

/*
2020-08-16 23:36:24 +08:00
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
2020-08-23 17:46:51 +08:00
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console
2020-09-01 08:52:24 +08:00
import kotlinx.coroutines.*
import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start
import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.data.MemoryPluginDataStorage
import net.mamoe.mirai.console.data.PluginDataStorage
import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader
import net.mamoe.mirai.console.plugin.loader.PluginLoader
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.console.util.ConsoleInput
import net.mamoe.mirai.console.util.ConsoleInternalApi
2020-09-20 18:01:02 +08:00
import net.mamoe.mirai.console.util.SemVersion
import net.mamoe.mirai.message.data.Message
2020-10-24 19:34:16 +08:00
import net.mamoe.mirai.utils.*
import java.nio.file.Path
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.resume
2020-11-25 17:19:52 +08:00
import kotlin.io.path.createTempDirectory
import kotlin.test.assertNotNull
2020-11-25 17:19:52 +08:00
@OptIn(ConsoleInternalApi::class, kotlin.io.path.ExperimentalPathApi::class)
fun initTestEnvironment() {
object : MiraiConsoleImplementation {
2020-11-25 17:19:52 +08:00
override val rootPath: Path = createTempDirectory()
@ConsoleExperimentalApi
override val frontEndDescription: MiraiConsoleFrontEndDescription
2020-08-26 22:58:52 +08:00
get() = object : MiraiConsoleFrontEndDescription {
override val name: String
get() = "Test"
override val vendor: String
get() = "Test"
2020-09-20 18:01:02 +08:00
override val version: SemVersion
get() = SemVersion("1.0.0")
2020-08-26 22:58:52 +08:00
}
override val builtInPluginLoaders: List<Lazy<PluginLoader<*, *>>> = listOf(lazy { JvmPluginLoader })
override val consoleCommandSender: MiraiConsoleImplementation.ConsoleCommandSenderImpl =
object : MiraiConsoleImplementation.ConsoleCommandSenderImpl {
override suspend fun sendMessage(message: Message) {
println(message)
}
override suspend fun sendMessage(message: String) {
println(message)
}
}
override val dataStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage()
override val configStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage()
override val dataStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage()
override val configStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage()
override val consoleInput: ConsoleInput = object : ConsoleInput {
override suspend fun requestInput(hint: String): String {
println(hint)
return readLine() ?: error("No stdin")
}
}
override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver =
2020-12-25 20:45:38 +08:00
LoginSolver.Default!!
2020-10-26 23:20:10 +08:00
override fun createLogger(identity: String?): MiraiLogger {
return PlatformLogger(identity)
}
2020-09-01 08:52:24 +08:00
override val coroutineContext: CoroutineContext = SupervisorJob() + CoroutineExceptionHandler { _, throwable ->
throwable.printStackTrace()
}
}.start()
CommandManager
}
internal object Testing {
@Volatile
internal var cont: Continuation<Any?>? = null
@Suppress("UNCHECKED_CAST")
2020-10-24 13:14:25 +08:00
suspend fun <R> withTesting(timeout: Long = 50000L, block: suspend () -> Unit): R {
@Suppress("RemoveExplicitTypeArguments") // bug
return if (timeout != -1L) {
withTimeout<R>(timeout) {
suspendCancellableCoroutine<R> { ct ->
this@Testing.cont = ct as Continuation<Any?>
runBlocking { block() }
}
}
} else {
suspendCancellableCoroutine<R> { ct ->
this.cont = ct as Continuation<Any?>
runBlocking { block() }
}
}
}
fun ok(result: Any? = Unit) {
val cont = cont
assertNotNull(cont)
cont.resume(result)
}
}