Massive test refactoring.

This commit is contained in:
pTalanov
2012-03-02 15:32:27 +04:00
parent 037eeae08d
commit 90de7f6ced
39 changed files with 326 additions and 398 deletions
@@ -16,15 +16,14 @@
package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel Talanov
*/
public abstract class AbstractExpressionTest extends TranslationTest {
private static final String SUITE = "expression/";
@Override
protected String suiteDirectoryName() {
return SUITE;
public AbstractExpressionTest(@NotNull String main) {
super("expression/" + main);
}
}
@@ -23,11 +23,8 @@ import org.mozilla.javascript.JavaScriptException;
*/
public final class ArrayListTest extends JavaClassesTest {
private static final String MAIN = "arrayList/";
@Override
protected String mainDirectory() {
return MAIN;
public ArrayListTest() {
super("arrayList/");
}
public void testEmptyList() throws Exception {
@@ -21,15 +21,12 @@ package org.jetbrains.k2js.test;
*/
public final class ClassInheritanceTest extends TranslationTest {
final private static String MAIN = "inheritance/";
@Override
protected String mainDirectory() {
return MAIN;
public ClassInheritanceTest() {
super("inheritance/");
}
public void testInitializersOfBasicClassExecute() throws Exception {
testFunctionOutput("initializersOfBasicClassExecute.kt", "foo", "box", 3);
runFunctionOutputTest("initializersOfBasicClassExecute.kt", "foo", "box", 3);
}
public void testMethodOverride() throws Exception {
@@ -19,15 +19,16 @@ package org.jetbrains.k2js.test;
import com.intellij.testFramework.UsefulTestCase;
import junit.framework.Test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.test.utils.SingleFileTest;
@SuppressWarnings("JUnitTestCaseWithNoTests")
public final class ExamplesTest extends UsefulTestCase {
public static Test suite() {
return Suite.suiteForDirectory("examples/", new Suite.SingleFileTester() {
return SingleFileTest.suiteForDirectory("examples/", new SingleFileTest.Tester() {
@Override
public void performTest(@NotNull Suite test, @NotNull String filename) throws Exception {
test.testFunctionOutput(filename, "Anonymous", "box", "OK");
public void performTest(@NotNull TranslationTest test, @NotNull String filename) throws Exception {
test.runFunctionOutputTest(filename, "Anonymous", "box", "OK");
}
});
}
@@ -20,11 +20,9 @@ package org.jetbrains.k2js.test;
* @author Pavel Talanov
*/
public final class ExtensionFunctionTest extends TranslationTest {
final private static String MAIN = "extensionFunction/";
@Override
protected String mainDirectory() {
return MAIN;
public ExtensionFunctionTest() {
super("extensionFunction/");
}
public void testIntExtension() throws Exception {
@@ -20,11 +20,9 @@ package org.jetbrains.k2js.test;
* @author Pavel Talanov
*/
public final class ExtensionPropertyTest extends TranslationTest {
final private static String MAIN = "extensionProperty/";
@Override
protected String mainDirectory() {
return MAIN;
public ExtensionPropertyTest() {
super("extensionProperty/");
}
public void testSimplePropertyWithGetter() throws Exception {
@@ -21,11 +21,8 @@ package org.jetbrains.k2js.test;
*/
public final class ForeachTest extends AbstractExpressionTest {
final private static String MAIN = "for/";
@Override
protected String mainDirectory() {
return MAIN;
public ForeachTest() {
super("for/");
}
public void testForIteratesOverArray() throws Exception {
@@ -21,11 +21,8 @@ package org.jetbrains.k2js.test;
*/
public class FunctionTest extends AbstractExpressionTest {
final private static String MAIN = "function/";
@Override
protected String mainDirectory() {
return MAIN;
public FunctionTest() {
super("function/");
}
public void testFunctionUsedBeforeDeclaration() throws Exception {
@@ -61,7 +58,7 @@ public class FunctionTest extends AbstractExpressionTest {
}
public void testEnclosingThis() throws Exception {
testFunctionOutput("enclosingThis.kt", "Anonymous", "box", "OK");
runFunctionOutputTest("enclosingThis.kt", "Anonymous", "box", "OK");
}
@@ -16,16 +16,14 @@
package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel Talanov
*/
@SuppressWarnings("FieldCanBeLocal")
public abstract class JavaClassesTest extends TranslationTest {
private static final String SUITE = "java/";
@Override
protected String suiteDirectoryName() {
return SUITE;
public JavaClassesTest(@NotNull String main) {
super("java/" + main);
}
}
@@ -17,21 +17,22 @@
package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
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;
/**
* @author Pavel Talanov
*/
public final class KotlinLibTest extends TranslationTest {
final private static String MAIN = "kotlinLib/";
@Override
protected String mainDirectory() {
return MAIN;
public KotlinLibTest() {
super("kotlinLib/");
}
public void testKotlinJsLibRunsWithRhino() throws Exception {
@@ -25,15 +25,14 @@ import org.mozilla.javascript.JavaScriptException;
* most probably because that functionality has very little support
*/
public final class MiscTest extends AbstractExpressionTest {
final private static String MAIN = "misc/";
@Override
protected String mainDirectory() {
return MAIN;
public MiscTest() {
super("misc/");
}
public void testLocalPropertys() throws Exception {
testFunctionOutput("localProperty.jet", "foo", "box", 50);
runFunctionOutputTest("localProperty.jet", "foo", "box", 50);
}
public void testIntRange() throws Exception {
@@ -46,7 +45,7 @@ public final class MiscTest extends AbstractExpressionTest {
}
public void testClassWithoutNamespace() throws Exception {
testFunctionOutput("classWithoutNamespace.kt", "Anonymous", "box", true);
runFunctionOutputTest("classWithoutNamespace.kt", "Anonymous", "box", true);
}
public void testIfElseAsExpressionWithThrow() throws Exception {
@@ -16,16 +16,16 @@
package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel Talanov
*/
public final class MultiFileTest extends TranslationTest {
final private static String MAIN = "multiFile/";
@Override
protected String mainDirectory() {
return MAIN;
public MultiFileTest() {
super("multiFile/");
}
public void testFunctionsVisibleFromOtherFile() throws Exception {
@@ -37,7 +37,7 @@ public final class MultiFileTest extends TranslationTest {
}
@Override
public void checkFooBoxIsTrue(String dirName) throws Exception {
public void checkFooBoxIsTrue(@NotNull String dirName) throws Exception {
testMultiFile(dirName, "foo", "box", true);
}
}
@@ -16,15 +16,16 @@
package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel Talanov
*/
public class MultiNamespaceTest extends TranslationTest {
final private static String MAIN = "multiNamespace/";
@Override
protected String mainDirectory() {
return MAIN;
public MultiNamespaceTest() {
super("multiNamespace/");
}
public void testFunctionsVisibleFromOtherNamespace() throws Exception {
@@ -36,7 +37,7 @@ public class MultiNamespaceTest extends TranslationTest {
}
@Override
public void checkFooBoxIsTrue(String dirName) throws Exception {
public void checkFooBoxIsTrue(@NotNull String dirName) throws Exception {
testMultiFile(dirName, "foo", "box", true);
}
}
@@ -21,11 +21,9 @@ package org.jetbrains.k2js.test;
*/
public final class NameClashesTest extends TranslationTest {
private static final String MAIN = "nameClashes/";
@Override
protected String mainDirectory() {
return MAIN;
public NameClashesTest() {
super("nameClashes/");
}
public void testMethodOverload() throws Exception {
@@ -21,11 +21,8 @@ package org.jetbrains.k2js.test;
*/
public final class ObjectTest extends TranslationTest {
private static final String MAIN = "object/";
@Override
protected String mainDirectory() {
return MAIN;
public ObjectTest() {
super("object/");
}
public void testObjectWithMethods() throws Exception {
@@ -21,14 +21,10 @@ package org.jetbrains.k2js.test;
*/
public final class OperatorOverloadingTest extends TranslationTest {
final private static String MAIN = "operatorOverloading/";
@Override
protected String mainDirectory() {
return MAIN;
public OperatorOverloadingTest() {
super("operatorOverloading/");
}
public void testPlusOverload() throws Exception {
checkFooBoxIsTrue("plusOverload.kt");
}
@@ -23,11 +23,8 @@ import org.mozilla.javascript.JavaScriptException;
*/
public final class PatternMatchingTest extends TranslationTest {
final private static String MAIN = "patternMatching/";
@Override
protected String mainDirectory() {
return MAIN;
public PatternMatchingTest() {
super("patternMatching/");
}
public void testWhenType() throws Exception {
@@ -59,7 +56,7 @@ public final class PatternMatchingTest extends TranslationTest {
}
public void testMultipleCases() throws Exception {
testFunctionOutput("multipleCases.kt", "foo", "box", 2.0);
runFunctionOutputTest("multipleCases.kt", "foo", "box", 2.0);
}
public void testMatchNullableType() throws Exception {
@@ -21,14 +21,10 @@ package org.jetbrains.k2js.test;
*/
public final class PropertyAccessTest extends TranslationTest {
final private static String MAIN = "propertyAccess/";
@Override
protected String mainDirectory() {
return MAIN;
public PropertyAccessTest() {
super("propertyAccess/");
}
public void testAccessToInstanceProperty() throws Exception {
checkFooBoxIsTrue("accessToInstanceProperty.kt");
}
@@ -40,7 +36,7 @@ public final class PropertyAccessTest extends TranslationTest {
public void testSetter() throws Exception {
testFunctionOutput("setter.kt", "foo", "f", 99.0);
runFunctionOutputTest("setter.kt", "foo", "f", 99.0);
}
@@ -21,11 +21,8 @@ package org.jetbrains.k2js.test;
*/
public class RTTITest extends TranslationTest {
final private static String MAIN = "rtti/";
@Override
protected String mainDirectory() {
return MAIN;
public RTTITest() {
super("rtti/");
}
public void testIsSameClass() throws Exception {
@@ -21,14 +21,10 @@ package org.jetbrains.k2js.test;
*/
public final class RangeTest extends TranslationTest {
final private static String MAIN = "range/";
@Override
protected String mainDirectory() {
return MAIN;
public RangeTest() {
super("range/");
}
public void testExplicitRange() throws Exception {
checkFooBoxIsTrue("explicitRange.kt");
}
@@ -1,61 +0,0 @@
/*
* 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;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import java.util.Map;
import static org.junit.Assert.assertTrue;
/**
* @author Pavel Talanov
*/
public final class RhinoPropertyTypesChecker implements RhinoResultChecker {
final private String objectName;
final private Map<String, Class<? extends Scriptable>> propertyToType;
public RhinoPropertyTypesChecker(String objectName, Map<String, Class<? extends Scriptable>> propertyToType) {
this.objectName = objectName;
this.propertyToType = propertyToType;
}
@Override
public void runChecks(Context context, Scriptable scope) throws Exception {
NativeObject object = RhinoUtils.extractObject(objectName, scope);
verifyObjectHasExpectedPropertiesOfExpectedTypes(object, propertyToType);
}
private static void verifyObjectHasExpectedPropertiesOfExpectedTypes
(NativeObject object, Map<String, Class<? extends Scriptable>> nameToClassMap) {
for (Map.Entry<String, Class<? extends Scriptable>> entry : nameToClassMap.entrySet()) {
String name = entry.getKey();
Class expectedClass = entry.getValue();
Object property = ScriptableObject.getProperty(object, name);
assertTrue(object + " must contain key " + name, property != null);
assertTrue(object + "'s property " + name + " must be of type " + expectedClass,
expectedClass.isInstance(property
));
}
}
}
@@ -1,38 +0,0 @@
/*
* 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;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import static org.junit.Assert.assertTrue;
/**
* @author Pavel Talanov
*/
public final class RhinoUtils {
private RhinoUtils() {
}
public static NativeObject extractObject(String objectName, Scriptable scope) {
Object nativeObject = scope.get(objectName, scope);
assertTrue(objectName + " should be JSON Object", nativeObject instanceof NativeObject);
return (NativeObject) nativeObject;
}
}
@@ -21,11 +21,8 @@ package org.jetbrains.k2js.test;
*/
public final class SafeCallTest extends TranslationTest {
final private static String MAIN = "safeCall/";
@Override
protected String mainDirectory() {
return MAIN;
public SafeCallTest() {
super("safeCall/");
}
public void testSafeAccess() throws Exception {
@@ -19,16 +19,19 @@ package org.jetbrains.k2js.test;
import com.intellij.testFramework.UsefulTestCase;
import junit.framework.Test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.test.utils.SingleFileTest;
/**
* @author Pavel Talanov
*/
@SuppressWarnings("JUnitTestCaseWithNoTests")
public final class SimpleTestSuite extends UsefulTestCase {
public static Test suite() {
return Suite.suiteForDirectory("simple/", new Suite.SingleFileTester() {
return SingleFileTest.suiteForDirectory("simple/", new SingleFileTest.Tester() {
@Override
public void performTest(@NotNull Suite test, @NotNull String filename) throws Exception {
public void performTest(@NotNull TranslationTest test, @NotNull String filename) throws Exception {
test.checkFooBoxIsTrue(filename);
}
});
@@ -20,14 +20,11 @@ package org.jetbrains.k2js.test;
* @author Pavel Talanov
*/
public class StandardClassesTest extends TranslationTest {
final private static String MAIN = "standardClasses/";
@Override
protected String mainDirectory() {
return MAIN;
public StandardClassesTest() {
super("standardClasses/");
}
public void testArray() throws Exception {
checkFooBoxIsTrue("array.kt");
}
@@ -19,13 +19,10 @@ package org.jetbrains.k2js.test;
/**
* @author Pavel Talanov
*/
public class StringTest extends AbstractExpressionTest {
public final class StringTest extends AbstractExpressionTest {
final private static String MAIN = "string/";
@Override
protected String mainDirectory() {
return MAIN;
public StringTest() {
super("string/");
}
public void testStringConstant() throws Exception {
@@ -37,15 +34,15 @@ public class StringTest extends AbstractExpressionTest {
}
public void testIntInTemplate() throws Exception {
testFunctionOutput("intInTemplate.kt", "foo", "box", "my age is 3");
runFunctionOutputTest("intInTemplate.kt", "foo", "box", "my age is 3");
}
public void testStringInTemplate() throws Exception {
testFunctionOutput("stringInTemplate.kt", "foo", "box", "oHelloo");
runFunctionOutputTest("stringInTemplate.kt", "foo", "box", "oHelloo");
}
public void testMultipleExpressionInTemplate() throws Exception {
testFunctionOutput("multipleExpressionsInTemplate.kt", "foo", "box", "left = 3\nright = 2\nsum = 5\n");
runFunctionOutputTest("multipleExpressionsInTemplate.kt", "foo", "box", "left = 3\nright = 2\nsum = 5\n");
}
public void testToStringMethod() throws Exception {
@@ -21,14 +21,10 @@ package org.jetbrains.k2js.test;
*/
public final class TraitTest extends TranslationTest {
final private static String MAIN = "trait/";
@Override
protected String mainDirectory() {
return MAIN;
public TraitTest() {
super("trait/");
}
public void testTraitAddsFunctionsToClass() throws Exception {
checkFooBoxIsTrue("traitAddsFunctionsToClass.kt");
}
@@ -16,31 +16,28 @@
package org.jetbrains.k2js.test;
import com.google.dart.compiler.backend.js.ast.JsProgram;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.TestConfig;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker;
import org.jetbrains.k2js.test.utils.TranslationUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList;
import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest;
import static org.jetbrains.k2js.test.utils.JsTestUtils.convertToDotJsFile;
import static org.jetbrains.k2js.test.utils.JsTestUtils.readFile;
import static org.jetbrains.k2js.test.utils.TranslationUtils.translateFiles;
//TODO: spread the test* methods amongst classes that actually use them
/**
* @author Pavel Talanov
*/
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
public abstract class TranslationTest extends BaseTest {
private static final boolean DELETE_OUT = false;
@@ -50,17 +47,16 @@ public abstract class TranslationTest extends BaseTest {
private static final String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js";
private static final String EXPECTED = "expected/";
public void translateFile(@NotNull String inputFile,
@NotNull String outputFile) throws Exception {
translateFiles(Collections.singletonList(inputFile), outputFile);
@NotNull
private String mainDirectory = "";
public TranslationTest(@NotNull String main) {
this.mainDirectory = main;
}
public void translateFiles(@NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception {
K2JSTranslator translator = new K2JSTranslator(new TestConfig(getProject()));
List<JetFile> psiFiles = createPsiFileList(inputFiles, getProject());
JsProgram program = translator.generateProgram(psiFiles);
K2JSTranslator.saveProgramToFile(outputFile, program);
@NotNull
public String getMainDirectory() {
return mainDirectory;
}
@Override
@@ -80,6 +76,7 @@ public abstract class TranslationTest extends BaseTest {
@Override
protected void tearDown() throws Exception {
super.tearDown();
//noinspection ConstantConditions,PointlessBooleanExpression
if (!shouldCreateOut() || !DELETE_OUT) {
return;
}
@@ -101,21 +98,20 @@ public abstract class TranslationTest extends BaseTest {
return OUT;
}
protected abstract String mainDirectory();
private String pathToTestFiles() {
return TEST_FILES + suiteDirectoryName() + mainDirectory();
private static String expectedDirectoryName() {
return EXPECTED;
}
protected String suiteDirectoryName() {
return "";
@NotNull
private String pathToTestFiles() {
return TEST_FILES + getMainDirectory();
}
private String getOutputPath() {
return pathToTestFiles() + outDirectoryName();
}
protected String getInputPath() {
private String getInputPath() {
return pathToTestFiles() + casesDirectoryName();
}
@@ -123,21 +119,16 @@ public abstract class TranslationTest extends BaseTest {
return pathToTestFiles() + expectedDirectoryName();
}
@SuppressWarnings("MethodMayBeStatic")
private String expectedDirectoryName() {
return EXPECTED;
}
protected void testFunctionOutput(String filename, String namespaceName,
String functionName, Object expectedResult) throws Exception {
translateFile(filename);
protected void runFunctionOutputTest(String filename, String namespaceName,
String functionName, Object expectedResult) throws Exception {
generateJsFromFile(filename);
runRhinoTest(generateFilenameList(getOutputFilePath(filename)),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
}
protected void testMultiFile(String dirName, String namespaceName,
String functionName, Object expectedResult) throws Exception {
translateFilesInDir(dirName);
generateJsFromDir(dirName);
runRhinoTest(generateFilenameList(getOutputFilePath(dirName + ".kt")),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
}
@@ -146,12 +137,11 @@ public abstract class TranslationTest extends BaseTest {
return true;
}
protected void translateFile(String filename) throws Exception {
translateFile(getInputFilePath(filename),
getOutputFilePath(filename));
protected void generateJsFromFile(@NotNull String filename) throws Exception {
TranslationUtils.translateFile(getProject(), getInputFilePath(filename), getOutputFilePath(filename));
}
protected void translateFilesInDir(String dirName) throws Exception {
protected void generateJsFromDir(@NotNull String dirName) throws Exception {
String dirPath = getInputFilePath(dirName);
File dir = new File(dirPath);
List<String> fullFilePaths = new ArrayList<String>();
@@ -159,65 +149,39 @@ public abstract class TranslationTest extends BaseTest {
fullFilePaths.add(getInputFilePath(dirName) + "/" + fileName);
}
assert dir.isDirectory();
translateFiles(fullFilePaths,
getOutputFilePath(dirName + ".kt"));
translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt"));
}
protected List<String> generateFilenameList(String inputFile) {
protected static List<String> generateFilenameList(@NotNull String inputFile) {
return Arrays.asList(kotlinLibraryPath(), inputFile);
}
//TODO: refactor filename generation logic
protected String getOutputFilePath(String filename) {
private String getOutputFilePath(@NotNull String filename) {
return getOutputPath() + convertToDotJsFile(filename);
}
private String convertToDotJsFile(String filename) {
return filename.substring(0, filename.lastIndexOf('.')) + ".js";
}
private String getInputFilePath(String filename) {
private String getInputFilePath(@NotNull String filename) {
return getInputPath() + filename;
}
protected String cases(String filename) {
protected String cases(@NotNull String filename) {
return getInputFilePath(filename);
}
private String expected(String testName) {
private String expected(@NotNull String testName) {
return getExpectedPath() + testName + ".out";
}
protected static void runFileWithRhino(String inputFile, Context context, Scriptable scope) throws Exception {
FileReader reader = new FileReader(inputFile);
try {
context.evaluateReader(scope, reader, inputFile, 1, null);
} finally {
reader.close();
}
public void checkFooBoxIsTrue(@NotNull String filename) throws Exception {
runFunctionOutputTest(filename, "foo", "box", true);
}
protected static void runRhinoTest(@NotNull List<String> fileNames,
@NotNull RhinoResultChecker checker) throws Exception {
Context context = Context.enter();
Scriptable scope = context.initStandardObjects();
for (String filename : fileNames) {
runFileWithRhino(filename, context, scope);
}
checker.runChecks(context, scope);
Context.exit();
}
public void checkFooBoxIsTrue(String filename) throws Exception {
testFunctionOutput(filename, "foo", "box", true);
}
public void checkFooBoxIsOk(String filename) throws Exception {
testFunctionOutput(filename, "foo", "box", "OK");
public void checkFooBoxIsOk(@NotNull String filename) throws Exception {
runFunctionOutputTest(filename, "foo", "box", "OK");
}
protected void checkOutput(String filename, String expectedResult, String... args) throws Exception {
translateFile(filename);
generateJsFromFile(filename);
runRhinoTest(generateFilenameList(getOutputFilePath(filename)),
new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args)));
}
@@ -225,13 +189,4 @@ public abstract class TranslationTest extends BaseTest {
protected void performTestWithMain(String testName, String testId, String... args) throws Exception {
checkOutput(testName + ".kt", readFile(expected(testName + testId)), args);
}
private static String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
return FileUtil.loadTextAndClose(stream);
} finally {
stream.close();
}
}
}
@@ -44,21 +44,21 @@ public abstract class TranslatorTestCaseBuilder {
}
@NotNull
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath,
public static TestSuite suiteForDirectory(@NotNull final String dataPath,
boolean recursive, @NotNull NamedTestFactory factory) {
return suiteForDirectory(baseDataDir, dataPath, recursive, emptyFilter, factory);
return suiteForDirectory(dataPath, recursive, emptyFilter, factory);
}
@NotNull
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive,
public static TestSuite suiteForDirectory(@NotNull final String dataPath, boolean recursive,
final FilenameFilter filter, @NotNull NamedTestFactory factory) {
TestSuite suite = new TestSuite(dataPath);
suite.setName(dataPath);
appendTestsInDirectory(baseDataDir, dataPath, recursive, filter, factory, suite);
appendTestsInDirectory(dataPath, recursive, filter, factory, suite);
return suite;
}
public static void appendTestsInDirectory(String baseDataDir, String dataPath, boolean recursive,
public static void appendTestsInDirectory(String dataPath, boolean recursive,
final FilenameFilter filter, NamedTestFactory factory, TestSuite suite) {
final String extensionJet = ".jet";
final String extensionKt = ".kt";
@@ -80,7 +80,7 @@ public abstract class TranslatorTestCaseBuilder {
else {
resultFilter = extensionFilter;
}
File dir = new File(baseDataDir + dataPath);
File dir = new File(dataPath);
FileFilter dirFilter = new FileFilter() {
@Override
public boolean accept(File pathname) {
@@ -93,7 +93,7 @@ public abstract class TranslatorTestCaseBuilder {
List<File> subdirs = Arrays.asList(files);
Collections.sort(subdirs);
for (File subdir : subdirs) {
suite.addTest(suiteForDirectory(baseDataDir, dataPath + "/" + subdir.getName(), recursive, filter, factory));
suite.addTest(suiteForDirectory(dataPath + "/" + subdir.getName(), recursive, filter, factory));
}
}
List<File> files = Arrays.asList(dir.listFiles(resultFilter));
@@ -21,11 +21,9 @@ package org.jetbrains.k2js.test;
*/
public final class TupleTest extends TranslationTest {
final private static String MAIN = "tuple/";
@Override
protected String mainDirectory() {
return MAIN;
public TupleTest() {
super("tuple/");
}
public void testTwoElements() throws Exception {
@@ -21,14 +21,10 @@ package org.jetbrains.k2js.test;
*/
public final class WebDemoExamples1Test extends TranslationTest {
final private static String MAIN = "webDemoExamples1/";
@Override
protected String mainDirectory() {
return MAIN;
public WebDemoExamples1Test() {
super("webDemoExamples1/");
}
public void testPrintArg() throws Exception {
checkOutput("printArg.kt", "Hello, world!", "Hello, world!");
}
@@ -62,26 +58,26 @@ public final class WebDemoExamples1Test extends TranslationTest {
public void testRanges() throws Exception {
checkOutput("ranges.kt", "OK\n" +
" 1 2 3 4 5\n" +
"Out: array has only 3 elements. x = 4\n" +
"Yes: array contains aaa\n" +
"No: array doesn't contains ddd\n", "4");
" 1 2 3 4 5\n" +
"Out: array has only 3 elements. x = 4\n" +
"Yes: array contains aaa\n" +
"No: array doesn't contains ddd\n", "4");
checkOutput("ranges.kt", " 1 2 3 4 5\n" +
"Out: array has only 3 elements. x = 10\n" +
"Yes: array contains aaa\n" +
"No: array doesn't contains ddd\n", "10");
"Out: array has only 3 elements. x = 10\n" +
"Yes: array contains aaa\n" +
"No: array doesn't contains ddd\n", "10");
}
public void testForLoop() throws Exception {
checkOutput("forLoop.kt", "a\n" +
"b\n" +
"c\n" +
"\n" +
"a\n" +
"b\n" +
"c\n", "a", "b", "c");
"b\n" +
"c\n" +
"\n" +
"a\n" +
"b\n" +
"c\n", "a", "b", "c");
checkOutput("forLoop.kt", "123\n\n123\n", "123");
}
@@ -93,8 +89,8 @@ public final class WebDemoExamples1Test extends TranslationTest {
public void testPatternMatching() throws Exception {
checkOutput("patternMatching.kt", "Greeting\n" +
"One\n" +
"Not a string\n" +
"Unknown\n");
"One\n" +
"Not a string\n" +
"Unknown\n");
}
}
@@ -21,11 +21,8 @@ package org.jetbrains.k2js.test;
*/
public final class WebDemoExamples2Test extends TranslationTest {
final private static String MAIN = "webDemoExamples2/";
@Override
protected String mainDirectory() {
return MAIN;
public WebDemoExamples2Test() {
super("webDemoExamples2/");
}
public void testBottles() throws Exception {
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.k2js.test;
package org.jetbrains.k2js.test.rhino;
import org.jetbrains.annotations.Nullable;
import org.mozilla.javascript.Context;
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.k2js.test;
package org.jetbrains.k2js.test.rhino;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.k2js.test;
package org.jetbrains.k2js.test.rhino;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.utils.GenerationUtils;
@@ -0,0 +1,56 @@
/*
* 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.rhino;
import org.jetbrains.annotations.NotNull;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import java.io.FileReader;
import java.util.List;
/**
* @author Pavel Talanov
*/
public final class RhinoUtils {
private RhinoUtils() {
}
private static void runFileWithRhino(@NotNull String inputFile,
@NotNull Context context,
@NotNull Scriptable scope) throws Exception {
FileReader reader = new FileReader(inputFile);
try {
context.evaluateReader(scope, reader, inputFile, 1, null);
} finally {
reader.close();
}
}
public static void runRhinoTest(@NotNull List<String> fileNames,
@NotNull RhinoResultChecker checker) throws Exception {
Context context = Context.enter();
Scriptable scope = context.initStandardObjects();
for (String filename : fileNames) {
runFileWithRhino(filename, context, scope);
}
checker.runChecks(context, scope);
Context.exit();
}
}
@@ -0,0 +1,47 @@
/*
* 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.utils;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author Pavel Talanov
*/
public final class JsTestUtils {
private JsTestUtils() {
}
public static String convertToDotJsFile(@NotNull String filename) {
return filename.substring(0, filename.lastIndexOf('.')) + ".js";
}
@NotNull
public static String readFile(@NotNull String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
return FileUtil.loadTextAndClose(stream);
} finally {
stream.close();
}
}
}
@@ -14,72 +14,49 @@
* limitations under the License.
*/
package org.jetbrains.k2js.test;
package org.jetbrains.k2js.test.utils;
import junit.framework.Test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.test.TranslationTest;
import org.jetbrains.k2js.test.TranslatorTestCaseBuilder;
/**
* @author Pavel Talanov
*/
//TODO: this class has strange behaviour. Should be refactored.
public final class Suite extends TranslationTest {
public abstract class SingleFileTest extends TranslationTest {
private String name;
private final SingleFileTester tester;
private final String testMain;
@NotNull
private final Tester tester;
@NotNull
private final String name;
public Suite(@NotNull String testName,
@NotNull String suiteDirName,
@NotNull final SingleFileTester tester) {
this.name = testName;
this.tester = tester;
this.testMain = suiteDirName;
protected SingleFileTest(@NotNull String pathToMain, @NotNull String name, @NotNull Tester tester) {
super(pathToMain);
setName(name);
this.tester = tester;
this.name = name;
}
public Suite() {
this("dummy", "dummy", new SingleFileTester() {
@Override
public void performTest(@NotNull Suite test, @NotNull String filename) throws Exception {
//do nothing
}
});
}
//NOTE: just to avoid warning
public void testNothing() {
}
@Override
protected boolean shouldCreateOut() {
return false;
}
@Override
protected String mainDirectory() {
return testMain;
public interface Tester {
void performTest(@NotNull TranslationTest test, @NotNull String filename) throws Exception;
}
public void runTest() throws Exception {
tester.performTest(this, name);
}
public static Test suiteForDirectory(@NotNull final String mainName, @NotNull final SingleFileTester testMethod) {
public static Test suiteForDirectory(@NotNull final String main, @NotNull final SingleFileTest.Tester testMethod) {
return TranslatorTestCaseBuilder.suiteForDirectory(TranslationTest.TEST_FILES,
mainName + casesDirectoryName(),
return TranslatorTestCaseBuilder.suiteForDirectory(TEST_FILES + main + casesDirectoryName(),
true,
new TranslatorTestCaseBuilder.NamedTestFactory() {
@NotNull
@Override
public Test createTest(@NotNull String name) {
return (new Suite(name, mainName, testMethod));
return (new SingleFileTest(main, name, testMethod) {
});
}
});
}
protected static interface SingleFileTester {
void performTest(@NotNull Suite test, @NotNull String filename) throws Exception;
}
}
@@ -0,0 +1,51 @@
/*
* 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.utils;
import com.google.dart.compiler.backend.js.ast.JsProgram;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.TestConfig;
import org.jetbrains.k2js.facade.K2JSTranslator;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList;
/**
* @author Pavel Talanov
*/
public final class TranslationUtils {
private TranslationUtils() {
}
public static void translateFile(@NotNull Project project, @NotNull String inputFile,
@NotNull String outputFile) throws Exception {
translateFiles(project, Collections.singletonList(inputFile), outputFile);
}
public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception {
K2JSTranslator translator = new K2JSTranslator(new TestConfig(project));
List<JetFile> psiFiles = createPsiFileList(inputFiles, project);
JsProgram program = translator.generateProgram(psiFiles);
K2JSTranslator.saveProgramToFile(outputFile, program);
}
}