change example
This commit is contained in:
parent
3b4e23cedf
commit
2c6a3a69b0
@ -8,114 +8,114 @@ class StructuralJumpTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun givenLoop_whenBreak_thenComplete() {
|
fun givenLoop_whenBreak_thenComplete() {
|
||||||
var value = 0
|
var value = ""
|
||||||
|
for (i in 'a'..'e') {
|
||||||
//break loop without label
|
value += i.toString()
|
||||||
for (i in 1..10) {
|
if (i == 'c')
|
||||||
value = i
|
|
||||||
if (value == 3)
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
assertEquals("abc", value)
|
||||||
assertEquals(value, 3)
|
}
|
||||||
|
@Test
|
||||||
//break loop with label
|
fun givenLoop_whenBreakWithLabel_thenComplete() {
|
||||||
outer_loop@ for (i in 1..10) {
|
var value = ""
|
||||||
for (j in 1..10) {
|
outer_loop@ for (i in 'a'..'d') {
|
||||||
value = i * j
|
for (j in 1..3) {
|
||||||
if (value == 30)
|
value += "" + i + j
|
||||||
|
if (i == 'b' && j == 1)
|
||||||
break@outer_loop
|
break@outer_loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assertEquals("a1a2a3b1", value)
|
||||||
assertEquals(value, 30)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun givenLoop_whenContinue_thenComplete() {
|
fun givenLoop_whenContinue_thenComplete() {
|
||||||
var processedList = mutableListOf<Int>()
|
var result = ""
|
||||||
//continue loop without label
|
for (i in 'a'..'d') {
|
||||||
for (i in 1..10) {
|
if (i == 'b')
|
||||||
if (i == 3)
|
|
||||||
continue
|
continue
|
||||||
processedList.add(i)
|
result += i
|
||||||
}
|
}
|
||||||
|
assertEquals("acd", result)
|
||||||
assert(processedList.all { it -> it != 3 })
|
}
|
||||||
|
@Test
|
||||||
//continue loop with label
|
fun givenLoop_whenContinueWithLabel_thenComplete() {
|
||||||
processedList = mutableListOf<Int>()
|
var result = ""
|
||||||
outer_loop@ for (i in 1..10) {
|
outer_loop@ for (i in 'a'..'c') {
|
||||||
for (j in 1..10) {
|
for (j in 1..3) {
|
||||||
if (i == 3)
|
if (i == 'b')
|
||||||
continue@outer_loop
|
continue@outer_loop
|
||||||
processedList.add(i*j)
|
result += "" + i + j
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assertEquals("a1a2a3c1c2c3", result)
|
||||||
assertEquals(processedList.size, 90)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun givenLambda_whenReturn_thenComplete() {
|
fun givenLambda_whenReturn_thenComplete() {
|
||||||
listOf(1, 2, 3, 4, 5).forEach {
|
var result = returnInLambda();
|
||||||
if (it == 3) return // non-local return directly to the caller
|
assertEquals("hello", result)
|
||||||
assert(it < 3)
|
|
||||||
}
|
}
|
||||||
//this point is unreachable
|
|
||||||
assert(false)
|
private fun returnInLambda(): String {
|
||||||
|
var result = ""
|
||||||
|
"hello_world".forEach {
|
||||||
|
// non-local return directly to the caller
|
||||||
|
if (it == '_') return result
|
||||||
|
result += it.toString()
|
||||||
|
}
|
||||||
|
//this line won't be reached
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun givenLambda_whenReturnWithExplicitLabel_thenComplete() {
|
fun givenLambda_whenReturnWithExplicitLabel_thenComplete() {
|
||||||
var result = mutableListOf<Int>()
|
var result = ""
|
||||||
|
"hello_world".forEach lit@{
|
||||||
listOf(1, 2, 3, 4, 5).forEach lit@{
|
if (it == 'o') {
|
||||||
if (it == 3) {
|
|
||||||
// local return to the caller of the lambda, i.e. the forEach loop
|
// local return to the caller of the lambda, i.e. the forEach loop
|
||||||
return@lit
|
return@lit
|
||||||
}
|
}
|
||||||
result.add(it)
|
result += it.toString()
|
||||||
}
|
}
|
||||||
|
assertEquals("hell_wrld", result)
|
||||||
assert(result.all { it -> it != 3 })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun givenLambda_whenReturnWithImplicitLabel_thenComplete() {
|
fun givenLambda_whenReturnWithImplicitLabel_thenComplete() {
|
||||||
var result = mutableListOf<Int>()
|
var result = ""
|
||||||
|
"hello_world".forEach {
|
||||||
listOf(1, 2, 3, 4, 5).forEach {
|
if (it == 'o') {
|
||||||
if (it == 3) {
|
|
||||||
// local return to the caller of the lambda, i.e. the forEach loop
|
// local return to the caller of the lambda, i.e. the forEach loop
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
result.add(it)
|
result += it.toString()
|
||||||
}
|
}
|
||||||
|
assertEquals("hell_wrld", result)
|
||||||
assert(result.all { it -> it != 3 })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun givenAnonymousFunction_return_thenComplete() {
|
fun givenAnonymousFunction_return_thenComplete() {
|
||||||
var result = mutableListOf<Int>()
|
var result = ""
|
||||||
listOf(1, 2, 3, 4, 5).forEach(fun(element: Int) {
|
"hello_world".forEach(fun(element) {
|
||||||
if (element == 3) return // local return to the caller of the anonymous fun, i.e. the forEach loop
|
// local return to the caller of the anonymous fun, i.e. the forEach loop
|
||||||
result.add(element)
|
if (element == 'o') return
|
||||||
|
result += element.toString()
|
||||||
})
|
})
|
||||||
|
assertEquals("hell_wrld", result)
|
||||||
assert(result.all { it -> it != 3 })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun givenAnonymousFunction_returnToLabel_thenComplete() {
|
fun givenAnonymousFunction_returnToLabel_thenComplete() {
|
||||||
var value = 0
|
var result = ""
|
||||||
run loop@{
|
run loop@{
|
||||||
listOf(1, 2, 3, 4, 5).forEach {
|
"hello_world".forEach {
|
||||||
value = it
|
// non-local return from the lambda passed to run
|
||||||
if (it == 3) return@loop // non-local return from the lambda passed to run
|
if (it == '_') return@loop
|
||||||
|
result += it.toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertEquals(value, 3)
|
assertEquals("hello", result)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user