diff --git a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java index ea5299bb2f3..8523265248a 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java @@ -16,11 +16,12 @@ package org.jetbrains.k2js.test; +import com.google.common.collect.Lists; import com.intellij.openapi.util.io.FileUtil; import org.jetbrains.annotations.NotNull; import java.io.File; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import static org.jetbrains.k2js.test.utils.JsTestUtils.convertToDotJsFile; @@ -81,8 +82,8 @@ public abstract class BasicTest extends TestWithEnvironment { assert success; } - protected static String kotlinLibraryPath() { - return KOTLIN_JS_LIB; + protected List additionalJSFiles() { + return Collections.singletonList(KOTLIN_JS_LIB); } protected static String casesDirectoryName() { @@ -98,7 +99,7 @@ public abstract class BasicTest extends TestWithEnvironment { } @NotNull - private String pathToTestFiles() { + protected String pathToTestFiles() { return pathToTestFilesRoot() + getMainDirectory(); } @@ -107,34 +108,44 @@ public abstract class BasicTest extends TestWithEnvironment { return TEST_FILES; } + @NotNull private String getOutputPath() { return pathToTestFiles() + outDirectoryName(); } + @NotNull private String getInputPath() { return pathToTestFiles() + casesDirectoryName(); } + @NotNull private String getExpectedPath() { return pathToTestFiles() + expectedDirectoryName(); } - protected static List withKotlinJsLib(@NotNull String inputFile) { - return Arrays.asList(kotlinLibraryPath(), inputFile); + @NotNull + protected List withAdditionalFiles(@NotNull String inputFile) { + List allFiles = Lists.newArrayList(additionalJSFiles()); + allFiles.add(inputFile); + return allFiles; } + @NotNull protected String getOutputFilePath(@NotNull String filename) { return getOutputPath() + convertToDotJsFile(filename); } + @NotNull protected String getInputFilePath(@NotNull String filename) { return getInputPath() + filename; } + @NotNull protected String cases(@NotNull String filename) { return getInputFilePath(filename); } + @NotNull protected String expected(@NotNull String testName) { return getExpectedPath() + testName + ".out"; } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java index 37e969015af..8af99d45c2a 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java @@ -19,11 +19,10 @@ package org.jetbrains.k2js.test; import org.jetbrains.annotations.NotNull; import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker; -import java.io.File; -import java.util.ArrayList; import java.util.List; import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; +import static org.jetbrains.k2js.test.utils.JsTestUtils.getAllFilesInDir; import static org.jetbrains.k2js.test.utils.TranslationUtils.translateFiles; /** @@ -36,20 +35,14 @@ public abstract class MultipleFilesTranslationTest extends BasicTest { } protected void generateJsFromDir(@NotNull String dirName) throws Exception { - String dirPath = getInputFilePath(dirName); - File dir = new File(dirPath); - List fullFilePaths = new ArrayList(); - for (String fileName : dir.list()) { - fullFilePaths.add(getInputFilePath(dirName) + "/" + fileName); - } - assert dir.isDirectory(); + List fullFilePaths = getAllFilesInDir(getInputFilePath(dirName)); translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt")); } protected void runMultiFileTest(@NotNull String dirName, @NotNull String namespaceName, @NotNull String functionName, @NotNull Object expectedResult) throws Exception { generateJsFromDir(dirName); - runRhinoTest(withKotlinJsLib(getOutputFilePath(dirName + ".kt")), + runRhinoTest(withAdditionalFiles(getOutputFilePath(dirName + ".kt")), new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult)); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java index ee2f23758bc..0636afea5ad 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java @@ -39,7 +39,7 @@ public abstract class SingleFileTranslationTest extends BasicTest { public void runFunctionOutputTest(String filename, String namespaceName, String functionName, Object expectedResult) throws Exception { generateJsFromFile(filename); - runRhinoTest(withKotlinJsLib(getOutputFilePath(filename)), + runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)), new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult)); } @@ -57,7 +57,7 @@ public abstract class SingleFileTranslationTest extends BasicTest { protected void checkOutput(@NotNull String filename, @NotNull String expectedResult, @NotNull String... args) throws Exception { generateJsFromFile(filename); - runRhinoTest(withKotlinJsLib(getOutputFilePath(filename)), + runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)), new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args))); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/KotlinLibTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/KotlinLibTest.java index 190b62690f8..e3003df6287 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/KotlinLibTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/KotlinLibTest.java @@ -23,8 +23,6 @@ import org.jetbrains.k2js.test.rhino.RhinoResultChecker; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; -import java.util.Arrays; - import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; /** @@ -37,7 +35,7 @@ public final class KotlinLibTest extends SingleFileTranslationTest { } public void testKotlinJsLibRunsWithRhino() throws Exception { - runRhinoTest(Arrays.asList(kotlinLibraryPath()), new RhinoResultChecker() { + runRhinoTest(additionalJSFiles(), new RhinoResultChecker() { @Override public void runChecks(Context context, Scriptable scope) throws Exception { //do nothing @@ -85,7 +83,7 @@ public final class KotlinLibTest extends SingleFileTranslationTest { private void runJavascriptTest(@NotNull String filename) throws Exception { - runRhinoTest(Arrays.asList(kotlinLibraryPath(), cases(filename)), + runRhinoTest(withAdditionalFiles(cases(filename)), new RhinoFunctionResultChecker("test", true)); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java new file mode 100644 index 00000000000..745dacc163d --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2012 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.k2js.test.semantics; + +import com.google.common.collect.Lists; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.k2js.test.SingleFileTranslationTest; +import org.jetbrains.k2js.test.utils.JsTestUtils; + +import java.util.List; + +/** + * @author Pavel Talanov + */ +public final class NativeInteropTest extends SingleFileTranslationTest { + + @NotNull + private static final String NATIVE = "native/"; + + public NativeInteropTest() { + super("native/"); + } + + @Override + protected List additionalJSFiles() { + List result = Lists.newArrayList(super.additionalJSFiles()); + result.addAll(JsTestUtils.getAllFilesInDir(pathToTestFiles() + NATIVE)); + return result; + } + + public void testSimple() throws Exception { + checkFooBoxIsTrue("simple.kt"); + } + + public void testClass() throws Exception { + checkFooBoxIsTrue("class.kt"); + } + + public void testVararg() throws Exception { + checkFooBoxIsTrue("vararg.kt"); + } + + public void testUndefined() throws Exception { + checkFooBoxIsTrue("undefined.kt"); + } +} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java index d7acfa400e5..465e025a358 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java @@ -22,6 +22,8 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** * @author Pavel Talanov @@ -44,4 +46,15 @@ public final class JsTestUtils { stream.close(); } } + + @NotNull + public static List getAllFilesInDir(@NotNull String dirName) { + File dir = new File(dirName); + assert dir.isDirectory(); + List fullFilePaths = new ArrayList(); + for (String fileName : dir.list()) { + fullFilePaths.add(dirName + "/" + fileName); + } + return fullFilePaths; + } } diff --git a/js/js.translator/testFiles/native/cases/class.kt b/js/js.translator/testFiles/native/cases/class.kt new file mode 100644 index 00000000000..9b1d3e907be --- /dev/null +++ b/js/js.translator/testFiles/native/cases/class.kt @@ -0,0 +1,27 @@ +package foo + +import js.* + +native +class A(b : Int) { + fun g() : Int = js.noImpl + fun m() : Int = js.noImpl +} + + +fun box() : Boolean { + if (A(2).g() != 4) { + return false; + } + if (A(3).m() != 2) { + return false; + } + val a = A(100) + if (a.g() != 200) { + return false; + } + if (a.m() != 99) { + return false; + } + return true; +} \ No newline at end of file diff --git a/js/js.translator/testFiles/native/cases/simple.kt b/js/js.translator/testFiles/native/cases/simple.kt new file mode 100644 index 00000000000..5a9540ae8b4 --- /dev/null +++ b/js/js.translator/testFiles/native/cases/simple.kt @@ -0,0 +1,8 @@ +package foo + +import js.* + +native +fun returnFalse() : Boolean = js.noImpl + +fun box() = !returnFalse() \ No newline at end of file diff --git a/js/js.translator/testFiles/native/cases/undefined.kt b/js/js.translator/testFiles/native/cases/undefined.kt new file mode 100644 index 00000000000..d62755570c9 --- /dev/null +++ b/js/js.translator/testFiles/native/cases/undefined.kt @@ -0,0 +1,22 @@ +package foo + +import js.* + +/*function g should return 0 if a parameter is undefined 1 if parameter is 1 and 3 if parameter is 3*/ +native +fun f(g : (Int?)-> Int) : Boolean = js.noImpl + + +fun box() : Boolean { + val b = { + (a : Int?) -> + if (a == null) { + 0 + } else if (a == 1) { + 1 + } else { + 3 + } + } + return f(b) +} \ No newline at end of file diff --git a/js/js.translator/testFiles/native/cases/vararg.kt b/js/js.translator/testFiles/native/cases/vararg.kt new file mode 100644 index 00000000000..b031e3de2b3 --- /dev/null +++ b/js/js.translator/testFiles/native/cases/vararg.kt @@ -0,0 +1,24 @@ +package foo + +import js.* + +native +fun paramCount(vararg a : Int) : Int = js.noImpl + +fun count(vararg a : Int) = a.size + +fun box() : Boolean { + if (paramCount(1, 2 ,3) != 3) { + return false; + } + if (paramCount() != 0) { + return false; + } + if (count() != 0) { + return false; + } + if (count(1, 1, 1, 1) != 4) { + return false; + } + return true; +} \ No newline at end of file diff --git a/js/js.translator/testFiles/native/native/class.js b/js/js.translator/testFiles/native/native/class.js new file mode 100644 index 00000000000..c1eeffd4643 --- /dev/null +++ b/js/js.translator/testFiles/native/native/class.js @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2012 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. + */ + +function A(b) { + this.g = function() { + return 2 * b; + } + this.m = function() { + return b - 1; + } +} \ No newline at end of file diff --git a/js/js.translator/testFiles/native/native/simple.js b/js/js.translator/testFiles/native/native/simple.js new file mode 100644 index 00000000000..fb1ae4c1d65 --- /dev/null +++ b/js/js.translator/testFiles/native/native/simple.js @@ -0,0 +1,19 @@ +/* + * Copyright 2000-2012 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. + */ + +function returnFalse() { + return false; +} \ No newline at end of file diff --git a/js/js.translator/testFiles/native/native/undefined.js b/js/js.translator/testFiles/native/native/undefined.js new file mode 100644 index 00000000000..a4615ccd145 --- /dev/null +++ b/js/js.translator/testFiles/native/native/undefined.js @@ -0,0 +1,12 @@ +function f(g) { + if (g() != 0) { + return false; + } + if (g(1) != 1) { + return false; + } + if (g(2) != 3) { + return false; + } + return true; +} \ No newline at end of file diff --git a/js/js.translator/testFiles/native/native/vararg.js b/js/js.translator/testFiles/native/native/vararg.js new file mode 100644 index 00000000000..6e3f181e893 --- /dev/null +++ b/js/js.translator/testFiles/native/native/vararg.js @@ -0,0 +1,19 @@ +/* + * Copyright 2000-2012 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. + */ + +function paramCount() { + return arguments.length +} \ No newline at end of file