From 90de7f6ced4d2558a641f4fda57556b6d28cd870 Mon Sep 17 00:00:00 2001 From: pTalanov Date: Fri, 2 Mar 2012 15:32:27 +0400 Subject: [PATCH] Massive test refactoring. --- .../k2js/test/AbstractExpressionTest.java | 9 +- .../jetbrains/k2js/test/ArrayListTest.java | 7 +- .../k2js/test/ClassInheritanceTest.java | 9 +- .../org/jetbrains/k2js/test/ExamplesTest.java | 7 +- .../k2js/test/ExtensionFunctionTest.java | 6 +- .../k2js/test/ExtensionPropertyTest.java | 6 +- .../org/jetbrains/k2js/test/ForeachTest.java | 7 +- .../org/jetbrains/k2js/test/FunctionTest.java | 9 +- .../jetbrains/k2js/test/JavaClassesTest.java | 10 +- .../jetbrains/k2js/test/KotlinLibTest.java | 11 +- .../org/jetbrains/k2js/test/MiscTest.java | 11 +- .../jetbrains/k2js/test/MultiFileTest.java | 10 +- .../k2js/test/MultiNamespaceTest.java | 11 +- .../jetbrains/k2js/test/NameClashesTest.java | 6 +- .../org/jetbrains/k2js/test/ObjectTest.java | 7 +- .../k2js/test/OperatorOverloadingTest.java | 8 +- .../k2js/test/PatternMatchingTest.java | 9 +- .../k2js/test/PropertyAccessTest.java | 10 +- .../org/jetbrains/k2js/test/RTTITest.java | 7 +- .../org/jetbrains/k2js/test/RangeTest.java | 8 +- .../k2js/test/RhinoPropertyTypesChecker.java | 61 --------- .../org/jetbrains/k2js/test/RhinoUtils.java | 38 ------ .../org/jetbrains/k2js/test/SafeCallTest.java | 7 +- .../jetbrains/k2js/test/SimpleTestSuite.java | 7 +- .../k2js/test/StandardClassesTest.java | 7 +- .../org/jetbrains/k2js/test/StringTest.java | 15 +-- .../org/jetbrains/k2js/test/TraitTest.java | 8 +- .../jetbrains/k2js/test/TranslationTest.java | 127 ++++++------------ .../k2js/test/TranslatorTestCaseBuilder.java | 14 +- .../org/jetbrains/k2js/test/TupleTest.java | 6 +- .../k2js/test/WebDemoExamples1Test.java | 40 +++--- .../k2js/test/WebDemoExamples2Test.java | 7 +- .../RhinoFunctionResultChecker.java | 2 +- .../test/{ => rhino}/RhinoResultChecker.java | 2 +- .../{ => rhino}/RhinoSystemOutputChecker.java | 2 +- .../jetbrains/k2js/test/rhino/RhinoUtils.java | 56 ++++++++ .../k2js/test/utils/JsTestUtils.java | 47 +++++++ .../{Suite.java => utils/SingleFileTest.java} | 59 +++----- .../k2js/test/utils/TranslationUtils.java | 51 +++++++ 39 files changed, 326 insertions(+), 398 deletions(-) delete mode 100644 js/js.tests/test/org/jetbrains/k2js/test/RhinoPropertyTypesChecker.java delete mode 100644 js/js.tests/test/org/jetbrains/k2js/test/RhinoUtils.java rename js/js.tests/test/org/jetbrains/k2js/test/{ => rhino}/RhinoFunctionResultChecker.java (98%) rename js/js.tests/test/org/jetbrains/k2js/test/{ => rhino}/RhinoResultChecker.java (95%) rename js/js.tests/test/org/jetbrains/k2js/test/{ => rhino}/RhinoSystemOutputChecker.java (98%) create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java rename js/js.tests/test/org/jetbrains/k2js/test/{Suite.java => utils/SingleFileTest.java} (52%) create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java diff --git a/js/js.tests/test/org/jetbrains/k2js/test/AbstractExpressionTest.java b/js/js.tests/test/org/jetbrains/k2js/test/AbstractExpressionTest.java index b4770f1177c..64391bc2eea 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/AbstractExpressionTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/AbstractExpressionTest.java @@ -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); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/ArrayListTest.java b/js/js.tests/test/org/jetbrains/k2js/test/ArrayListTest.java index 9ce795dcee1..44922ccf05e 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/ArrayListTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/ArrayListTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/ClassInheritanceTest.java b/js/js.tests/test/org/jetbrains/k2js/test/ClassInheritanceTest.java index b9535cb062e..562d48d6b76 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/ClassInheritanceTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/ClassInheritanceTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/ExamplesTest.java b/js/js.tests/test/org/jetbrains/k2js/test/ExamplesTest.java index b2aea775f6c..e882f31fe34 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/ExamplesTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/ExamplesTest.java @@ -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"); } }); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/ExtensionFunctionTest.java b/js/js.tests/test/org/jetbrains/k2js/test/ExtensionFunctionTest.java index 428fdad5ed7..fec04364292 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/ExtensionFunctionTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/ExtensionFunctionTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/ExtensionPropertyTest.java b/js/js.tests/test/org/jetbrains/k2js/test/ExtensionPropertyTest.java index c6387d097bb..fcf333c8b9c 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/ExtensionPropertyTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/ExtensionPropertyTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/ForeachTest.java b/js/js.tests/test/org/jetbrains/k2js/test/ForeachTest.java index 88d80b89b78..206b8f32da6 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/ForeachTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/ForeachTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/FunctionTest.java b/js/js.tests/test/org/jetbrains/k2js/test/FunctionTest.java index 5c60891fdc5..1bc0eb87e58 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/FunctionTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/FunctionTest.java @@ -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"); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/JavaClassesTest.java b/js/js.tests/test/org/jetbrains/k2js/test/JavaClassesTest.java index e5e844d7ed5..14cd1352b23 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/JavaClassesTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/JavaClassesTest.java @@ -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); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/KotlinLibTest.java b/js/js.tests/test/org/jetbrains/k2js/test/KotlinLibTest.java index 095285a777c..e18b387d8c5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/KotlinLibTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/KotlinLibTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MiscTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MiscTest.java index 39eb7ab2391..28d9843f459 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MiscTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MiscTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java index 4b0a375f952..fba47c22724 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java @@ -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); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java index cdc9b928b36..6e3de7f9774 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java @@ -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); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/NameClashesTest.java b/js/js.tests/test/org/jetbrains/k2js/test/NameClashesTest.java index c3c7d2c5bc8..4a425dcf3f8 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/NameClashesTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/NameClashesTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/ObjectTest.java b/js/js.tests/test/org/jetbrains/k2js/test/ObjectTest.java index 1b1a1f604c8..5b904d7f197 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/ObjectTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/ObjectTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/OperatorOverloadingTest.java b/js/js.tests/test/org/jetbrains/k2js/test/OperatorOverloadingTest.java index 61a0fe93124..04c481846b8 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/OperatorOverloadingTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/OperatorOverloadingTest.java @@ -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"); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/PatternMatchingTest.java b/js/js.tests/test/org/jetbrains/k2js/test/PatternMatchingTest.java index 1cbfc010a22..f7d6e6e3153 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/PatternMatchingTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/PatternMatchingTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/PropertyAccessTest.java b/js/js.tests/test/org/jetbrains/k2js/test/PropertyAccessTest.java index 9d15dbf0f2c..d0f201a620a 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/PropertyAccessTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/PropertyAccessTest.java @@ -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); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/RTTITest.java b/js/js.tests/test/org/jetbrains/k2js/test/RTTITest.java index 2529fbcc5ee..c151bb36c9f 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/RTTITest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/RTTITest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/RangeTest.java b/js/js.tests/test/org/jetbrains/k2js/test/RangeTest.java index 6fbc5269a41..f4a120ba508 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/RangeTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/RangeTest.java @@ -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"); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/RhinoPropertyTypesChecker.java b/js/js.tests/test/org/jetbrains/k2js/test/RhinoPropertyTypesChecker.java deleted file mode 100644 index 03ac0a4b841..00000000000 --- a/js/js.tests/test/org/jetbrains/k2js/test/RhinoPropertyTypesChecker.java +++ /dev/null @@ -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> propertyToType; - - public RhinoPropertyTypesChecker(String objectName, Map> 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> nameToClassMap) { - for (Map.Entry> 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 - )); - } - } - -} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/RhinoUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/RhinoUtils.java deleted file mode 100644 index 6ddf610539d..00000000000 --- a/js/js.tests/test/org/jetbrains/k2js/test/RhinoUtils.java +++ /dev/null @@ -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; - } -} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/SafeCallTest.java b/js/js.tests/test/org/jetbrains/k2js/test/SafeCallTest.java index 56180ddcddc..c9e8b2640c5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/SafeCallTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/SafeCallTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/SimpleTestSuite.java b/js/js.tests/test/org/jetbrains/k2js/test/SimpleTestSuite.java index 5b7637a36ba..4176748b64d 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/SimpleTestSuite.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/SimpleTestSuite.java @@ -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); } }); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java b/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java index 51578051cc4..483b8d91309 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java @@ -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"); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/StringTest.java b/js/js.tests/test/org/jetbrains/k2js/test/StringTest.java index b01412c5f06..609088b26e6 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/StringTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/StringTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/TraitTest.java b/js/js.tests/test/org/jetbrains/k2js/test/TraitTest.java index 0363121db1c..ad1f696c461 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/TraitTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TraitTest.java @@ -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"); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java index a3ddd42b756..9bec40c0de6 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java @@ -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 inputFiles, - @NotNull String outputFile) throws Exception { - K2JSTranslator translator = new K2JSTranslator(new TestConfig(getProject())); - List 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 fullFilePaths = new ArrayList(); @@ -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 generateFilenameList(String inputFile) { + protected static List 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 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(); - } - } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java b/js/js.tests/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java index 4f6b8c7cdb2..06d319c0852 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TranslatorTestCaseBuilder.java @@ -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 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 files = Arrays.asList(dir.listFiles(resultFilter)); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/TupleTest.java b/js/js.tests/test/org/jetbrains/k2js/test/TupleTest.java index e2e18fbe6e8..eda5b553a71 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/TupleTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TupleTest.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples1Test.java b/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples1Test.java index 7490e89ca94..9cc28b35bd9 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples1Test.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples1Test.java @@ -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"); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples2Test.java b/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples2Test.java index f62bdbd1259..e3db9c0e825 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples2Test.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/WebDemoExamples2Test.java @@ -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 { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/RhinoFunctionResultChecker.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionResultChecker.java similarity index 98% rename from js/js.tests/test/org/jetbrains/k2js/test/RhinoFunctionResultChecker.java rename to js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionResultChecker.java index 947a5f49062..23c4248bbeb 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/RhinoFunctionResultChecker.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionResultChecker.java @@ -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; diff --git a/js/js.tests/test/org/jetbrains/k2js/test/RhinoResultChecker.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoResultChecker.java similarity index 95% rename from js/js.tests/test/org/jetbrains/k2js/test/RhinoResultChecker.java rename to js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoResultChecker.java index 273895ed8a8..4a279b5d455 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/RhinoResultChecker.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoResultChecker.java @@ -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; diff --git a/js/js.tests/test/org/jetbrains/k2js/test/RhinoSystemOutputChecker.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoSystemOutputChecker.java similarity index 98% rename from js/js.tests/test/org/jetbrains/k2js/test/RhinoSystemOutputChecker.java rename to js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoSystemOutputChecker.java index 2e8a3e38e5f..fee298fbcc5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/RhinoSystemOutputChecker.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoSystemOutputChecker.java @@ -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; diff --git a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java new file mode 100644 index 00000000000..5abdc4c3a43 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java @@ -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 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(); + } +} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java new file mode 100644 index 00000000000..d7acfa400e5 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/utils/JsTestUtils.java @@ -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(); + } + } +} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/Suite.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/SingleFileTest.java similarity index 52% rename from js/js.tests/test/org/jetbrains/k2js/test/Suite.java rename to js/js.tests/test/org/jetbrains/k2js/test/utils/SingleFileTest.java index 4151de967e5..71041608898 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/Suite.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/utils/SingleFileTest.java @@ -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; - } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java new file mode 100644 index 00000000000..24f3297dde3 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/utils/TranslationUtils.java @@ -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 inputFiles, + @NotNull String outputFile) throws Exception { + K2JSTranslator translator = new K2JSTranslator(new TestConfig(project)); + List psiFiles = createPsiFileList(inputFiles, project); + JsProgram program = translator.generateProgram(psiFiles); + K2JSTranslator.saveProgramToFile(outputFile, program); + } +}