JS backend: added tests for infix calls.

#KT-3998   in progress
 #EA-56241 in progress
This commit is contained in:
Zalim Bashorov
2014-05-08 15:54:05 +04:00
parent 235b03c407
commit 898275e658
6 changed files with 99 additions and 1 deletions
@@ -146,4 +146,8 @@ public class FunctionTest extends AbstractExpressionTest {
public void testLocalExtFunction() throws Exception {
checkFooBoxIsOk();
}
public void testInfixCall() throws Exception {
checkFooBoxIsOk();
}
}
@@ -53,4 +53,8 @@ public final class InvokeConventionTest extends AbstractExpressionTest {
public void testInvokeWithThisObjectAndReceiver() throws Exception {
fooBoxTest();
}
}
public void testInfixCall() throws Exception {
checkFooBoxIsOk();
}
}
@@ -116,4 +116,8 @@ public final class OperatorOverloadingTest extends SingleFileTranslationTest {
public void testOverloadUnaryOperationsViaExtensionFunctions() throws Exception {
fooBoxTest();
}
public void testOverloadByLambda() throws Exception {
checkFooBoxIsOk();
}
}
@@ -0,0 +1,23 @@
// EA-56241
package foo
fun Int.foo(a: Int) = this + a
val bar = { Int.(a: Int) -> this * a }
fun test(op: Int.(Int) -> Int) = 3 op 20
fun assertEquals<T>(expected: T, actual: T) {
if (expected != actual) throw Exception("expected: $expected, actual: $actual")
}
fun box(): String {
val op = { Int.(a: Int) -> this / a }
assertEquals(41, 34 foo 7)
assertEquals(28, 4 bar 7)
assertEquals(-17, test { this - it })
assertEquals(7, 49 op 7)
return "OK"
}
@@ -0,0 +1,40 @@
// KT-3998 Infix call doesn't work for function literals and another classes which implements invoke convention (in JS backend)
package foo
class A(val s: String) {
fun A.invoke(a: A) = "${this.s} ${this@A.s} ${a.s}"
}
fun test1(): String {
val a = A("a")
val b = A("b")
val c = A("c")
val f = a.b(c) // works
val s = a b c //compiler crashes
return "$f | $s"
}
fun test2(): String {
val a: ExtensionFunction1<*, *, *>.(ExtensionFunction1<*, *, *>)->String = { "a" }
val b: ExtensionFunction1<*, *, *>.(ExtensionFunction1<*, *, *>)->String = {
val aa = this as ExtensionFunction1<Any?, Any?, Any?>;
val cc = it as ExtensionFunction1<Any?, Any?, Any?>;
"${null.aa(null)} b ${null.cc(null)}"
}
val c: ExtensionFunction1<*, *, *>.(ExtensionFunction1<*, *, *>)->String = { "c" }
val f = a.b(c) // works
val s = a b c //compiler crashes
return "$f | $s"
}
fun assertEquals<T>(expected: T, actual: T) {
if (expected != actual) throw Exception("expected: $expected, actual: $actual")
}
fun box(): String {
assertEquals("a b c | a b c", test1())
assertEquals("a b c | a b c", test2())
return "OK"
}
@@ -0,0 +1,23 @@
// EA-56241
package foo
class A(val v: Int)
val times = { A.(a: Int) -> this.v * a }
fun test(div: A.(Int) -> Int) = A(20) / 4
fun assertEquals<T>(expected: T, actual: T) {
if (expected != actual) throw Exception("expected: $expected, actual: $actual")
}
fun box(): String {
val compareTo = { A.(a: A) -> this.v - a.v }
assertEquals(28, A(4) * 7)
assertEquals(5, test { this.v / it })
assertEquals(false, A(49) <= A(7))
assertEquals(true, A(5) > A(1))
return "OK"
}