JS: move expressions test to box tests
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var result = "fail1"
|
||||
var i = 1
|
||||
do
|
||||
when (i) {
|
||||
1 -> result = "OK"
|
||||
else -> result = "fail2"
|
||||
}
|
||||
while (i==0)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
when {}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var result = "fail1"
|
||||
for (i in arrayOf(1))
|
||||
when (i) {
|
||||
1 -> result = "OK"
|
||||
else -> result = "fail2"
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// KT-2221 if in when
|
||||
|
||||
package foo
|
||||
|
||||
fun test(caseId: Int, value: Int, expected: Int) {
|
||||
val actual: Int
|
||||
when (caseId) {
|
||||
0 -> if (value < 0) actual = -value else actual = value
|
||||
1 -> actual = if (value < 0) -value else value
|
||||
else -> throw Exception("Unexpected case: $caseId")
|
||||
}
|
||||
|
||||
if (expected != actual) throw Exception("expected = $expected, actual = $actual")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
test(0, 3, 3)
|
||||
test(0, -13, 13)
|
||||
test(1, 23, 23)
|
||||
test(1, -3, 3)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// http://youtrack.jetbrains.com/issue/KT-5253
|
||||
// JS: generated wrong code when use `if` inside `when`
|
||||
|
||||
package foo
|
||||
|
||||
|
||||
fun test(caseId: Int, value: Int, expected: Int) {
|
||||
var actual: Int = 0
|
||||
when (caseId) {
|
||||
2 -> if (value < 0) actual = -value
|
||||
3 -> actual = if (value < 0) {-value} else value
|
||||
else -> throw Exception("Unexpected case: $caseId")
|
||||
}
|
||||
|
||||
if (expected != actual) throw Exception("expected = $expected, actual = $actual")
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
|
||||
test(2, 33, 0)
|
||||
test(2, -1, 1)
|
||||
test(3, 23, 23)
|
||||
test(3, -3, 3)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var result = "fail1"
|
||||
var i = 1
|
||||
if (i==1)
|
||||
when (i) {
|
||||
1 -> result = "OK"
|
||||
else -> result = "fail2"
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a = 10
|
||||
val b = 3
|
||||
when {
|
||||
a > b -> return "OK"
|
||||
b > a -> return "b"
|
||||
else -> return "else"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var a: A? = null
|
||||
when(a) {
|
||||
is A? -> return "OK"
|
||||
else -> return "fail"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val c = 3
|
||||
val d = 5
|
||||
var z = 0
|
||||
when(c) {
|
||||
5, 3 -> z++;
|
||||
else -> {
|
||||
z = -1000;
|
||||
}
|
||||
}
|
||||
|
||||
when(d) {
|
||||
5, 3 -> z++;
|
||||
else -> {
|
||||
z = -1000;
|
||||
}
|
||||
}
|
||||
assertEquals(2, z)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
|
||||
val success = (when(1) {
|
||||
2 -> 3
|
||||
1 -> 1
|
||||
else -> 5
|
||||
} == 1)
|
||||
|
||||
return if (success) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
|
||||
try {
|
||||
(when (1) {
|
||||
3 -> {
|
||||
3
|
||||
}
|
||||
1 -> {
|
||||
throw Exception()
|
||||
}
|
||||
else -> {
|
||||
return "fail1"
|
||||
}
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
return "fail2"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun test(): Int {
|
||||
return when {
|
||||
(return 23) is Int -> 24
|
||||
else -> 25
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(23, test())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package foo
|
||||
|
||||
data class A(val bar: Int)
|
||||
|
||||
@native class B
|
||||
|
||||
fun makeB(): B = js("new Object();")
|
||||
|
||||
fun intAgainstInt(x: Int) = when (x) {
|
||||
1 -> "a"
|
||||
2 -> "b"
|
||||
else -> "*"
|
||||
}
|
||||
|
||||
fun intAgainstNullableInt(x: Int?) = when (x) {
|
||||
1 -> "a"
|
||||
2 -> "b"
|
||||
null -> "c"
|
||||
else -> "*"
|
||||
}
|
||||
|
||||
fun anyAgainstInt(x: Any?) = when (x) {
|
||||
1 -> "a"
|
||||
2 -> "b"
|
||||
else -> "*"
|
||||
}
|
||||
|
||||
fun longAgainstLong(x: Long) = when (x) {
|
||||
1L -> "a"
|
||||
2L -> "b"
|
||||
else -> "*"
|
||||
}
|
||||
|
||||
fun anyAgainstLong(x: Any?) = when (x) {
|
||||
1L -> "a"
|
||||
2L -> "b"
|
||||
null -> "c"
|
||||
else -> "*"
|
||||
}
|
||||
|
||||
fun anyAgainstAny(x: Any) = when (x) {
|
||||
A(1) -> "a"
|
||||
1 -> "b"
|
||||
else -> "*"
|
||||
}
|
||||
|
||||
fun dynamicAgainstPattern(x: dynamic) = when(x) {
|
||||
1 -> "a"
|
||||
"2" -> "b"
|
||||
else -> "*"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("a", intAgainstInt(1))
|
||||
assertEquals("b", intAgainstInt(2))
|
||||
assertEquals("*", intAgainstInt(23))
|
||||
|
||||
assertEquals("a", intAgainstNullableInt(1))
|
||||
assertEquals("b", intAgainstNullableInt(2))
|
||||
assertEquals("c", intAgainstNullableInt(null))
|
||||
assertEquals("*", intAgainstNullableInt(23))
|
||||
|
||||
assertEquals("a", anyAgainstInt(1))
|
||||
assertEquals("b", anyAgainstInt(2))
|
||||
assertEquals("*", anyAgainstInt(A(23)))
|
||||
|
||||
assertEquals("a", longAgainstLong(1))
|
||||
assertEquals("b", longAgainstLong(2))
|
||||
assertEquals("*", longAgainstLong(23))
|
||||
|
||||
assertEquals("a", anyAgainstLong(1L))
|
||||
assertEquals("b", anyAgainstLong(2L))
|
||||
assertEquals("c", anyAgainstLong(null))
|
||||
assertEquals("*", anyAgainstLong(A(23)))
|
||||
|
||||
assertEquals("a", anyAgainstAny(A(1)))
|
||||
assertEquals("b", anyAgainstAny(1))
|
||||
assertEquals("*", anyAgainstAny(listOf(1)))
|
||||
|
||||
assertEquals("a", dynamicAgainstPattern(1))
|
||||
assertEquals("a", dynamicAgainstPattern(js("1")))
|
||||
assertEquals("b", dynamicAgainstPattern("2"))
|
||||
assertEquals("b", dynamicAgainstPattern(js("'2'")))
|
||||
assertEquals("*", dynamicAgainstPattern(js("{}")))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var a = 0
|
||||
var i = 0
|
||||
when(i++) {
|
||||
-100 -> a++
|
||||
100 -> a++
|
||||
else -> a++
|
||||
}
|
||||
|
||||
return if ((a == 1) && (i == 1)) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var a = 0
|
||||
when(A()) {
|
||||
is A -> a++;
|
||||
is A -> a++;
|
||||
else -> a++;
|
||||
}
|
||||
if (a != 1) return "fail: $a"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
when(A()) {
|
||||
!is A -> return "fail"
|
||||
else -> return "OK"
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// see KT-7683
|
||||
// WhenTranslator must recognize KtWhenConditionInRange for when statement
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var result = testFun(-1) + testFun(5) + testFun(50) + testFun(150)
|
||||
return if (result == "[miss][hit1][miss][hit2]") "OK" else "fail"
|
||||
}
|
||||
fun testFun(index: Int): String {
|
||||
var r = "[miss]"
|
||||
when (index) {
|
||||
in 0..9 -> r = "[hit1]"
|
||||
in 100..200 -> r = "[hit2]"
|
||||
}
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
when(A()) {
|
||||
is A -> return "OK"
|
||||
else -> return "fail"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var a = 4
|
||||
when(a) {
|
||||
3 -> {
|
||||
a = 10;
|
||||
}
|
||||
4 -> {
|
||||
a = 20;
|
||||
}
|
||||
else -> {
|
||||
a = 30;
|
||||
}
|
||||
}
|
||||
if (a != 20) return "fail: $a"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
class B() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var c: Int = 0
|
||||
var a: Any? = A()
|
||||
var b: Any? = null
|
||||
when(a) {
|
||||
null -> c = 10;
|
||||
is B -> c = 10000
|
||||
is A -> c = 20;
|
||||
else -> c = 1000
|
||||
}
|
||||
when(b) {
|
||||
null -> c += 5
|
||||
is B -> c += 100
|
||||
else -> c = 1000
|
||||
}
|
||||
|
||||
if (c != 25) return "fail: $c"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// see KT-7683
|
||||
// WhenTranslator must recognize KtWhenConditionInRange
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var result = testFun('1') + testFun('Q') + testFun('z')
|
||||
return if (result == "misshitmiss") "OK" else "fail"
|
||||
}
|
||||
fun testFun(index: Char): String {
|
||||
return when (index) {
|
||||
in 'A'..'Z' -> "hit"
|
||||
else -> "miss"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// see KT-7683
|
||||
// WhenTranslator must recognize KtWhenConditionInRange for custom classes that implement ClosedRange
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var result = testFun(-1) + testFun(0) + testFun(5) + testFun(9) + testFun(10) + testFun(150)
|
||||
if (result != "misshithithitmisshit!") return "fail1: $result"
|
||||
result = testFun2(-1) + testFun2(0) + testFun2(9) + testFun2(10)
|
||||
if (result != "hitmissmisshit") return "fail2: $result"
|
||||
return "OK"
|
||||
}
|
||||
fun testFun(index: Int): String {
|
||||
var lower = Wrapper(0)
|
||||
var upper = Wrapper(9)
|
||||
var secondRange = Wrapper(100)..Wrapper(200)
|
||||
return when (Wrapper(index)) {
|
||||
in lower..upper -> "hit"
|
||||
in secondRange -> "hit!"
|
||||
else -> "miss"
|
||||
}
|
||||
}
|
||||
fun testFun2(index: Int): String {
|
||||
return when (Wrapper(index)) {
|
||||
!in Wrapper(0)..Wrapper(9) -> "hit"
|
||||
else -> "miss"
|
||||
}
|
||||
}
|
||||
|
||||
class Wrapper(val value: Int) : Comparable<Wrapper> {
|
||||
operator fun rangeTo(upper: Wrapper) = WrapperRange(this, upper)
|
||||
override operator fun compareTo(other: Wrapper) = value.compareTo(other.value)
|
||||
}
|
||||
class WrapperRange(override val start: Wrapper, override val endInclusive: Wrapper) : ClosedRange<Wrapper>
|
||||
@@ -0,0 +1,26 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
|
||||
var c = when(3) {
|
||||
3 -> 1
|
||||
2 -> 100
|
||||
else -> 100
|
||||
} + when (2) {
|
||||
1 -> 100
|
||||
else -> 1
|
||||
} + when (0) {
|
||||
1 -> if (true) 100 else 100
|
||||
0 -> if (false) {
|
||||
100
|
||||
}
|
||||
else {
|
||||
1
|
||||
}
|
||||
else -> 100
|
||||
}
|
||||
|
||||
if (c != 3) return "fail"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
var global = ""
|
||||
|
||||
fun <T> bar(a: T, i: Int): T {
|
||||
global += "$i"
|
||||
return a
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val x = 3
|
||||
when(if (x == 4) return bar("fail1", 1) else 4) {
|
||||
else -> return bar("OK", 2)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// see KT-7683
|
||||
// WhenTranslator must recognize KtWhenConditionInRange in general case of a class that has rangeTo method
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var result = testFun(-1) + testFun(0) + testFun(5) + testFun(9) + testFun(10) + testFun(150) + testFun2(50) + testFun2(5)
|
||||
return if (result == "misshithithitmisshit![miss][hit]") "OK" else "fail"
|
||||
}
|
||||
fun testFun(index: Long): String {
|
||||
return when (index) {
|
||||
in 0..9 -> "hit"
|
||||
in 100L..200L -> "hit!"
|
||||
else -> "miss"
|
||||
}
|
||||
}
|
||||
fun testFun2(index: Int): String {
|
||||
return when (index) {
|
||||
in 0L..9L -> "[hit]"
|
||||
else -> "[miss]"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var result = "fail1"
|
||||
val i = 1
|
||||
when (i) {
|
||||
1 ->
|
||||
when (i) {
|
||||
1 -> result = "OK"
|
||||
else -> result = "fail2"
|
||||
}
|
||||
|
||||
else ->
|
||||
when (i) {
|
||||
1 -> result = "OK"
|
||||
else -> result = "fail3"
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var a = 0
|
||||
when (a) {
|
||||
else -> a = 2
|
||||
}
|
||||
if (a != 2) return "fail"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// see KT-7683
|
||||
// WhenTranslator must recognize KtWhenConditionInRange and produce faster code when matched expression is Int
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var result = testFun(-1) + testFun(0) + testFun(5) + testFun(9) + testFun(10) + testFun(150) + testFun(800)
|
||||
if (result != "misshithithitmisshit!@@@" || invocationCount != 7) return "fail1:" + result
|
||||
result = testFun2(-1) + testFun2(0) + testFun2(9) + testFun2(10)
|
||||
if (result != "hitmissmisshit") return "fail2:" + result
|
||||
return "OK"
|
||||
}
|
||||
fun testFun(index: Int): String {
|
||||
val thirdRange = 500..1000
|
||||
return when (get(index)) {
|
||||
in 0..9 -> "hit"
|
||||
in 100.rangeTo(200) -> "hit!"
|
||||
in thirdRange -> "@@@"
|
||||
else -> "miss"
|
||||
}
|
||||
}
|
||||
fun testFun2(index: Int): String {
|
||||
return when(index) {
|
||||
!in 0..9 -> "hit"
|
||||
else -> "miss"
|
||||
}
|
||||
}
|
||||
|
||||
fun get(value: Int): Int {
|
||||
invocationCount++
|
||||
return value
|
||||
}
|
||||
var invocationCount = 0
|
||||
@@ -0,0 +1,6 @@
|
||||
package foo
|
||||
|
||||
fun box() = when {
|
||||
1 > 3 -> "fail"
|
||||
else -> "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var result = "fail1"
|
||||
var i = 1
|
||||
loop@ while(i==1)
|
||||
when (i) {
|
||||
1 -> { result = "OK"; break@loop }
|
||||
else -> result = "fail"
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user