2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.testIntegration;
19 import com.intellij.openapi.extensions.Extensions;
20 import com.intellij.openapi.util.Comparing;
21 import com.intellij.openapi.util.Pair;
22 import com.intellij.openapi.util.text.StringUtil;
23 import com.intellij.psi.PsiElement;
24 import com.intellij.psi.PsiNamedElement;
25 import com.intellij.psi.codeStyle.NameUtil;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
31 public class TestFinderHelper {
32 public static PsiElement findSourceElement(@NotNull final PsiElement from) {
33 for (TestFinder each : getFinders()) {
34 PsiElement result = each.findSourceElement(from);
35 if (result != null) return result;
40 public static Collection<PsiElement> findTestsForClass(@NotNull final PsiElement element) {
41 Collection<PsiElement> result = new LinkedHashSet<>();
42 for (TestFinder each : getFinders()) {
43 final PsiElement selectedElement = each.findSelectedElement(element);
44 if (selectedElement != null) result.addAll(each.findTestsForClass(selectedElement));
49 public static Collection<PsiElement> findClassesForTest(@NotNull final PsiElement element) {
50 Collection<PsiElement> result = new LinkedHashSet<>();
51 for (TestFinder each : getFinders()) {
52 final PsiElement selectedElement = each.findSelectedElement(element);
53 if (selectedElement != null) result.addAll(each.findClassesForTest(selectedElement));
58 public static boolean isTest(PsiElement element) {
59 if (element == null) return false;
60 for (TestFinder each : getFinders()) {
61 if (each.isTest(element)) return true;
66 public static TestFinder[] getFinders() {
67 return Extensions.getExtensions(TestFinder.EP_NAME);
70 public static Integer calcTestNameProximity(final String className, final String testName) {
71 int posProximity = testName.indexOf(className);
72 int sizeProximity = testName.length() - className.length();
74 return posProximity + sizeProximity;
77 public static List<PsiElement> getSortedElements(final List<Pair<? extends PsiNamedElement, Integer>> elementsWithWeights,
78 final boolean weightsAscending) {
79 return getSortedElements(elementsWithWeights, weightsAscending, null);
82 public static List<PsiElement> getSortedElements(final List<Pair<? extends PsiNamedElement, Integer>> elementsWithWeights,
83 final boolean weightsAscending,
84 @Nullable final Comparator<PsiElement> sameNameComparator) {
85 Collections.sort(elementsWithWeights, (o1, o2) -> {
86 int result = weightsAscending ? o1.second.compareTo(o2.second) : o2.second.compareTo(o1.second);
87 if (result == 0) result = Comparing.compare(o1.first.getName(), o2.first.getName());
88 if (result == 0 && sameNameComparator != null) result = sameNameComparator.compare(o1.first, o2.first);
93 final List<PsiElement> result = new ArrayList<>(elementsWithWeights.size());
94 for (Pair<? extends PsiNamedElement, Integer> each : elementsWithWeights) {
95 result.add(each.first);
101 public static List<Pair<String, Integer>> collectPossibleClassNamesWithWeights(String testName) {
102 String[] words = NameUtil.splitNameIntoWords(testName);
103 List<Pair<String, Integer>> result = new ArrayList<>();
105 for (int from = 0; from < words.length; from++) {
106 for (int to = from; to < words.length; to++) {
107 result.add(new Pair<>(StringUtil.join(words, from, to + 1, ""),
108 words.length - from + to));