From 8f99212d9ed65e8a4c5d8e2b608b4bad83bef192 Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Wed, 16 Nov 2011 17:12:16 +0400 Subject: [PATCH] Refactored test code a lot. Introduced KotlinLibTest. --- .idea/workspace.xml | 584 ++++++------------ .../jetbrains/k2js/test/BasicClassTest.java | 2 +- .../k2js/test/ClassInheritanceTest.java | 2 +- .../jetbrains/k2js/test/ExpressionTest.java | 8 +- .../jetbrains/k2js/test/KotlinLibTest.java | 36 +- .../k2js/test/PropertyAccessorTest.java | 2 +- .../k2js/test/RhinoFunctionResultChecker.java | 42 ++ .../k2js/test/RhinoResultChecker.java | 11 + .../org/jetbrains/k2js/test/RhinoUtils.java | 22 + .../jetbrains/k2js/test/TranslationTest.java | 33 +- 10 files changed, 315 insertions(+), 427 deletions(-) create mode 100644 translator/test/org/jetbrains/k2js/test/RhinoFunctionResultChecker.java create mode 100644 translator/test/org/jetbrains/k2js/test/RhinoResultChecker.java create mode 100644 translator/test/org/jetbrains/k2js/test/RhinoUtils.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a456749f6cd..70df3a11aad 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,15 +6,15 @@ - - - + + + - - + - + + @@ -66,7 +66,7 @@ - + - + - + @@ -133,7 +133,65 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -142,76 +200,22 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + - - - - - - - - - - - - - - - - - - - - + + + + @@ -247,19 +251,19 @@ - + - + - + - + @@ -331,10 +335,6 @@ @@ -435,179 +439,7 @@ - - - - - - - - - - + + + + + + + + + + + + - - - - - + + + + + localhost @@ -1024,7 +814,7 @@ - + @@ -1040,7 +830,7 @@ - + @@ -1078,7 +868,7 @@ @@ -1125,86 +915,46 @@ - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + + + - + - + @@ -1218,29 +968,77 @@ - + - + + + + + + + + + + + + + + + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/translator/test/org/jetbrains/k2js/test/BasicClassTest.java b/translator/test/org/jetbrains/k2js/test/BasicClassTest.java index ff1f6d47556..c05c9c072f4 100644 --- a/translator/test/org/jetbrains/k2js/test/BasicClassTest.java +++ b/translator/test/org/jetbrains/k2js/test/BasicClassTest.java @@ -32,7 +32,7 @@ public class BasicClassTest extends AbstractClassTest { @Test public void incrementProperty() throws Exception { - performTest("incrementProperty.kt", "foo", "box", "OK"); + testFunctionOutput("incrementProperty.kt", "foo", "box", "OK"); } @Test diff --git a/translator/test/org/jetbrains/k2js/test/ClassInheritanceTest.java b/translator/test/org/jetbrains/k2js/test/ClassInheritanceTest.java index 23b0b80cf1e..0d2f901d00b 100644 --- a/translator/test/org/jetbrains/k2js/test/ClassInheritanceTest.java +++ b/translator/test/org/jetbrains/k2js/test/ClassInheritanceTest.java @@ -16,7 +16,7 @@ public final class ClassInheritanceTest extends AbstractClassTest { @Test public void initializersOfBasicClassExecute() throws Exception { - performTest("initializersOfBasicClassExecute.kt", "foo", "box", 3); + testFunctionOutput("initializersOfBasicClassExecute.kt", "foo", "box", 3); } @Test diff --git a/translator/test/org/jetbrains/k2js/test/ExpressionTest.java b/translator/test/org/jetbrains/k2js/test/ExpressionTest.java index b664b0835d2..2415c9a6ef7 100644 --- a/translator/test/org/jetbrains/k2js/test/ExpressionTest.java +++ b/translator/test/org/jetbrains/k2js/test/ExpressionTest.java @@ -31,12 +31,12 @@ public final class ExpressionTest extends TranslationTest { @Test public void testAssign() throws Exception { - performTest("assign.jet", "foo", "f", 2.0); + testFunctionOutput("assign.jet", "foo", "f", 2.0); } @Test public void namespaceProperties() throws Exception { - performTest("localProperty.jet", "foo", "box", 50); + testFunctionOutput("localProperty.jet", "foo", "box", 50); } @Test @@ -46,12 +46,12 @@ public final class ExpressionTest extends TranslationTest { @Test public void ifElse() throws Exception { - performTest("if.kt", "foo", "box", 5); + testFunctionOutput("if.kt", "foo", "box", 5); } //TODO: test fails due to isStatement issue, include when issue is solved or implement another solution // @Test // public void ifElseIf() throws Exception { -// performTest("elseif.kt", "foo", "box", 5); +// testFunctionOutput("elseif.kt", "foo", "box", 5); // } @Test diff --git a/translator/test/org/jetbrains/k2js/test/KotlinLibTest.java b/translator/test/org/jetbrains/k2js/test/KotlinLibTest.java index 25dcac162f4..a209e9f492e 100644 --- a/translator/test/org/jetbrains/k2js/test/KotlinLibTest.java +++ b/translator/test/org/jetbrains/k2js/test/KotlinLibTest.java @@ -2,17 +2,23 @@ package org.jetbrains.k2js.test; import org.junit.Test; import org.mozilla.javascript.Context; +import org.mozilla.javascript.Function; +import org.mozilla.javascript.NativeObject; import org.mozilla.javascript.Scriptable; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertTrue; /** * @author Talanov Pavel */ public class KotlinLibTest extends TranslationTest { - final private static String MAIN = "kotlin_lib/"; + final private static String MAIN = "kotlinLib/"; @Override protected List generateFilenameList(String inputFile) { @@ -24,6 +30,17 @@ public class KotlinLibTest extends TranslationTest { return MAIN; } + protected void verifyObjectHasExpectedPropertiesOfExpectedTypes + (NativeObject object, Map> nameToClassMap) { + for (Map.Entry> entry : nameToClassMap.entrySet()) { + String name = entry.getKey(); + Class expectedClass = entry.getValue(); + assertTrue(object + " must contain key " + name, object.containsKey(name)); + assertTrue(object + "'s property " + name + " must be of type " + expectedClass, + expectedClass.isInstance(object.get(name))); + } + } + @Test public void kotlinJsLibRunsWithRhino() throws Exception { Context context = Context.enter(); @@ -32,4 +49,21 @@ public class KotlinLibTest extends TranslationTest { Context.exit(); } + @Test + public void classObjectHasCreateMethod() throws Exception { + final Map> nameToClassMap = + new HashMap>(); + nameToClassMap.put("create", Function.class); + + runRhinoTest(Arrays.asList(kotlinLibraryPath()), + new RhinoResultChecker() { + @Override + public void runChecks(Context context, Scriptable scope) throws Exception { + NativeObject object = RhinoUtils.extractObject("Class", scope); + verifyObjectHasExpectedPropertiesOfExpectedTypes(object, nameToClassMap); + } + }); + } + + } diff --git a/translator/test/org/jetbrains/k2js/test/PropertyAccessorTest.java b/translator/test/org/jetbrains/k2js/test/PropertyAccessorTest.java index fb5f56c4341..aa410782026 100644 --- a/translator/test/org/jetbrains/k2js/test/PropertyAccessorTest.java +++ b/translator/test/org/jetbrains/k2js/test/PropertyAccessorTest.java @@ -26,7 +26,7 @@ public final class PropertyAccessorTest extends AbstractClassTest { @Test public void setter() throws Exception { - performTest("setter.kt", "foo", "f", 99.0); + testFunctionOutput("setter.kt", "foo", "f", 99.0); } @Test diff --git a/translator/test/org/jetbrains/k2js/test/RhinoFunctionResultChecker.java b/translator/test/org/jetbrains/k2js/test/RhinoFunctionResultChecker.java new file mode 100644 index 00000000000..5b0fb36bd09 --- /dev/null +++ b/translator/test/org/jetbrains/k2js/test/RhinoFunctionResultChecker.java @@ -0,0 +1,42 @@ +package org.jetbrains.k2js.test; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Function; +import org.mozilla.javascript.NativeObject; +import org.mozilla.javascript.Scriptable; + +import static org.junit.Assert.assertTrue; + +/** + * @author Talanov Pavel + */ +public final class RhinoFunctionResultChecker implements RhinoResultChecker { + + final String namespaceName; + final String functionName; + final Object expectedResult; + + public RhinoFunctionResultChecker(String namespaceName, String functionName, Object expectedResult) { + this.namespaceName = namespaceName; + this.functionName = functionName; + this.expectedResult = expectedResult; + } + + @Override + public void runChecks(Context context, Scriptable scope) throws Exception { + Object result = extractAndCallFunctionObject(namespaceName, functionName, context, scope); + assertTrue("Result is not what expected!", result.equals(expectedResult)); + String report = namespaceName + "." + functionName + "() = " + Context.toString(result); + System.out.println(report); + } + + private Object extractAndCallFunctionObject(String namespaceName, String functionName, + Context cx, Scriptable scope) { + NativeObject namespaceObject = RhinoUtils.extractObject(namespaceName, scope); + Object box = namespaceObject.get(functionName, namespaceObject); + assertTrue("Function " + functionName + " not defined in namespace " + namespaceName, box instanceof Function); + Object functionArgs[] = {}; + Function function = (Function) box; + return function.call(cx, scope, scope, functionArgs); + } +} diff --git a/translator/test/org/jetbrains/k2js/test/RhinoResultChecker.java b/translator/test/org/jetbrains/k2js/test/RhinoResultChecker.java new file mode 100644 index 00000000000..e62a7b9ca73 --- /dev/null +++ b/translator/test/org/jetbrains/k2js/test/RhinoResultChecker.java @@ -0,0 +1,11 @@ +package org.jetbrains.k2js.test; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Scriptable; + +/** + * @author Talanov Pavel + */ +public interface RhinoResultChecker { + public void runChecks(Context context, Scriptable scope) throws Exception; +} diff --git a/translator/test/org/jetbrains/k2js/test/RhinoUtils.java b/translator/test/org/jetbrains/k2js/test/RhinoUtils.java new file mode 100644 index 00000000000..391b1938bc4 --- /dev/null +++ b/translator/test/org/jetbrains/k2js/test/RhinoUtils.java @@ -0,0 +1,22 @@ +package org.jetbrains.k2js.test; + +import org.mozilla.javascript.NativeObject; +import org.mozilla.javascript.Scriptable; + +import static org.junit.Assert.assertTrue; + +/** + * @author Talanov Pavel + */ +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/translator/test/org/jetbrains/k2js/test/TranslationTest.java b/translator/test/org/jetbrains/k2js/test/TranslationTest.java index 7840f49b9c3..600b54cc3b4 100644 --- a/translator/test/org/jetbrains/k2js/test/TranslationTest.java +++ b/translator/test/org/jetbrains/k2js/test/TranslationTest.java @@ -3,15 +3,11 @@ package org.jetbrains.k2js.test; import org.jetbrains.k2js.K2JSTranslator; import org.junit.Before; import org.mozilla.javascript.Context; -import org.mozilla.javascript.Function; -import org.mozilla.javascript.NativeObject; import org.mozilla.javascript.Scriptable; import java.io.FileReader; import java.util.List; -import static org.junit.Assert.assertTrue; - /** * @author Talanov Pavel @@ -48,13 +44,14 @@ public abstract class TranslationTest { return testFilesDirectory + testCasesDirectory; } - protected void performTest(String filename, String namespaceName, - String functionName, Object expectedResult) throws Exception { + protected void testFunctionOutput(String filename, String namespaceName, + String functionName, Object expectedResult) throws Exception { K2JSTranslator.Arguments args = new K2JSTranslator.Arguments(); args.src = getInputFilePath(filename); args.outputDir = getOutputFilePath(filename); K2JSTranslator.translate(args); - runWithRhino(generateFilenameList(args.outputDir), namespaceName, functionName, expectedResult); + runRhinoTest(generateFilenameList(args.outputDir), + new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult)); } abstract protected List generateFilenameList(String inputfile); @@ -77,33 +74,17 @@ public abstract class TranslationTest { reader.close(); } - protected void runWithRhino(List filenames, String namespaceName, - String functionName, Object expectedResult) throws Exception { + protected void runRhinoTest(List filenames, RhinoResultChecker checker) throws Exception { Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); for (String filename : filenames) { runFileWithRhino(filename, cx, scope); } - Object result = extractAndCallFunctionObject(namespaceName, functionName, cx, scope); - assertTrue("Result is not what expected!", result.equals(expectedResult)); - String report = namespaceName + "." + functionName + "() = " + Context.toString(result); - System.out.println(report); + checker.runChecks(cx, scope); Context.exit(); } - protected Object extractAndCallFunctionObject(String namespaceName, String functionName, - Context cx, Scriptable scope) { - Object foo = scope.get(namespaceName, scope); - assertTrue(foo instanceof NativeObject); - NativeObject namespaceObject = (NativeObject) foo; - Object box = namespaceObject.get(functionName, namespaceObject); - assertTrue("Function " + functionName + " not defined in namespace " + namespaceName, box instanceof Function); - Object functionArgs[] = {}; - Function function = (Function) box; - return function.call(cx, scope, scope, functionArgs); - } - protected void testFooBoxIsTrue(String filename) throws Exception { - performTest(filename, "foo", "box", true); + testFunctionOutput(filename, "foo", "box", true); } }