From 1ccbf241724236c113cd418c92c73facdfb2e05a Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 5 Jul 2012 21:18:17 +0400 Subject: [PATCH] Trying to run stdlib unit tests as part or js.tests: step 2 --- .../k2js/test/semantics/JsUnitTestBase.java | 12 ++++- .../src/org/jetbrains/k2js/config/Config.java | 9 ++++ .../k2js/translate/general/Translation.java | 13 +++++- .../k2js/translate/test/CommonUnitTester.java | 46 +++++++++++++++++++ ...tionTester.java => JSRhinoUnitTester.java} | 19 ++++---- .../k2js/translate/test/JSTestGenerator.java | 6 +-- .../k2js/translate/test/JSTester.java | 36 ++++++++++++--- .../k2js/translate/test/QUnitTester.java | 22 ++++----- .../testFiles/jsTester/jsTester.js | 4 +- 9 files changed, 130 insertions(+), 37 deletions(-) create mode 100644 js/js.translator/src/org/jetbrains/k2js/translate/test/CommonUnitTester.java rename js/js.translator/src/org/jetbrains/k2js/translate/test/{PlainAssertionTester.java => JSRhinoUnitTester.java} (60%) diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java index ab16d71c08a..5de1231b42c 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsUnitTestBase.java @@ -20,9 +20,12 @@ import closurecompiler.internal.com.google.common.collect.Lists; import org.jetbrains.annotations.NotNull; import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.EcmaVersion; +import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.test.MultipleFilesTranslationTest; +import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static org.jetbrains.k2js.test.utils.LibraryFilePathsUtil.getAdditionalLibraryFiles; @@ -61,10 +64,15 @@ public class JsUnitTestBase extends MultipleFilesTranslationTest { assert removed; result.addAll(additionalLibraryFiles); return result; - } public void testDummy() throws Exception { - checkFooBoxIsTrue("test"); + performUnitTest("libraries/stdlib/test/ListTest.kt"); + } + + private void performUnitTest(String... testFiles) throws Exception { + Iterable versions = Collections.singletonList(EcmaVersion.v3); + generateJavaScriptFiles(Lists.newArrayList(testFiles), "myTest", MainCallParameters.noCall(), versions); + runRhinoTests("myTest", versions, new RhinoSystemOutputChecker("")); } } diff --git a/js/js.translator/src/org/jetbrains/k2js/config/Config.java b/js/js.translator/src/org/jetbrains/k2js/config/Config.java index 79a9cd0104e..dd815eb0b02 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -22,6 +22,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.k2js.translate.test.JSTester; +import org.jetbrains.k2js.translate.test.QUnitTester; import java.util.Arrays; import java.util.Collection; @@ -84,6 +86,7 @@ public abstract class Config { @NotNull public static final List LIB_FILE_NAMES = Lists.newArrayList(); + static { LIB_FILE_NAMES.addAll(LIB_FILES_WITH_DECLARATIONS); LIB_FILE_NAMES.addAll(LIB_FILES_WITH_CODE); @@ -187,4 +190,10 @@ public abstract class Config { allFiles.addAll(config.getLibFiles()); return allFiles; } + + //TODO: should be null by default I suppose but we can't communicate it to K2JSCompiler atm + @Nullable + public JSTester getTester() { + return new QUnitTester(); + } } diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java index 4f8b441875f..f379841a45e 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java @@ -45,6 +45,7 @@ import org.jetbrains.k2js.translate.initializer.ClassInitializerTranslator; import org.jetbrains.k2js.translate.initializer.NamespaceInitializerTranslator; import org.jetbrains.k2js.translate.reference.CallBuilder; import org.jetbrains.k2js.translate.test.JSTestGenerator; +import org.jetbrains.k2js.translate.test.JSTester; import org.jetbrains.k2js.translate.utils.JsAstUtils; import org.jetbrains.k2js.translate.utils.TranslationUtils; import org.jetbrains.k2js.translate.utils.dangerous.DangerousData; @@ -186,11 +187,21 @@ public final class Translation { statements.add(statement); } } - JSTestGenerator.generateTestCalls(context, files, rootBlock); + mayBeGenerateTests(files, config, rootBlock, context); performSimpleNameMangling(context.program()); return context.program(); } + private static void mayBeGenerateTests(@NotNull Collection files, @NotNull Config config, + @NotNull JsBlock rootBlock, @NotNull TranslationContext context) { + JSTester tester = config.getTester(); + if (tester != null) { + tester.initialize(context, rootBlock); + JSTestGenerator.generateTestCalls(context, files, tester); + tester.deinitialize(); + } + } + private static void performSimpleNameMangling(@NotNull JsProgram program) { JsNamer namer = new JsPrettyNamer(); namer.exec(program); diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/test/CommonUnitTester.java b/js/js.translator/src/org/jetbrains/k2js/translate/test/CommonUnitTester.java new file mode 100644 index 00000000000..7c9146c44d0 --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/translate/test/CommonUnitTester.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010-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.translate.test; + +import com.google.dart.compiler.backend.js.ast.JsExpression; +import com.google.dart.compiler.backend.js.ast.JsFunction; +import com.google.dart.compiler.backend.js.ast.JsStringLiteral; +import org.jetbrains.annotations.NotNull; + +import static com.google.dart.compiler.util.AstUtil.newBlock; +import static com.google.dart.compiler.util.AstUtil.newInvocation; + +/** + * @author Pavel Talanov + */ +public abstract class CommonUnitTester extends JSTester { + + public CommonUnitTester() { + super(); + } + + @Override + public void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall, + @NotNull JsStringLiteral testName) { + JsFunction functionToTest = new JsFunction(getContext().jsScope()); + functionToTest.setBody(newBlock(functionToTestCall.makeStmt())); + getBlock().getStatements().add(newInvocation(getTestMethodRef(), testName, functionToTest).makeStmt()); + } + + @NotNull + protected abstract JsExpression getTestMethodRef(); +} diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/test/PlainAssertionTester.java b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSRhinoUnitTester.java similarity index 60% rename from js/js.translator/src/org/jetbrains/k2js/translate/test/PlainAssertionTester.java rename to js/js.translator/src/org/jetbrains/k2js/translate/test/JSRhinoUnitTester.java index 4ccd99157aa..02cabcc0127 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/test/PlainAssertionTester.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSRhinoUnitTester.java @@ -16,22 +16,25 @@ package org.jetbrains.k2js.translate.test; -import com.google.dart.compiler.backend.js.ast.JsBlock; import com.google.dart.compiler.backend.js.ast.JsExpression; -import com.google.dart.compiler.backend.js.ast.JsStringLiteral; +import com.google.dart.compiler.backend.js.ast.JsNameRef; +import com.google.dart.compiler.util.AstUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.k2js.translate.context.TranslationContext; /** * @author Pavel Talanov */ -public final class PlainAssertionTester extends JSTester { - public PlainAssertionTester(@NotNull JsBlock block, @NotNull TranslationContext context) { - super(block, context); +public final class JSRhinoUnitTester extends CommonUnitTester { + + public static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("JsTests.test"); + + public JSRhinoUnitTester() { + super(); } + @NotNull @Override - public void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name) { - block.getStatements().add(call.makeStmt()); + protected JsExpression getTestMethodRef() { + return TEST_FUN_REF; } } diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTestGenerator.java b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTestGenerator.java index 48b46e53fc9..cfcdcd6ac4a 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTestGenerator.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTestGenerator.java @@ -16,7 +16,6 @@ package org.jetbrains.k2js.translate.test; -import com.google.dart.compiler.backend.js.ast.JsBlock; import com.google.dart.compiler.backend.js.ast.JsExpression; import com.google.dart.compiler.backend.js.ast.JsNew; import com.google.dart.compiler.backend.js.ast.JsStringLiteral; @@ -42,10 +41,9 @@ public final class JSTestGenerator { } public static void generateTestCalls(@NotNull TranslationContext context, - @NotNull Collection files, - @NotNull JsBlock block) { + @NotNull Collection files, @NotNull JSTester tester) { List functionDescriptors = JetTestFunctionDetector.getTestFunctionDescriptors(context.bindingContext(), files); - doGenerateTestCalls(functionDescriptors, context, new PlainAssertionTester(block, context)); + doGenerateTestCalls(functionDescriptors, context, tester); } private static void doGenerateTestCalls(@NotNull List functionDescriptors, diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java index d82ab662a71..50e899630c5 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/test/JSTester.java @@ -20,6 +20,7 @@ import com.google.dart.compiler.backend.js.ast.JsBlock; import com.google.dart.compiler.backend.js.ast.JsExpression; import com.google.dart.compiler.backend.js.ast.JsStringLiteral; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.k2js.translate.context.TranslationContext; /** @@ -27,15 +28,38 @@ import org.jetbrains.k2js.translate.context.TranslationContext; */ public abstract class JSTester { - @NotNull - protected final JsBlock block; - @NotNull - protected final TranslationContext context; + @Nullable + private JsBlock block; - public JSTester(@NotNull JsBlock block, @NotNull TranslationContext context) { + @Nullable + private TranslationContext context; + + public JSTester() { + this.block = null; + this.context = null; + } + + public abstract void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name); + + @NotNull + protected JsBlock getBlock() { + assert block != null : "Call initialize before using tester."; + return block; + } + + @NotNull + protected TranslationContext getContext() { + assert context != null : "Call initialize before using tester."; + return context; + } + + public void initialize(@NotNull TranslationContext context, @NotNull JsBlock block) { this.block = block; this.context = context; } - public abstract void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name); + public void deinitialize() { + this.block = null; + this.context = null; + } } diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/test/QUnitTester.java b/js/js.translator/src/org/jetbrains/k2js/translate/test/QUnitTester.java index b19a7392edb..50566ad0009 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/test/QUnitTester.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/test/QUnitTester.java @@ -16,30 +16,24 @@ package org.jetbrains.k2js.translate.test; -import com.google.dart.compiler.backend.js.ast.*; +import com.google.dart.compiler.backend.js.ast.JsExpression; +import com.google.dart.compiler.backend.js.ast.JsNameRef; import com.google.dart.compiler.util.AstUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.k2js.translate.context.TranslationContext; - -import static com.google.dart.compiler.util.AstUtil.newBlock; -import static com.google.dart.compiler.util.AstUtil.newInvocation; /** * @author Pavel Talanov */ -public final class QUnitTester extends JSTester { +public final class QUnitTester extends CommonUnitTester { @NotNull private static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("QUnit.test"); - public QUnitTester(@NotNull JsBlock block, @NotNull TranslationContext context) { - super(block, context); + public QUnitTester() { + super(); } - @Override - public void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall, - @NotNull JsStringLiteral testName) { - JsFunction functionToTest = new JsFunction(this.context.jsScope()); - functionToTest.setBody(newBlock(functionToTestCall.makeStmt())); - block.getStatements().add(newInvocation(TEST_FUN_REF, testName, functionToTest).makeStmt()); + @NotNull + protected JsExpression getTestMethodRef() { + return TEST_FUN_REF; } } diff --git a/js/js.translator/testFiles/jsTester/jsTester.js b/js/js.translator/testFiles/jsTester/jsTester.js index 0b7b0698eb5..57244907a93 100644 --- a/js/js.translator/testFiles/jsTester/jsTester.js +++ b/js/js.translator/testFiles/jsTester/jsTester.js @@ -16,7 +16,7 @@ /*Asserter*/ var JsTests = (function() { - var out = null; + var out = Kotlin.System.out(); function setOut(newOut) { out = newOut; } @@ -28,7 +28,7 @@ var JsTests = (function() { } }; this.assertEquals = function (message, actual) { - + throw null; }; };