From 53dfd77d3132c8df24d01fa765cc41a471ec0834 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Wed, 4 Mar 2015 17:47:23 +0300 Subject: [PATCH] JS: added inline stdlib cases --- .../kotlin/generators/tests/GenerateTests.kt | 4 + .../semantics/AbstractInlineJsStdlibTest.java | 25 ++++++ .../InlineJsStdlibTestGenerated.java | 80 +++++++++++++++++++ .../inlineStdlib/cases/callNameClash.kt | 27 +++++++ .../testData/inlineStdlib/cases/closure.kt | 21 +++++ .../cases/closureInObjectLiteral.kt | 13 +++ .../inlineStdlib/cases/closureNested.kt | 24 ++++++ .../inlineStdlib/cases/localNamesClash.kt | 19 +++++ .../testData/inlineStdlib/cases/simple.kt | 19 +++++ .../inlineStdlib/cases/thisInExtension.kt | 31 +++++++ 10 files changed, 263 insertions(+) create mode 100644 js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractInlineJsStdlibTest.java create mode 100644 js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InlineJsStdlibTestGenerated.java create mode 100644 js/js.translator/testData/inlineStdlib/cases/callNameClash.kt create mode 100644 js/js.translator/testData/inlineStdlib/cases/closure.kt create mode 100644 js/js.translator/testData/inlineStdlib/cases/closureInObjectLiteral.kt create mode 100644 js/js.translator/testData/inlineStdlib/cases/closureNested.kt create mode 100644 js/js.translator/testData/inlineStdlib/cases/localNamesClash.kt create mode 100644 js/js.translator/testData/inlineStdlib/cases/simple.kt create mode 100644 js/js.translator/testData/inlineStdlib/cases/thisInExtension.kt diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 5c8820eab72..2e25a389fd0 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -797,6 +797,10 @@ fun main(args: Array) { model("inline/cases") } + testClass(javaClass()) { + model("inlineStdlib/cases") + } + testClass(javaClass()) { model("labels/cases") } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractInlineJsStdlibTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractInlineJsStdlibTest.java new file mode 100644 index 00000000000..06f67bb414d --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/AbstractInlineJsStdlibTest.java @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.js.test.semantics; + +import org.jetbrains.kotlin.js.test.AbstractSingleFileTranslationWithDirectivesTest; + +public abstract class AbstractInlineJsStdlibTest extends AbstractSingleFileTranslationWithDirectivesTest { + public AbstractInlineJsStdlibTest() { + super("inlineStdlib/"); + } +} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InlineJsStdlibTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InlineJsStdlibTestGenerated.java new file mode 100644 index 00000000000..52d1f8a762c --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/InlineJsStdlibTestGenerated.java @@ -0,0 +1,80 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.js.test.semantics; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.InnerTestClasses; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.JetTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("js/js.translator/testData/inlineStdlib/cases") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class InlineJsStdlibTestGenerated extends AbstractInlineJsStdlibTest { + public void testAllFilesPresentInCases() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("js/js.translator/testData/inlineStdlib/cases"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("callNameClash.kt") + public void testCallNameClash() throws Exception { + String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/inlineStdlib/cases/callNameClash.kt"); + doTest(fileName); + } + + @TestMetadata("closure.kt") + public void testClosure() throws Exception { + String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/inlineStdlib/cases/closure.kt"); + doTest(fileName); + } + + @TestMetadata("closureInObjectLiteral.kt") + public void testClosureInObjectLiteral() throws Exception { + String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/inlineStdlib/cases/closureInObjectLiteral.kt"); + doTest(fileName); + } + + @TestMetadata("closureNested.kt") + public void testClosureNested() throws Exception { + String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/inlineStdlib/cases/closureNested.kt"); + doTest(fileName); + } + + @TestMetadata("localNamesClash.kt") + public void testLocalNamesClash() throws Exception { + String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/inlineStdlib/cases/localNamesClash.kt"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/inlineStdlib/cases/simple.kt"); + doTest(fileName); + } + + @TestMetadata("thisInExtension.kt") + public void testThisInExtension() throws Exception { + String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/inlineStdlib/cases/thisInExtension.kt"); + doTest(fileName); + } +} diff --git a/js/js.translator/testData/inlineStdlib/cases/callNameClash.kt b/js/js.translator/testData/inlineStdlib/cases/callNameClash.kt new file mode 100644 index 00000000000..15f2a01562d --- /dev/null +++ b/js/js.translator/testData/inlineStdlib/cases/callNameClash.kt @@ -0,0 +1,27 @@ +package foo + +// CHECK_NOT_CALLED: with_dbz3ex + +fun f(x: Int): Int = x * 2 + +fun Int.f(): Int = this * 3 + +class A(var value: Int) { + class object { + fun f(): Int = 5 + } + + fun f(): Int = value +} + +fun test(a: A): Int = + // TODO: check that with second parameter is named f? + with (a) { + f(f() + A.f()).f() + } + +fun box(): String { + assertEquals(90, test(A(10))) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/inlineStdlib/cases/closure.kt b/js/js.translator/testData/inlineStdlib/cases/closure.kt new file mode 100644 index 00000000000..a917f1eb1c2 --- /dev/null +++ b/js/js.translator/testData/inlineStdlib/cases/closure.kt @@ -0,0 +1,21 @@ +package foo + +// CHECK_CONTAINS_NO_CALLS: test + +fun test(a: Int, b: Int): Int { + var c = 0 + + b.times { + c += a + } + + return c +} + +fun box(): String { + assertEquals(6, test(2, 3)) + assertEquals(6, test(3, 2)) + assertEquals(20, test(4, 5)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/inlineStdlib/cases/closureInObjectLiteral.kt b/js/js.translator/testData/inlineStdlib/cases/closureInObjectLiteral.kt new file mode 100644 index 00000000000..c1ada1f4fee --- /dev/null +++ b/js/js.translator/testData/inlineStdlib/cases/closureInObjectLiteral.kt @@ -0,0 +1,13 @@ +package foo + +val Int.abs: Int + get() = if (this >= 0) this else -this + +fun test(xs: List): List = + xs.sortBy(comparator {(a, b) -> a.abs.compareTo(b.abs) }) + +fun box(): String { + assertEquals(listOf(1, -2, 3, -4), test(listOf(-2, 1, -4, 3))) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/inlineStdlib/cases/closureNested.kt b/js/js.translator/testData/inlineStdlib/cases/closureNested.kt new file mode 100644 index 00000000000..e3501f7d60b --- /dev/null +++ b/js/js.translator/testData/inlineStdlib/cases/closureNested.kt @@ -0,0 +1,24 @@ +package foo + +// CHECK_CONTAINS_NO_CALLS: test + +fun test(a: Int, b: Int): Int { + var res = 0 + + with (a + b) { + val t = this + + t.times { + res += t - b + } + } + + return res +} + +fun box(): String { + assertEquals(10, test(2, 3)) + assertEquals(15, test(3, 2)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/inlineStdlib/cases/localNamesClash.kt b/js/js.translator/testData/inlineStdlib/cases/localNamesClash.kt new file mode 100644 index 00000000000..ebb0dbe5010 --- /dev/null +++ b/js/js.translator/testData/inlineStdlib/cases/localNamesClash.kt @@ -0,0 +1,19 @@ +package foo + +// CHECK_CONTAINS_NO_CALLS: test + +fun test(x: Int, y: Int): Int = + with (x + x) { + val xx = this + + with (y + y) { + xx + this + } + } + +fun box(): String { + assertEquals(10, test(2, 3)) + assertEquals(18, test(4, 5)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/inlineStdlib/cases/simple.kt b/js/js.translator/testData/inlineStdlib/cases/simple.kt new file mode 100644 index 00000000000..d73adebb927 --- /dev/null +++ b/js/js.translator/testData/inlineStdlib/cases/simple.kt @@ -0,0 +1,19 @@ +package foo + +// CHECK_CONTAINS_NO_CALLS: test + +var counter = 0 + +fun test(a: Int) { + a.times { + counter += 1 + } +} + +fun box(): String { + assertEquals(0, counter) + test(5) + assertEquals(5, counter) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/inlineStdlib/cases/thisInExtension.kt b/js/js.translator/testData/inlineStdlib/cases/thisInExtension.kt new file mode 100644 index 00000000000..f26b1e48687 --- /dev/null +++ b/js/js.translator/testData/inlineStdlib/cases/thisInExtension.kt @@ -0,0 +1,31 @@ +package foo + +// CHECK_CONTAINS_NO_CALLS: testImplicitThis +// CHECK_CONTAINS_NO_CALLS: testExplicitThis + +class A(var value: Int) + +fun testImplicitThis(a: A, newValue: Int) { + with (a) { + value = newValue + } +} + +fun testExplicitThis(a: A, newValue: Int) { + with (a) { + this.value = newValue + } +} + +fun box(): String { + val a = A(0) + assertEquals(0, a.value) + + testImplicitThis(a, 10) + assertEquals(10, a.value) + + testExplicitThis(a, 20) + assertEquals(20, a.value) + + return "OK" +} \ No newline at end of file