Made LibrariesWithSourcesTest possible to be executed with multi-file test library.
This commit is contained in:
@@ -4,6 +4,7 @@ fun foo() {
|
||||
WithInnerAndObject.foo()
|
||||
}
|
||||
|
||||
// library.kt
|
||||
//public class <1>WithInnerAndObject {
|
||||
// class object {
|
||||
// fun <2>foo() {
|
||||
@@ -3,4 +3,5 @@ import testData.libraries.*
|
||||
val color: Color? = Color.RED
|
||||
val rgb = color?.rgb
|
||||
|
||||
// library.kt
|
||||
//public enum class <1><2>Color(val <3>rgb : Int) {
|
||||
@@ -2,5 +2,6 @@ import testData.libraries.*
|
||||
|
||||
val v = 5.filter { it % 2 == 1 }
|
||||
|
||||
// library.kt
|
||||
//public inline fun <T> T.<1>filter(predicate: (T)-> Boolean) : T? = this
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ fun foo() {
|
||||
println(#(1, 2).exProp)
|
||||
}
|
||||
|
||||
// library.kt
|
||||
//public val String.<1>exProp : String
|
||||
//get() {
|
||||
// return this
|
||||
|
||||
@@ -7,6 +7,7 @@ fun foo() {
|
||||
func()
|
||||
}
|
||||
|
||||
// library.kt
|
||||
//public fun <1><2>func(a : Int, b : String = "55") {
|
||||
//}
|
||||
//
|
||||
|
||||
@@ -5,6 +5,7 @@ fun foo() {
|
||||
println(globalValWithGetter)
|
||||
}
|
||||
|
||||
// library.kt
|
||||
//public val <1>globalVal : #(Int, String) = #(239, "239")
|
||||
//
|
||||
//public val <2>globalValWithGetter : Long
|
||||
@@ -5,6 +5,7 @@ fun foo(a : ClassWithAbstractAndOpenMembers) {
|
||||
println(a.abstractVar)
|
||||
}
|
||||
|
||||
// library.kt
|
||||
//public abstract class <1>ClassWithAbstractAndOpenMembers {
|
||||
// public abstract fun abstractFun()
|
||||
// public open fun openFun() {
|
||||
|
||||
@@ -35,18 +35,21 @@ import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
public abstract class AbstractLibrariesTest extends PlatformTestCase {
|
||||
protected static final String PACKAGE = "testData.libraries";
|
||||
protected static final String TEST_DATA_PATH = PluginTestCaseBase.getTestDataPathBase() + "/libraries";
|
||||
protected VirtualFile myLibraryDir;
|
||||
protected VirtualFile libraryDir;
|
||||
protected VirtualFile librarySourceDir;
|
||||
|
||||
protected abstract boolean isWithSources();
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
String libraryDir = FileUtil.getTempDirectory();
|
||||
KotlinCompiler.ExitCode compilerExec = new KotlinCompiler().exec("-src", TEST_DATA_PATH + "/library", "-output", libraryDir);
|
||||
String libraryPath = FileUtil.getTempDirectory();
|
||||
librarySourceDir = LocalFileSystem.getInstance().findFileByPath(TEST_DATA_PATH + "/library");
|
||||
assertNotNull(librarySourceDir);
|
||||
KotlinCompiler.ExitCode compilerExec = new KotlinCompiler().exec("-src", librarySourceDir.getPath(), "-output", libraryPath);
|
||||
assertEquals(KotlinCompiler.ExitCode.OK, compilerExec);
|
||||
myLibraryDir = LocalFileSystem.getInstance().findFileByPath(libraryDir);
|
||||
assertNotNull(myLibraryDir);
|
||||
libraryDir = LocalFileSystem.getInstance().findFileByPath(libraryPath);
|
||||
assertNotNull(libraryDir);
|
||||
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
@@ -54,11 +57,9 @@ public abstract class AbstractLibrariesTest extends PlatformTestCase {
|
||||
ModifiableRootModel moduleModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
|
||||
|
||||
Library.ModifiableModel libraryModel = moduleModel.getModuleLibraryTable().getModifiableModel().createLibrary("myKotlinLib").getModifiableModel();
|
||||
libraryModel.addRoot(myLibraryDir, OrderRootType.CLASSES);
|
||||
libraryModel.addRoot(libraryDir, OrderRootType.CLASSES);
|
||||
if (isWithSources()) {
|
||||
VirtualFile sourceDir = LocalFileSystem.getInstance().findFileByPath(TEST_DATA_PATH + "/library");
|
||||
assert sourceDir != null;
|
||||
libraryModel.addRoot(sourceDir, OrderRootType.SOURCES);
|
||||
libraryModel.addRoot(librarySourceDir, OrderRootType.SOURCES);
|
||||
}
|
||||
libraryModel.commit();
|
||||
|
||||
|
||||
@@ -20,11 +20,15 @@ import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.EditorFactory;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtilCore;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import jet.Tuple2;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.plugin.references.JetPsiReference;
|
||||
|
||||
import java.util.*;
|
||||
@@ -36,8 +40,6 @@ import java.util.*;
|
||||
* @since 3/23/12
|
||||
*/
|
||||
public class LibrariesWithSourcesTest extends AbstractLibrariesTest {
|
||||
|
||||
private VirtualFile librarySourceFile;
|
||||
private VirtualFile userFile;
|
||||
|
||||
public void testEnum() {
|
||||
@@ -69,8 +71,6 @@ public class LibrariesWithSourcesTest extends AbstractLibrariesTest {
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
librarySourceFile = LocalFileSystem.getInstance().findFileByPath(TEST_DATA_PATH + "/library/library.kt");
|
||||
assertNotNull(librarySourceFile);
|
||||
userFile = LocalFileSystem.getInstance().findFileByPath(TEST_DATA_PATH + "/usercode/" + getTestName(false) + ".kt");
|
||||
assertNotNull(userFile);
|
||||
|
||||
@@ -87,14 +87,12 @@ public class LibrariesWithSourcesTest extends AbstractLibrariesTest {
|
||||
PsiReference ref = psiFile.findReferenceAt(offset);
|
||||
if (ref instanceof JetPsiReference && !referenceContainersToReferences.containsKey(ref.getElement())) {
|
||||
PsiElement target = ref.resolve();
|
||||
if (target == null) {
|
||||
continue;
|
||||
}
|
||||
if (target == null) continue;
|
||||
PsiFile targetNavPsiFile = target.getNavigationElement().getContainingFile();
|
||||
if (targetNavPsiFile == null) {
|
||||
continue;
|
||||
}
|
||||
if (librarySourceFile.equals(targetNavPsiFile.getVirtualFile())) {
|
||||
if (targetNavPsiFile == null) continue;
|
||||
VirtualFile targetNavFile = targetNavPsiFile.getVirtualFile();
|
||||
if (targetNavFile == null) continue;
|
||||
if (VfsUtilCore.isAncestor(librarySourceDir, targetNavFile, true)) {
|
||||
referenceContainersToReferences.put(ref.getElement(), (JetPsiReference)ref);
|
||||
}
|
||||
}
|
||||
@@ -102,44 +100,69 @@ public class LibrariesWithSourcesTest extends AbstractLibrariesTest {
|
||||
return referenceContainersToReferences.values();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getRelativePath(@NotNull PsiFile librarySourceFile) {
|
||||
VirtualFile virtualFile = librarySourceFile.getVirtualFile();
|
||||
if (virtualFile == null) {
|
||||
return "";
|
||||
}
|
||||
String relativePath = VfsUtilCore.getRelativePath(virtualFile, librarySourceDir, '/');
|
||||
return relativePath == null ? "" : relativePath;
|
||||
}
|
||||
|
||||
private String getActualAnnotatedLibraryCode() {
|
||||
List<Tuple2<Integer, Integer>> numbersAndOffsets = new ArrayList<Tuple2<Integer, Integer>>();
|
||||
MultiMap<PsiFile, Tuple2<Integer, Integer>> filesToNumbersAndOffsets = new MultiMap<PsiFile, Tuple2<Integer, Integer>>();
|
||||
int refNumber = 1;
|
||||
for (JetPsiReference ref : collectInterestingReferences()) {
|
||||
PsiElement target = ref.resolve();
|
||||
assertNotNull(target);
|
||||
PsiElement navigationElement = target.getNavigationElement();
|
||||
numbersAndOffsets.add(new Tuple2<Integer, Integer>(numbersAndOffsets.size() + 1, navigationElement.getTextOffset()));
|
||||
Tuple2<Integer, Integer> numberAndOffset = new Tuple2<Integer, Integer>(refNumber++, navigationElement.getTextOffset());
|
||||
filesToNumbersAndOffsets.putValue(navigationElement.getContainingFile(), numberAndOffset);
|
||||
}
|
||||
|
||||
Collections.sort(numbersAndOffsets, Collections.reverseOrder(new Comparator<Tuple2<Integer, Integer>>() {
|
||||
List<PsiFile> files = new ArrayList<PsiFile>(filesToNumbersAndOffsets.keySet());
|
||||
Collections.sort(files, new Comparator<PsiFile>() {
|
||||
@Override
|
||||
public int compare(Tuple2<Integer, Integer> t1, Tuple2<Integer, Integer> t2) {
|
||||
int offsets = t1._2.compareTo(t2._2);
|
||||
return offsets == 0 ? t1._1.compareTo(t2._1) : offsets;
|
||||
public int compare(PsiFile o1, PsiFile o2) {
|
||||
return getRelativePath(o1).compareTo(getRelativePath(o2));
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
Document document = FileDocumentManager.getInstance().getDocument(librarySourceFile);
|
||||
assertNotNull(document);
|
||||
StringBuilder result = new StringBuilder(document.getText());
|
||||
for (Tuple2<Integer, Integer> numberOffset : numbersAndOffsets) {
|
||||
result.insert(numberOffset._2, String.format("<%d>", numberOffset._1));
|
||||
}
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (PsiFile file : files) {
|
||||
List<Tuple2<Integer, Integer>> numbersAndOffsets = new ArrayList<Tuple2<Integer, Integer>>(filesToNumbersAndOffsets.get(file));
|
||||
|
||||
if (numbersAndOffsets.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
int minLine = Integer.MAX_VALUE;
|
||||
int maxLine = Integer.MIN_VALUE;
|
||||
for (Tuple2<Integer, Integer> numberOffset : numbersAndOffsets) {
|
||||
int lineNumber = document.getLineNumber(numberOffset._2);
|
||||
minLine = Math.min(minLine, lineNumber);
|
||||
maxLine = Math.max(maxLine, lineNumber);
|
||||
}
|
||||
Collections.sort(numbersAndOffsets, Collections.reverseOrder(new Comparator<Tuple2<Integer, Integer>>() {
|
||||
@Override
|
||||
public int compare(Tuple2<Integer, Integer> t1, Tuple2<Integer, Integer> t2) {
|
||||
int offsets = t1._2.compareTo(t2._2);
|
||||
return offsets == 0 ? t1._1.compareTo(t2._1) : offsets;
|
||||
}
|
||||
}));
|
||||
|
||||
Document annotated = EditorFactory.getInstance().createDocument(result);
|
||||
return annotated.getText().substring(annotated.getLineStartOffset(minLine),
|
||||
annotated.getLineEndOffset(maxLine));
|
||||
Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
|
||||
assertNotNull(document);
|
||||
StringBuilder resultForFile = new StringBuilder(document.getText());
|
||||
for (Tuple2<Integer, Integer> numberOffset : numbersAndOffsets) {
|
||||
resultForFile.insert(numberOffset._2, String.format("<%d>", numberOffset._1));
|
||||
}
|
||||
|
||||
int minLine = Integer.MAX_VALUE;
|
||||
int maxLine = Integer.MIN_VALUE;
|
||||
for (Tuple2<Integer, Integer> numberOffset : numbersAndOffsets) {
|
||||
int lineNumber = document.getLineNumber(numberOffset._2);
|
||||
minLine = Math.min(minLine, lineNumber);
|
||||
maxLine = Math.max(maxLine, lineNumber);
|
||||
}
|
||||
|
||||
Document annotated = EditorFactory.getInstance().createDocument(resultForFile);
|
||||
String filePart = annotated.getText().substring(annotated.getLineStartOffset(minLine),
|
||||
annotated.getLineEndOffset(maxLine));
|
||||
result.append(" ").append(getRelativePath(file)).append("\n");
|
||||
result.append(filePart).append("\n");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private String getExpectedAnnotatedLibraryCode() {
|
||||
|
||||
@@ -125,7 +125,7 @@ public class LibrariesWithoutSourcesTest extends AbstractLibrariesTest {
|
||||
}
|
||||
|
||||
private VirtualFile getClassFile() {
|
||||
VirtualFile packageDir = myLibraryDir.findFileByRelativePath(PACKAGE.replace(".", "/"));
|
||||
VirtualFile packageDir = libraryDir.findFileByRelativePath(PACKAGE.replace(".", "/"));
|
||||
assertNotNull(packageDir);
|
||||
VirtualFile classFile = packageDir.findChild(getTestName(false) + ".class");
|
||||
assertNotNull(classFile);
|
||||
|
||||
Reference in New Issue
Block a user