[KT-7683] Implement translation of 'when .. in' clause to JS
This commit is contained in:
+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,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,24 @@
|
||||
// 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)
|
||||
return if (result == "misshithithitmisshit!") "OK" else "fail"
|
||||
}
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
||||
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,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,23 @@
|
||||
// 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)
|
||||
return if (result == "misshithithitmisshit!@@@" && invocationCount == 7) "OK" else "fail"
|
||||
}
|
||||
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 get(value: Int): Int {
|
||||
invocationCount++
|
||||
return value
|
||||
}
|
||||
var invocationCount = 0
|
||||
Reference in New Issue
Block a user