JS: move expressions test to box tests
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var foo = { x: String -> x + "K" }
|
||||
return foo.invoke("O")
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
class A(val f: (B.() -> Int)?)
|
||||
|
||||
class B(val x: Int)
|
||||
|
||||
fun test(g: (B.() -> Int)?): Int? {
|
||||
val a = A(g)
|
||||
val b = B(2)
|
||||
return a.f?.invoke(b)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(5, test { x + 3 })
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* NOTE: this test originally checked that values of classes inheriting from functions could be invoked as functions.
|
||||
* However, Function{n} / ExtensionFunction{n} classes were incompatible with JS functions our lambdas were compiled to.
|
||||
* This led to runtime errors (see KT-7692), so the test is temporarily disabled.
|
||||
*
|
||||
* TODO: support inheritance from function types and re-enable this test
|
||||
* NOTE: inheritance from extension function is forbidden now
|
||||
*/
|
||||
|
||||
package foo
|
||||
|
||||
class Bar/* : Function0<String>*/ {
|
||||
operator fun invoke() = "Bar.invoke()"
|
||||
}
|
||||
|
||||
class Baz/* : Function2<Int, Boolean, String>*/ {
|
||||
operator fun invoke(i: Int, b: Boolean) = "Baz.invoke($i, $b)"
|
||||
}
|
||||
|
||||
class Mixed/* :
|
||||
Function1<Int, String>,
|
||||
Function2<Int, Boolean, String>*/
|
||||
{
|
||||
operator fun invoke(i: Int) = "Mixed.invoke($i)"
|
||||
operator fun invoke(i: Int, b: Boolean) = "Mixed.invoke($i, $b)"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val bar = Bar()
|
||||
val baz = Baz()
|
||||
val mixed = Mixed()
|
||||
|
||||
assertEquals("Bar.invoke()", bar())
|
||||
assertEquals("Baz.invoke(2, false)", baz(2, false))
|
||||
|
||||
assertEquals("Mixed.invoke(45)", mixed(45))
|
||||
assertEquals("Mixed.invoke(552, true)", mixed(552, true))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val v1 = 1.(fun Int.(x: Int) = this + x)(2)
|
||||
|
||||
val f = fun Int.(x: Int) = this + x
|
||||
val v2 = 1.(f)(2)
|
||||
|
||||
if (v1 != 3) return "fail1: $v1"
|
||||
if (v2 != 3) return "fail2: $v2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val v1 = { x: Int -> x}(2)
|
||||
|
||||
val f = { x: Int -> x}
|
||||
val v2 = (f)(2)
|
||||
|
||||
if (v1 != 2) return "fail1: $v1"
|
||||
if (v2 != 2) return "fail2: $v2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
class Foo(val postfix: String) {
|
||||
operator fun invoke(text: String): String {
|
||||
return text + postfix
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = Foo(" world!")
|
||||
assertEquals("hello world!", a("hello"))
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
operator fun invoke() = "##"
|
||||
operator fun invoke(i: Int) = "#${i}"
|
||||
}
|
||||
|
||||
fun foo() = A()
|
||||
|
||||
fun box(): String {
|
||||
if (A()() != "##") return "fail1"
|
||||
if (A()(1) != "#1") return "fail2"
|
||||
if (foo()() != "##") return "fail3"
|
||||
if (foo()(42) != "#42") return "fail4"
|
||||
if ((foo())(42) != "#42") return "fail5"
|
||||
if ({ -> A()}()() != "##") return "fail6"
|
||||
if ({ -> A()}()(37) != "#37") return "fail7"
|
||||
return "OK"
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
val b = fun A.(i: Int) = i
|
||||
val result = a.(b)(1)
|
||||
if (result != 1) return "fail: $result"
|
||||
return "OK"
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
operator fun invoke(i: Int) = i
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = A()(1)
|
||||
if (result != 1) return "fail: $result"
|
||||
return "OK"
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
operator fun Int.invoke(x: Int) = this + x
|
||||
fun box(): String {
|
||||
val result = 1(2)
|
||||
if (result != 3) return "fail: $result"
|
||||
return "OK"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
fun A.f(s: String) = value + s
|
||||
|
||||
class A(val value: String) {
|
||||
fun bar(s: String) = (A::f)(this, s)
|
||||
}
|
||||
|
||||
fun A.baz(s: String) = (A::f)(this, s)
|
||||
|
||||
fun box(): String {
|
||||
val a = A("aaa")
|
||||
|
||||
assertEquals("aaa.bar()", a.bar(".bar()"))
|
||||
assertEquals("aaa.baz()", a.baz(".baz()"))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user