Rework existing unit tests used as samples.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package samples.collections
|
||||
|
||||
import samples.*
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
|
||||
@RunWith(Enclosed::class)
|
||||
class Collections {
|
||||
|
||||
class Lists {
|
||||
@Sample
|
||||
fun lastIndexOfList() {
|
||||
assertPrints(emptyList<Any>().lastIndex, "-1")
|
||||
assertPrints(listOf("a", "x", "y").lastIndex, "2")
|
||||
}
|
||||
}
|
||||
|
||||
class Transformations {
|
||||
|
||||
@Sample
|
||||
fun groupBy() {
|
||||
val words = listOf("a", "abc", "ab", "def", "abcd")
|
||||
val byLength = words.groupBy { it.length }
|
||||
|
||||
assertPrints(byLength.keys, "[1, 3, 2, 4]")
|
||||
assertPrints(byLength.values, "[[a], [abc, def], [ab], [abcd]]")
|
||||
|
||||
val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
|
||||
// same content as in byLength map, but the map is mutable
|
||||
assertTrue(mutableByLength == byLength)
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun groupByKeysAndValues() {
|
||||
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
|
||||
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
|
||||
assertPrints(namesByTeam, "{Marketing=[Alice, Carol], Sales=[Bob]}")
|
||||
|
||||
val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
|
||||
// same content as in namesByTeam map, but the map is mutable
|
||||
assertTrue(mutableNamesByTeam == namesByTeam)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package samples.collections
|
||||
|
||||
import samples.*
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
import kotlin.comparisons.*
|
||||
|
||||
@RunWith(Enclosed::class)
|
||||
class Maps {
|
||||
|
||||
class Instantiation {
|
||||
|
||||
@Sample
|
||||
fun mapFromPairs() {
|
||||
val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
|
||||
assertPrints(map, "{1=x, 2=y, -1=zz}")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun mutableMapFromPairs() {
|
||||
val map = mutableMapOf(1 to "x", 2 to "y", -1 to "zz")
|
||||
assertPrints(map, "{1=x, 2=y, -1=zz}")
|
||||
|
||||
map[1] = "a"
|
||||
assertPrints(map, "{1=a, 2=y, -1=zz}")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun hashMapFromPairs() {
|
||||
val map: HashMap<Int, String> = hashMapOf(1 to "x", 2 to "y", -1 to "zz")
|
||||
assertPrints(map, "{-1=zz, 1=x, 2=y}")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun linkedMapFromPairs() {
|
||||
val map: LinkedHashMap<Int, String> = linkedMapOf(1 to "x", 2 to "y", -1 to "zz")
|
||||
assertPrints(map, "{1=x, 2=y, -1=zz}")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun sortedMapFromPairs() {
|
||||
val map = sortedMapOf(Pair("c", 3), Pair("b", 2), Pair("d", 1))
|
||||
assertPrints(map.keys, "[b, c, d]")
|
||||
assertPrints(map.values, "[2, 3, 1]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun emptyReadOnlyMap() {
|
||||
val map = emptyMap<String, Int>()
|
||||
assertTrue(map.isEmpty())
|
||||
|
||||
val anotherMap = mapOf<String, Int>()
|
||||
assertTrue(map == anotherMap, "Empty maps are equal")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun emptyMutableMap() {
|
||||
val map = mutableMapOf<Int, Any?>()
|
||||
assertTrue(map.isEmpty())
|
||||
|
||||
map[1] = "x"
|
||||
map[2] = 1.05
|
||||
// Now map contains something:
|
||||
assertPrints(map, "{1=x, 2=1.05}")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Usage {
|
||||
|
||||
@Sample
|
||||
fun getOrElse() {
|
||||
val map = mutableMapOf<String, Int?>()
|
||||
assertPrints(map.getOrElse("x") { 1 }, "1")
|
||||
|
||||
map["x"] = 3
|
||||
assertPrints(map.getOrElse("x") { 1 }, "3")
|
||||
|
||||
map["x"] = null
|
||||
assertPrints(map.getOrElse("x") { 1 }, "1")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun getOrPut() {
|
||||
val map = mutableMapOf<String, Int?>()
|
||||
assertPrints(map.getOrPut("x") { 2 }, "2")
|
||||
assertPrints(map.getOrPut("x") { 3 }, "2") // still
|
||||
|
||||
assertPrints(map.getOrPut("y") { null }, "null")
|
||||
assertPrints(map.getOrPut("y") { 42 }, "42") // but!
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun forOverEntries() {
|
||||
val map = mapOf("beverage" to 2.7, "meal" to 12.4, "dessert" to 5.8)
|
||||
|
||||
for ((key, value) in map) {
|
||||
println("$key - $value") // prints: beverage - 2.7
|
||||
// prints: meal - 12.4
|
||||
// prints: dessert - 5.8
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Transformations {
|
||||
|
||||
@Sample
|
||||
fun mapKeys() {
|
||||
val map1 = mapOf("beer" to 2.7, "bisquit" to 5.8)
|
||||
val map2 = map1.mapKeys { it.key.length }
|
||||
assertPrints(map2, "{4=2.7, 7=5.8}")
|
||||
|
||||
val map3 = map1.mapKeys { it.key.take(1) }
|
||||
assertPrints(map3, "{b=5.8}")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun mapValues() {
|
||||
val map1 = mapOf("beverage" to 2.7, "meal" to 12.4)
|
||||
val map2 = map1.mapValues { it.value.toString() + "$" }
|
||||
|
||||
assertPrints(map2, "{beverage=2.7$, meal=12.4$}")
|
||||
}
|
||||
|
||||
|
||||
@Sample
|
||||
fun mapToSortedMap() {
|
||||
val map = mapOf(Pair("c", 3), Pair("b", 2), Pair("d", 1))
|
||||
val sorted = map.toSortedMap()
|
||||
assertPrints(sorted.keys, "[b, c, d]")
|
||||
assertPrints(sorted.values, "[2, 3, 1]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun mapToSortedMapWithComparator() {
|
||||
val map = mapOf(Pair("abc", 1), Pair("c", 3), Pair("bd", 4), Pair("bc", 2))
|
||||
val sorted = map.toSortedMap(compareBy<String> { it.length }.thenBy { it })
|
||||
assertPrints(sorted.keys, "[c, bc, bd, abc]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun mapToProperties() {
|
||||
val map = mapOf("x" to "value A", "y" to "value B")
|
||||
val props = map.toProperties()
|
||||
|
||||
assertPrints(props.getProperty("x"), "value A")
|
||||
assertPrints(props.getProperty("y", "fail"), "value B")
|
||||
assertPrints(props.getProperty("z", "fail"), "fail")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package samples.misc
|
||||
|
||||
import samples.*
|
||||
import kotlin.test.*
|
||||
|
||||
class Preconditions {
|
||||
|
||||
@Sample
|
||||
fun failWithError() {
|
||||
val name: String? = null
|
||||
|
||||
val exception = assertFailsWith<IllegalStateException> {
|
||||
val nonNullName = name ?: error("Name is missing")
|
||||
}
|
||||
|
||||
assertPrints(exception.message, "Name is missing")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun failRequireWithLazyMessage() {
|
||||
|
||||
fun getIndices(count: Int) {
|
||||
require(count >= 0) { "Count must be non-negative, was $count" }
|
||||
// ...
|
||||
}
|
||||
|
||||
val exception = assertFailsWith<IllegalArgumentException> {
|
||||
getIndices(-1)
|
||||
}
|
||||
assertPrints(exception.message, "Count must be non-negative, was -1")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun failCheckWithLazyMessage() {
|
||||
|
||||
var someState: String? = null
|
||||
fun getStateValue(): String {
|
||||
val state = checkNotNull(someState) { "State must be set beforehand" }
|
||||
check(state.isNotEmpty()) { "State must be non-empty" }
|
||||
// ...
|
||||
return state
|
||||
}
|
||||
|
||||
assertFailsWith<IllegalStateException> {
|
||||
getStateValue()
|
||||
}
|
||||
|
||||
assertFailsWith<IllegalStateException> {
|
||||
someState = ""
|
||||
getStateValue()
|
||||
}.let { exception ->
|
||||
assertPrints(exception.message, "State must be non-empty")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package samples.misc
|
||||
|
||||
import samples.*
|
||||
import kotlin.test.*
|
||||
|
||||
class Tuples {
|
||||
|
||||
@Sample
|
||||
fun pairDestructuring() {
|
||||
val (a, b) = Pair(1, "x")
|
||||
assertPrints(a, "1")
|
||||
assertPrints(b, "x")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun tripleDestructuring() {
|
||||
val (a, b, c) = Triple(2, "x", listOf(null))
|
||||
assertPrints(a, "2")
|
||||
assertPrints(b, "x")
|
||||
assertPrints(c, "[null]")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package samples.text
|
||||
|
||||
import samples.*
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
class Regexps {
|
||||
|
||||
@Sample
|
||||
fun matchDestructuringToGroupValues() {
|
||||
val inputString = "John 9731879"
|
||||
val match = Regex("(\\w+) (\\d+)").find(inputString)!!
|
||||
val (name, phone) = match.destructured
|
||||
|
||||
assertPrints(name, "John")
|
||||
assertPrints(phone, "9731879")
|
||||
|
||||
assertPrints(match.groupValues, "[John 9731879, John, 9731879]")
|
||||
|
||||
val numberedGroupValues = match.destructured.toList()
|
||||
assertPrints(numberedGroupValues, "[John, 9731879]")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package samples.text
|
||||
|
||||
import samples.*
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
class Strings {
|
||||
|
||||
@Sample
|
||||
fun capitalize() {
|
||||
assertPrints("abcd".capitalize(), "Abcd")
|
||||
assertPrints("Abcd".capitalize(), "Abcd")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun decaptialize() {
|
||||
assertPrints("abcd".decapitalize(), "abcd")
|
||||
assertPrints("Abcd".decapitalize(), "abcd")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun repeat() {
|
||||
assertPrints("Word".repeat(4), "WordWordWordWord")
|
||||
assertPrints("Word".repeat(0), "")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user