Compare commits

...

4 Commits

Author SHA1 Message Date
Anton Bannykh
8e42071b7d + kotlin.test.{Test, Ignore}; Check FqName 2017-05-26 21:41:45 +03:00
Anton Bannykh
0f61525851 kinda works 2017-05-25 20:56:09 +03:00
Anton Bannykh
30f82f665a wip 2017-05-24 17:11:47 +03:00
Anton Bannykh
cee61da368 wip 2017-05-22 16:23:23 +03:00
13 changed files with 287 additions and 316 deletions

View File

@@ -575,6 +575,17 @@ public class DescriptorUtils {
@NotNull
public static FunctionDescriptor getFunctionByName(@NotNull MemberScope scope, @NotNull Name name) {
FunctionDescriptor result = getFunctionByNameOrNull(scope, name);
if (result == null) {
throw new IllegalStateException("Function not found");
}
return result;
}
@Nullable
public static FunctionDescriptor getFunctionByNameOrNull(@NotNull MemberScope scope, @NotNull Name name) {
Collection<DeclarationDescriptor> functions = scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS,
MemberScope.Companion.getALL_NAME_FILTER());
for (DeclarationDescriptor d : functions) {
@@ -583,7 +594,7 @@ public class DescriptorUtils {
}
}
throw new IllegalStateException("Function not found");
return null;
}
@NotNull

View File

@@ -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);
}
}

View File

@@ -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;
@@ -392,8 +390,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();
}

View File

@@ -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();
}

View File

@@ -1,68 +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.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 = context.program().getStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName());
tester.constructTestMethodInvocation(functionToTestCall, testName);
}
}

View File

