Guide to "when" block in Kotlin pull request (#1296)
* Char array to string and string to char array test cases added * Minor code renames * Added groupingBy collector unit tests * Added test case for int summary calculation on grouped results * Added the grouping by classes to the main source path * Reverting char array to string test class * Reverting char array to string test class * Reverting char array to string test class * Reverting char array to string test class * Unit test class for Kotlin when block + required types * Minor changes to kotlin when block tests * Minor change * Minor change
This commit is contained in:
parent
90969c9d58
commit
693f7b9689
28
kotlin/src/main/kotlin/com/baeldung/kotlin/WhenBlockTypes.kt
Normal file
28
kotlin/src/main/kotlin/com/baeldung/kotlin/WhenBlockTypes.kt
Normal file
@ -0,0 +1,28 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
enum class UnixFileType {
|
||||
D, HYPHEN_MINUS, L
|
||||
}
|
||||
|
||||
sealed class UnixFile {
|
||||
|
||||
abstract fun getFileType(): UnixFileType
|
||||
|
||||
class RegularFile(val content: String) : UnixFile() {
|
||||
override fun getFileType(): UnixFileType {
|
||||
return UnixFileType.HYPHEN_MINUS
|
||||
}
|
||||
}
|
||||
|
||||
class Directory(val children: List<UnixFile>) : UnixFile() {
|
||||
override fun getFileType(): UnixFileType {
|
||||
return UnixFileType.D
|
||||
}
|
||||
}
|
||||
|
||||
class SymbolicLink(val originalFile: UnixFile) : UnixFile() {
|
||||
override fun getFileType(): UnixFileType {
|
||||
return UnixFileType.L
|
||||
}
|
||||
}
|
||||
}
|
136
kotlin/src/test/kotlin/com/baeldung/kotlin/WhenBlockUnitTest.kt
Normal file
136
kotlin/src/test/kotlin/com/baeldung/kotlin/WhenBlockUnitTest.kt
Normal file
@ -0,0 +1,136 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class WhenBlockUnitTest {
|
||||
|
||||
@Test
|
||||
fun testWhenExpression() {
|
||||
val directoryType = UnixFileType.D
|
||||
|
||||
val objectType = when (directoryType) {
|
||||
UnixFileType.D -> "d"
|
||||
UnixFileType.HYPHEN_MINUS -> "-"
|
||||
UnixFileType.L -> "l"
|
||||
}
|
||||
|
||||
assertEquals("d", objectType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWhenExpressionWithDefaultCase() {
|
||||
val fileType = UnixFileType.L
|
||||
|
||||
val result = when (fileType) {
|
||||
UnixFileType.L -> "linking to another file"
|
||||
else -> "not a link"
|
||||
}
|
||||
|
||||
assertEquals("linking to another file", result)
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException::class)
|
||||
fun testWhenExpressionWithThrowException() {
|
||||
val fileType = UnixFileType.L
|
||||
|
||||
val result: Boolean = when (fileType) {
|
||||
UnixFileType.HYPHEN_MINUS -> true
|
||||
else -> throw IllegalArgumentException("Wrong type of file")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWhenStatement() {
|
||||
val fileType = UnixFileType.HYPHEN_MINUS
|
||||
|
||||
when (fileType) {
|
||||
UnixFileType.HYPHEN_MINUS -> println("Regular file type")
|
||||
UnixFileType.D -> println("Directory file type")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCaseCombination() {
|
||||
val fileType = UnixFileType.D
|
||||
|
||||
val frequentFileType: Boolean = when (fileType) {
|
||||
UnixFileType.HYPHEN_MINUS, UnixFileType.D -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
assertTrue(frequentFileType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWhenWithoutArgument() {
|
||||
val fileType = UnixFileType.L
|
||||
|
||||
val objectType = when {
|
||||
fileType === UnixFileType.L -> "l"
|
||||
fileType === UnixFileType.HYPHEN_MINUS -> "-"
|
||||
fileType === UnixFileType.D -> "d"
|
||||
else -> "unknown file type"
|
||||
}
|
||||
|
||||
assertEquals("l", objectType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDynamicCaseExpression() {
|
||||
val unixFile = UnixFile.SymbolicLink(UnixFile.RegularFile("Content"))
|
||||
|
||||
when {
|
||||
unixFile.getFileType() == UnixFileType.D -> println("It's a directory!")
|
||||
unixFile.getFileType() == UnixFileType.HYPHEN_MINUS -> println("It's a regular file!")
|
||||
unixFile.getFileType() == UnixFileType.L -> println("It's a soft link!")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCollectionCaseExpressions() {
|
||||
val regularFile = UnixFile.RegularFile("Test Content")
|
||||
val symbolicLink = UnixFile.SymbolicLink(regularFile)
|
||||
val directory = UnixFile.Directory(listOf(regularFile, symbolicLink))
|
||||
|
||||
val isRegularFileInDirectory = when (regularFile) {
|
||||
in directory.children -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
val isSymbolicLinkInDirectory = when {
|
||||
symbolicLink in directory.children -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
assertTrue(isRegularFileInDirectory)
|
||||
assertTrue(isSymbolicLinkInDirectory)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRangeCaseExpressions() {
|
||||
val fileType = UnixFileType.HYPHEN_MINUS
|
||||
|
||||
val isCorrectType = when (fileType) {
|
||||
in UnixFileType.D..UnixFileType.L -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
assertTrue(isCorrectType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWhenWithIsOperatorWithSmartCase() {
|
||||
val unixFile: UnixFile = UnixFile.RegularFile("Test Content")
|
||||
|
||||
val result = when (unixFile) {
|
||||
is UnixFile.RegularFile -> unixFile.content
|
||||
is UnixFile.Directory -> unixFile.children.map { it.getFileType() }.joinToString(", ")
|
||||
is UnixFile.SymbolicLink -> unixFile.originalFile.getFileType()
|
||||
}
|
||||
|
||||
assertEquals("Test Content", result)
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user