mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
tests to community
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package com.intellij.psi.util;
|
||||
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElementFactory;
|
||||
import com.intellij.psi.PsiTypeParameter;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @author dsl
|
||||
*/
|
||||
public class PsiUtilTest extends LightCodeInsightTestCase {
|
||||
public void testTypeParameterIterator() throws Exception {
|
||||
PsiClass classA = createClass("class A<T> {}");
|
||||
final Iterator<PsiTypeParameter> iterator = PsiUtil.typeParametersIterator(classA);
|
||||
compareIterator(new String[]{"T"}, iterator);
|
||||
}
|
||||
|
||||
private static PsiClass createClass(String text) throws IncorrectOperationException {
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(ourProject).getElementFactory();
|
||||
final PsiClass classA = factory.createClassFromText(text, null).getInnerClasses()[0];
|
||||
return classA;
|
||||
}
|
||||
|
||||
public void testTypeParameterIterator1() throws Exception {
|
||||
final PsiClass classA = createClass("class A<T> { class B<X> {}}");
|
||||
final Iterator<PsiTypeParameter> iterator = PsiUtil.typeParametersIterator(classA.getInnerClasses()[0]);
|
||||
compareIterator(new String[]{"X","T"}, iterator);
|
||||
}
|
||||
|
||||
public void testTypeParameterIterator2() throws Exception {
|
||||
final PsiClass classA = createClass("class A<T> { static class B<X> {}}");
|
||||
final Iterator<PsiTypeParameter> iterator = PsiUtil.typeParametersIterator(classA.getInnerClasses()[0]);
|
||||
compareIterator(new String[]{"X"}, iterator);
|
||||
}
|
||||
|
||||
public void testTypeParameterIterator3() throws Exception {
|
||||
final PsiClass classA = createClass("class A<T> { class B<X, Y> {}}");
|
||||
final Iterator<PsiTypeParameter> iterator = PsiUtil.typeParametersIterator(classA.getInnerClasses()[0]);
|
||||
compareIterator(new String[]{"Y", "X", "T"}, iterator);
|
||||
}
|
||||
|
||||
|
||||
private static void compareIterator(String[] expected, Iterator<PsiTypeParameter> it) {
|
||||
final ArrayList<String> actual = new ArrayList<String>();
|
||||
while (it.hasNext()) {
|
||||
PsiTypeParameter typeParameter = it.next();
|
||||
actual.add(typeParameter.getName());
|
||||
}
|
||||
assertEquals(Arrays.asList(expected).toString(), actual.toString());
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
<orderEntry type="module" module-name="relaxng" scope="RUNTIME" />
|
||||
<orderEntry type="module" module-name="spellchecker" scope="RUNTIME" />
|
||||
<orderEntry type="module" module-name="xdebugger-impl" scope="RUNTIME" />
|
||||
<orderEntry type="module" module-name="xml-openapi" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
package com.intellij.psi;
|
||||
|
||||
import com.intellij.mock.MockDocument;
|
||||
import com.intellij.openapi.editor.impl.event.DocumentEventImpl;
|
||||
import com.intellij.psi.impl.TextBlock;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TextBlockTest extends TestCase {
|
||||
private TextBlock myTextBlock;
|
||||
private MockDocument myDocument;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myDocument = new MockDocument();
|
||||
myTextBlock = new TextBlock();
|
||||
}
|
||||
|
||||
public void testIsEmpty_AfterCreate() throws Exception {
|
||||
assertTrue(myTextBlock.isEmpty());
|
||||
}
|
||||
|
||||
public void testTextInserted_Once() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "", "xxx", 1, false));
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(13, myTextBlock.getTextEndOffset());
|
||||
assertEquals(10, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testReset() throws Exception {
|
||||
assertTrue(myTextBlock.isEmpty());
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "", "xxx", 1, false));
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
myTextBlock.clear();
|
||||
assertTrue(myTextBlock.isEmpty());
|
||||
}
|
||||
|
||||
public void testTextInserted_SecondNonAdjFragmentsAfter() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 20, "", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(23, myTextBlock.getTextEndOffset());
|
||||
assertEquals(17, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextInserted_SecondNonAdjFragmentsBefore() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 5, "", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(5, myTextBlock.getStartOffset());
|
||||
assertEquals(16, myTextBlock.getTextEndOffset());
|
||||
assertEquals(10, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextInserted_SecondAdjFragmentsAfter() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 13, "", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(16, myTextBlock.getTextEndOffset());
|
||||
assertEquals(10, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextInserted_SecondAdjFragmentsBefore() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(16, myTextBlock.getTextEndOffset());
|
||||
assertEquals(10, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextDeleted_Once() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(10, myTextBlock.getTextEndOffset());
|
||||
assertEquals(13, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextDeleted_SecondNonAdjFragmentAfter() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 17, "xxx", "", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(17, myTextBlock.getTextEndOffset());
|
||||
assertEquals(23, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextDeleted_SecondNonAdjFragmentBefore() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 5, "xxx", "", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(5, myTextBlock.getStartOffset());
|
||||
assertEquals(7, myTextBlock.getTextEndOffset());
|
||||
assertEquals(13, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextDeleted_SecondAdjFragment() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(10, myTextBlock.getTextEndOffset());
|
||||
assertEquals(16, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToTheSameSize_Once1() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(13, myTextBlock.getTextEndOffset());
|
||||
assertEquals(13, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToTheSameSize_Once2() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 0, "xxx", "yyy", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(0, myTextBlock.getStartOffset());
|
||||
assertEquals(3, myTextBlock.getTextEndOffset());
|
||||
assertEquals(3, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToTheSameSize_SecondNonAdjFragmentAfter() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 20, "xxx", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(23, myTextBlock.getTextEndOffset());
|
||||
assertEquals(23, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToTheSameSize_SecondNonAdjFragmentBefore() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 5, "xxx", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(5, myTextBlock.getStartOffset());
|
||||
assertEquals(13, myTextBlock.getTextEndOffset());
|
||||
assertEquals(13, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToTheSameSize_SecondNonFragmentAfter() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 13, "xxx", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(16, myTextBlock.getTextEndOffset());
|
||||
assertEquals(16, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToTheSameSize_SecondNonFragmentBefore() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "xxx", 1, false));
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 7, "xxx", "xxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(7, myTextBlock.getStartOffset());
|
||||
assertEquals(13, myTextBlock.getTextEndOffset());
|
||||
assertEquals(13, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToLessSize() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "xx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(12, myTextBlock.getTextEndOffset());
|
||||
assertEquals(13, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
|
||||
public void testTextChanged_ChangedToGreaterSize() throws Exception {
|
||||
myTextBlock.documentChanged(new DocumentEventImpl(myDocument, 10, "xxx", "xxxx", 1, false));
|
||||
|
||||
assertTrue(!myTextBlock.isEmpty());
|
||||
assertEquals(10, myTextBlock.getStartOffset());
|
||||
assertEquals(14, myTextBlock.getTextEndOffset());
|
||||
assertEquals(13, myTextBlock.getPsiEndOffset());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package com.intellij.psi.impl.meta;
|
||||
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.filters.ElementFilter;
|
||||
import com.intellij.psi.meta.PsiMetaData;
|
||||
import com.intellij.psi.xml.XmlFile;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.testFramework.LightPlatformTestCase;
|
||||
import com.intellij.testFramework.UsefulTestCase;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class MetaRegistryTest extends LightPlatformTestCase {
|
||||
|
||||
public void testChangingMetaData() throws Throwable {
|
||||
final boolean[] flag = new boolean[]{false};
|
||||
MetaRegistry.addMetadataBinding(new ElementFilter() {
|
||||
@Override
|
||||
public boolean isAcceptable(Object element, PsiElement context) {
|
||||
return flag[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClassAcceptable(Class hintClass) {
|
||||
return true;
|
||||
}
|
||||
}, MyTrueMetaData.class, myTestRootDisposable);
|
||||
MetaRegistry.addMetadataBinding(new ElementFilter() {
|
||||
@Override
|
||||
public boolean isAcceptable(Object element, PsiElement context) {
|
||||
return !flag[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClassAcceptable(Class hintClass) {
|
||||
return true;
|
||||
}
|
||||
}, MyFalseMetaData.class, myTestRootDisposable);
|
||||
|
||||
final XmlTag tag = ((XmlFile)LightPlatformTestCase.createFile("a.xml", "<a/>")).getDocument().getRootTag();
|
||||
UsefulTestCase.assertInstanceOf(tag.getMetaData(), MyFalseMetaData.class);
|
||||
flag[0] = true;
|
||||
new WriteCommandAction(LightPlatformTestCase.getProject()) {
|
||||
@Override
|
||||
protected void run(Result result) throws Throwable {
|
||||
tag.setName("b");
|
||||
}
|
||||
}.execute();
|
||||
UsefulTestCase.assertInstanceOf(tag.getMetaData(), MyTrueMetaData.class);
|
||||
}
|
||||
|
||||
public static class MyAbstractMetaData implements PsiMetaData {
|
||||
private PsiElement myDeclaration;
|
||||
|
||||
@Override
|
||||
public PsiElement getDeclaration() {
|
||||
return myDeclaration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] getDependences() {
|
||||
return new Object[]{myDeclaration};
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNls
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNls
|
||||
public String getName(PsiElement context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(PsiElement element) {
|
||||
myDeclaration = element;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class MyTrueMetaData extends MyAbstractMetaData {}
|
||||
public static class MyFalseMetaData extends MyAbstractMetaData {}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user