@@ -0,0 +1,122 @@
/*
* 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.descriptors.annotations.AnnotationDescriptor
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
//TODO: use method object instead of static functions
class JSTestGenerator(val context: TranslationContext) {
private fun findFunction(name: String): JsExpression {
for (d in context.config.moduleDescriptors) {
if ("<kotlin-test>" == d.name) {
val descriptor = DescriptorUtils.getFunctionByNameOrNull(d.data.getPackage(FqName.ROOT).memberScope,
Name.identifier(name)) ?: continue
return context.getQualifiedReference(descriptor)
}
}
return JsNameRef(name, JsNameRef("Kotlin"))
}
private val suiteRef: JsExpression = findFunction("suite")
private val testRef: JsExpression = findFunction("test")
private val ignoreRef: JsExpression = findFunction("ignore")
fun generateTestCalls(moduleDescriptor: ModuleDescriptor) = generateTestCalls(moduleDescriptor, FqName.ROOT)
private fun generateTestCalls(moduleDescriptor: ModuleDescriptor, packageName: FqName) {
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)
}
}
}
for (subpackageName in moduleDescriptor.getSubPackagesOf(packageName, MemberScope.ALL_NAME_FILTER)) {
generateTestCalls(moduleDescriptor, subpackageName)
}
}
private fun generateTestFunctions(classDescriptor: ClassDescriptor) {
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 && isTest(it)) {
generateCodeForTestMethod(it, classDescriptor, suiteFunction)
}
}
if (!suiteFunction.body.isEmpty) {
val suiteName = context.program().getStringLiteral(classDescriptor.name.toString())
context.addTopLevelStatement(JsInvocation(suiteRef, suiteName, suiteFunction).makeStmt())
}
}
private fun generateCodeForTestMethod(functionDescriptor: FunctionDescriptor, classDescriptor: ClassDescriptor, parentFun: JsFunction) {
val functionToTest = generateTestFunction(functionDescriptor, classDescriptor, parentFun.scope)
val ref = if (isIgnore(functionDescriptor)) ignoreRef else testRef
val testName = context.program().getStringLiteral(functionDescriptor.name.toString())
parentFun.body.statements += JsInvocation(ref, testName, 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
}
/**
* JUnit3 style:
* if (function.getName().startsWith("test")) {
* List<JetParameter> parameters = function.getValueParameters();
* return parameters.size() == 0;
* }
*/
private fun isTest(functionDescriptor: FunctionDescriptor)
= functionDescriptor.annotations.any(annotationFinder("Test", "kotlin.test.Test"))
private fun isIgnore(functionDescriptor: FunctionDescriptor)
= functionDescriptor.annotations.any(annotationFinder("Ignore", "kotlin.test.Ignore"))
private fun annotationFinder(shortName: String, fqName: String) = { annotation: AnnotationDescriptor ->
annotation.type.toString() == shortName && run {
val descriptor = annotation.type.constructor.declarationDescriptor
descriptor != null && FqNameUnsafe(fqName) == DescriptorUtils.getFqName(descriptor)
}
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -1,6 +1,5 @@
package foo
fun box(): String {
val s = StringBuilder()
s.append("a")

View File

@@ -0,0 +1,127 @@
/*
* 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.
*/
import kotlin.test.okFun
@JsName("suite")
fun suite(name: String, suiteFn: () -> Unit) {
currentAdapter.suite(name, suiteFn)
}
@JsName("test")
fun test(name: String, testFn: () -> Unit) {
currentAdapter.test(name, testFn)
}
@JsName("ignore")
fun ignore(name: String, testFn: () -> Unit) {
currentAdapter.ignore(name, testFn)
}
internal var currentAdapter: FrameworkAdapter = DefaultAdapters.AUTODETECT
@JsName("setAdapter")
fun setAdapter(adapter: FrameworkAdapter) {
currentAdapter = adapter
}
interface FrameworkAdapter {
fun suite(name: String, suiteFn: () -> Unit)
fun test(name: String, testFn: () -> Unit)
fun ignore(name: String, testFn: () -> Unit)
}
enum class DefaultAdapters : FrameworkAdapter {
QUNIT {
override fun suite(name: String, suiteFn: () -> Unit) {
QUnit.module(name, suiteFn)
}
override fun test(name: String, testFn: () -> Unit) {
QUnit.test(name) { assert ->
okFun = { actual, message -> assert.ok(actual, message) }
testFn()
}
}
override fun ignore(name: String, testFn: () -> Unit) {
QUnit.skip(name) { assert ->
okFun = { actual, message -> assert.ok(actual, message) }
testFn()
}
}
},
JASMINE {
override fun suite(name: String, suiteFn: () -> Unit) {
describe(name, suiteFn)
}
override fun test(name: String, testFn: () -> Unit) {
it(name, testFn)
}
override fun ignore(name: String, testFn: () -> Unit) {
xit(name, testFn)
}
},
AUTODETECT {
private fun detect(): FrameworkAdapter {
if (js("typeof QUnit !== 'undefined'")) {
return QUNIT
}
else if (js("typeof describe === 'function' && typeof it === 'function'")) {
return JASMINE
}
else throw Error("Couldn't detect testing framework")
}
override fun suite(name: String, suiteFn: () -> Unit) {
detect().suite(name, suiteFn)
}
override fun test(name: String, testFn: () -> Unit) {
detect().test(name, testFn)
}
override fun ignore(name: String, testFn: () -> Unit) {
detect().ignore(name, testFn)
}
}
}
/**
* The [QUnit](http://qunitjs.com/) API
*/
external object QUnit {
fun module(name: String, testFn: () -> Unit): Unit
fun test(name: String, testFn: (dynamic) -> Unit): Unit
fun skip(name: String, testFn: (dynamic) -> Unit): Unit
}
/**
* Jasmine/Mocha API
*/
external fun describe(name: String, fn: () -> Unit)
external fun it(name: String, fn: () -> Unit)
external fun xit(name: String, fn: () -> Unit)

View File

@@ -42,24 +42,34 @@ impl fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: Str
/**
* Provides the JS implementation of asserter using [QUnit](http://QUnitjs.com/)
*/
internal impl fun lookupAsserter(): Asserter = qunitAsserter
internal impl fun lookupAsserter(): Asserter = currentAsserter
private val qunitAsserter = QUnitAsserter()
private var currentAsserter: Asserter = qunitAsserter
internal fun withAsserter(asserter: Asserter, fn: () -> Unit) {
val prevAsserter = currentAsserter
fn()
currentAsserter = prevAsserter
}
internal var okFun: (Boolean, String?) -> Unit = { _, _ -> }
// TODO: make object in 1.2
class QUnitAsserter : Asserter {
class QUnitAsserter() : Asserter {
override fun assertTrue(lazyMessage: () -> String?, actual: Boolean) {
assertTrue(actual, lazyMessage())
}
override fun assertTrue(message: String?, actual: Boolean) {
QUnit.ok(actual, message)
okFun(actual, message)
if (!actual) failWithMessage(message)
}
override fun fail(message: String?): Nothing {
QUnit.ok(false, message)
okFun(false, message)
failWithMessage(message)
}
@@ -69,4 +79,4 @@ class QUnitAsserter : Asserter {
else
throw AssertionError(message)
}
}
}

View File

@@ -1,8 +0,0 @@
package QUnit
/**
* The [QUnit](http://qunitjs.com/) API
*/
external fun ok(actual: Boolean, message: String?): Unit

View File

@@ -0,0 +1,9 @@
/**
* Created by user on 5/26/17.
*/
package kotlin.test
annotation class Test
annotation class Ignore