Add simple tests for interop with native code.

This commit is contained in:
pTalanov
2012-03-02 20:01:18 +04:00
parent 320ed18134
commit ab3cdb9bd8
14 changed files with 252 additions and 22 deletions
@@ -16,11 +16,12 @@
package org.jetbrains.k2js.test; package org.jetbrains.k2js.test;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Collections;
import java.util.List; import java.util.List;
import static org.jetbrains.k2js.test.utils.JsTestUtils.convertToDotJsFile; import static org.jetbrains.k2js.test.utils.JsTestUtils.convertToDotJsFile;
@@ -81,8 +82,8 @@ public abstract class BasicTest extends TestWithEnvironment {
assert success; assert success;
} }
protected static String kotlinLibraryPath() { protected List<String> additionalJSFiles() {
return KOTLIN_JS_LIB; return Collections.singletonList(KOTLIN_JS_LIB);
} }
protected static String casesDirectoryName() { protected static String casesDirectoryName() {
@@ -98,7 +99,7 @@ public abstract class BasicTest extends TestWithEnvironment {
} }
@NotNull @NotNull
private String pathToTestFiles() { protected String pathToTestFiles() {
return pathToTestFilesRoot() + getMainDirectory(); return pathToTestFilesRoot() + getMainDirectory();
} }
@@ -107,34 +108,44 @@ public abstract class BasicTest extends TestWithEnvironment {
return TEST_FILES; return TEST_FILES;
} }
@NotNull
private String getOutputPath() { private String getOutputPath() {
return pathToTestFiles() + outDirectoryName(); return pathToTestFiles() + outDirectoryName();
} }
@NotNull
private String getInputPath() { private String getInputPath() {
return pathToTestFiles() + casesDirectoryName(); return pathToTestFiles() + casesDirectoryName();
} }
@NotNull
private String getExpectedPath() { private String getExpectedPath() {
return pathToTestFiles() + expectedDirectoryName(); return pathToTestFiles() + expectedDirectoryName();
} }
protected static List<String> withKotlinJsLib(@NotNull String inputFile) { @NotNull
return Arrays.asList(kotlinLibraryPath(), inputFile); protected List<String> withAdditionalFiles(@NotNull String inputFile) {
List<String> allFiles = Lists.newArrayList(additionalJSFiles());
allFiles.add(inputFile);
return allFiles;
} }
@NotNull
protected String getOutputFilePath(@NotNull String filename) { protected String getOutputFilePath(@NotNull String filename) {
return getOutputPath() + convertToDotJsFile(filename); return getOutputPath() + convertToDotJsFile(filename);
} }
@NotNull
protected String getInputFilePath(@NotNull String filename) { protected String getInputFilePath(@NotNull String filename) {
return getInputPath() + filename; return getInputPath() + filename;
} }
@NotNull
protected String cases(@NotNull String filename) { protected String cases(@NotNull String filename) {
return getInputFilePath(filename); return getInputFilePath(filename);
} }
@NotNull
protected String expected(@NotNull String testName) { protected String expected(@NotNull String testName) {
return getExpectedPath() + testName + ".out"; return getExpectedPath() + testName + ".out";
} }
@@ -19,11 +19,10 @@ package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker; import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; 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; 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 { protected void generateJsFromDir(@NotNull String dirName) throws Exception {
String dirPath = getInputFilePath(dirName); List<String> fullFilePaths = getAllFilesInDir(getInputFilePath(dirName));
File dir = new File(dirPath);
List<String> fullFilePaths = new ArrayList<String>();
for (String fileName : dir.list()) {
fullFilePaths.add(getInputFilePath(dirName) + "/" + fileName);
}
assert dir.isDirectory();
translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt")); translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt"));
} }
protected void runMultiFileTest(@NotNull String dirName, @NotNull String namespaceName, protected void runMultiFileTest(@NotNull String dirName, @NotNull String namespaceName,
@NotNull String functionName, @NotNull Object expectedResult) throws Exception { @NotNull String functionName, @NotNull Object expectedResult) throws Exception {
generateJsFromDir(dirName); generateJsFromDir(dirName);
runRhinoTest(withKotlinJsLib(getOutputFilePath(dirName + ".kt")), runRhinoTest(withAdditionalFiles(getOutputFilePath(dirName + ".kt")),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult)); new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
} }
@@ -39,7 +39,7 @@ public abstract class SingleFileTranslationTest extends BasicTest {
public void runFunctionOutputTest(String filename, String namespaceName, public void runFunctionOutputTest(String filename, String namespaceName,
String functionName, Object expectedResult) throws Exception { String functionName, Object expectedResult) throws Exception {
generateJsFromFile(filename); generateJsFromFile(filename);
runRhinoTest(withKotlinJsLib(getOutputFilePath(filename)), runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult)); 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 { protected void checkOutput(@NotNull String filename, @NotNull String expectedResult, @NotNull String... args) throws Exception {
generateJsFromFile(filename); generateJsFromFile(filename);
runRhinoTest(withKotlinJsLib(getOutputFilePath(filename)), runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)),
new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args))); new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args)));
} }
@@ -23,8 +23,6 @@ import org.jetbrains.k2js.test.rhino.RhinoResultChecker;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.Scriptable;
import java.util.Arrays;
import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest;
/** /**
@@ -37,7 +35,7 @@ public final class KotlinLibTest extends SingleFileTranslationTest {
} }
public void testKotlinJsLibRunsWithRhino() throws Exception { public void testKotlinJsLibRunsWithRhino() throws Exception {
runRhinoTest(Arrays.asList(kotlinLibraryPath()), new RhinoResultChecker() { runRhinoTest(additionalJSFiles(), new RhinoResultChecker() {
@Override @Override
public void runChecks(Context context, Scriptable scope) throws Exception { public void runChecks(Context context, Scriptable scope) throws Exception {
//do nothing //do nothing
@@ -85,7 +83,7 @@ public final class KotlinLibTest extends SingleFileTranslationTest {
private void runJavascriptTest(@NotNull String filename) throws Exception { private void runJavascriptTest(@NotNull String filename) throws Exception {
runRhinoTest(Arrays.asList(kotlinLibraryPath(), cases(filename)), runRhinoTest(withAdditionalFiles(cases(filename)),
new RhinoFunctionResultChecker("test", true)); new RhinoFunctionResultChecker("test", true));
} }
@@ -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<String> additionalJSFiles() {
List<String> 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");
}
}
@@ -22,6 +22,8 @@ import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
@@ -44,4 +46,15 @@ public final class JsTestUtils {
stream.close(); stream.close();
} }
} }
@NotNull
public static List<String> getAllFilesInDir(@NotNull String dirName) {
File dir = new File(dirName);
assert dir.isDirectory();
List<String> fullFilePaths = new ArrayList<String>();
for (String fileName : dir.list()) {
fullFilePaths.add(dirName + "/" + fileName);
}
return fullFilePaths;
}
} }
@@ -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;
}
@@ -0,0 +1,8 @@
package foo
import js.*
native
fun returnFalse() : Boolean = js.noImpl
fun box() = !returnFalse()
@@ -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)
}
@@ -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;
}
@@ -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;
}
}
@@ -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;
}
@@ -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;
}
@@ -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
}