Fix test: when sorting import candidates use qualified names as tie breaker
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Wed, 28 Sep 2016 17:06:02 +0000 (20:06 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Wed, 28 Sep 2016 17:39:05 +0000 (20:39 +0300)
if lengths of both qualified names are the same. Previously their order
and hence the candidate selected in tests were unpredictable due to
this flaw.

python/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java
python/testData/quickFixes/AddImportQuickFixTest/canonicalNamesFromHigherLevelPackage/main_after.py
python/testSrc/com/jetbrains/python/quickFixes/AddImportQuickFixTest.java

index c60178052609244b845dcd33628cb8690669835b..c97941077f4ff00cf858a902ad24435082331189 100644 (file)
@@ -19,13 +19,13 @@ import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.roots.ProjectFileIndex;
 import com.intellij.openapi.roots.ProjectRootManager;
+import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiFileSystemItem;
 import com.intellij.psi.util.QualifiedName;
-import com.intellij.util.Function;
 import com.intellij.util.containers.ContainerUtil;
 import com.jetbrains.python.psi.*;
 import org.jetbrains.annotations.NotNull;
@@ -160,17 +160,20 @@ public class ImportCandidateHolder implements Comparable<ImportCandidateHolder>
     return sb.toString();
   }
 
-  public int compareTo(@NotNull ImportCandidateHolder rhs) {
+  public int compareTo(@NotNull ImportCandidateHolder other) {
     final int lRelevance = getRelevance();
-    final int rRelevance = rhs.getRelevance();
+    final int rRelevance = other.getRelevance();
     if (rRelevance != lRelevance) {
       return rRelevance - lRelevance;
     }
-    // prefer shorter paths
-    if (myPath != null && rhs.myPath != null) {
-      return myPath.getComponentCount() - rhs.myPath.getComponentCount();
+    if (myPath != null && other.myPath != null) {
+      // prefer shorter paths
+      final int lengthDiff = myPath.getComponentCount() - other.myPath.getComponentCount();
+      if (lengthDiff != 0) {
+        return lengthDiff;
+      }
     }
-    return 0;
+    return Comparing.compare(myPath, other.myPath);
   }
 
   int getRelevance() {
index 7eef13fc19a594dfb05fc7792d94d7e89562cb1f..a24591f8695173c7b3f0dcaf42e565accc329cb9 100644 (file)
@@ -52,7 +52,7 @@ public class AddImportQuickFixTest extends PyQuickFixTestCase {
     doMultiFileAutoImportTest("Import", fix -> {
       final List<ImportCandidateHolder> candidates = fix.getCandidates();
       final List<String> names = ContainerUtil.map(candidates, c -> c.getPresentableText("MyClass"));
-      assertSameElements(names, "foo.MyClass", "bar.MyClass");
+      assertOrderedEquals(names, "bar.MyClass", "foo.MyClass");
       return true;
     });
   }