KT-15363 Collection Builders
Added container builders for lists, sets, and maps. The new experimental type inference only works for the simple builders with a single generic type. The versions with nullability and the maps still require explicit specification of the types. Obviously explicit specification is required for all users who are not using the new experimental inference. Improving the type inference is something that should be done separately and many things – including these builders – will benefit from it, however, this is not a blocker for these builders in my opinion.
This commit is contained in:
committed by
Ilya Gorbunov
parent
fa811c731f
commit
bf9d3d87a6
@@ -0,0 +1,68 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
class ContainerBuilderTest {
|
||||
@Test fun buildList() {
|
||||
val x = buildList {
|
||||
add('b')
|
||||
add('c')
|
||||
}
|
||||
|
||||
val y = buildList(4) {
|
||||
add('a')
|
||||
addAll(x)
|
||||
add('d')
|
||||
}
|
||||
|
||||
assertEquals(listOf('a', 'b', 'c', 'd'), y)
|
||||
}
|
||||
|
||||
@Test fun exceptionIsThrownIfExpectedListSizeIsNegative() {
|
||||
assertFailsWith(IllegalArgumentException::class) {
|
||||
buildList<Any?>(-1) {}
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun buildSet() {
|
||||
val x = buildSet {
|
||||
add('b')
|
||||
add('c')
|
||||
}
|
||||
|
||||
val y = buildSet(4) {
|
||||
add('a')
|
||||
addAll(x)
|
||||
add('d')
|
||||
}
|
||||
|
||||
assertEquals(setOf('a', 'b', 'c', 'd'), y)
|
||||
}
|
||||
|
||||
@Test fun exceptionIsThrownIfExpectedSetSizeIsNegative() {
|
||||
assertFailsWith(IllegalArgumentException::class) {
|
||||
buildSet<Any?>(-1) {}
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun buildMap() {
|
||||
val x = buildMap<Char, Int> {
|
||||
put('b', 2)
|
||||
put('c', 3)
|
||||
}
|
||||
|
||||
val y = buildMap<Char, Int>(4) {
|
||||
put('a', 1)
|
||||
putAll(x)
|
||||
put('d', 4)
|
||||
}
|
||||
|
||||
assertEquals(mapOf('a' to 1, 'b' to 2, 'c' to 3, 'd' to 4), y)
|
||||
}
|
||||
|
||||
@Test fun exceptionIsThrownIfExpectedMapSizeIsNegative() {
|
||||
assertFailsWith(IllegalArgumentException::class) {
|
||||
buildMap<Any?, Any?>(-1) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user