|
| 1 | +package com.baeldung.kotlin |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import kotlin.test.assertEquals |
| 5 | +import kotlin.test.assertFalse |
| 6 | + |
| 7 | +class StructuralJumpUnitTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + fun givenLoop_whenBreak_thenComplete() { |
| 11 | + var value = "" |
| 12 | + for (i in "hello_world") { |
| 13 | + if (i == '_') |
| 14 | + break |
| 15 | + value += i.toString() |
| 16 | + } |
| 17 | + assertEquals("hello", 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) |
| 26 | + break@outer_loop |
| 27 | + } |
| 28 | + } |
| 29 | + assertEquals("a1a2a3b1", value) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun givenLoop_whenContinue_thenComplete() { |
| 34 | + var result = "" |
| 35 | + for (i in "hello_world") { |
| 36 | + if (i == '_') |
| 37 | + continue |
| 38 | + result += i |
| 39 | + } |
| 40 | + assertEquals("helloworld", 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') |
| 48 | + continue@outer_loop |
| 49 | + result += "" + i + j |
| 50 | + } |
| 51 | + } |
| 52 | + assertEquals("a1a2a3c1c2c3", result) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun givenLambda_whenReturn_thenComplete() { |
| 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() |
| 67 | + } |
| 68 | + //this line won't be reached |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun givenLambda_whenReturnWithExplicitLabel_thenComplete() { |
| 74 | + var result = "" |
| 75 | + "hello_world".forEach lit@{ |
| 76 | + if (it == '_') { |
| 77 | + // local return to the caller of the lambda, i.e. the forEach loop |
| 78 | + return@lit |
| 79 | + } |
| 80 | + result += it.toString() |
| 81 | + } |
| 82 | + assertEquals("helloworld", result) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun givenLambda_whenReturnWithImplicitLabel_thenComplete() { |
| 87 | + var result = "" |
| 88 | + "hello_world".forEach { |
| 89 | + if (it == '_') { |
| 90 | + // local return to the caller of the lambda, i.e. the forEach loop |
| 91 | + return@forEach |
| 92 | + } |
| 93 | + result += it.toString() |
| 94 | + } |
| 95 | + assertEquals("helloworld", result) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun givenAnonymousFunction_return_thenComplete() { |
| 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 == '_') return |
| 104 | + result += element.toString() |
| 105 | + }) |
| 106 | + assertEquals("helloworld", result) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun givenAnonymousFunction_returnToLabel_thenComplete() { |
| 111 | + var result = "" |
| 112 | + run loop@{ |
| 113 | + "hello_world".forEach { |
| 114 | + // non-local return from the lambda passed to run |
| 115 | + if (it == '_') return@loop |
| 116 | + result += it.toString() |
| 117 | + } |
| 118 | + } |
| 119 | + assertEquals("hello", result) |
| 120 | + } |
| 121 | +} |
0 commit comments