1
1
package com.baeldung.functionAsParameter
2
2
3
- import com.baeldung.functionAsParameter.Sender.*
4
3
import org.junit.jupiter.api.Test
5
4
import kotlin.test.assertEquals
6
5
7
6
fun joinByOperation (theList : List <String >, operation : (List <String >) -> String ): String {
8
7
return operation(theList)
9
8
}
10
9
11
-
12
- data class TheMessage (val sender : Sender , val segments : List <String >) {
13
- fun parse (operation : (List <String >) -> String ): String {
14
- return " Message From $sender is: ${operation(segments)} "
15
- }
16
- }
17
-
18
- enum class Sender {
19
- User , RemoteAPI , PartnerApp , CsvSender
20
- }
21
-
22
-
23
10
class MessageParser {
24
11
fun joinWithoutPlaceholder (segments : List <String >): String {
25
12
return segments.joinToString(separator = " " ).replace(" [SPACE] " , " " )
@@ -32,7 +19,6 @@ class MessageParser {
32
19
}
33
20
}
34
21
35
-
36
22
object ParserInObject {
37
23
fun joinWithoutComma (segments : List <String >): String {
38
24
return segments.joinToString(separator = " " ) { it.replace(" ," , " " ) }
@@ -43,51 +29,62 @@ fun decrypt(segments: List<String>): String {
43
29
return segments.reversed().joinToString(separator = " " ) { it.reversed() }
44
30
}
45
31
46
- val messageParser = MessageParser ()
47
- fun parseMessage (message : TheMessage ): String {
48
- return when (message.sender) {
49
- User -> message.parse(MessageParser ::simplyJoin)
50
- PartnerApp -> message.parse(messageParser::joinWithoutPlaceholder)
51
- CsvSender -> message.parse(ParserInObject ::joinWithoutComma)
52
- RemoteAPI -> message.parse(::decrypt)
53
- }
54
- }
55
-
56
32
class FunctionAsParameterUnitTest {
57
33
58
34
@Test
59
35
fun `when passing lambda as parameters then get expected result` () {
60
- val input = listOf (" a b c" , " x y z " , " kotlin " )
36
+ val input = listOf (" a b c" , " d e f " , " x y z " )
61
37
val result1 = joinByOperation(input) { theList ->
62
38
theList.joinToString(separator = " " ) { str -> str.reversed() }.replace(" " , " , " )
63
39
}
64
- assertEquals(" c, b, a, z, y, x, niltok " , result1)
40
+ assertEquals(" c, b, a, f, e, d, z, y, x" , result1)
65
41
66
42
val result2 = joinByOperation(input) { theList ->
67
43
theList.reversed().joinToString(separator = " " ) { str -> str }.uppercase()
68
44
}
69
- assertEquals(" KOTLIN X Y Z A B C" , result2)
45
+ assertEquals(" X Y Z D E F A B C" , result2)
70
46
71
47
}
72
48
73
49
@Test
74
- fun `when passing existing fun as parameters then get expected result` () {
50
+ fun `when passing instance function ref as parameters then get expected result` () {
51
+ val messageParser = MessageParser ()
52
+ val input = listOf (" a [SPACE] b [SPACE] c" , " d [SPACE] e [SPACE] f" , " x [SPACE] y [SPACE] z" )
53
+ val result = joinByOperation(input, messageParser::joinWithoutPlaceholder)
54
+ assertEquals(" a b c d e f x y z" , result)
55
+ }
56
+
57
+ @Test
58
+ fun `when passing companion object function ref as parameters then get expected result` () {
59
+ val input = listOf (" a b c" , " d e f" , " x y z" )
60
+ val result = joinByOperation(input, MessageParser ::simplyJoin)
61
+ assertEquals(" a b c d e f x y z" , result)
62
+ }
75
63
64
+ @Test
65
+ fun `when passing object function ref as parameters then get expected result` () {
66
+ val input = listOf (" a, b, c" , " d, e, f" , " x, y, z" )
67
+ val result = joinByOperation(input, ParserInObject ::joinWithoutComma)
68
+ assertEquals(" a b c d e f x y z" , result)
69
+ }
76
70
77
- val msgFromUser = TheMessage (User , listOf (" a b c" , " d e f" , " x y z" ))
78
- val resultUser = parseMessage(msgFromUser)
79
- assertEquals(" Message From User is: a b c d e f x y z" , resultUser)
71
+ @Test
72
+ fun `when passing top-level function ref as parameters then get expected result` () {
73
+ val input = listOf (" z y x" , " f e d" , " c b a" )
74
+ val result = joinByOperation(input, ::decrypt)
75
+ assertEquals(" a b c d e f x y z" , result)
76
+ }
80
77
81
- val msgFromPartner = TheMessage ( PartnerApp , listOf ( " a [SPACE] b [SPACE] c " , " d [SPACE] e [SPACE] f " , " x [SPACE] y [SPACE] z " ))
82
- val resultPartner = parseMessage(msgFromPartner)
83
- assertEquals( " Message From PartnerApp is: a b c d e f x y z" , resultPartner )
78
+ @Test
79
+ fun `when passing variable with function type as parameter then get expected result` () {
80
+ val input = listOf ( " a, b, c " , " d, e, f " , " x, y, z" )
84
81
85
- val msgFromCsv = TheMessage ( CsvSender , listOf ( " a, b, c " , " d, e, f " , " x, y, z " ))
86
- val resultCsv = parseMessage(msgFromCsv )
87
- assertEquals(" Message From CsvSender is: a b c d e f x y z" , resultCsv )
82
+ val funRef = ParserInObject ::joinWithoutComma
83
+ val resultFunRef = joinByOperation(input, funRef )
84
+ assertEquals(" a b c d e f x y z" , resultFunRef )
88
85
89
- val msgFromAPI = TheMessage ( RemoteAPI , listOf ( " z y x " , " f e d " , " c b a " ))
90
- val resultApi = parseMessage(msgFromAPI )
91
- assertEquals(" Message From RemoteAPI is: a b c d e f x y z " , resultApi )
86
+ val funLambda = { theList : List < String > -> theList.reversed().joinToString(separator = " , " ) { str -> str }.uppercase() }
87
+ val resultFunLambda = joinByOperation(input, funLambda )
88
+ assertEquals(" X, Y, Z, D, E, F, A, B, C " , resultFunLambda )
92
89
}
93
90
}
0 commit comments