JS backend: new tests for use break, continue and return in &&, ||, ?:
This commit is contained in:
@@ -100,4 +100,16 @@ public class EvaluationOrderTest extends AbstractExpressionTest {
|
|||||||
public void testWhenWithComplexConditions() throws Exception {
|
public void testWhenWithComplexConditions() throws Exception {
|
||||||
checkFooBoxIsOk();
|
checkFooBoxIsOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testElvisWithBreakContinueReturn() throws Exception {
|
||||||
|
checkFooBoxIsOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAndAndWithBreakContinueReturn() throws Exception {
|
||||||
|
checkFooBoxIsOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testOrOrWithBreakContinueReturn() throws Exception {
|
||||||
|
checkFooBoxIsOk();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
fun bar(b: Boolean): String {
|
||||||
|
if (b && return "A")
|
||||||
|
return "B"
|
||||||
|
else
|
||||||
|
return "C"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testBreak(b: Boolean, expected: Int) {
|
||||||
|
var i = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) b && break
|
||||||
|
}
|
||||||
|
assertEquals(expected, i, "break 1")
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) {
|
||||||
|
var x = b && break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(expected, i, "break 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testContinue(b: Boolean, expected: Int) {
|
||||||
|
var i = 0
|
||||||
|
var n = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) b && continue
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
assertEquals(expected, n, "continue 1")
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
n = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) {
|
||||||
|
var x = b && continue
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
assertEquals(expected, n, "continue 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
var b: Boolean
|
||||||
|
|
||||||
|
testBreak(true, 2)
|
||||||
|
testBreak(false, 6)
|
||||||
|
|
||||||
|
testContinue(true, 4)
|
||||||
|
testContinue(false, 5)
|
||||||
|
|
||||||
|
assertEquals("A", bar(true))
|
||||||
|
assertEquals("C", bar(false))
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
class A
|
||||||
|
|
||||||
|
fun bar(a: A?): String {
|
||||||
|
if ((a ?: return "A") != null)
|
||||||
|
return "B"
|
||||||
|
else
|
||||||
|
return "C"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testBreak(a: A?, expected: Int) {
|
||||||
|
var i = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) a ?: break
|
||||||
|
}
|
||||||
|
assertEquals(expected, i, "break 1")
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) {
|
||||||
|
var x = a ?: break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(expected, i, "break 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testContinue(a: A?, expected: Int) {
|
||||||
|
var i = 0
|
||||||
|
var n = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) a ?: continue
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
assertEquals(expected, n)
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
n = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) {
|
||||||
|
var x = a ?: continue
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
assertEquals(expected, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
|
||||||
|
testBreak(null, 2)
|
||||||
|
testBreak(A(), 6)
|
||||||
|
|
||||||
|
testContinue(null, 4)
|
||||||
|
testContinue(A(), 5)
|
||||||
|
|
||||||
|
assertEquals("A", bar(null))
|
||||||
|
assertEquals("B", bar(A()))
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
fun bar(b: Boolean): String {
|
||||||
|
if (b || return "A")
|
||||||
|
return "B"
|
||||||
|
else
|
||||||
|
return "C"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testBreak(b: Boolean, expected: Int) {
|
||||||
|
var i = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) b || break
|
||||||
|
}
|
||||||
|
assertEquals(expected, i, "break 1")
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) {
|
||||||
|
var x = b || break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(expected, i, "break 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testContinue(b: Boolean, expected: Int) {
|
||||||
|
var i = 0
|
||||||
|
var n = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) b || continue
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
assertEquals(expected, n, "continue 1")
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
n = 0
|
||||||
|
while (i++ < 5) {
|
||||||
|
if (i == 2) {
|
||||||
|
var x = b || continue
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
assertEquals(expected, n, "continue 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
|
||||||
|
testBreak(true, 6)
|
||||||
|
testBreak(false, 2)
|
||||||
|
|
||||||
|
testContinue(true, 5)
|
||||||
|
testContinue(false, 4)
|
||||||
|
|
||||||
|
assertEquals("B", bar(true))
|
||||||
|
assertEquals("A", bar(false))
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user