JS tests: changes to kotlin.test + the way compiler tests are generated.
This commit is contained in:
-115
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.kotlin.js.translate.general;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.descriptors.*;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter;
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
|
||||
import org.jetbrains.kotlin.types.KotlinType;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helps find functions which are annotated with a @Test annotation from junit
|
||||
*/
|
||||
public class JetTestFunctionDetector {
|
||||
private JetTestFunctionDetector() {
|
||||
}
|
||||
|
||||
private static boolean isTest(@NotNull FunctionDescriptor functionDescriptor) {
|
||||
Annotations annotations = functionDescriptor.getAnnotations();
|
||||
for (AnnotationDescriptor annotation : annotations) {
|
||||
// TODO ideally we should find the fully qualified name here...
|
||||
KotlinType type = annotation.getType();
|
||||
String name = type.toString();
|
||||
if (name.equals("Test")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (function.getName().startsWith("test")) {
|
||||
List<JetParameter> parameters = function.getValueParameters();
|
||||
return parameters.size() == 0;
|
||||
}
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<FunctionDescriptor> getTestFunctionDescriptors(
|
||||
@NotNull ModuleDescriptor moduleDescriptor
|
||||
) {
|
||||
List<FunctionDescriptor> answer = Lists.newArrayList();
|
||||
getTestFunctions(FqName.ROOT, moduleDescriptor, answer);
|
||||
return answer;
|
||||
}
|
||||
|
||||
private static void getTestFunctions(
|
||||
@NotNull FqName packageName,
|
||||
@NotNull ModuleDescriptor moduleDescriptor,
|
||||
@NotNull List<FunctionDescriptor> foundFunctions
|
||||
) {
|
||||
for (PackageFragmentDescriptor packageDescriptor : moduleDescriptor.getPackage(packageName).getFragments()) {
|
||||
if (DescriptorUtils.getContainingModule(packageDescriptor) != moduleDescriptor) continue;
|
||||
Collection<DeclarationDescriptor> descriptors = packageDescriptor.getMemberScope().getContributedDescriptors(
|
||||
DescriptorKindFilter.CLASSIFIERS, MemberScope.Companion.getALL_NAME_FILTER());
|
||||
for (DeclarationDescriptor descriptor : descriptors) {
|
||||
if (descriptor instanceof ClassDescriptor) {
|
||||
getTestFunctions((ClassDescriptor) descriptor, foundFunctions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (FqName subpackageName : moduleDescriptor.getSubPackagesOf(packageName, MemberScope.Companion.getALL_NAME_FILTER())) {
|
||||
getTestFunctions(subpackageName, moduleDescriptor, foundFunctions);
|
||||
}
|
||||
}
|
||||
|
||||
private static void getTestFunctions(
|
||||
@NotNull ClassDescriptor classDescriptor,
|
||||
@NotNull List<FunctionDescriptor> foundFunctions
|
||||
) {
|
||||
if (classDescriptor.getModality() == Modality.ABSTRACT) return;
|
||||
|
||||
|
||||
Collection<DeclarationDescriptor> allDescriptors = classDescriptor.getUnsubstitutedMemberScope().getContributedDescriptors(
|
||||
DescriptorKindFilter.FUNCTIONS, MemberScope.Companion.getALL_NAME_FILTER());
|
||||
List<FunctionDescriptor> testFunctions = ContainerUtil.mapNotNull(
|
||||
allDescriptors,
|
||||
descriptor-> {
|
||||
if (descriptor instanceof FunctionDescriptor) {
|
||||
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
|
||||
if (isTest(functionDescriptor)) return functionDescriptor;
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
|
||||
foundFunctions.addAll(testFunctions);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -39,8 +39,6 @@ import org.jetbrains.kotlin.js.translate.declaration.FileDeclarationVisitor;
|
||||
import org.jetbrains.kotlin.js.translate.expression.ExpressionVisitor;
|
||||
import org.jetbrains.kotlin.js.translate.expression.PatternTranslator;
|
||||
import org.jetbrains.kotlin.js.translate.test.JSTestGenerator;
|
||||
import org.jetbrains.kotlin.js.translate.test.JSTester;
|
||||
import org.jetbrains.kotlin.js.translate.test.QUnitTester;
|
||||
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils;
|
||||
import org.jetbrains.kotlin.js.translate.utils.BindingUtils;
|
||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
|
||||
@@ -395,8 +393,8 @@ public final class Translation {
|
||||
) {
|
||||
StaticContext staticContext = new StaticContext(trace, config, moduleDescriptor);
|
||||
TranslationContext context = TranslationContext.rootContext(staticContext);
|
||||
JSTester tester = new QUnitTester(context);
|
||||
JSTestGenerator.generateTestCalls(context, moduleDescriptor, tester);
|
||||
|
||||
new JSTestGenerator(context).generateTestCalls(moduleDescriptor);
|
||||
|
||||
return staticContext.getFragment();
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.kotlin.js.translate.test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.js.backend.ast.*;
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
|
||||
public abstract class CommonUnitTester extends JSTester {
|
||||
public CommonUnitTester(@NotNull TranslationContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall,
|
||||
@NotNull JsStringLiteral testName) {
|
||||
JsFunction functionToTest = new JsFunction(getContext().scope(), "test function");
|
||||
functionToTest.setBody(new JsBlock(functionToTestCall.makeStmt()));
|
||||
getContext().addTopLevelStatement(new JsInvocation(getTestMethodRef(), testName, functionToTest).makeStmt());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract JsExpression getTestMethodRef();
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.kotlin.js.translate.test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsNew;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsStringLiteral;
|
||||
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
import org.jetbrains.kotlin.js.translate.general.JetTestFunctionDetector;
|
||||
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
//TODO: use method object instead of static functions
|
||||
public final class JSTestGenerator {
|
||||
private JSTestGenerator() {
|
||||
}
|
||||
|
||||
public static void generateTestCalls(@NotNull TranslationContext context,
|
||||
@NotNull ModuleDescriptor moduleDescriptor, @NotNull JSTester tester) {
|
||||
List<FunctionDescriptor> functionDescriptors =
|
||||
JetTestFunctionDetector.getTestFunctionDescriptors(moduleDescriptor);
|
||||
doGenerateTestCalls(functionDescriptors, context, tester);
|
||||
}
|
||||
|
||||
private static void doGenerateTestCalls(@NotNull List<FunctionDescriptor> functionDescriptors,
|
||||
@NotNull TranslationContext context, @NotNull JSTester jsTester) {
|
||||
for (FunctionDescriptor functionDescriptor : functionDescriptors) {
|
||||
ClassDescriptor classDescriptor = DescriptorUtils.getContainingClass(functionDescriptor);
|
||||
if (classDescriptor == null) {
|
||||
return;
|
||||
}
|
||||
generateCodeForTestMethod(context, functionDescriptor, classDescriptor, jsTester);
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateCodeForTestMethod(@NotNull TranslationContext context,
|
||||
@NotNull FunctionDescriptor functionDescriptor,
|
||||
@NotNull ClassDescriptor classDescriptor, @NotNull JSTester tester) {
|
||||
JsExpression expression = ReferenceTranslator.translateAsValueReference(classDescriptor, context);
|
||||
JsNew testClass = new JsNew(expression);
|
||||
JsExpression functionToTestCall =
|
||||
CallTranslator.INSTANCE.buildCall(context, functionDescriptor, Collections.emptyList(), testClass);
|
||||
JsStringLiteral testName = new JsStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName());
|
||||
tester.constructTestMethodInvocation(functionToTestCall, testName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.kotlin.js.translate.test
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.js.backend.ast.*
|
||||
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext
|
||||
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
|
||||
class JSTestGenerator(val context: TranslationContext) {
|
||||
|
||||
fun generateTestCalls(moduleDescriptor: ModuleDescriptor) {
|
||||
generateTestCalls(moduleDescriptor, FqName.ROOT)
|
||||
}
|
||||
|
||||
private fun generateTestCalls(moduleDescriptor: ModuleDescriptor, packageName: FqName) {
|
||||
val packageFunction = JsFunction(context.scope(), JsBlock(), "${packageName.asString()} package suite function")
|
||||
|
||||
for (packageDescriptor in moduleDescriptor.getPackage(packageName).fragments) {
|
||||
if (DescriptorUtils.getContainingModule(packageDescriptor) !== moduleDescriptor) continue
|
||||
|
||||
packageDescriptor.getMemberScope().getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS, MemberScope.ALL_NAME_FILTER).forEach {
|
||||
if (it is ClassDescriptor) {
|
||||
generateTestFunctions(it, packageFunction)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!packageFunction.body.isEmpty) {
|
||||
val suiteName = JsStringLiteral(packageName.asString())
|
||||
context.addTopLevelStatement(JsInvocation(suiteRef, suiteName, JsBooleanLiteral(false), packageFunction).makeStmt())
|
||||
}
|
||||
|
||||
for (subpackageName in moduleDescriptor.getSubPackagesOf(packageName, MemberScope.ALL_NAME_FILTER)) {
|
||||
generateTestCalls(moduleDescriptor, subpackageName)
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateTestFunctions(classDescriptor: ClassDescriptor, parentFun: JsFunction) {
|
||||
if (classDescriptor.modality === Modality.ABSTRACT) return
|
||||
|
||||
val suiteFunction = JsFunction(context.scope(), JsBlock(), "suite function")
|
||||
|
||||
classDescriptor.unsubstitutedMemberScope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS, MemberScope.ALL_NAME_FILTER).forEach {
|
||||
if (it is FunctionDescriptor && it.isTest) {
|
||||
generateCodeForTestMethod(it, classDescriptor, suiteFunction)
|
||||
}
|
||||
}
|
||||
|
||||
if (!suiteFunction.body.isEmpty) {
|
||||
val suiteName = JsStringLiteral(classDescriptor.name.toString())
|
||||
|
||||
parentFun.body.statements += JsInvocation(suiteRef, suiteName, JsBooleanLiteral(classDescriptor.isIgnored), suiteFunction).makeStmt()
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateCodeForTestMethod(functionDescriptor: FunctionDescriptor, classDescriptor: ClassDescriptor, parentFun: JsFunction) {
|
||||
val functionToTest = generateTestFunction(functionDescriptor, classDescriptor, parentFun.scope)
|
||||
|
||||
val testName = JsStringLiteral(functionDescriptor.name.toString())
|
||||
parentFun.body.statements += JsInvocation(testRef, testName, JsBooleanLiteral(functionDescriptor.isIgnored), functionToTest).makeStmt()
|
||||
}
|
||||
|
||||
private fun generateTestFunction(functionDescriptor: FunctionDescriptor, classDescriptor: ClassDescriptor, scope: JsScope): JsFunction {
|
||||
val expression = ReferenceTranslator.translateAsValueReference(classDescriptor, context)
|
||||
val testClass = JsNew(expression)
|
||||
val functionToTestCall = CallTranslator.buildCall(context, functionDescriptor, emptyList<JsExpression>(), testClass)
|
||||
val functionToTest = JsFunction(scope, "test function")
|
||||
functionToTest.body = JsBlock(functionToTestCall.makeStmt())
|
||||
|
||||
return functionToTest
|
||||
}
|
||||
|
||||
private val suiteRef: JsExpression by lazy { findFunction("suite") }
|
||||
private val testRef: JsExpression by lazy { findFunction("test") }
|
||||
|
||||
private fun findFunction(name: String): JsExpression {
|
||||
val descriptor = DescriptorUtils.getFunctionByNameOrNull(
|
||||
context.currentModule.getPackage(FqNameUnsafe("kotlin.test").toSafe()).memberScope,
|
||||
Name.identifier(name)) ?: return JsNameRef(name, JsNameRef("Kotlin"))
|
||||
return ReferenceTranslator.translateAsValueReference(descriptor, context)
|
||||
}
|
||||
|
||||
private val FunctionDescriptor.isTest
|
||||
get() = annotationFinder("Test", "kotlin.test")
|
||||
|
||||
private val DeclarationDescriptor.isIgnored
|
||||
get() = annotationFinder("Ignore", "kotlin.test")
|
||||
|
||||
private fun DeclarationDescriptor.annotationFinder(shortName: String, vararg packages: String) = packages.any { packageName ->
|
||||
annotations.hasAnnotation(FqName("$packageName.$shortName"))
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.kotlin.js.translate.test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsStringLiteral;
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
|
||||
public abstract class JSTester {
|
||||
|
||||
@NotNull
|
||||
private final TranslationContext context;
|
||||
|
||||
public JSTester(@NotNull TranslationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public abstract void constructTestMethodInvocation(@NotNull JsExpression call, @NotNull JsStringLiteral name);
|
||||
|
||||
@NotNull
|
||||
protected TranslationContext getContext() {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.kotlin.js.translate.test;
|
||||
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsNameRef;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
|
||||
public final class QUnitTester extends CommonUnitTester {
|
||||
public QUnitTester(@NotNull TranslationContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final JsNameRef TEST_FUN_REF = new JsNameRef("test", "QUnit");
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected JsExpression getTestMethodRef() {
|
||||
return TEST_FUN_REF;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user