refactor some code

This commit is contained in:
nnhai1991@gmail.com 2018-09-30 23:40:19 +08:00
parent 6c0c94449d
commit 3b4e23cedf
1 changed files with 14 additions and 14 deletions

View File

@ -14,7 +14,7 @@ class StructuralJumpTest {
for (i in 1..10) { for (i in 1..10) {
value = i value = i
if (value == 3) if (value == 3)
break; break
} }
assertEquals(value, 3) assertEquals(value, 3)
@ -24,7 +24,7 @@ class StructuralJumpTest {
for (j in 1..10) { for (j in 1..10) {
value = i * j value = i * j
if (value == 30) if (value == 30)
break@outer_loop; break@outer_loop
} }
} }
@ -37,7 +37,7 @@ class StructuralJumpTest {
//continue loop without label //continue loop without label
for (i in 1..10) { for (i in 1..10) {
if (i == 3) if (i == 3)
continue; continue
processedList.add(i) processedList.add(i)
} }
@ -48,7 +48,7 @@ class StructuralJumpTest {
outer_loop@ for (i in 1..10) { outer_loop@ for (i in 1..10) {
for (j in 1..10) { for (j in 1..10) {
if (i == 3) if (i == 3)
continue@outer_loop; continue@outer_loop
processedList.add(i*j) processedList.add(i*j)
} }
} }
@ -63,12 +63,12 @@ class StructuralJumpTest {
assert(it < 3) assert(it < 3)
} }
//this point is unreachable //this point is unreachable
assert(false); assert(false)
} }
@Test @Test
fun givenLambda_whenReturnWithExplicitLabel_thenComplete() { fun givenLambda_whenReturnWithExplicitLabel_thenComplete() {
var result = mutableListOf<Int>(); var result = mutableListOf<Int>()
listOf(1, 2, 3, 4, 5).forEach lit@{ listOf(1, 2, 3, 4, 5).forEach lit@{
if (it == 3) { if (it == 3) {
@ -78,12 +78,12 @@ class StructuralJumpTest {
result.add(it) result.add(it)
} }
assert(result.all { it -> it != 3 }); assert(result.all { it -> it != 3 })
} }
@Test @Test
fun givenLambda_whenReturnWithImplicitLabel_thenComplete() { fun givenLambda_whenReturnWithImplicitLabel_thenComplete() {
var result = mutableListOf<Int>(); var result = mutableListOf<Int>()
listOf(1, 2, 3, 4, 5).forEach { listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) { if (it == 3) {
@ -93,26 +93,26 @@ class StructuralJumpTest {
result.add(it) result.add(it)
} }
assert(result.all { it -> it != 3 }); assert(result.all { it -> it != 3 })
} }
@Test @Test
fun givenAnonymousFunction_return_thenComplete() { fun givenAnonymousFunction_return_thenComplete() {
var result = mutableListOf<Int>(); var result = mutableListOf<Int>()
listOf(1, 2, 3, 4, 5).forEach(fun(element: Int) { listOf(1, 2, 3, 4, 5).forEach(fun(element: Int) {
if (element == 3) return // local return to the caller of the anonymous fun, i.e. the forEach loop if (element == 3) return // local return to the caller of the anonymous fun, i.e. the forEach loop
result.add(element); result.add(element)
}) })
assert(result.all { it -> it != 3 }); assert(result.all { it -> it != 3 })
} }
@Test @Test
fun givenAnonymousFunction_returnToLabel_thenComplete() { fun givenAnonymousFunction_returnToLabel_thenComplete() {
var value = 0; var value = 0
run loop@{ run loop@{
listOf(1, 2, 3, 4, 5).forEach { listOf(1, 2, 3, 4, 5).forEach {
value = it; value = it
if (it == 3) return@loop // non-local return from the lambda passed to run if (it == 3) return@loop // non-local return from the lambda passed to run
} }
} }