move folding testing support to CodeInsightTestFixture
authorDmitry Jemerov <yole@jetbrains.com>
Sun, 19 Dec 2010 12:19:00 +0000 (15:19 +0300)
committerDmitry Jemerov <yole@jetbrains.com>
Sun, 19 Dec 2010 12:19:00 +0000 (15:19 +0300)
platform/testFramework/src/com/intellij/testFramework/fixtures/CodeInsightTestFixture.java
platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java

index 462be22a2139a6564099860905f9e4ba56ac49e7..5d40d2512048e082b7fb3239f46a495e43e032eb 100644 (file)
@@ -364,4 +364,6 @@ public interface CodeInsightTestFixture extends IdeaProjectTestFixture {
                              boolean searchTextOccurrences);
 
   <T extends PsiElement> T findElementByText(String text, Class<T> elementClass);
+
+  void testFolding(String fileName);
 }
index 658b86ff86c3112aa62ec1cfe440e73c70fb3435..4679f4bf0eb21b748bb65fb3941a5bd635277cf4 100644 (file)
@@ -26,6 +26,7 @@ import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
 import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings;
 import com.intellij.codeInsight.daemon.HighlightDisplayKey;
 import com.intellij.codeInsight.daemon.impl.*;
+import com.intellij.codeInsight.folding.impl.CodeFoldingManagerImpl;
 import com.intellij.codeInsight.highlighting.actions.HighlightUsagesAction;
 import com.intellij.codeInsight.intention.IntentionAction;
 import com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler;
@@ -51,10 +52,7 @@ import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.application.ModalityState;
 import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.WriteCommandAction;
-import com.intellij.openapi.editor.Document;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.editor.EditorFactory;
-import com.intellij.openapi.editor.RangeMarker;
+import com.intellij.openapi.editor.*;
 import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
 import com.intellij.openapi.editor.actionSystem.EditorActionManager;
 import com.intellij.openapi.editor.ex.DocumentEx;
@@ -1529,4 +1527,81 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
   public void canChangeDocumentDuringHighlighting(boolean canI) {
     myAllowDirt = canI;
   }
+
+  private static final String START_FOLD = "<fold\\stext=\'[^\']*\'(\\sexpand=\'[^\']*\')*>";
+  private static final String END_FOLD = "</fold>";
+
+  private class Border implements Comparable<Border> {
+    public static final boolean LEFT = true;
+    public static final boolean RIGHT = false;
+    public boolean mySide;
+    public int myOffset;
+    public String myText;
+    public boolean myIsExpanded;
+
+    private Border(boolean side, int offset, String text, boolean isExpanded) {
+      mySide = side;
+      myOffset = offset;
+      myText = text;
+      myIsExpanded = isExpanded;
+    }
+
+    public boolean isExpanded() {
+      return myIsExpanded;
+    }
+
+    public boolean isSide() {
+      return mySide;
+    }
+
+    public int getOffset() {
+      return myOffset;
+    }
+
+    public String getText() {
+      return myText;
+    }
+
+    public int compareTo(Border o) {
+      return getOffset() < o.getOffset() ? 1 : -1;
+    }
+  }
+  private String getFoldingDescription(@NotNull String content, @NotNull String initialFileName) {
+    configureByText(FileTypeManager.getInstance().getFileTypeByFileName(initialFileName), content);
+    CodeFoldingManagerImpl.getInstance(getProject()).buildInitialFoldings(myEditor);
+
+    final FoldingModel model = myEditor.getFoldingModel();
+    final FoldRegion[] foldingRegions = model.getAllFoldRegions();
+    final List<Border> borders = new LinkedList<Border>();
+
+    for (FoldRegion region : foldingRegions) {
+      borders.add(new Border(Border.LEFT, region.getStartOffset(), region.getPlaceholderText(), region.isExpanded()));
+      borders.add(new Border(Border.RIGHT, region.getEndOffset(), "", region.isExpanded()));
+    }
+    Collections.sort(borders);
+
+    StringBuilder result = new StringBuilder(myEditor.getDocument().getText());
+    for (Border border : borders) {
+      result.insert(border.getOffset(), border.isSide() == Border.LEFT ? "<fold text=\'" + border.getText() + "\'>" : END_FOLD);
+    }
+
+    return result.toString();
+  }
+
+  public void testFolding(final String verificationFileName) {
+    String expectedContent = null;
+    try {
+      expectedContent = new String(FileUtil.loadFileText(new File(verificationFileName)));
+    }
+    catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+    Assert.assertNotNull(expectedContent);
+
+    expectedContent = StringUtil.replace(expectedContent, "\r", "");
+    final String cleanContent = expectedContent.replaceAll(START_FOLD, "").replaceAll(END_FOLD, "");
+    final String actual = getFoldingDescription(cleanContent, verificationFileName);
+
+    assertEquals(expectedContent, actual);
+  }
 }