From fb7165c7fadc4bee0d02bab81999e7b048fdf4a2 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Thu, 6 Mar 2014 14:35:13 +0400 Subject: [PATCH] JS backend: added regression tests for closure bugs. #KT-4236 In Progress #KT-4600 In Progress #KT-4237 In Progress #KT-4207 In Progress #KT-4218 In Progress #KT-4263 Obsolete --- .../k2js/test/semantics/ClosureTest.java | 20 +++++++++++++++ .../cases/closureInNestedLambdasInObject.kt | 18 +++++++++++++ .../closure/cases/closureInWithInsideWith.kt | 25 +++++++++++++++++++ .../closure/cases/closureLambdaVarInLambda.kt | 23 +++++++++++++++++ .../closureThisInExtLambdaInsideMethod.kt | 11 ++++++++ .../cases/closureThisInLambdaInsideMethod.kt | 17 +++++++++++++ 6 files changed, 114 insertions(+) create mode 100644 js/js.translator/testData/closure/cases/closureInNestedLambdasInObject.kt create mode 100644 js/js.translator/testData/closure/cases/closureInWithInsideWith.kt create mode 100644 js/js.translator/testData/closure/cases/closureLambdaVarInLambda.kt create mode 100644 js/js.translator/testData/closure/cases/closureThisInExtLambdaInsideMethod.kt create mode 100644 js/js.translator/testData/closure/cases/closureThisInLambdaInsideMethod.kt diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java index c9c208415a0..850b8433d0c 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClosureTest.java @@ -143,4 +143,24 @@ public final class ClosureTest extends SingleFileTranslationTest { public void testClosureInFewFunctionWithDifferentName() throws Exception { checkFooBoxIsOk(); } + + public void testClosureLambdaVarInLambda() throws Exception { + checkFooBoxIsOk(); + } + + public void testClosureThisInExtLambdaInsideMethod() throws Exception { + checkFooBoxIsOk(); + } + + public void testClosureInWithInsideWith() throws Exception { + checkFooBoxIsOk(); + } + + public void testClosureInNestedLambdasInObject() throws Exception { + checkFooBoxIsOk(); + } + + public void testClosureThisInLambdaInsideMethod() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/testData/closure/cases/closureInNestedLambdasInObject.kt b/js/js.translator/testData/closure/cases/closureInNestedLambdasInObject.kt new file mode 100644 index 00000000000..44f816fd4a4 --- /dev/null +++ b/js/js.translator/testData/closure/cases/closureInNestedLambdasInObject.kt @@ -0,0 +1,18 @@ +// KT-4218 Nested function literal on singleton object fails + +package foo + +object SomeObject { + val values = create() + fun create() = Array>(1) { y -> + Array(1) { x -> + "(${x}, ${y})" + } + } +} + +fun box(): String { + if (SomeObject.values[0][0] != "(0, 0)") return SomeObject.values[0][0] + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/closure/cases/closureInWithInsideWith.kt b/js/js.translator/testData/closure/cases/closureInWithInsideWith.kt new file mode 100644 index 00000000000..edaae9306d0 --- /dev/null +++ b/js/js.translator/testData/closure/cases/closureInWithInsideWith.kt @@ -0,0 +1,25 @@ +// KT-4237 With in with + +package foo + +class A { + val ok = "OK" +} + +class B + +fun with(o: T, body: T.() -> Unit) { + o.body() +} + +fun box(): String { + var o = "" + + with(A()) { + with(B()) { + o = ok + } + } + + return o +} diff --git a/js/js.translator/testData/closure/cases/closureLambdaVarInLambda.kt b/js/js.translator/testData/closure/cases/closureLambdaVarInLambda.kt new file mode 100644 index 00000000000..a164438e9ad --- /dev/null +++ b/js/js.translator/testData/closure/cases/closureLambdaVarInLambda.kt @@ -0,0 +1,23 @@ +// KT-4263 Wrong capturing a function literal variable + +package foo + +fun assertEquals(expected: T, actual: T) { + if (expected != actual) throw Exception("expected = $expected, actual = $actual") +} + +fun box(): String { + var foo = { 1 } + var bar = 1 + + val t = { "${foo()} $bar" } + fun b() = "${foo()} $bar" + + foo = { 2 } + bar = 2 + + assertEquals("2 2", t()) + assertEquals("2 2", b()) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/closure/cases/closureThisInExtLambdaInsideMethod.kt b/js/js.translator/testData/closure/cases/closureThisInExtLambdaInsideMethod.kt new file mode 100644 index 00000000000..22fa1762c78 --- /dev/null +++ b/js/js.translator/testData/closure/cases/closureThisInExtLambdaInsideMethod.kt @@ -0,0 +1,11 @@ +// KT-4600 Generated wrong code when capturing `this` in extension function inside a method + +package foo + +fun run(f: Int.() -> T) = 1.f() + +public class Foo(val trigger: () -> Any) { + fun test() = run {trigger()}; +} + +fun box() = Foo({ "OK" }).test() diff --git a/js/js.translator/testData/closure/cases/closureThisInLambdaInsideMethod.kt b/js/js.translator/testData/closure/cases/closureThisInLambdaInsideMethod.kt new file mode 100644 index 00000000000..4a278f6388c --- /dev/null +++ b/js/js.translator/testData/closure/cases/closureThisInLambdaInsideMethod.kt @@ -0,0 +1,17 @@ +// KT-4207 Closure this doesn't work in JS backend + +package foo + +fun String.foo() = { this } + +class A { + fun foo() = { this } + val ok = "OK" +} + +fun box(): String { + if ("OK".foo()() != "OK") return "\"OK\".foo()() != \"OK\"" + if (A().foo()().ok != "OK") return "A().foo()().ok != \"OK\"" + + return "OK" +}