1
+ package com.baeldung.returnAtLabel
2
+
3
+ import com.baeldung.returnAtLabel.Answer.*
4
+ import org.junit.jupiter.api.Assertions.assertEquals
5
+ import org.junit.jupiter.api.Assertions.assertNotEquals
6
+ import org.junit.jupiter.api.Test
7
+ import org.slf4j.LoggerFactory
8
+
9
+
10
+ val input = """
11
+ T, T, T, T, T, T
12
+ F, O, F, O, F, O
13
+ T, F, O, F
14
+ T, O, T, O, T, O
15
+ T, X, T, X, T, X
16
+ F, F, F, F, F, F
17
+ """ .trimIndent()
18
+
19
+ enum class Answer {
20
+ True , False , Empty
21
+ }
22
+
23
+ val expectedResult = listOf (
24
+ listOf (True , True , True , True , True , True ),
25
+ listOf (False , Empty , False , Empty , False , Empty ),
26
+ listOf (True , Empty , True , Empty , True , Empty ),
27
+ listOf (True , True , True ),
28
+ listOf (False , False , False , False , False , False ),
29
+ )
30
+
31
+
32
+ class ReturnAtLabelUnitTest {
33
+ val log = LoggerFactory .getLogger(this ::class .java)
34
+
35
+ lateinit var resultList: MutableList <List <Answer >>
36
+
37
+
38
+ fun printResult () {
39
+ log.info(
40
+ """
41
+ |The Result After Processing:
42
+ |----------------
43
+ |${resultList.joinToString(separator = System .lineSeparator()) { " $it " }}
44
+ |----------------
45
+ """ .trimMargin())
46
+ }
47
+
48
+ fun processInputV1 (input : String ) {
49
+ resultList = mutableListOf ()
50
+ input.lines().forEach { line ->
51
+ val fields = line.split(" , " )
52
+ if (fields.size != 6 ) return
53
+ val answerList: MutableList <Answer > = mutableListOf ()
54
+ fields.forEach { field ->
55
+ answerList + = when (field) {
56
+ " T" -> True
57
+ " F" -> False
58
+ " O" -> Empty
59
+ else -> return
60
+ }
61
+ }
62
+ resultList + = answerList
63
+ }
64
+ }
65
+
66
+ fun processInputV2 (input : String ) {
67
+ resultList = mutableListOf ()
68
+ input.lines().forEach { line ->
69
+ val fields = line.split(" , " )
70
+ if (fields.size != 6 ) return @forEach
71
+ val answerList: MutableList <Answer > = mutableListOf ()
72
+ fields.forEach { field ->
73
+ answerList + = when (field) {
74
+ " T" -> True
75
+ " F" -> False
76
+ " O" -> Empty
77
+ else -> return
78
+ }
79
+ }
80
+ resultList + = answerList
81
+ }
82
+ }
83
+
84
+ fun processInputV3 (input : String ) {
85
+ resultList = mutableListOf ()
86
+ input.lines().forEach { line ->
87
+ val fields = line.split(" , " )
88
+ if (fields.size != 6 ) return @forEach
89
+ val answerList: MutableList <Answer > = mutableListOf ()
90
+ fields.forEach { field ->
91
+ answerList + = when (field) {
92
+ " T" -> True
93
+ " F" -> False
94
+ " O" -> Empty
95
+ else -> return @forEach
96
+ }
97
+ }
98
+ resultList + = answerList
99
+ }
100
+ }
101
+
102
+ fun processInputV4 (input : String ) {
103
+ resultList = mutableListOf ()
104
+ input.lines().forEach lineProcessing@{ line ->
105
+ val fields = line.split(" , " )
106
+ if (fields.size != 6 ) return @lineProcessing
107
+ val answerList: MutableList <Answer > = mutableListOf ()
108
+ fields.forEach answerProcessing@{ field ->
109
+ answerList + = when (field) {
110
+ " T" -> True
111
+ " F" -> False
112
+ " O" -> Empty
113
+ else -> return @answerProcessing
114
+ }
115
+ }
116
+ resultList + = answerList
117
+ }
118
+ }
119
+
120
+ @Test
121
+ fun `when using return directly in lambda, then exit the enclosing function` () {
122
+ processInputV1(input)
123
+ assertNotEquals(expectedResult, resultList)
124
+ printResult()
125
+ }
126
+
127
+ @Test
128
+ fun `when using return directly in the inner foreach lambda, then exit the enclosing function` () {
129
+ processInputV2(input)
130
+ assertNotEquals(expectedResult, resultList)
131
+ printResult()
132
+ }
133
+
134
+ @Test
135
+ fun `when using return with the default label in both foreach lambdas, then get the expected result` () {
136
+ processInputV3(input)
137
+ assertEquals(expectedResult, resultList)
138
+ }
139
+
140
+ @Test
141
+ fun `when using return with custom labels in both foreach lambdas, then get the expected result` () {
142
+ processInputV4(input)
143
+ assertEquals(expectedResult, resultList)
144
+ }
145
+
146
+ }
0 commit comments