-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make MessageChain truly read-only; fix #1676
- Loading branch information
Showing
4 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
mirai-core-api/src/commonTest/kotlin/message.data/MessageChainImmutableTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") | ||
|
||
package net.mamoe.mirai.message.data | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertFails | ||
import java.util.List as JdkList | ||
|
||
internal open class MessageChainImmutableTest { | ||
fun msg0(): MessageChain = messageChainOf( | ||
AtAll, PlainText("Hello!"), At(114514), | ||
) | ||
|
||
fun msgAsJdk(): JdkList<SingleMessage> { | ||
return msg0() as java.util.List<SingleMessage> | ||
} | ||
|
||
@Test | ||
fun `direct access`() { | ||
val chain = msgAsJdk() | ||
|
||
assertFails { chain.set(0, AtAll) } | ||
assertFails { chain.remove(0) } | ||
assertFails { chain.clear() } | ||
assertFails { chain.add(PlainText("Hey Hey!")) } | ||
} | ||
|
||
@Test | ||
fun `iterator access`() { | ||
val chain = msgAsJdk() | ||
assertFails { chain.iterator().remove() } | ||
assertFails { chain.iterator().also { it.next() }.remove() } | ||
assertFails { chain.listIterator().remove() } | ||
assertFails { chain.listIterator().also { it.next() }.remove() } | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
mirai-core-api/src/jvmTest/kotlin/message/data/MessageChainImmutableTest_JDK8.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") | ||
|
||
package net.mamoe.mirai.message.data | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertFails | ||
|
||
internal class MessageChainImmutableTest_JDK8 : MessageChainImmutableTest() { | ||
@Test | ||
fun `access with JDK8 lambda`() { | ||
val chain = msgAsJdk() | ||
assertFails { chain.removeIf { true } } | ||
assertFails { chain.replaceAll { AtAll } } | ||
assertFails { chain.sort { o1, o2 -> o1.javaClass.name.compareTo(o2.javaClass.name) } } | ||
assertFails { chain.clear() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters