Skip to content

Commit 6e137f8

Browse files
committed
Verify immutable list behavior by comparing it with standard readonly list.
1 parent e09a5b0 commit 6e137f8

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

kotlinx-collections-immutable/tests/src/test/kotlin/CollectionBehaviors.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public fun <T> CompareContext<List<T>>.listBehavior() {
3131
for (index in expected.indices)
3232
propertyEquals { this[index] }
3333

34-
propertyFails { this[size] }
34+
propertyFailsWith<IndexOutOfBoundsException> { this[size] }
3535

3636
propertyEquals { indexOf(elementAtOrNull(0)) }
3737
propertyEquals { lastIndexOf(elementAtOrNull(0)) }

kotlinx-collections-immutable/tests/src/test/kotlin/ComparisonDSL.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@ public class CompareContext<out T>(public val expected: T, public val actual: T)
1515
assertEquals(expected.getter(), actual.getter(), message)
1616
}
1717
public fun propertyFails(getter: T.() -> Unit) { assertFailEquals({expected.getter()}, {actual.getter()}) }
18+
public inline fun <reified E> propertyFailsWith(noinline getter: T.() -> Unit) = propertyFailsWith({ it is E }, getter)
19+
public fun propertyFailsWith(exceptionPredicate: (Throwable) -> Boolean, getter: T.() -> Unit) { assertFailEquals({expected.getter()}, {actual.getter()}, exceptionPredicate) }
1820
public fun <P> compareProperty(getter: T.() -> P, block: CompareContext<P>.() -> Unit) {
1921
compare(expected.getter(), actual.getter(), block)
2022
}
2123

22-
private fun assertFailEquals(expected: () -> Unit, actual: () -> Unit) {
24+
private fun assertFailEquals(expected: () -> Unit, actual: () -> Unit, exceptionPredicate: ((Throwable) -> Boolean)? = null) {
2325
val expectedFail = assertFails(expected)
2426
val actualFail = assertFails(actual)
25-
//assertEquals(expectedFail != null, actualFail != null)
26-
assertTypeEquals(expectedFail, actualFail)
27+
28+
if (exceptionPredicate != null) {
29+
assertTrue(exceptionPredicate(expectedFail), "expected fail")
30+
assertTrue(exceptionPredicate(actualFail), "actual fail")
31+
} else {
32+
//assertEquals(expectedFail != null, actualFail != null)
33+
assertTypeEquals(expectedFail, actualFail)
34+
}
2735
}
2836

2937
public fun assertTypeEquals(expected: Any?, actual: Any?) {

kotlinx-collections-immutable/tests/src/test/kotlin/kotlinx.collections.immutable/ImmutableListTest.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package kotlinx.collections.immutable
22

33
import org.junit.Test
4+
import test.collections.behaviors.listBehavior
5+
import test.collections.compare
46
import kotlin.test.*
57

68
class ImmutableListTest {
79

10+
private fun <T> compareLists(expected: List<T>, actual: List<T>) = compare(expected, actual) { listBehavior() }
11+
12+
813
@Test fun empty() {
914
val empty1 = immutableListOf<Int>()
1015
val empty2 = immutableListOf<String>()
1116
assertEquals<ImmutableList<Any>>(empty1, empty2)
1217
assertEquals<List<Any>>(listOf(), empty1)
1318
assertTrue(empty1 === empty2)
19+
20+
assertFailsWith<NoSuchElementException> { empty1.iterator().next() }
21+
22+
compareLists(emptyList(), empty1)
23+
1424
}
1525

1626
@Test fun ofElements() {
1727
val list0 = listOf("a", "d", 1, null)
1828
val list1 = immutableListOf("a", "d", 1, null)
1929
val list2 = immutableListOf("a", "d", 1, null)
2030

21-
assertEquals(list0, list1)
31+
compareLists(list0, list1)
2232
assertEquals(list1, list2)
2333
}
2434

@@ -30,15 +40,13 @@ class ImmutableListTest {
3040
val immList2 = immList.toImmutableList()
3141
assertTrue(immList2 === immList)
3242

33-
assertEquals<List<*>>(list, immList) // problem
34-
assertEquals(list.toString(), immList.toString())
35-
assertEquals(list.hashCode(), immList.hashCode())
43+
compareLists(original, immList)
3644

3745
list.removeAt(0)
3846
assertNotEquals<List<*>>(list, immList)
3947

4048
immList = immList.removeAt(0)
41-
assertEquals<List<*>>(list, immList) // problem
49+
compareLists(list, immList)
4250
}
4351

4452
@Test fun addElements() {
@@ -50,7 +58,7 @@ class ImmutableListTest {
5058
list = list + "y"
5159
list += "z"
5260
list += arrayOf("1", "2").asIterable()
53-
assertEquals("abcxaxyz12".map { it.toString() }, list)
61+
compareLists("abcxaxyz12".map { it.toString() }, list)
5462
}
5563

5664
@Test fun replaceElements() {
@@ -68,7 +76,7 @@ class ImmutableListTest {
6876
@Test fun removeElements() {
6977
val list = "abcxaxyz12".toImmutableList()
7078
fun expectList(content: String, list: ImmutableList<Char>) {
71-
assertEquals(content, list.joinToString(""))
79+
compareLists(content.toList(), list)
7280
}
7381

7482
expectList("bcxaxyz12", list.removeAt(0))
@@ -86,7 +94,7 @@ class ImmutableListTest {
8694
val list = "abcxaxyz12".toImmutableList()
8795
val subList = list.subList(2, 5) // 2, 3, 4
8896
assertTrue(subList is ImmutableList)
89-
assertEquals(listOf('c', 'x', 'a'), subList)
97+
compareLists(listOf('c', 'x', 'a'), subList)
9098

9199
assertFailsWith<IndexOutOfBoundsException> { list.subList(-1, 2) }
92100
assertFailsWith<IndexOutOfBoundsException> { list.subList(0, list.size + 1) }
@@ -136,8 +144,8 @@ class ImmutableListTest {
136144
operation(mutable)
137145
operation(builder)
138146

139-
assertEquals(mutable, builder)
140-
assertEquals<List<*>>(mutable, builder.build())
147+
compareLists(mutable, builder)
148+
compareLists(mutable, builder.build())
141149
}
142150

143151
@Test fun noOperation() {

0 commit comments

Comments
 (0)