Use K2JSTranslator in StdLibTestBase, add copyright to ClassAliasingMap
This commit is contained in:
@@ -17,18 +17,23 @@
|
||||
*/
|
||||
package org.jetbrains.k2js.test.semantics;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.cli.common.ExitCode;
|
||||
import org.jetbrains.jet.cli.js.K2JSCompiler;
|
||||
import org.jetbrains.jet.cli.js.K2JSCompilerArguments;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||
import org.jetbrains.k2js.test.SingleFileTranslationTest;
|
||||
import org.jetbrains.k2js.test.utils.TranslationUtils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("UseOfSystemOutOrSystemErr")
|
||||
abstract class StdLibTestBase extends SingleFileTranslationTest {
|
||||
|
||||
protected StdLibTestBase() {
|
||||
@@ -37,6 +42,44 @@ abstract class StdLibTestBase extends SingleFileTranslationTest {
|
||||
|
||||
protected void performStdLibTest(@NotNull EnumSet<EcmaVersion> ecmaVersions,
|
||||
@NotNull String sourceDir, @NotNull String... stdLibFiles) throws Exception {
|
||||
List<String> files = constructFilesToCompileList(sourceDir, stdLibFiles);
|
||||
compileFiles(ecmaVersions, files);
|
||||
}
|
||||
|
||||
private void compileFiles(@NotNull EnumSet<EcmaVersion> ecmaVersions, @NotNull List<String> files) {
|
||||
List<String> libFiles = createLibFilesList();
|
||||
for (EcmaVersion version : ecmaVersions) {
|
||||
String outputFilePath = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
|
||||
invokeCompiler(files, libFiles, version, outputFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: reuse this in CompileMavenGeneratedJSLibrary
|
||||
private static void invokeCompiler(@NotNull List<String> files, @NotNull List<String> libFiles,
|
||||
@NotNull EcmaVersion version, @NotNull String outputFilePath) {
|
||||
K2JSCompiler compiler = new K2JSCompiler();
|
||||
K2JSCompilerArguments arguments = new K2JSCompilerArguments();
|
||||
arguments.outputFile = outputFilePath;
|
||||
arguments.sourceFiles = ArrayUtil.toStringArray(files);
|
||||
arguments.verbose = true;
|
||||
arguments.libraryFiles = ArrayUtil.toStringArray(libFiles);
|
||||
System.out.println("Compiling with version: " + version + " to: " + arguments.outputFile);
|
||||
ExitCode answer = compiler.exec(System.out, arguments);
|
||||
assertEquals("Compile failed", ExitCode.OK, answer);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> createLibFilesList() {
|
||||
return Lists.transform(Config.LIB_FILE_NAMES, new Function<String, String>() {
|
||||
@Override
|
||||
public String apply(@Nullable String s) {
|
||||
return Config.LIBRARIES_LOCATION + s;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> constructFilesToCompileList(@NotNull String sourceDir, @NotNull String[] stdLibFiles) {
|
||||
List<String> files = Lists.newArrayList();
|
||||
|
||||
File stdlibDir = new File(sourceDir);
|
||||
@@ -45,7 +88,6 @@ abstract class StdLibTestBase extends SingleFileTranslationTest {
|
||||
files.add(new File(stdlibDir, file).getPath());
|
||||
}
|
||||
|
||||
|
||||
// lets add the standard JS library files
|
||||
Iterable<String> names = Config.LIB_FILE_NAMES_DEPENDENT_ON_STDLIB;
|
||||
for (String libFileName : names) {
|
||||
@@ -58,9 +100,6 @@ abstract class StdLibTestBase extends SingleFileTranslationTest {
|
||||
System.out.println("Compiling " + libFileName);
|
||||
files.add(Config.STDLIB_LOCATION + libFileName);
|
||||
}
|
||||
for (EcmaVersion version : ecmaVersions) {
|
||||
String outputFilePath = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
|
||||
TranslationUtils.translateFiles(getProject(), files, outputFilePath, MainCallParameters.noCall(), version);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* 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.declaration;
|
||||
|
||||
import com.google.dart.compiler.backend.js.ast.JsNameRef;
|
||||
|
||||
@@ -44,7 +44,7 @@ import org.jetbrains.k2js.translate.expression.WhenTranslator;
|
||||
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.TestGenerator;
|
||||
import org.jetbrains.k2js.translate.test.JSTestGenerator;
|
||||
import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
||||
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
||||
import org.jetbrains.k2js.translate.utils.dangerous.DangerousData;
|
||||
@@ -186,7 +186,7 @@ public final class Translation {
|
||||
statements.add(statement);
|
||||
}
|
||||
}
|
||||
TestGenerator.generateTestCalls(context, files, rootBlock);
|
||||
JSTestGenerator.generateTestCalls(context, files, rootBlock);
|
||||
performSimpleNameMangling(context.program());
|
||||
return context.program();
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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.*;
|
||||
import com.google.dart.compiler.util.AstUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.general.JetTestFunctionDetector;
|
||||
import org.jetbrains.k2js.translate.reference.CallBuilder;
|
||||
import org.jetbrains.k2js.translate.reference.ReferenceTranslator;
|
||||
import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static com.google.dart.compiler.util.AstUtil.newBlock;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class TestGenerator {
|
||||
private TestGenerator() {
|
||||
}
|
||||
|
||||
public static void generateTestCalls(@NotNull TranslationContext context,
|
||||
@NotNull Collection<JetFile> files,
|
||||
@NotNull JsBlock block) {
|
||||
List<FunctionDescriptor> functionDescriptors = JetTestFunctionDetector.getTestFunctionDescriptors(context.bindingContext(), files);
|
||||
doGenerateTestCalls(functionDescriptors, block, context);
|
||||
}
|
||||
|
||||
private static void doGenerateTestCalls(@NotNull List<FunctionDescriptor> functionDescriptors,
|
||||
@NotNull JsBlock block,
|
||||
@NotNull TranslationContext context) {
|
||||
for (FunctionDescriptor functionDescriptor : functionDescriptors) {
|
||||
ClassDescriptor classDescriptor = JsDescriptorUtils.getContainingClass(functionDescriptor);
|
||||
if (classDescriptor == null) {
|
||||
return;
|
||||
}
|
||||
JsExpression expression = ReferenceTranslator.translateAsFQReference(classDescriptor, context);
|
||||
JsNew constructClassExpr = new JsNew(expression);
|
||||
JsExpression functionToTestCall =
|
||||
CallBuilder.build(context).descriptor(functionDescriptor).receiver(constructClassExpr).translate();
|
||||
JsNameRef qUnitTestFunRef = AstUtil.newQualifiedNameRef("QUnit.test");
|
||||
JsFunction functionToTest = new JsFunction(context.jsScope());
|
||||
functionToTest.setBody(newBlock(functionToTestCall.makeStmt()));
|
||||
String testName = classDescriptor.getName() + "." + functionDescriptor.getName();
|
||||
block.getStatements()
|
||||
.add(AstUtil.newInvocation(qUnitTestFunRef, context.program().getStringLiteral(testName), functionToTest)
|
||||
.makeStmt());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user