Collection.toString() should not throw if it contains itself

Merge-request: KT-MR-10591
Merged-by: Abduqodiri Qurbonzoda <abduqodiri.qurbonzoda@jetbrains.com>
This commit is contained in:
Abduqodiri Qurbonzoda
2023-07-11 09:58:38 +00:00
committed by Space Team
parent 6fc02c3408
commit f152fa537d
7 changed files with 72 additions and 28 deletions
@@ -5,9 +5,7 @@
package test.collections
import test.assertIsNegativeZero
import test.assertIsPositiveZero
import test.assertStaticAndRuntimeTypeIs
import test.*
import kotlin.test.*
import test.collections.behaviors.*
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
@@ -1224,6 +1222,57 @@ class CollectionTest {
assertEquals("[1, a, null, ${Long.MAX_VALUE.toString()}]", listOf(1, "a", null, Long.MAX_VALUE).toString())
}
@Test fun toStringContainingThis() = testExceptOn(TestPlatform.Js) {
// resulting string is platform-dependent, but shouldn't throw
arrayOf<Any>("a", "b", "c").apply { this[1] = this }.toString()
assertEquals(
"[a, (this Collection), c]",
arrayListOf<Any>("a", "b", "c").apply { this[1] = this }.toString()
)
assertEquals(
"[a, (this Collection), c]",
buildList<Any> {
addAll(listOf("a", "b", "c"))
this[1] = this
}.toString()
)
assertEquals(
"[a, (this Collection), c]",
linkedSetOf<Any>().apply {
add("a")
add(this)
add("c")
}.toString()
)
assertEquals(
"[a, (this Collection), c]",
buildSet<Any> {
add("a")
add(this)
add("c")
}.toString()
)
assertEquals(
"{a=1, (this Map)=(this Map), c=3}",
linkedMapOf<Any, Any>().apply {
put("a", "1")
put(this, this)
put("c", "3")
}.toString()
)
assertEquals(
"{a=1, (this Map)=(this Map), c=3}",
buildMap<Any, Any> {
put("a", "1")
put(this, this)
put("c", "3")
}.toString()
)
}
@Test fun randomAccess() {
assertStaticAndRuntimeTypeIs<RandomAccess>(arrayListOf(1))
assertTrue(listOf(1, 2) is RandomAccess, "Default read-only list implementation is RandomAccess")