From b883bf6ef599a8546b125123a5b495d55f852263 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Tue, 31 Jul 2012 18:49:30 +0400 Subject: [PATCH] Adopt more tests for functions from codegen tests. Create test for KT-2565. --- .../k2js/test/semantics/MiscTest.java | 10 ++++ .../additional/cases/localFunction.kt | 46 +++++++++++++++++++ .../misc/cases/functionExpression.kt | 28 +++++++++++ .../misc/cases/localVarAsFunction.kt | 17 +++++++ 4 files changed, 101 insertions(+) create mode 100644 js/js.translator/testFiles/additional/cases/localFunction.kt create mode 100644 js/js.translator/testFiles/expression/misc/cases/functionExpression.kt create mode 100644 js/js.translator/testFiles/expression/misc/cases/localVarAsFunction.kt diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java index 8dd344d1a48..9830beb96aa 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java @@ -166,6 +166,16 @@ public final class MiscTest extends AbstractExpressionTest { checkOutput("lazyProperty.kt", "Hello, world! Gotcha 3"); } + public void testLocalVarAsFunction() throws Exception { + fooBoxIsValue("OK"); + } + + //TODO:see http://youtrack.jetbrains.com/issue/KT-2565 + @SuppressWarnings("UnusedDeclaration") + public void TODO_testFunctionExpression() throws Exception { + fooBoxIsValue("OK"); + } + public void testExclExclThrows() throws Exception { try { fooBoxTest(); diff --git a/js/js.translator/testFiles/additional/cases/localFunction.kt b/js/js.translator/testFiles/additional/cases/localFunction.kt new file mode 100644 index 00000000000..9ec49ecbeda --- /dev/null +++ b/js/js.translator/testFiles/additional/cases/localFunction.kt @@ -0,0 +1,46 @@ +fun IntRange.forEach(body : (Int) -> Unit) { + for(i in this) { + body(i) + } +} + +fun box() : String { + var seed = 0 + + fun local(x: Int) { + fun deep() { + seed += x + } + fun deep2(x : Int) { + seed += x + } + fun Int.iter() { + seed += this + } + + deep() + deep2(-x) + x.iter() + seed += x + } + + for(i in 1..5) { + fun Int.iter() { + seed += this + } + + local(i) + (-i).iter() + } + + fun local2(y: Int) { + seed += y + } + + (1..5).forEach { + local2(it) + } + + + return if(seed == 30) "OK" else seed.toString() +} \ No newline at end of file diff --git a/js/js.translator/testFiles/expression/misc/cases/functionExpression.kt b/js/js.translator/testFiles/expression/misc/cases/functionExpression.kt new file mode 100644 index 00000000000..ec7a4146568 --- /dev/null +++ b/js/js.translator/testFiles/expression/misc/cases/functionExpression.kt @@ -0,0 +1,28 @@ +package foo + +fun Any.foo1() : ()-> String { + return { "239" + this } +} + +fun Int.foo2() : (i : Int) -> Int { + return { x -> x + this } +} + +fun fooT1(t : T) = { t.toString() } + +fun fooT2(t: T) = { (x:T) -> t.toString() + x.toString() } + +fun box() : Any? { + if( (10.foo1())() != "23910") return "foo1 fail" + if( (10.foo2())(1) != 11 ) return "foo2 fail" + + if(1.{Int.() -> this + 1}() != 2) return "test 3 failed"; + if( {1}() != 1) return "test 4 failed"; + if( {(x : Int) -> x}(1) != 1) return "test 5 failed"; + if( 1.{Int.(x : Int) -> x + this}(1) != 2) return "test 6 failed"; + val tmp = 1.({Int.() -> this})() + if(tmp != 1) return "test 7 failed, res: $tmp ${tmp is Int}"; + if( (fooT1("mama"))() != "mama") return "test 8 failed"; + if( (fooT2("mama"))("papa") != "mamapapa") return "test 9 failed"; + return "OK" +} diff --git a/js/js.translator/testFiles/expression/misc/cases/localVarAsFunction.kt b/js/js.translator/testFiles/expression/misc/cases/localVarAsFunction.kt new file mode 100644 index 00000000000..b31c76e7b43 --- /dev/null +++ b/js/js.translator/testFiles/expression/misc/cases/localVarAsFunction.kt @@ -0,0 +1,17 @@ +package foo + +var c = 2 + +fun loop(var times : Int) { + while(times > 0) { + val u : (value : Int) -> Unit = { + c++ + } + u(times--) + } +} + +fun box() : Any? { + loop(5) + return if (c == 7) return "OK" else c +} \ No newline at end of file