KT-15363 Collection builders: improve tests and samples
This commit is contained in:
@@ -22,15 +22,15 @@ class Builders {
|
||||
class Sets {
|
||||
@Sample
|
||||
fun buildSetSample() {
|
||||
val x = setOf('b', 'c')
|
||||
val x = setOf('a', 'b')
|
||||
|
||||
val y = buildSet(x.size + 2) {
|
||||
add('a')
|
||||
add('b')
|
||||
addAll(x)
|
||||
add('d')
|
||||
add('c')
|
||||
}
|
||||
|
||||
assertPrints(y, "[a, b, c, d]")
|
||||
assertPrints(y, "[b, a, c]")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,11 +41,12 @@ class Builders {
|
||||
|
||||
val y = buildMap<Char, Int>(x.size + 2) {
|
||||
put('a', 1)
|
||||
put('c', 0)
|
||||
putAll(x)
|
||||
put('d', 4)
|
||||
}
|
||||
|
||||
assertPrints(y, "{a=1, b=2, c=3, d=4}")
|
||||
assertPrints(y, "{a=1, c=3, b=2, d=4}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ package test.collections
|
||||
import kotlin.test.*
|
||||
|
||||
class ContainerBuilderTest {
|
||||
@Test fun buildList() {
|
||||
@Test
|
||||
fun buildList() {
|
||||
val x = buildList {
|
||||
add('b')
|
||||
add('c')
|
||||
@@ -16,36 +17,34 @@ class ContainerBuilderTest {
|
||||
}
|
||||
|
||||
assertEquals(listOf('a', 'b', 'c', 'd'), y)
|
||||
}
|
||||
|
||||
@Test fun exceptionIsThrownIfExpectedListSizeIsNegative() {
|
||||
assertFailsWith(IllegalArgumentException::class) {
|
||||
buildList<Any?>(-1) {}
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
buildList(-1) { add(0) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun buildSet() {
|
||||
@Test
|
||||
fun buildSet() {
|
||||
val x = buildSet {
|
||||
add('b')
|
||||
add('c')
|
||||
}
|
||||
|
||||
val y = buildSet(4) {
|
||||
add('a')
|
||||
add('c')
|
||||
addAll(x)
|
||||
add('d')
|
||||
}
|
||||
|
||||
assertEquals(setOf('a', 'b', 'c', 'd'), y)
|
||||
}
|
||||
assertEquals(setOf('c', 'b', 'd'), y)
|
||||
|
||||
@Test fun exceptionIsThrownIfExpectedSetSizeIsNegative() {
|
||||
assertFailsWith(IllegalArgumentException::class) {
|
||||
buildSet<Any?>(-1) {}
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
buildSet(-1) { add(0) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun buildMap() {
|
||||
@Test
|
||||
fun buildMap() {
|
||||
val x = buildMap<Char, Int> {
|
||||
put('b', 2)
|
||||
put('c', 3)
|
||||
@@ -53,16 +52,15 @@ class ContainerBuilderTest {
|
||||
|
||||
val y = buildMap<Char, Int>(4) {
|
||||
put('a', 1)
|
||||
put('c', 0)
|
||||
putAll(x)
|
||||
put('d', 4)
|
||||
}
|
||||
|
||||
assertEquals(mapOf('a' to 1, 'b' to 2, 'c' to 3, 'd' to 4), y)
|
||||
}
|
||||
assertEquals(mapOf('a' to 1, 'c' to 3, 'b' to 2, 'd' to 4), y)
|
||||
|
||||
@Test fun exceptionIsThrownIfExpectedMapSizeIsNegative() {
|
||||
assertFailsWith(IllegalArgumentException::class) {
|
||||
buildMap<Any?, Any?>(-1) {}
|
||||
assertFailsWith<IllegalArgumentException> {
|
||||
buildMap<String, Int>(-1) { put("x", 1) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user