Add samples for take* and drop* extensions (KT-20357)

- Add samples for take, takeLast, takeWhile, takeLastWhile
- Add samples for drop, dropLast, dropWhile, dropLastWhile
This commit is contained in:
gzoritchak
2018-02-20 00:45:48 +01:00
committed by Ilya Gorbunov
parent fda40723dc
commit 1c1fe10e12
8 changed files with 562 additions and 14 deletions
@@ -316,12 +316,12 @@ class Collections {
val sb = StringBuilder("An existing string and a list: ")
val numbers = listOf(1, 2, 3)
assertPrints(numbers.joinTo(sb, prefix = "[", postfix = "]").toString(), "An existing string and a list: [1, 2, 3]")
val lotOfNumbers: Iterable<Int> = 1..100
val firstNumbers = StringBuilder("First five numbers: ")
assertPrints(lotOfNumbers.joinTo(firstNumbers, limit = 5).toString(), "First five numbers: 1, 2, 3, 4, 5, ...")
}
@Sample
fun joinToString() {
val numbers = listOf(1, 2, 3, 4, 5, 6)
@@ -333,6 +333,24 @@ class Collections {
assertPrints(chars.joinToString(limit = 5, truncated = "...!") { it.toUpperCase().toString() }, "A, B, C, D, E, ...!")
}
@Sample
fun take() {
val chars = ('a'..'z').toList()
assertPrints(chars.take(3), "[a, b, c]")
assertPrints(chars.takeWhile { it < 'f' }, "[a, b, c, d, e]")
assertPrints(chars.takeLast(2), "[y, z]")
assertPrints(chars.takeLastWhile { it > 'w' }, "[x, y, z]")
}
@Sample
fun drop() {
val chars = ('a'..'z').toList()
assertPrints(chars.drop(23), "[x, y, z]")
assertPrints(chars.dropLast(23), "[a, b, c]")
assertPrints(chars.dropWhile { it < 'x' }, "[x, y, z]")
assertPrints(chars.dropLastWhile { it > 'c' }, "[a, b, c]")
}
@Sample
fun chunked() {
val words = "one two three four five six seven eight nine ten".split(' ')
@@ -341,7 +359,6 @@ class Collections {
assertPrints(chunks, "[[one, two, three], [four, five, six], [seven, eight, nine], [ten]]")
}
@Sample
fun zipWithNext() {
val letters = ('a'..'f').toList()