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 result.addAll(each.findTestsForClass(element));
48 public static Collection<PsiElement> findClassesForTest(@NotNull final PsiElement element) {
49 Collection<PsiElement> result = new LinkedHashSet<>();
50 for (TestFinder each : getFinders()) {
51 result.addAll(each.findClassesForTest(element));
56 public static boolean isTest(PsiElement element) {
57 if (element == null) return false;
58 for (TestFinder each : getFinders()) {
59 if (each.isTest(element)) return true;
64 public static TestFinder[] getFinders() {
65 return Extensions.getExtensions(TestFinder.EP_NAME);
68 public static Integer calcTestNameProximity(final String className, final String testName) {
69 int posProximity = testName.indexOf(className);
70 int sizeProximity = testName.length() - className.length();
72 return posProximity + sizeProximity;
75 public static List<PsiElement> getSortedElements(final List<Pair<? extends PsiNamedElement, Integer>> elementsWithWeights,
76 final boolean weightsAscending) {
77 return getSortedElements(elementsWithWeights, weightsAscending, null);
80 public static List<PsiElement> getSortedElements(final List<Pair<? extends PsiNamedElement, Integer>> elementsWithWeights,
81 final boolean weightsAscending,
82 @Nullable final Comparator<PsiElement> sameNameComparator) {
83 Collections.sort(elementsWithWeights, (o1, o2) -> {
84 int result = weightsAscending ? o1.second.compareTo(o2.second) : o2.second.compareTo(o1.second);
85 if (result == 0) result = Comparing.compare(o1.first.getName(), o2.first.getName());
86 if (result == 0 && sameNameComparator != null) result = sameNameComparator.compare(o1.first, o2.first);
91 final List<PsiElement> result = new ArrayList<>(elementsWithWeights.size());
92 for (Pair<? extends PsiNamedElement, Integer> each : elementsWithWeights) {
93 result.add(each.first);
99 public static List<Pair<String, Integer>> collectPossibleClassNamesWithWeights(String testName) {
100 String[] words = NameUtil.splitNameIntoWords(testName);
101 List<Pair<String, Integer>> result = new ArrayList<>();
103 for (int from = 0; from < words.length; from++) {
104 for (int to = from; to < words.length; to++) {
105 result.add(new Pair<>(StringUtil.join(words, from, to + 1, ""),
106 words.length - from + to));