All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.js.translate.general.JetTestFunctionDetector Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * 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.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.Modality;
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.js.translate.utils.BindingUtils;
import org.jetbrains.kotlin.psi.JetClass;
import org.jetbrains.kotlin.psi.JetDeclaration;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.types.JetType;

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...
            JetType type = annotation.getType();
            String name = type.toString();
            if (name.equals("Test")) {
                return true;
            }
        }

        /*
        if (function.getName().startsWith("test")) {
            List parameters = function.getValueParameters();
            return parameters.size() == 0;
        }
        */
        return false;
    }

    @NotNull
    public static List getTestFunctionDescriptors(
            @NotNull BindingContext bindingContext,
            @NotNull Collection files
    ) {
        List answer = Lists.newArrayList();
        for (JetFile file : files) {
            answer.addAll(getTestFunctions(bindingContext, file.getDeclarations()));
        }
        return answer;
    }

    @NotNull
    private static List getTestFunctions(
            @NotNull BindingContext bindingContext,
            @NotNull List declarations
    ) {
        List answer = Lists.newArrayList();
        for (JetDeclaration declaration : declarations) {
            JetScope scope = null;

            if (declaration instanceof JetClass) {
                JetClass klass = (JetClass) declaration;
                ClassDescriptor classDescriptor = BindingUtils.getClassDescriptor(bindingContext, klass);

                if (classDescriptor.getModality() != Modality.ABSTRACT) {
                    scope = classDescriptor.getDefaultType().getMemberScope();
                }
            }

            if (scope != null) {
                Collection allDescriptors = scope.getAllDescriptors();
                List testFunctions = ContainerUtil.mapNotNull(
                        allDescriptors,
                        new Function() {
                            @Override
                            public FunctionDescriptor fun(DeclarationDescriptor descriptor) {
                                if (descriptor instanceof FunctionDescriptor) {
                                    FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
                                    if (isTest(functionDescriptor)) return functionDescriptor;
                                }

                                return null;
                            }
                        });

                answer.addAll(testFunctions);
            }
        }
        return answer;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy