more tests for XML tags add/remove PSI events

This commit is contained in:
Alexey Kudravtsev
2015-02-13 14:47:36 +03:00
parent fbf31a07f8
commit 3b6ebdeac4
11 changed files with 49 additions and 51 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,7 @@ import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.util.TextRange;
import com.intellij.pom.PomManager;
import com.intellij.pom.PomModel;
import com.intellij.pom.PomModelAspect;
@@ -34,12 +33,8 @@ import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiFileImpl;
import com.intellij.psi.xml.*;
import com.intellij.testFramework.LightCodeInsightTestCase;
import com.intellij.testFramework.PlatformTestUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileNotFoundException;
public class XmlEventsTest extends LightCodeInsightTestCase {
public void test1() throws Exception{
final Listener listener = addPomListener();
@@ -51,7 +46,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
}
});
assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString());
assertEquals("(Attribute \"a\" for tag \"a\" set to \"b\")\n", listener.getEventString());
}
private Listener addPomListener() {
@@ -73,7 +68,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
}
});
assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString());
assertEquals("(text changed to 'aa' was: 'aaa'), (child added to a child: XmlText), (child added to a child: XmlTag:a)\n", listener.getEventString());
}
public void test3() throws Exception{
@@ -87,7 +82,39 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
}
});
assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString());
assertEquals("(text changed to 'aabba' was: 'aaa')\n", listener.getEventString());
}
public void testTagDelete() throws Exception{
final Listener listener = addPomListener();
configureFromFileText("x.xml", "<a>aaa\n<x>xxx</x>\n<y>yyy</y></a>");
final XmlTag x = ((XmlFile)getFile()).getRootTag().findSubTags("x")[0];
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
TextRange range = x.getTextRange();
getEditor().getDocument().deleteString(range.getStartOffset(), range.getEndOffset() + 1); // plus \n
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
}
});
assertEquals("(child removed from a child: XmlTag:x), (child removed from a child: XmlText)\n", listener.getEventString());
}
public void testTagInsert() throws Exception{
final Listener listener = addPomListener();
configureFromFileText("x.xml", "<a>aaa\n<x>xxx</x>\n<y>yyy</y></a>");
final XmlTag x = ((XmlFile)getFile()).getRootTag().findSubTags("x")[0];
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
TextRange range = x.getTextRange();
getEditor().getDocument().insertString(range.getEndOffset() + 1, "<z>zxzz</z>\n"); // plus \n
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
}
});
assertEquals("(child added to a child: XmlText), (child added to a child: XmlTag:z)\n", listener.getEventString());
}
public void test4() throws Exception{
@@ -100,20 +127,20 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
}
});
assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString());
assertEquals("(text changed to 'a a ' was: 'a ')\n", listener.getEventString());
}
public void test5() throws Exception{
final Listener listener = addPomListener();
final XmlTag tagFromText = XmlElementFactory.getInstance(getProject()).createTagFromText("<a>aaa</a>");
WriteCommandAction.runWriteCommandAction(null, new Runnable(){
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
tagFromText.delete();
}
});
assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString());
assertEquals("(Xml document changed)\n", listener.getEventString());
}
public void testBulkUpdate() throws Exception{
@@ -121,7 +148,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
final PsiFile file = createFile("a.xml", "<a/>");
new WriteCommandAction(getProject()) {
@Override
protected void run(Result result) throws Throwable {
protected void run(@NotNull Result result) throws Throwable {
final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
((DocumentEx)document).setInBulkUpdate(true);
document.insertString(0, " ");
@@ -129,7 +156,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
((DocumentEx)document).setInBulkUpdate(false);
}
}.execute();
assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString());
assertEquals("(Xml document changed)\n", listener.getEventString());
}
public void testDocumentChange1() throws Exception{
@@ -137,7 +164,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
final String stringToInsert = "b=\"c\"";
final int positionToInsert = 2;
checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert);
checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert, "(Xml document changed)\n");
}
public void testDocumentChange2() throws Exception{
@@ -145,7 +172,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
final String stringToInsert = "b=\"c\"";
final int positionToInsert = 3;
checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert);
checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert, "(Xml document changed)\n");
}
@@ -154,7 +181,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
final String stringToInsert = "b=\"c\"";
final int positionToInsert = 6;
checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert);
checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert, "(child changed in b child: XmlTag:a)\n");
}
public void testAttributeValueReplace() throws Exception {
@@ -176,7 +203,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
assertEquals("(Attribute \"name\" for tag \"target\" set to \"\"new\"\")\n", listener.getEventString());
}
private void checkEventsByDocumentChange(final String rootTagText, final int positionToInsert, final String stringToInsert)
private void checkEventsByDocumentChange(final String rootTagText, final int positionToInsert, final String stringToInsert, String events)
throws Exception {
final Listener listener = addPomListener();
final XmlTag tagFromText = ((XmlFile)createFile("file.xml", rootTagText)).getDocument().getRootTag();
@@ -191,14 +218,14 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
}
});
assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString());
assertEquals(events, listener.getEventString());
}
private static class Listener implements PomModelListener{
private final XmlAspect myAspect;
private final StringBuffer myBuffer = new StringBuffer();
public Listener(XmlAspect modelAspect) {
private Listener(XmlAspect modelAspect) {
myAspect = modelAspect;
}
@@ -215,30 +242,11 @@ public class XmlEventsTest extends LightCodeInsightTestCase {
return aspect == myAspect;
}
String getEventString(){
private String getEventString(){
return myBuffer.toString();
}
}
private static void assertFileTextEquals(String targetDataName, String treeText) throws Exception {
String fullName = PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/')+ "/xml/tests/testData/psi/events" + File.separatorChar + targetDataName;
try{
String expectedText = loadFile(fullName);
assertEquals(expectedText.trim(), treeText.trim());
}
catch(FileNotFoundException e){
//FileUtil.writeToFile(new File(fullName), StringUtil.convertLineSeparators(treeText).getBytes("UTF-8"));
fail("No output file found. Created file "+fullName);
}
}
protected static String loadFile(String fullName) throws Exception {
String text = FileUtil.loadFile(new File(fullName)).trim();
text = StringUtil.convertLineSeparators(text);
return text;
}
public void testDocumentChange() throws Exception {
final String xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
-1
View File
@@ -1 +0,0 @@
(Attribute "a" for tag "a" set to "b")
-1
View File
@@ -1 +0,0 @@
(text changed to 'aa' was: 'aaa'), (child added to a child: XmlText), (child added to a child: XmlTag:a)
-1
View File
@@ -1 +0,0 @@
(text changed to 'aabba' was: 'aaa')
-1
View File
@@ -1 +0,0 @@
(text changed to 'a a ' was: 'a ')
-1
View File
@@ -1 +0,0 @@
(Xml document changed)
@@ -1 +0,0 @@
(Xml document changed)
@@ -1 +0,0 @@
(Xml document changed)
@@ -1 +0,0 @@
(Xml document changed)
@@ -1 +0,0 @@
(child changed in b child: XmlTag:a)
@@ -1 +0,0 @@
(child changed in html child: XmlText), (child removed from html child: XmlTag:b), (child removed from html child: XmlText)