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