Skip to content

Commit 693f7b9

Browse files
Tryfonpivovarit
authored andcommitted
Guide to "when" block in Kotlin pull request (eugenp#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
1 parent 90969c9 commit 693f7b9

2 files changed

Lines changed: 164 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.kotlin
2+
3+
enum class UnixFileType {
4+
D, HYPHEN_MINUS, L
5+
}
6+
7+
sealed class UnixFile {
8+
9+
abstract fun getFileType(): UnixFileType
10+
11+
class RegularFile(val content: String) : UnixFile() {
12+
override fun getFileType(): UnixFileType {
13+
return UnixFileType.HYPHEN_MINUS
14+
}
15+
}
16+
17+
class Directory(val children: List<UnixFile>) : UnixFile() {
18+
override fun getFileType(): UnixFileType {
19+
return UnixFileType.D
20+
}
21+
}
22+
23+
class SymbolicLink(val originalFile: UnixFile) : UnixFile() {
24+
override fun getFileType(): UnixFileType {
25+
return UnixFileType.L
26+
}
27+
}
28+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.baeldung.kotlin
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Assert.assertTrue
5+
import org.junit.Test
6+
7+
class WhenBlockUnitTest {
8+
9+
@Test
10+
fun testWhenExpression() {
11+
val directoryType = UnixFileType.D
12+
13+
val objectType = when (directoryType) {
14+
UnixFileType.D -> "d"
15+
UnixFileType.HYPHEN_MINUS -> "-"
16+
UnixFileType.L -> "l"
17+
}
18+
19+
assertEquals("d", objectType)
20+
}
21+
22+
@Test
23+
fun testWhenExpressionWithDefaultCase() {
24+
val fileType = UnixFileType.L
25+
26+
val result = when (fileType) {
27+
UnixFileType.L -> "linking to another file"
28+
else -> "not a link"
29+
}
30+
31+
assertEquals("linking to another file", result)
32+
}
33+
34+
@Test(expected = IllegalArgumentException::class)
35+
fun testWhenExpressionWithThrowException() {
36+
val fileType = UnixFileType.L
37+
38+
val result: Boolean = when (fileType) {
39+
UnixFileType.HYPHEN_MINUS -> true
40+
else -> throw IllegalArgumentException("Wrong type of file")
41+
}
42+
}
43+
44+
@Test
45+
fun testWhenStatement() {
46+
val fileType = UnixFileType.HYPHEN_MINUS
47+
48+
when (fileType) {
49+
UnixFileType.HYPHEN_MINUS -> println("Regular file type")
50+
UnixFileType.D -> println("Directory file type")
51+
}
52+
}
53+
54+
@Test
55+
fun testCaseCombination() {
56+
val fileType = UnixFileType.D
57+
58+
val frequentFileType: Boolean = when (fileType) {
59+
UnixFileType.HYPHEN_MINUS, UnixFileType.D -> true
60+
else -> false
61+
}
62+
63+
assertTrue(frequentFileType)
64+
}
65+
66+
@Test
67+
fun testWhenWithoutArgument() {
68+
val fileType = UnixFileType.L
69+
70+
val objectType = when {
71+
fileType === UnixFileType.L -> "l"
72+
fileType === UnixFileType.HYPHEN_MINUS -> "-"
73+
fileType === UnixFileType.D -> "d"
74+
else -> "unknown file type"
75+
}
76+
77+
assertEquals("l", objectType)
78+
}
79+
80+
@Test
81+
fun testDynamicCaseExpression() {
82+
val unixFile = UnixFile.SymbolicLink(UnixFile.RegularFile("Content"))
83+
84+
when {
85+
unixFile.getFileType() == UnixFileType.D -> println("It's a directory!")
86+
unixFile.getFileType() == UnixFileType.HYPHEN_MINUS -> println("It's a regular file!")
87+
unixFile.getFileType() == UnixFileType.L -> println("It's a soft link!")
88+
}
89+
}
90+
91+
@Test
92+
fun testCollectionCaseExpressions() {
93+
val regularFile = UnixFile.RegularFile("Test Content")
94+
val symbolicLink = UnixFile.SymbolicLink(regularFile)
95+
val directory = UnixFile.Directory(listOf(regularFile, symbolicLink))
96+
97+
val isRegularFileInDirectory = when (regularFile) {
98+
in directory.children -> true
99+
else -> false
100+
}
101+
102+
val isSymbolicLinkInDirectory = when {
103+
symbolicLink in directory.children -> true
104+
else -> false
105+
}
106+
107+
assertTrue(isRegularFileInDirectory)
108+
assertTrue(isSymbolicLinkInDirectory)
109+
}
110+
111+
@Test
112+
fun testRangeCaseExpressions() {
113+
val fileType = UnixFileType.HYPHEN_MINUS
114+
115+
val isCorrectType = when (fileType) {
116+
in UnixFileType.D..UnixFileType.L -> true
117+
else -> false
118+
}
119+
120+
assertTrue(isCorrectType)
121+
}
122+
123+
@Test
124+
fun testWhenWithIsOperatorWithSmartCase() {
125+
val unixFile: UnixFile = UnixFile.RegularFile("Test Content")
126+
127+
val result = when (unixFile) {
128+
is UnixFile.RegularFile -> unixFile.content
129+
is UnixFile.Directory -> unixFile.children.map { it.getFileType() }.joinToString(", ")
130+
is UnixFile.SymbolicLink -> unixFile.originalFile.getFileType()
131+
}
132+
133+
assertEquals("Test Content", result)
134+
}
135+
136+
}

0 commit comments

Comments
 (0)