TestBase.kt 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. package net.mamoe.mirai.mock.test
  10. import kotlinx.coroutines.CoroutineScope
  11. import kotlinx.coroutines.runBlocking
  12. import net.mamoe.mirai.event.Event
  13. import net.mamoe.mirai.event.events.MessageEvent
  14. import net.mamoe.mirai.event.events.MessagePostSendEvent
  15. import net.mamoe.mirai.event.events.MessagePreSendEvent
  16. import org.junit.jupiter.api.fail
  17. import java.net.URL
  18. import kotlin.reflect.jvm.jvmName
  19. import kotlin.test.assertFails
  20. internal open class TestBase {
  21. internal inline fun <reified T> assertIsInstance(value: Any?, block: T.() -> Unit = {}) {
  22. if (value !is T) {
  23. fail { "Actual value $value (${value?.javaClass}) is not instanceof ${T::class.jvmName}" }
  24. }
  25. block(value)
  26. }
  27. internal fun runTest(action: suspend CoroutineScope.() -> Unit) {
  28. runBlocking(block = action)
  29. }
  30. internal fun List<Event>.dropMessagePrePost() = filterNot {
  31. it is MessagePreSendEvent || it is MessagePostSendEvent<*>
  32. }
  33. internal fun List<Event>.dropMsgChat() = filterNot {
  34. it is MessageEvent || it is MessagePreSendEvent || it is MessagePostSendEvent<*>
  35. }
  36. internal fun String.toUrl(): URL = URL(this)
  37. internal inline fun <T> T.runAndAssertFails(block: T.() -> Unit) {
  38. assertFails { block() }
  39. }
  40. }