JS backend: added tests for KT-4159.

This commit is contained in:
Alexey Tsvetkov
2014-03-12 04:35:57 +04:00
committed by Zalim Bashorov
parent 569f139d8a
commit 21f87350b9
4 changed files with 106 additions and 3 deletions
@@ -66,7 +66,6 @@ public final class MiscTest extends AbstractExpressionTest {
checkOutput("KT-1052.kt", "true\n");
}
public void testKt740_1() throws Exception {
checkFooBoxIsTrue("KT-740.kt");
}
@@ -123,6 +122,18 @@ public final class MiscTest extends AbstractExpressionTest {
fooBoxTest();
}
public void testElvisReturnSimple() throws Exception {
checkFooBoxIsOk();
}
public void testElvisReturnNested() throws Exception {
checkFooBoxIsOk();
}
public void testElvisWithThrow() throws Exception {
checkFooBoxIsOk();
}
public void testExtensionLiteralCalledInsideExtensionFunction() throws Exception {
fooBoxTest();
}
@@ -135,12 +146,10 @@ public final class MiscTest extends AbstractExpressionTest {
checkOutput("mainFunInNestedPackage.kt", "ayee");
}
public void testPropertiesWithExplicitlyDefinedAccessorsWithoutBodies() throws Exception {
fooBoxTest();
}
public void testExclExcl() throws Exception {
fooBoxTest();
}
@@ -0,0 +1,27 @@
/*
* Issue: KT-4159 Kotlin to JS compiler crashes on code with ?: return
*
* Expression like "val s1 : String = s ?: return null" causes compiler to crash
*/
package foo
fun assertEquals<T>(expected: T, actual: T) {
if (expected != actual) throw Exception("expected: $expected, actual: $actual")
}
fun firstNotNullLen(s1 : String?, s2 : String?, s3 : String?) : Int {
val len = (s1?.length() ?: s2?.length()) ?:
(s2?.length() ?: s3?.length()) ?:
return 0
return len
}
fun box(): String {
assertEquals(1, firstNotNullLen("a", null, null))
assertEquals(2, firstNotNullLen(null, "ab", null))
assertEquals(3, firstNotNullLen(null, null, "abc"))
assertEquals(0, firstNotNullLen(null, null, null))
return "OK"
}
@@ -0,0 +1,32 @@
/*
* Issue: KT-4159 Kotlin to JS compiler crashes on code with ?: return
*
* Expression like "val s1 : String = s ?: return null" causes compiler to crash
*/
package foo
fun assertEquals<T>(expected: T, actual: T) {
if (expected != actual) throw Exception("expected: $expected, actual: $actual")
}
fun stringLen(s : String?) : Int {
val s1 : String = s ?: return 0
return s1.length
}
fun stringReturnInLeftLen(s : String?) : Int {
val s1 : String = (if (s != null) { return s.length() } else { null }) ?: return 0
}
fun box(): String {
assertEquals(3, stringLen("box"))
assertEquals(0, stringLen(""))
assertEquals(0, stringLen(null))
assertEquals(3, stringReturnInLeftLen("box"))
assertEquals(0, stringReturnInLeftLen(""))
assertEquals(0, stringReturnInLeftLen(null))
return "OK"
}
@@ -0,0 +1,35 @@
package foo
fun assertEquals<T>(expected: T, actual: T) {
if (expected != actual) throw Exception("expected: $expected, actual: $actual")
}
var i = 0
fun bar(): Any? {
i++
return null
}
native
val Exception.message: String = noImpl
fun box(): String {
val a: String? = null
try {
a ?: throw Exception("a")
}
catch (e: Exception) {
assertEquals("a", e.message)
}
try {
bar() ?: throw Exception("bar()")
}
catch (e: Exception) {
assertEquals("bar()", e.message)
}
assertEquals(1, i)
return "OK"
}