add/delete answer placeholder tests
[idea/community.git] / python / educational-core / course-creator / testSrc / com / jetbrains / edu / coursecreator / CCTestCase.java
index d1756f2d31e3a57846965bdd19d020fea1931edc..9f1fd16ca1731e2dfac10b6ef52d4ef4d7d803e6 100644 (file)
@@ -7,16 +7,21 @@ import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.EditorFactory;
+import com.intellij.openapi.editor.markup.MarkupModel;
+import com.intellij.openapi.editor.markup.RangeHighlighter;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.util.Pair;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.testFramework.EditorTestUtil;
 import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
 import com.jetbrains.edu.learning.StudyTaskManager;
 import com.jetbrains.edu.learning.courseFormat.*;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.junit.ComparisonFailure;
 
 import java.io.File;
 import java.io.IOException;
@@ -28,6 +33,43 @@ import java.util.regex.Pattern;
 public abstract class CCTestCase extends LightPlatformCodeInsightFixtureTestCase {
   private static final Logger LOG = Logger.getInstance(CCTestCase.class);
 
+  @Nullable
+  public static RangeHighlighter getHighlighter(MarkupModel model, AnswerPlaceholder placeholder) {
+    for (RangeHighlighter highlighter : model.getAllHighlighters()) {
+      int endOffset = placeholder.getOffset() + placeholder.getRealLength();
+      if (highlighter.getStartOffset() == placeholder.getOffset() && highlighter.getEndOffset() == endOffset) {
+        return highlighter;
+      }
+    }
+    return null;
+  }
+
+  protected static void checkHighlighters(TaskFile taskFile, MarkupModel markupModel) {
+    for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
+      if (getHighlighter(markupModel, answerPlaceholder) == null) {
+        throw new AssertionError("No highlighter for placeholder: " + CCTestsUtil.getPlaceholderPresentation(answerPlaceholder));
+      }
+    }
+  }
+
+  public void checkByFile(TaskFile taskFile, String fileName, boolean useLength) {
+    Pair<Document, List<AnswerPlaceholder>> placeholders = getPlaceholders(fileName, useLength, true);
+    String message = "Placeholders don't match";
+    if (taskFile.getAnswerPlaceholders().size() != placeholders.second.size()) {
+      throw new ComparisonFailure(message,
+                                  CCTestsUtil.getPlaceholdersPresentation(taskFile.getAnswerPlaceholders()),
+                                  CCTestsUtil.getPlaceholdersPresentation(placeholders.second));
+    }
+    for (AnswerPlaceholder answerPlaceholder : placeholders.getSecond()) {
+      AnswerPlaceholder placeholder = taskFile.getAnswerPlaceholder(answerPlaceholder.getOffset());
+      if (!CCTestsUtil.comparePlaceholders(placeholder, answerPlaceholder)) {
+        throw new ComparisonFailure(message,
+                                    CCTestsUtil.getPlaceholdersPresentation(taskFile.getAnswerPlaceholders()),
+                                    CCTestsUtil.getPlaceholdersPresentation(placeholders.second));
+      }
+    }
+  }
+
   @Override
   protected String getTestDataPath() {
     //TODO: rewrite to work for plugin
@@ -63,7 +105,6 @@ public abstract class CCTestCase extends LightPlatformCodeInsightFixtureTestCase
         }
       }
     });
-
   }
 
   protected VirtualFile configureByTaskFile(String name) {
@@ -88,7 +129,7 @@ public abstract class CCTestCase extends LightPlatformCodeInsightFixtureTestCase
     new WriteCommandAction(null) {
       @Override
       protected void run(@NotNull Result result) {
-        final String openingTagRx = "<placeholder( taskText=\"(.+?)\")?( possibleAnswer=\"(.+?)\")?>";
+        final String openingTagRx = "<placeholder( taskText=\"(.+?)\")?( possibleAnswer=\"(.+?)\")?( hint=\"(.+?)\")?>";
         final String closingTagRx = "</placeholder>";
         CharSequence text = document.getCharsSequence();
         final Matcher openingMatcher = Pattern.compile(openingTagRx).matcher(text);
@@ -106,6 +147,10 @@ public abstract class CCTestCase extends LightPlatformCodeInsightFixtureTestCase
           if (possibleAnswer != null) {
             answerPlaceholder.setPossibleAnswer(possibleAnswer);
           }
+          String hint = openingMatcher.group(6);
+          if (hint != null) {
+            answerPlaceholder.setHint(hint);
+          }
           answerPlaceholder.setOffset(openingMatcher.start());
           if (!closingMatcher.find(openingMatcher.end())) {
             LOG.error("No matching closing tag found");
@@ -128,12 +173,24 @@ public abstract class CCTestCase extends LightPlatformCodeInsightFixtureTestCase
   }
 
   public Pair<Document, List<AnswerPlaceholder>> getPlaceholders(String name) {
+    return getPlaceholders(name, true, false);
+  }
+
+  public Pair<Document, List<AnswerPlaceholder>> getPlaceholders(String name, boolean useLength, boolean removeMarkers) {
     VirtualFile resultFile = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + "/" + name);
     Document document = FileDocumentManager.getInstance().getDocument(resultFile);
     Document tempDocument = EditorFactory.getInstance().createDocument(document.getCharsSequence());
-    List<AnswerPlaceholder> placeholders = getPlaceholders(tempDocument, true);
+    if (removeMarkers) {
+      EditorTestUtil.extractCaretAndSelectionMarkers(tempDocument);
+    }
+    List<AnswerPlaceholder> placeholders = getPlaceholders(tempDocument, useLength);
     return Pair.create(tempDocument, placeholders);
   }
+
+  @Override
+  protected boolean shouldContainTempFiles() {
+    return false;
+  }
 }