tests for CCShowPreview
[idea/community.git] / python / educational-core / course-creator / testSrc / com / jetbrains / edu / coursecreator / CCTestCase.java
1 package com.jetbrains.edu.coursecreator;
2
3 import com.intellij.openapi.application.ApplicationManager;
4 import com.intellij.openapi.application.PathManager;
5 import com.intellij.openapi.application.Result;
6 import com.intellij.openapi.command.WriteCommandAction;
7 import com.intellij.openapi.diagnostic.Logger;
8 import com.intellij.openapi.editor.Document;
9 import com.intellij.openapi.editor.EditorFactory;
10 import com.intellij.openapi.fileEditor.FileDocumentManager;
11 import com.intellij.openapi.util.Pair;
12 import com.intellij.openapi.util.TextRange;
13 import com.intellij.openapi.util.io.FileUtil;
14 import com.intellij.openapi.vfs.LocalFileSystem;
15 import com.intellij.openapi.vfs.VirtualFile;
16 import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
17 import com.jetbrains.edu.learning.StudyTaskManager;
18 import com.jetbrains.edu.learning.courseFormat.*;
19 import org.jetbrains.annotations.NotNull;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 public abstract class CCTestCase extends LightPlatformCodeInsightFixtureTestCase {
29   private static final Logger LOG = Logger.getInstance(CCTestCase.class);
30
31   @Override
32   protected String getTestDataPath() {
33     //TODO: rewrite to work for plugin
34     return new File(PathManager.getHomePath(), "community/python/educational-core/course-creator/testData").getPath();
35   }
36
37   @Override
38   protected void setUp() throws Exception {
39     super.setUp();
40     Course course = new Course();
41     course.setName("test course");
42     StudyTaskManager.getInstance(getProject()).setCourse(course);
43
44     Lesson lesson = new Lesson();
45     lesson.setName("lesson1");
46     Task task = new Task();
47     task.setName("task1");
48     task.setIndex(1);
49     lesson.addTask(task);
50     lesson.setIndex(1);
51     course.getLessons().add(lesson);
52     course.setCourseMode(CCUtils.COURSE_MODE);
53     course.initCourse(false);
54     ApplicationManager.getApplication().runWriteAction(new Runnable() {
55       @Override
56       public void run() {
57         try {
58           VirtualFile lesson1 = myFixture.getProject().getBaseDir().createChildDirectory(this, "lesson1");
59           lesson1.createChildDirectory(this, "task1");
60         }
61         catch (IOException e) {
62           //ignore
63         }
64       }
65     });
66
67   }
68
69   protected VirtualFile configureByTaskFile(String name) {
70     VirtualFile file =
71       myFixture.copyFileToProject(name, FileUtil.join(getProject().getBasePath(), "lesson1", "task1", name));
72     myFixture.configureFromExistingVirtualFile(file);
73
74     Document document = FileDocumentManager.getInstance().getDocument(file);
75     Task task = StudyTaskManager.getInstance(getProject()).getCourse().getLessons().get(0).getTaskList().get(0);
76     TaskFile taskFile = new TaskFile();
77     taskFile.setTask(task);
78     task.getTaskFiles().put(name, taskFile);
79     for (AnswerPlaceholder placeholder : getPlaceholders(document, false)) {
80       taskFile.addAnswerPlaceholder(placeholder);
81     }
82     taskFile.sortAnswerPlaceholders();
83     return file;
84   }
85
86   private static List<AnswerPlaceholder> getPlaceholders(Document document, boolean useLength) {
87     final List<AnswerPlaceholder> placeholders = new ArrayList<>();
88     new WriteCommandAction(null) {
89       @Override
90       protected void run(@NotNull Result result) {
91         final String openingTagRx = "<placeholder( taskText=\"(.+?)\")?( possibleAnswer=\"(.+?)\")?>";
92         final String closingTagRx = "</placeholder>";
93         CharSequence text = document.getCharsSequence();
94         final Matcher openingMatcher = Pattern.compile(openingTagRx).matcher(text);
95         final Matcher closingMatcher = Pattern.compile(closingTagRx).matcher(text);
96         int pos = 0;
97         while (openingMatcher.find(pos)) {
98           AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
99           answerPlaceholder.setUseLength(useLength);
100           String taskText = openingMatcher.group(2);
101           if (taskText != null) {
102             answerPlaceholder.setTaskText(taskText);
103             answerPlaceholder.setLength(taskText.length());
104           }
105           String possibleAnswer = openingMatcher.group(4);
106           if (possibleAnswer != null) {
107             answerPlaceholder.setPossibleAnswer(possibleAnswer);
108           }
109           answerPlaceholder.setOffset(openingMatcher.start());
110           if (!closingMatcher.find(openingMatcher.end())) {
111             LOG.error("No matching closing tag found");
112           }
113           if (useLength) {
114             answerPlaceholder.setLength(closingMatcher.start() - openingMatcher.end());
115           } else {
116             if (possibleAnswer == null) {
117               answerPlaceholder.setPossibleAnswer(document.getText(TextRange.create(openingMatcher.end(), closingMatcher.start())));
118             }
119           }
120           document.deleteString(closingMatcher.start(), closingMatcher.end());
121           document.deleteString(openingMatcher.start(), openingMatcher.end());
122           placeholders.add(answerPlaceholder);
123           pos = answerPlaceholder.getOffset() + answerPlaceholder.getRealLength();
124         }
125       }
126     }.execute();
127     return placeholders;
128   }
129
130   public Pair<Document, List<AnswerPlaceholder>> getPlaceholders(String name) {
131     VirtualFile resultFile = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + "/" + name);
132     Document document = FileDocumentManager.getInstance().getDocument(resultFile);
133     Document tempDocument = EditorFactory.getInstance().createDocument(document.getCharsSequence());
134     List<AnswerPlaceholder> placeholders = getPlaceholders(tempDocument, true);
135     return Pair.create(tempDocument, placeholders);
136   }
137 }
138
139