change example

This commit is contained in:
Hai Nguyen 2018-10-02 10:45:31 +08:00
parent 2c6a3a69b0
commit 9eb81e7059

View File

@ -9,12 +9,12 @@ class StructuralJumpTest {
@Test @Test
fun givenLoop_whenBreak_thenComplete() { fun givenLoop_whenBreak_thenComplete() {
var value = "" var value = ""
for (i in 'a'..'e') { for (i in "hello_world") {
value += i.toString() if (i == '_')
if (i == 'c')
break break
value += i.toString()
} }
assertEquals("abc", value) assertEquals("hello", value)
} }
@Test @Test
fun givenLoop_whenBreakWithLabel_thenComplete() { fun givenLoop_whenBreakWithLabel_thenComplete() {
@ -32,12 +32,12 @@ class StructuralJumpTest {
@Test @Test
fun givenLoop_whenContinue_thenComplete() { fun givenLoop_whenContinue_thenComplete() {
var result = "" var result = ""
for (i in 'a'..'d') { for (i in "hello_world") {
if (i == 'b') if (i == '_')
continue continue
result += i result += i
} }
assertEquals("acd", result) assertEquals("helloworld", result)
} }
@Test @Test
fun givenLoop_whenContinueWithLabel_thenComplete() { fun givenLoop_whenContinueWithLabel_thenComplete() {
@ -73,26 +73,26 @@ class StructuralJumpTest {
fun givenLambda_whenReturnWithExplicitLabel_thenComplete() { fun givenLambda_whenReturnWithExplicitLabel_thenComplete() {
var result = "" var result = ""
"hello_world".forEach lit@{ "hello_world".forEach lit@{
if (it == 'o') { if (it == '_') {
// 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 += it.toString() result += it.toString()
} }
assertEquals("hell_wrld", result) assertEquals("helloworld", result)
} }
@Test @Test
fun givenLambda_whenReturnWithImplicitLabel_thenComplete() { fun givenLambda_whenReturnWithImplicitLabel_thenComplete() {
var result = "" var result = ""
"hello_world".forEach { "hello_world".forEach {
if (it == 'o') { if (it == '_') {
// 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 += it.toString() result += it.toString()
} }
assertEquals("hell_wrld", result) assertEquals("helloworld", result)
} }
@Test @Test
@ -100,10 +100,10 @@ class StructuralJumpTest {
var result = "" var result = ""
"hello_world".forEach(fun(element) { "hello_world".forEach(fun(element) {
// 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
if (element == 'o') return if (element == '_') return
result += element.toString() result += element.toString()
}) })
assertEquals("hell_wrld", result) assertEquals("helloworld", result)
} }
@Test @Test