JS backend: added more tests for closures.

This commit is contained in:
Zalim Bashorov
2014-02-26 16:04:33 +04:00
parent ec71a90394
commit a48dc25c46
18 changed files with 518 additions and 5 deletions
@@ -0,0 +1,61 @@
package foo
// HACKS
native
val ROOT = "Kotlin.modules.JS_TESTS"
native
val PATH_TO_F_CREATOR = "foo.B.B\$f"
native
val PATH_TO_G_CREATOR = "foo.B.B\$f_0"
native("$ROOT.$PATH_TO_F_CREATOR")
val F_CREATOR: Any = noImpl
native("$ROOT.$PATH_TO_G_CREATOR")
val G_CREATOR: Any = noImpl
// Test
open class A {
fun foo() = "A::foo"
}
class B : A() {
fun boo() = "B::boo"
val f = { foo() }
val g = { boo() }
}
fun box(): String {
val b = B()
val f = b.f
val g = b.g
assertEquals("A::foo", f())
assertEquals("B::boo", g())
val fs = F_CREATOR.toString()
val gs = G_CREATOR.toString().replaceAll("boo", "foo")
assertEquals(gs, fs)
return "OK"
}
// Helpers
fun assertEquals<T>(expected: T, actual: T) {
if (expected != actual) throw Exception("expected: $expected, actual: $actual");
}
native
fun String.replace(regexp: RegExp, replacement: String): String = noImpl
fun String.replaceAll(regexp: String, replacement: String): String = replace(RegExp(regexp, "g"), replacement)
native
class RegExp(regexp: String, flags: String)