@@ -4,118 +4,118 @@ import org.junit.Test
44import kotlin.test.assertEquals
55import kotlin.test.assertFalse
66
7- class StructuralJumpTest {
7+ class StructuralJumpTest {
88
99 @Test
1010 fun givenLoop_whenBreak_thenComplete () {
11- var value = 0
12-
13- // break loop without label
14- for (i in 1 .. 10 ) {
15- value = i
16- if (value == 3 )
11+ var value = " "
12+ for (i in ' a' .. ' e' ) {
13+ value + = i.toString()
14+ if (i == ' c' )
1715 break
1816 }
19-
20- assertEquals(value, 3 )
21-
22- // break loop with label
23- outer_loop@ for (i in 1 .. 10 ) {
24- for (j in 1 .. 10 ) {
25- value = i * j
26- if (value == 30 )
17+ assertEquals(" abc" , value)
18+ }
19+ @Test
20+ fun givenLoop_whenBreakWithLabel_thenComplete () {
21+ var value = " "
22+ outer_loop@ for (i in ' a' .. ' d' ) {
23+ for (j in 1 .. 3 ) {
24+ value + = " " + i + j
25+ if (i == ' b' && j == 1 )
2726 break @outer_loop
2827 }
2928 }
30-
31- assertEquals(value, 30 )
29+ assertEquals(" a1a2a3b1" , value)
3230 }
3331
3432 @Test
3533 fun givenLoop_whenContinue_thenComplete () {
36- var processedList = mutableListOf<Int >()
37- // continue loop without label
38- for (i in 1 .. 10 ) {
39- if (i == 3 )
34+ var result = " "
35+ for (i in ' a' .. ' d' ) {
36+ if (i == ' b' )
4037 continue
41- processedList.add(i)
38+ result + = i
4239 }
43-
44- assert (processedList.all { it -> it != 3 })
45-
46- // continue loop with label
47- processedList = mutableListOf< Int >()
48- outer_loop@ for (i in 1 .. 10 ) {
49- for (j in 1 .. 10 ) {
50- if (i == 3 )
40+ assertEquals( " acd " , result)
41+ }
42+ @Test
43+ fun givenLoop_whenContinueWithLabel_thenComplete () {
44+ var result = " "
45+ outer_loop@ for (i in ' a ' .. ' c ' ) {
46+ for (j in 1 .. 3 ) {
47+ if (i == ' b ' )
5148 continue @outer_loop
52- processedList.add(i * j)
49+ result + = " " + i + j
5350 }
5451 }
55-
56- assertEquals(processedList.size, 90 )
52+ assertEquals(" a1a2a3c1c2c3" , result)
5753 }
5854
5955 @Test
6056 fun givenLambda_whenReturn_thenComplete () {
61- listOf (1 , 2 , 3 , 4 , 5 ).forEach {
62- if (it == 3 ) return // non-local return directly to the caller
63- assert (it < 3 )
57+ var result = returnInLambda();
58+ assertEquals(" hello" , result)
59+ }
60+
61+ private fun returnInLambda (): String {
62+ var result = " "
63+ " hello_world" .forEach {
64+ // non-local return directly to the caller
65+ if (it == ' _' ) return result
66+ result + = it.toString()
6467 }
65- // this point is unreachable
66- assert ( false )
68+ // this line won't be reached
69+ return result;
6770 }
6871
6972 @Test
7073 fun givenLambda_whenReturnWithExplicitLabel_thenComplete () {
71- var result = mutableListOf<Int >()
72-
73- listOf (1 , 2 , 3 , 4 , 5 ).forEach lit@{
74- if (it == 3 ) {
74+ var result = " "
75+ " hello_world" .forEach lit@{
76+ if (it == ' o' ) {
7577 // local return to the caller of the lambda, i.e. the forEach loop
7678 return @lit
7779 }
78- result.add(it )
80+ result + = it.toString( )
7981 }
80-
81- assert (result.all { it -> it != 3 })
82+ assertEquals(" hell_wrld" , result)
8283 }
8384
8485 @Test
8586 fun givenLambda_whenReturnWithImplicitLabel_thenComplete () {
86- var result = mutableListOf<Int >()
87-
88- listOf (1 , 2 , 3 , 4 , 5 ).forEach {
89- if (it == 3 ) {
87+ var result = " "
88+ " hello_world" .forEach {
89+ if (it == ' o' ) {
9090 // local return to the caller of the lambda, i.e. the forEach loop
9191 return @forEach
9292 }
93- result.add(it )
93+ result + = it.toString( )
9494 }
95-
96- assert (result.all { it -> it != 3 })
95+ assertEquals(" hell_wrld" , result)
9796 }
9897
9998 @Test
10099 fun givenAnonymousFunction_return_thenComplete () {
101- var result = mutableListOf<Int >()
102- listOf (1 , 2 , 3 , 4 , 5 ).forEach(fun (element : Int ) {
103- if (element == 3 ) return // local return to the caller of the anonymous fun, i.e. the forEach loop
104- result.add(element)
100+ var result = " "
101+ " hello_world" .forEach(fun (element) {
102+ // local return to the caller of the anonymous fun, i.e. the forEach loop
103+ if (element == ' o' ) return
104+ result + = element.toString()
105105 })
106-
107- assert (result.all { it -> it != 3 })
106+ assertEquals(" hell_wrld" , result)
108107 }
109108
110109 @Test
111110 fun givenAnonymousFunction_returnToLabel_thenComplete () {
112- var value = 0
111+ var result = " "
113112 run loop@{
114- listOf (1 , 2 , 3 , 4 , 5 ).forEach {
115- value = it
116- if (it == 3 ) return @loop // non-local return from the lambda passed to run
113+ " hello_world" .forEach {
114+ // non-local return from the lambda passed to run
115+ if (it == ' _' ) return @loop
116+ result + = it.toString()
117117 }
118118 }
119- assertEquals(value, 3 )
119+ assertEquals(" hello " , result )
120120 }
121121}
0 commit comments