From dcb7b3264c963aaa6c02acb948625c22597a5995 Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Wed, 16 Nov 2011 16:09:50 +0400 Subject: [PATCH] Refactored tests a bit. Working on KotlinLibTest --- .idea/workspace.xml | 314 +++++++++--------- .../k2js/test/AbstractClassTest.java | 33 +- .../jetbrains/k2js/test/BasicClassTest.java | 10 - .../jetbrains/k2js/test/ExpressionTest.java | 8 + .../jetbrains/k2js/test/KotlinLibTest.java | 35 ++ .../org/jetbrains/k2js/test/TraitTest.java | 11 +- .../jetbrains/k2js/test/TranslationTest.java | 38 ++- translator/testFiles/kotlin_lib.js | 34 +- .../testFiles/trait/cases/traitCompiles.kt | 9 - .../trait/cases/traitExtendsTrait.kt | 21 ++ 10 files changed, 264 insertions(+), 249 deletions(-) create mode 100644 translator/test/org/jetbrains/k2js/test/KotlinLibTest.java delete mode 100644 translator/testFiles/trait/cases/traitCompiles.kt create mode 100644 translator/testFiles/trait/cases/traitExtendsTrait.kt diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a9c2d67fa2a..a456749f6cd 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,10 +6,16 @@ - + + + - + + + + + @@ -60,7 +66,7 @@ - + - - + + - - + + + + - - - - - - - - - - - + + @@ -143,37 +142,48 @@ - - + + - + - - + + - + - - + + - + + + + + + + + + + + + - - + + - + @@ -200,7 +210,7 @@ - + @@ -210,8 +220,26 @@ - - + + + + + + + + + + + + + + + + + + + + @@ -219,37 +247,19 @@ - - - - - - - - - - - - - - - - - - - + - - + + - + @@ -321,22 +331,22 @@ @@ -428,32 +438,6 @@ @@ -1141,69 +1125,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1211,9 +1132,9 @@ - + - + @@ -1225,30 +1146,97 @@ - + - + - + - + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/translator/test/org/jetbrains/k2js/test/AbstractClassTest.java b/translator/test/org/jetbrains/k2js/test/AbstractClassTest.java index 046bd0460b4..a5214f7271f 100644 --- a/translator/test/org/jetbrains/k2js/test/AbstractClassTest.java +++ b/translator/test/org/jetbrains/k2js/test/AbstractClassTest.java @@ -1,41 +1,16 @@ package org.jetbrains.k2js.test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; - -import java.io.FileReader; - -import static org.junit.Assert.assertTrue; +import java.util.Arrays; +import java.util.List; /** * @author Talanov Pavel */ public abstract class AbstractClassTest extends TranslationTest { - private final String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js"; - - protected String kotlinLibraryPath() { - return KOTLIN_JS_LIB; - } - - protected void runFileWithRhino(String inputFile, Context context, Scriptable scope) throws Exception { - FileReader reader = new FileReader(inputFile); - context.evaluateReader(scope, reader, inputFile, 1, null); - reader.close(); - } - @Override - protected void runWithRhino(String inputFile, String namespaceName, - String functionName, Object expectedResult) throws Exception { - Context cx = Context.enter(); - Scriptable scope = cx.initStandardObjects(); - runFileWithRhino(kotlinLibraryPath(), cx, scope); - runFileWithRhino(inputFile, 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); - Context.exit(); + protected List generateFilenameList(String inputFile) { + return Arrays.asList(kotlinLibraryPath(), inputFile); } } diff --git a/translator/test/org/jetbrains/k2js/test/BasicClassTest.java b/translator/test/org/jetbrains/k2js/test/BasicClassTest.java index 4f705df22dc..ff1f6d47556 100644 --- a/translator/test/org/jetbrains/k2js/test/BasicClassTest.java +++ b/translator/test/org/jetbrains/k2js/test/BasicClassTest.java @@ -1,8 +1,6 @@ package org.jetbrains.k2js.test; import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; /** * @author Talanov Pavel @@ -16,14 +14,6 @@ public class BasicClassTest extends AbstractClassTest { return MAIN; } - @Test - public void kotlinJsLibRunsWithRhino() throws Exception { - Context context = Context.enter(); - Scriptable scope = context.initStandardObjects(); - runFileWithRhino(kotlinLibraryPath(), context, scope); - Context.exit(); - } - @Test public void classInstantiation() throws Exception { testFooBoxIsTrue("classInstantiation.kt"); diff --git a/translator/test/org/jetbrains/k2js/test/ExpressionTest.java b/translator/test/org/jetbrains/k2js/test/ExpressionTest.java index 091a8501c6c..b664b0835d2 100644 --- a/translator/test/org/jetbrains/k2js/test/ExpressionTest.java +++ b/translator/test/org/jetbrains/k2js/test/ExpressionTest.java @@ -2,6 +2,9 @@ package org.jetbrains.k2js.test; import org.junit.Test; +import java.util.Arrays; +import java.util.List; + /** * @author Talanov Pavel *

@@ -16,6 +19,11 @@ public final class ExpressionTest extends TranslationTest { return MAIN; } + @Override + protected List generateFilenameList(String inputFile) { + return Arrays.asList(inputFile); + } + @Test public void currentTest() throws Exception { testFooBoxIsTrue("test.kt"); diff --git a/translator/test/org/jetbrains/k2js/test/KotlinLibTest.java b/translator/test/org/jetbrains/k2js/test/KotlinLibTest.java new file mode 100644 index 00000000000..25dcac162f4 --- /dev/null +++ b/translator/test/org/jetbrains/k2js/test/KotlinLibTest.java @@ -0,0 +1,35 @@ +package org.jetbrains.k2js.test; + +import org.junit.Test; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Scriptable; + +import java.util.Arrays; +import java.util.List; + +/** + * @author Talanov Pavel + */ +public class KotlinLibTest extends TranslationTest { + + final private static String MAIN = "kotlin_lib/"; + + @Override + protected List generateFilenameList(String inputFile) { + return Arrays.asList(kotlinLibraryPath()); + } + + @Override + protected String mainDirectory() { + return MAIN; + } + + @Test + public void kotlinJsLibRunsWithRhino() throws Exception { + Context context = Context.enter(); + Scriptable scope = context.initStandardObjects(); + runFileWithRhino(kotlinLibraryPath(), context, scope); + Context.exit(); + } + +} diff --git a/translator/test/org/jetbrains/k2js/test/TraitTest.java b/translator/test/org/jetbrains/k2js/test/TraitTest.java index c77284576a1..b08ef053be9 100644 --- a/translator/test/org/jetbrains/k2js/test/TraitTest.java +++ b/translator/test/org/jetbrains/k2js/test/TraitTest.java @@ -14,11 +14,6 @@ public final class TraitTest extends AbstractClassTest { return MAIN; } - @Test - public void traitCompiles() throws Exception { - testFooBoxIsTrue("traitCompiles.kt"); - } - @Test public void traitAddsFunctionsToClass() throws Exception { testFooBoxIsTrue("traitAddsFunctionsToClass.kt"); @@ -38,4 +33,10 @@ public final class TraitTest extends AbstractClassTest { public void example() throws Exception { testFooBoxIsTrue("example.kt"); } + + @Test + public void traitExtendsTrait() throws Exception { + testFooBoxIsTrue("traitExtendsTrait.kt"); + } + } diff --git a/translator/test/org/jetbrains/k2js/test/TranslationTest.java b/translator/test/org/jetbrains/k2js/test/TranslationTest.java index ef26af30a61..7840f49b9c3 100644 --- a/translator/test/org/jetbrains/k2js/test/TranslationTest.java +++ b/translator/test/org/jetbrains/k2js/test/TranslationTest.java @@ -8,6 +8,7 @@ import org.mozilla.javascript.NativeObject; import org.mozilla.javascript.Scriptable; import java.io.FileReader; +import java.util.List; import static org.junit.Assert.assertTrue; @@ -20,6 +21,7 @@ public abstract class TranslationTest { final protected static String TEST_FILES = "testFiles/"; final private static String CASES = "cases/"; final private static String OUT = "out/"; + final private String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js"; protected String testFilesDirectory; protected String testCasesDirectory; @@ -34,6 +36,10 @@ public abstract class TranslationTest { protected abstract String mainDirectory(); + protected String kotlinLibraryPath() { + return KOTLIN_JS_LIB; + } + private String getOutputDirectory() { return testFilesDirectory + outputDirectory; } @@ -48,9 +54,11 @@ public abstract class TranslationTest { args.src = getInputFilePath(filename); args.outputDir = getOutputFilePath(filename); K2JSTranslator.translate(args); - runWithRhino(args.outputDir, namespaceName, functionName, expectedResult); + runWithRhino(generateFilenameList(args.outputDir), namespaceName, functionName, expectedResult); } + abstract protected List generateFilenameList(String inputfile); + private String getOutputFilePath(String filename) { return getOutputDirectory() + convertToDotJsFile(filename); } @@ -63,22 +71,24 @@ public abstract class TranslationTest { return getInputDirectory() + filename; } - protected void runWithRhino(String inputFile, String namespaceName, + protected void runFileWithRhino(String inputFile, Context context, Scriptable scope) throws Exception { + FileReader reader = new FileReader(inputFile); + context.evaluateReader(scope, reader, inputFile, 1, null); + reader.close(); + } + + protected void runWithRhino(List filenames, String namespaceName, String functionName, Object expectedResult) throws Exception { Context cx = Context.enter(); - FileReader reader = new FileReader(inputFile); - try { - Scriptable scope = cx.initStandardObjects(); - cx.evaluateReader(scope, reader, "test case", 1, null); - 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); - - } finally { - Context.exit(); - reader.close(); + 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); + Context.exit(); } protected Object extractAndCallFunctionObject(String namespaceName, String functionName, diff --git a/translator/testFiles/kotlin_lib.js b/translator/testFiles/kotlin_lib.js index f0b131b734b..8c0e002e123 100644 --- a/translator/testFiles/kotlin_lib.js +++ b/translator/testFiles/kotlin_lib.js @@ -10,13 +10,6 @@ var emptyFunction = function() {} var Class = (function() { - var IS_DONTENUM_BUGGY = (function(){ - for (var p in { toString: 1 }) { - if (p === 'toString') return false; - } - return true; - })(); - function subclass() {}; function create() { var parent = null, properties = $A(arguments); @@ -43,10 +36,8 @@ var Class = (function() { klass.addMethods( { 'super_init' : function () { - //if (this.initializing != null) { - this.initializing = this.initializing.superclass; - this.initializing.prototype.initialize.apply(this, arguments) - // } + this.initializing = this.initializing.superclass; + this.initializing.prototype.initialize.apply(this, arguments) } }); } @@ -65,12 +56,6 @@ var Class = (function() { var ancestor = this.superclass && this.superclass.prototype, properties = Object.keys(source); - if (IS_DONTENUM_BUGGY) { - if (source.toString != Object.prototype.toString) - properties.push("toString"); - if (source.valueOf != Object.prototype.valueOf) - properties.push("valueOf"); - } for (var i = 0, length = properties.length; i < length; i++) { var property = properties[i], value = source[property]; @@ -81,8 +66,8 @@ var Class = (function() { return function() { return ancestor[m].apply(this, arguments); }; })(property).wrap(method); - value.valueOf = method.valueOf.bind(method); - value.toString = method.toString.bind(method); + // value.valueOf = method.valueOf.bind(method); + // value.toString = method.toString.bind(method); } this.prototype[property] = value; } @@ -98,6 +83,17 @@ var Class = (function() { }; })(); +var Trait = (function() { + + function create() { + return new Class.create(arguments); + } + + return { + create: create + }; +})(); + (function() { var _toString = Object.prototype.toString, diff --git a/translator/testFiles/trait/cases/traitCompiles.kt b/translator/testFiles/trait/cases/traitCompiles.kt deleted file mode 100644 index 93f090310d2..00000000000 --- a/translator/testFiles/trait/cases/traitCompiles.kt +++ /dev/null @@ -1,9 +0,0 @@ -namespace foo - -trait Test { - fun addFoo(s:String) : String { - return s + "FOO" - } -} - -fun box() = true \ No newline at end of file diff --git a/translator/testFiles/trait/cases/traitExtendsTrait.kt b/translator/testFiles/trait/cases/traitExtendsTrait.kt new file mode 100644 index 00000000000..10ac25a2ac5 --- /dev/null +++ b/translator/testFiles/trait/cases/traitExtendsTrait.kt @@ -0,0 +1,21 @@ +namespace foo + +trait Test { + fun addFoo(s:String) : String { + return s + "FOO" + } +} + +trait ExtendedTest : Test { + fun hooray() : String { + return "hooray" + } +} + +class A() : ExtendedTest { + fun eval() : String { + return addFoo(hooray()); + } +} + +fun box() = (A().eval() == "hoorayFOO") \ No newline at end of file