1
+ package com.baeldung.functionAsParameter
2
+
3
+ import org.junit.jupiter.api.Test
4
+ import kotlin.test.assertEquals
5
+
6
+ fun joinByOperation (theList : List <String >, operation : (List <String >) -> String ): String {
7
+ return operation(theList)
8
+ }
9
+
10
+ class MessageParser {
11
+ fun joinWithoutPlaceholder (segments : List <String >): String {
12
+ return segments.joinToString(separator = " " ).replace(" [SPACE] " , " " )
13
+ }
14
+
15
+ companion object {
16
+ fun simplyJoin (segments : List <String >): String {
17
+ return segments.joinToString(separator = " " )
18
+ }
19
+ }
20
+ }
21
+
22
+ object ParserInObject {
23
+ fun joinWithoutComma (segments : List <String >): String {
24
+ return segments.joinToString(separator = " " ) { it.replace(" ," , " " ) }
25
+ }
26
+ }
27
+
28
+ fun decrypt (segments : List <String >): String {
29
+ return segments.reversed().joinToString(separator = " " ) { it.reversed() }
30
+ }
31
+
32
+ class FunctionAsParameterUnitTest {
33
+
34
+ @Test
35
+ fun `when passing lambda as parameters then get expected result` () {
36
+ val input = listOf (" a b c" , " d e f" , " x y z" )
37
+ val result1 = joinByOperation(input) { theList ->
38
+ theList.joinToString(separator = " " ) { str -> str.reversed() }.replace(" " , " , " )
39
+ }
40
+ assertEquals(" c, b, a, f, e, d, z, y, x" , result1)
41
+
42
+ val result2 = joinByOperation(input) { theList ->
43
+ theList.reversed().joinToString(separator = " " ) { str -> str }.uppercase()
44
+ }
45
+ assertEquals(" X Y Z D E F A B C" , result2)
46
+
47
+ }
48
+
49
+ @Test
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
+ }
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
+ }
70
+
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
+ }
77
+
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" )
81
+
82
+ val funRef = ParserInObject ::joinWithoutComma
83
+ val resultFunRef = joinByOperation(input, funRef)
84
+ assertEquals(" a b c d e f x y z" , resultFunRef)
85
+
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)
89
+ }
90
+ }
0 commit comments