Remastering of documentation provider

- Remove whole file resolve
- Take already resolved element
- Get kotlin element behind the wrapper directly
- Remove outdated isKotlinDeclaration method
- More tests
This commit is contained in:
Nikolay Krasko
2013-11-11 17:46:10 +04:00
parent 3d3464d163
commit 8f12db341e
17 changed files with 257 additions and 89 deletions
@@ -16,20 +16,32 @@
package org.jetbrains.jet.editor.quickDoc;
import com.beust.jcommander.internal.Lists;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.intellij.codeInsight.documentation.DocumentationManager;
import com.intellij.codeInsight.navigation.CtrlMouseHandler;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.rt.execution.junit.FileComparisonFailure;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.util.ArrayUtil;
import junit.framework.Assert;
import junit.framework.ComparisonFailure;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.InTextDirectivesUtils;
import org.jetbrains.jet.plugin.ProjectDescriptorWithStdlibSources;
import java.io.File;
import java.util.Collection;
import java.util.List;
public abstract class AbstractJetQuickDocProviderTest extends LightCodeInsightFixtureTestCase {
public void doTest(@NotNull String path) throws Exception {
myFixture.configureByFile(path);
myFixture.configureByFiles(ArrayUtil.toStringArray(getTestFiles(path)));
PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset());
assertNotNull("Can't find element at caret in file: " + path, element);
@@ -37,10 +49,35 @@ public abstract class AbstractJetQuickDocProviderTest extends LightCodeInsightFi
DocumentationManager documentationManager = DocumentationManager.getInstance(myFixture.getProject());
PsiElement targetElement = documentationManager.findTargetElement(myFixture.getEditor(), myFixture.getFile());
List<String> directives = InTextDirectivesUtils.findLinesWithPrefixesRemoved(myFixture.getFile().getText(), "INFO:");
assertTrue("Documentation to check should be added to test file with // INFO: directive " + path, 1 == directives.size());
String info = CtrlMouseHandler.getInfo(targetElement, element);
assertEquals(directives.get(0), CtrlMouseHandler.getInfo(targetElement, element));
File testDataFile = new File(path);
String textData = FileUtil.loadFile(testDataFile);
List<String> directives = InTextDirectivesUtils.findLinesWithPrefixesRemoved(textData, "INFO:");
if (directives.isEmpty()) {
throw new FileComparisonFailure(
"'// INFO:' directive was expected",
textData,
textData + "\n\n//INFO: " + info,
testDataFile.getAbsolutePath());
}
else if (directives.size() == 1) {
String expectedInfo = directives.get(0);
// We can avoid testing for too long comments with \n character by placing '...' in test data
if (info != null && expectedInfo.endsWith("...")) {
if (!info.startsWith(StringUtil.trimEnd(expectedInfo, "..."))) {
throw new ComparisonFailure(null, expectedInfo, info);
}
}
else {
Assert.assertEquals(expectedInfo, info);
}
}
else {
Assert.fail("Too many '// INFO:' directives in file " + path);
}
}
@NotNull
@@ -48,4 +85,29 @@ public abstract class AbstractJetQuickDocProviderTest extends LightCodeInsightFi
protected LightProjectDescriptor getProjectDescriptor() {
return ProjectDescriptorWithStdlibSources.INSTANCE;
}
@Nullable
private static Collection<String> getTestFiles(@NotNull String path) {
File testFile = new File(path);
String testFileName = FileUtil.getNameWithoutExtension(testFile);
List<String> filePaths = Lists.newArrayList();
filePaths.add(path);
filePaths.add(checkDataFileWithSuffix(testFile, testFileName, "_Data.kt"));
filePaths.add(checkDataFileWithSuffix(testFile, testFileName, "_Data.java"));
return Collections2.filter(filePaths, Predicates.notNull());
}
private static String checkDataFileWithSuffix(File testFile, String testFileName, String dataFileSuffix) {
String ktDataFileName = testFileName + dataFileSuffix;
File ktDataFile = new File(testFile.getParent(), ktDataFileName);
if (ktDataFile.exists()) {
return FileUtil.normalize(ktDataFile.getPath());
}
return null;
}
}
@@ -33,7 +33,42 @@ import org.jetbrains.jet.editor.quickDoc.AbstractJetQuickDocProviderTest;
@TestMetadata("idea/testData/editor/quickDoc")
public class JetQuickDocProviderTestGenerated extends AbstractJetQuickDocProviderTest {
public void testAllFilesPresentInQuickDoc() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/editor/quickDoc"), Pattern.compile("^(.+)\\.kt$"), true);
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/editor/quickDoc"), Pattern.compile("^([^_]+)\\.[^\\.]*$"), true);
}
@TestMetadata("AtFunctionParameter.kt")
public void testAtFunctionParameter() throws Exception {
doTest("idea/testData/editor/quickDoc/AtFunctionParameter.kt");
}
@TestMetadata("AtTypeParameter.kt")
public void testAtTypeParameter() throws Exception {
doTest("idea/testData/editor/quickDoc/AtTypeParameter.kt");
}
@TestMetadata("AtVariableDeclaration.kt")
public void testAtVariableDeclaration() throws Exception {
doTest("idea/testData/editor/quickDoc/AtVariableDeclaration.kt");
}
@TestMetadata("JavaClassUsedInKotlin.kt")
public void testJavaClassUsedInKotlin() throws Exception {
doTest("idea/testData/editor/quickDoc/JavaClassUsedInKotlin.kt");
}
@TestMetadata("JavaMethodUsedInKotlin.kt")
public void testJavaMethodUsedInKotlin() throws Exception {
doTest("idea/testData/editor/quickDoc/JavaMethodUsedInKotlin.kt");
}
@TestMetadata("KotlinClassUsedFromJava.java")
public void testKotlinClassUsedFromJava() throws Exception {
doTest("idea/testData/editor/quickDoc/KotlinClassUsedFromJava.java");
}
@TestMetadata("KotlinPackageClassUsedFromJava.java")
public void testKotlinPackageClassUsedFromJava() throws Exception {
doTest("idea/testData/editor/quickDoc/KotlinPackageClassUsedFromJava.java");
}
@TestMetadata("MethodFromStdLib.kt")
@@ -56,4 +91,9 @@ public class JetQuickDocProviderTestGenerated extends AbstractJetQuickDocProvide
doTest("idea/testData/editor/quickDoc/OnMethodUsage.kt");
}
@TestMetadata("TopLevelMethodFromJava.java")
public void testTopLevelMethodFromJava() throws Exception {
doTest("idea/testData/editor/quickDoc/TopLevelMethodFromJava.java");
}
}