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