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