From 2a014c660b71443a9fe1b80942c2bd4c05f9ee73 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Fri, 1 Jun 2012 12:53:15 +0400 Subject: [PATCH] moving xml-tests to community: getting rid of ultimate dependencies --- .../intellij/psi/AbstractReparseTestCase.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 java/testFramework/src/com/intellij/psi/AbstractReparseTestCase.java diff --git a/java/testFramework/src/com/intellij/psi/AbstractReparseTestCase.java b/java/testFramework/src/com/intellij/psi/AbstractReparseTestCase.java new file mode 100644 index 000000000000..43bf6ccc20ec --- /dev/null +++ b/java/testFramework/src/com/intellij/psi/AbstractReparseTestCase.java @@ -0,0 +1,105 @@ +package com.intellij.psi; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.command.CommandProcessor; +import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.fileTypes.FileType; +import com.intellij.psi.impl.DebugUtil; +import com.intellij.psi.impl.source.SourceTreeToPsiMap; +import com.intellij.psi.text.BlockSupport; +import com.intellij.testFramework.PsiTestCase; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NonNls; + +/** + * @author maxim + */ +public abstract class AbstractReparseTestCase extends PsiTestCase { + protected FileType myFileType; + protected PsiFile myDummyFile; + private int myInsertOffset; + + protected void setFileType(final FileType fileType) { + myFileType = fileType; + } + + protected void insert(@NonNls final String s) throws IncorrectOperationException { + CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { + @Override + public void run() { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + String oldText = myDummyFile.getText(); + String expectedNewText = oldText.substring(0, myInsertOffset) + s + oldText.substring(myInsertOffset); + + try { + doReparse(s, expectedNewText, 0); + } + catch (IncorrectOperationException e) { + LOG.error(e); + } + myInsertOffset += s.length(); + } + }); + } + }, "asd", null); + } + + protected void moveEditPointLeft(int count) { + myInsertOffset -= count; + } + + protected void moveEditPointRight(int count) { + myInsertOffset += count; + } + + protected void setEditPoint(int pos) { + myInsertOffset = pos; + } + + protected void remove(int count) throws IncorrectOperationException { + String oldText = myDummyFile.getText(); + String expectedNewText = oldText.substring(0, myInsertOffset-count) + oldText.substring(myInsertOffset); + + doReparse("", expectedNewText, count); + myInsertOffset -= count; + } + + protected void doReparse(final String s, final String expectedNewText, final int length) throws IncorrectOperationException { + CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { + @Override + public void run() { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + BlockSupport blockSupport = ServiceManager.getService(myProject, BlockSupport.class); + try { + blockSupport.reparseRange(myDummyFile, myInsertOffset - length, myInsertOffset, s); + String foundStructure = DebugUtil.treeToString(SourceTreeToPsiMap.psiElementToTree(myDummyFile), false); + final PsiFile psiFile = createDummyFile(getName() + "." + myFileType.getDefaultExtension(), expectedNewText); + String expectedStructure = DebugUtil.treeToString(SourceTreeToPsiMap.psiElementToTree(psiFile), false); + if (!expectedStructure.equals(foundStructure)) { + System.out.println("expected: "); + System.out.println(expectedStructure); + System.out.println("found: "); + System.out.println(foundStructure); + assertEquals(expectedStructure, foundStructure); + } + + assertEquals("Reparse tree should be equal to the document",expectedNewText,myDummyFile.getText()); + } + catch (IncorrectOperationException e) { + LOG.error(e); + } + } + }); + } + }, "asd", null); + } + + protected void prepareFile(@NonNls String prefix, @NonNls String suffix) throws IncorrectOperationException { + myDummyFile = createDummyFile(getName() + "." + myFileType.getDefaultExtension(), prefix + suffix); + myInsertOffset = prefix.length(); + } +}