mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-04 07:19:18 +08:00
Rewrite console testFramework
This commit is contained in:
parent
d0c2304e1a
commit
6db81a50ac
@ -31,69 +31,6 @@ import kotlin.coroutines.resume
|
||||
import kotlin.io.path.createTempDirectory
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
@OptIn(ConsoleInternalApi::class, kotlin.io.path.ExperimentalPathApi::class)
|
||||
fun initTestEnvironment() {
|
||||
object : MiraiConsoleImplementation {
|
||||
override val rootPath: Path = createTempDirectory()
|
||||
|
||||
override val frontEndDescription: MiraiConsoleFrontEndDescription
|
||||
get() = object : MiraiConsoleFrontEndDescription {
|
||||
override val name: String
|
||||
get() = "Test"
|
||||
override val vendor: String
|
||||
get() = "Test"
|
||||
override val version: SemVersion
|
||||
get() = SemVersion("1.0.0")
|
||||
|
||||
}
|
||||
override val builtInPluginLoaders: List<Lazy<PluginLoader<*, *>>> = listOf(lazy { JvmPluginLoader })
|
||||
override val jvmPluginLoader: JvmPluginLoader by lazy {
|
||||
backendAccess.createDefaultJvmPluginLoader(coroutineContext)
|
||||
}
|
||||
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 =
|
||||
LoginSolver.Default!!
|
||||
|
||||
override fun createLogger(identity: String?): MiraiLogger {
|
||||
return PlatformLogger(identity)
|
||||
}
|
||||
|
||||
override val consoleDataScope: MiraiConsoleImplementation.ConsoleDataScope by lazy {
|
||||
MiraiConsoleImplementation.ConsoleDataScope.createDefault(
|
||||
coroutineContext,
|
||||
dataStorageForBuiltIns,
|
||||
configStorageForBuiltIns
|
||||
)
|
||||
}
|
||||
override val coroutineContext: CoroutineContext =
|
||||
CoroutineName("Console Main") + SupervisorJob() + CoroutineExceptionHandler { _, throwable ->
|
||||
throwable.printStackTrace()
|
||||
}
|
||||
}.start()
|
||||
CommandManager
|
||||
}
|
||||
|
||||
internal object Testing {
|
||||
@Volatile
|
||||
internal var cont: Continuation<Any?>? = null
|
||||
|
@ -22,7 +22,7 @@ import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterCommand
|
||||
import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser
|
||||
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
||||
import net.mamoe.mirai.console.command.descriptor.buildCommandArgumentContext
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleTest
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest
|
||||
import net.mamoe.mirai.console.internal.command.CommandManagerImpl
|
||||
import net.mamoe.mirai.console.internal.command.flattenCommandComponents
|
||||
import net.mamoe.mirai.message.data.*
|
||||
@ -98,7 +98,7 @@ internal val owner by lazy { TestUnitCommandOwner }
|
||||
|
||||
|
||||
@OptIn(ExperimentalCommandDescriptors::class)
|
||||
internal class TestCommand : AbstractConsoleTest() {
|
||||
internal class InstanceTestCommand : AbstractConsoleInstanceTest() {
|
||||
@Test
|
||||
fun testRegister() {
|
||||
try {
|
@ -8,10 +8,10 @@
|
||||
*/
|
||||
package net.mamoe.mirai.console.data
|
||||
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleTest
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest
|
||||
|
||||
@Suppress("unused")
|
||||
class JavaPluginDescriptionTests : AbstractConsoleTest() {
|
||||
class JavaPluginDescriptionTests : AbstractConsoleInstanceTest() {
|
||||
|
||||
// @Test
|
||||
// fun testSimpleValue() {
|
||||
|
@ -10,7 +10,7 @@
|
||||
package net.mamoe.mirai.console.data
|
||||
|
||||
import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleTest
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest
|
||||
import net.mamoe.mirai.console.internal.data.mkdir
|
||||
import net.mamoe.mirai.console.plugin.PluginManager
|
||||
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load
|
||||
@ -21,7 +21,7 @@ import net.mamoe.mirai.console.plugin.name
|
||||
import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class PluginMovingTests : AbstractConsoleTest() {
|
||||
class PluginMovingTests : AbstractConsoleInstanceTest() {
|
||||
private val mockPluginWithName = object : KotlinPlugin(JvmPluginDescription("org.test1.test1", "1.0.0", "test1")) {}
|
||||
private val mockPluginWithName2 =
|
||||
object : KotlinPlugin(JvmPluginDescription("org.test2.test2", "1.0.0", "test2")) {}
|
||||
|
@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertFails
|
||||
|
||||
internal class PermissionsBasicsTest {
|
||||
@Suppress("ILLEGAL_PERMISSION_NAMESPACE", "ILLEGAL_PERMISSION_NAME") // inspection by mirai-console-intellij
|
||||
@Test
|
||||
fun testInvalidPermissionId() {
|
||||
assertFails { PermissionId("space namespace", "name") }
|
||||
@ -22,9 +23,4 @@ internal class PermissionsBasicsTest {
|
||||
assertFails { PermissionId("namespace:name", "name") }
|
||||
assertFails { PermissionId("namespace", "namespace:name") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parentsWithSelfSequence() {
|
||||
|
||||
}
|
||||
}
|
@ -14,22 +14,26 @@ import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.MiraiConsoleImplementation
|
||||
import net.mamoe.mirai.console.initTestEnvironment
|
||||
import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start
|
||||
import net.mamoe.mirai.console.command.CommandManager
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
|
||||
abstract class AbstractConsoleTest {
|
||||
abstract class AbstractConsoleInstanceTest {
|
||||
val mockPlugin by lazy { mockKotlinPlugin() }
|
||||
private lateinit var implementation: MiraiConsoleImplementation
|
||||
val consoleImplementation: MiraiConsoleImplementation by ::implementation
|
||||
|
||||
@BeforeEach
|
||||
protected open fun initializeConsole() {
|
||||
initTestEnvironment()
|
||||
this.implementation = MockConsoleImplementation().apply { start() }
|
||||
CommandManager
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
protected open fun cancelConsole() {
|
||||
protected open fun stopConsole() {
|
||||
if (MiraiConsoleImplementation.instanceInitialized) {
|
||||
try {
|
||||
runBlocking { MiraiConsole.job.cancelAndJoin() }
|
||||
@ -44,8 +48,8 @@ abstract class AbstractConsoleTest {
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun mockKotlinPlugin(): KotlinPlugin {
|
||||
return object : KotlinPlugin(JvmPluginDescription("org.test.test", "1.0.0")) {}
|
||||
fun mockKotlinPlugin(id: String = "org.test.test"): KotlinPlugin {
|
||||
return object : KotlinPlugin(JvmPluginDescription(id, "1.0.0")) {}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.console.testFramework
|
||||
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.CoroutineName
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import net.mamoe.mirai.console.MiraiConsoleFrontEndDescription
|
||||
import net.mamoe.mirai.console.MiraiConsoleImplementation
|
||||
import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start
|
||||
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.ConsoleInput
|
||||
import net.mamoe.mirai.console.util.SemVersion
|
||||
import net.mamoe.mirai.message.data.Message
|
||||
import net.mamoe.mirai.utils.BotConfiguration
|
||||
import net.mamoe.mirai.utils.LoginSolver
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.PlatformLogger
|
||||
import java.nio.file.Path
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.io.path.createTempDirectory
|
||||
|
||||
open class MockConsoleImplementation : MiraiConsoleImplementation {
|
||||
override val rootPath: Path = createTempDirectory()
|
||||
|
||||
override val frontEndDescription: MiraiConsoleFrontEndDescription
|
||||
get() = object : MiraiConsoleFrontEndDescription {
|
||||
override val name: String get() = "Test"
|
||||
override val vendor: String get() = "Test"
|
||||
override val version: SemVersion get() = SemVersion("1.0.0")
|
||||
}
|
||||
override val builtInPluginLoaders: List<Lazy<PluginLoader<*, *>>> = listOf(lazy { JvmPluginLoader })
|
||||
override val jvmPluginLoader: JvmPluginLoader by lazy {
|
||||
backendAccess.createDefaultJvmPluginLoader(coroutineContext)
|
||||
}
|
||||
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 =
|
||||
LoginSolver.Default!!
|
||||
|
||||
override fun createLogger(identity: String?): MiraiLogger {
|
||||
return PlatformLogger(identity)
|
||||
}
|
||||
|
||||
override val consoleDataScope: MiraiConsoleImplementation.ConsoleDataScope by lazy {
|
||||
MiraiConsoleImplementation.ConsoleDataScope.createDefault(
|
||||
coroutineContext,
|
||||
dataStorageForBuiltIns,
|
||||
configStorageForBuiltIns
|
||||
)
|
||||
}
|
||||
override val coroutineContext: CoroutineContext =
|
||||
CoroutineName("Console Main") + SupervisorJob() + CoroutineExceptionHandler { _, throwable ->
|
||||
throwable.printStackTrace()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline fun <R> withConsoleImplementation(crossinline block: MockConsoleImplementation.() -> R): R {
|
||||
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
|
||||
MockConsoleImplementation().run {
|
||||
start()
|
||||
try {
|
||||
return block()
|
||||
} finally {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
@ -7,14 +7,14 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.console.framework.test
|
||||
package net.mamoe.mirai.console.testFramework.test
|
||||
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleTest
|
||||
import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest
|
||||
import net.mamoe.mirai.console.plugin.PluginManager
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class FrameworkTest : AbstractConsoleTest() {
|
||||
class FrameworkInstanceTest : AbstractConsoleInstanceTest() {
|
||||
|
||||
@Test
|
||||
fun testConsole1() {
|
Loading…
Reference in New Issue
Block a user