Trying to run stdlib unit tests as part or js.tests: step 2

This commit is contained in:
Pavel V. Talanov
2012-07-05 21:18:17 +04:00
parent ec7b0a1d3a
commit 1ccbf24172
9 changed files with 130 additions and 37 deletions
@@ -20,9 +20,12 @@ import closurecompiler.internal.com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.MultipleFilesTranslationTest; import org.jetbrains.k2js.test.MultipleFilesTranslationTest;
import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import static org.jetbrains.k2js.test.utils.LibraryFilePathsUtil.getAdditionalLibraryFiles; import static org.jetbrains.k2js.test.utils.LibraryFilePathsUtil.getAdditionalLibraryFiles;
@@ -61,10 +64,15 @@ public class JsUnitTestBase extends MultipleFilesTranslationTest {
assert removed; assert removed;
result.addAll(additionalLibraryFiles); result.addAll(additionalLibraryFiles);
return result; return result;
} }
public void testDummy() throws Exception { public void testDummy() throws Exception {
checkFooBoxIsTrue("test"); performUnitTest("libraries/stdlib/test/ListTest.kt");
}
private void performUnitTest(String... testFiles) throws Exception {
Iterable<EcmaVersion> versions = Collections.singletonList(EcmaVersion.v3);
generateJavaScriptFiles(Lists.newArrayList(testFiles), "myTest", MainCallParameters.noCall(), versions);
runRhinoTests("myTest", versions, new RhinoSystemOutputChecker(""));
} }
} }
@@ -22,6 +22,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext; 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.Arrays;
import java.util.Collection; import java.util.Collection;
@@ -84,6 +86,7 @@ public abstract class Config {
@NotNull @NotNull
public static final List<String> LIB_FILE_NAMES = Lists.newArrayList(); public static final List<String> LIB_FILE_NAMES = Lists.newArrayList();
static { static {
LIB_FILE_NAMES.addAll(LIB_FILES_WITH_DECLARATIONS); LIB_FILE_NAMES.addAll(LIB_FILES_WITH_DECLARATIONS);
LIB_FILE_NAMES.addAll(LIB_FILES_WITH_CODE); LIB_FILE_NAMES.addAll(LIB_FILES_WITH_CODE);
@@ -187,4 +190,10 @@ public abstract class Config {
allFiles.addAll(config.getLibFiles()); allFiles.addAll(config.getLibFiles());
return allFiles; 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();
}
} }
@@ -45,6 +45,7 @@ import org.jetbrains.k2js.translate.initializer.ClassInitializerTranslator;
import org.jetbrains.k2js.translate.initializer.NamespaceInitializerTranslator; import org.jetbrains.k2js.translate.initializer.NamespaceInitializerTranslator;
import org.jetbrains.k2js.translate.reference.CallBuilder; import org.jetbrains.k2js.translate.reference.CallBuilder;
import org.jetbrains.k2js.translate.test.JSTestGenerator; 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.JsAstUtils;
import org.jetbrains.k2js.translate.utils.TranslationUtils; import org.jetbrains.k2js.translate.utils.TranslationUtils;
import org.jetbrains.k2js.translate.utils.dangerous.DangerousData; import org.jetbrains.k2js.translate.utils.dangerous.DangerousData;
@@ -186,11 +187,21 @@ public final class Translation {
statements.add(statement); statements.add(statement);
} }
} }
JSTestGenerator.generateTestCalls(context, files, rootBlock); mayBeGenerateTests(files, config, rootBlock, context);
performSimpleNameMangling(context.program()); performSimpleNameMangling(context.program());
return context.program(); return context.program();
} }
private static void mayBeGenerateTests(@NotNull Collection<JetFile> 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) { private static void performSimpleNameMangling(@NotNull JsProgram program) {
JsNamer namer = new JsPrettyNamer(); JsNamer namer = new JsPrettyNamer();
namer.exec(program); namer.exec(program);
@@ -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();
}
@@ -16,22 +16,25 @@
package org.jetbrains.k2js.translate.test; 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.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.annotations.NotNull;
import org.jetbrains.k2js.translate.context.TranslationContext;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
*/ */
public final class PlainAssertionTester extends JSTester { public final class JSRhinoUnitTester extends CommonUnitTester {
public PlainAssertionTester(@NotNull JsBlock block, @NotNull TranslationContext context) {
super(block, context); public static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("JsTests.test");
public JSRhinoUnitTester() {
super();
} }
@NotNull
@Override @Override
public void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name) { protected JsExpression getTestMethodRef() {
block.getStatements().add(call.makeStmt()); return TEST_FUN_REF;
} }
} }
@@ -16,7 +16,6 @@
package org.jetbrains.k2js.translate.test; 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.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNew; import com.google.dart.compiler.backend.js.ast.JsNew;
import com.google.dart.compiler.backend.js.ast.JsStringLiteral; import com.google.dart.compiler.backend.js.ast.JsStringLiteral;
@@ -42,10 +41,9 @@ public final class JSTestGenerator {
} }
public static void generateTestCalls(@NotNull TranslationContext context, public static void generateTestCalls(@NotNull TranslationContext context,
@NotNull Collection<JetFile> files, @NotNull Collection<JetFile> files, @NotNull JSTester tester) {
@NotNull JsBlock block) {
List<FunctionDescriptor> functionDescriptors = JetTestFunctionDetector.getTestFunctionDescriptors(context.bindingContext(), files); List<FunctionDescriptor> functionDescriptors = JetTestFunctionDetector.getTestFunctionDescriptors(context.bindingContext(), files);
doGenerateTestCalls(functionDescriptors, context, new PlainAssertionTester(block, context)); doGenerateTestCalls(functionDescriptors, context, tester);
} }
private static void doGenerateTestCalls(@NotNull List<FunctionDescriptor> functionDescriptors, private static void doGenerateTestCalls(@NotNull List<FunctionDescriptor> functionDescriptors,
@@ -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.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsStringLiteral; import com.google.dart.compiler.backend.js.ast.JsStringLiteral;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.context.TranslationContext;
/** /**
@@ -27,15 +28,38 @@ import org.jetbrains.k2js.translate.context.TranslationContext;
*/ */
public abstract class JSTester { public abstract class JSTester {
@NotNull @Nullable
protected final JsBlock block; private JsBlock block;
@NotNull
protected final TranslationContext context;
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.block = block;
this.context = context; this.context = context;
} }
public abstract void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name); public void deinitialize() {
this.block = null;
this.context = null;
}
} }
@@ -16,30 +16,24 @@
package org.jetbrains.k2js.translate.test; 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 com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull; 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 * @author Pavel Talanov
*/ */
public final class QUnitTester extends JSTester { public final class QUnitTester extends CommonUnitTester {
@NotNull @NotNull
private static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("QUnit.test"); private static final JsNameRef TEST_FUN_REF = AstUtil.newQualifiedNameRef("QUnit.test");
public QUnitTester(@NotNull JsBlock block, @NotNull TranslationContext context) { public QUnitTester() {
super(block, context); super();
} }
@Override @NotNull
public void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall, protected JsExpression getTestMethodRef() {
@NotNull JsStringLiteral testName) { return TEST_FUN_REF;
JsFunction functionToTest = new JsFunction(this.context.jsScope());
functionToTest.setBody(newBlock(functionToTestCall.makeStmt()));
block.getStatements().add(newInvocation(TEST_FUN_REF, testName, functionToTest).makeStmt());
} }
} }
@@ -16,7 +16,7 @@
/*Asserter*/ /*Asserter*/
var JsTests = (function() { var JsTests = (function() {
var out = null; var out = Kotlin.System.out();
function setOut(newOut) { function setOut(newOut) {
out = newOut; out = newOut;
} }
@@ -28,7 +28,7 @@ var JsTests = (function() {
} }
}; };
this.assertEquals = function (message, actual) { this.assertEquals = function (message, actual) {
throw null;
}; };
}; };