JS backend: move patternMatching tests to expression/when
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): Boolean {
|
||||
var result = false
|
||||
var i = 1
|
||||
do
|
||||
when (i) {
|
||||
1 -> result = true
|
||||
else -> result = false
|
||||
}
|
||||
while (i==0)
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): Boolean {
|
||||
var result = false
|
||||
for (i in array(1))
|
||||
when (i) {
|
||||
1 -> result = true
|
||||
else -> result = false
|
||||
}
|
||||
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,13 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): Boolean {
|
||||
var result = false
|
||||
var i = 1
|
||||
if (i==1)
|
||||
when (i) {
|
||||
1 -> result = true
|
||||
else -> result = false
|
||||
}
|
||||
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(): Boolean {
|
||||
var a = null : A?
|
||||
when(a) {
|
||||
is A? -> return true
|
||||
else -> return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
fun box(): Int {
|
||||
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;
|
||||
}
|
||||
}
|
||||
return z
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): Boolean {
|
||||
|
||||
return (when(1) {
|
||||
2 -> 3
|
||||
1 -> 1
|
||||
else -> 5
|
||||
} == 1)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
fun box(): Boolean {
|
||||
|
||||
|
||||
(when (1) {
|
||||
3 -> {
|
||||
3
|
||||
}
|
||||
1 -> {
|
||||
throw Exception();
|
||||
}
|
||||
else -> {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
fun box(): Boolean {
|
||||
var a = 0
|
||||
var i = 0
|
||||
when(i++) {
|
||||
-100 -> a++
|
||||
100 -> a++
|
||||
else -> a++
|
||||
}
|
||||
return (a == 1) && (i == 1)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): Boolean {
|
||||
var a = 0
|
||||
when(A()) {
|
||||
is A -> a++;
|
||||
is A -> a++;
|
||||
else -> a++;
|
||||
}
|
||||
return (a == 1)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): Boolean {
|
||||
when(A()) {
|
||||
!is A -> return false;
|
||||
else -> return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): Boolean {
|
||||
when(A()) {
|
||||
is A -> return true;
|
||||
else -> return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package foo
|
||||
|
||||
fun box(): Boolean {
|
||||
var a = 4
|
||||
when(a) {
|
||||
3 -> {
|
||||
a = 10;
|
||||
}
|
||||
4 -> {
|
||||
a = 20;
|
||||
}
|
||||
else -> {
|
||||
a = 30;
|
||||
}
|
||||
}
|
||||
return (a == 20)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
}
|
||||
|
||||
class B() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): Boolean {
|
||||
var c: Int = 0
|
||||
var a = A() : Any?
|
||||
var b = null : Any?
|
||||
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
|
||||
}
|
||||
return (c == 25)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package foo
|
||||
|
||||
fun box(): Boolean {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return c == 3
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): Boolean {
|
||||
var result = false
|
||||
val i = 1
|
||||
when (i) {
|
||||
1 ->
|
||||
when (i) {
|
||||
1 -> result = true
|
||||
else -> result = false
|
||||
}
|
||||
|
||||
else ->
|
||||
when (i) {
|
||||
1 -> result = true
|
||||
else -> result = false
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): Boolean {
|
||||
var a = 0
|
||||
when (a) {
|
||||
else -> a = 2
|
||||
}
|
||||
return a == 2
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package foo
|
||||
|
||||
fun box() = when {
|
||||
1 > 3 -> false
|
||||
else -> true
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): Boolean {
|
||||
var result = false
|
||||
var i = 1
|
||||
while(i==1)
|
||||
when (i) {
|
||||
1 -> { result = true; break }
|
||||
else -> result = false
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user