replace subtaskInfo map with list for stepik
[idea/community.git] / python / educational-core / student / testSrc / com / jetbrains / edu / learning / stepic / StudyStepicFormatTest.java
index 313303990950da5f14683e278f20e2122fe5b05c..f333d28933c5d4e05d315068896d0c206a1335e0 100644 (file)
@@ -1,8 +1,11 @@
 package com.jetbrains.edu.learning.stepic;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.testFramework.PlatformTestUtil;
 import com.intellij.util.containers.ContainerUtil;
+import com.jetbrains.edu.learning.StudySerializationUtils;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
@@ -62,7 +65,33 @@ public class StudyStepicFormatTest {
     assertNotNull(container.courses);
     assertTrue("Incorrect number of courses", container.courses.size() == 4);
     List<CourseInfo> filtered = ContainerUtil.filter(container.courses, info -> EduStepicConnector.canBeOpened(info));
-    assertEquals(ContainerUtil.newArrayList("Adaptive Python", "Introduction to Python", "format2"), ContainerUtil.map(filtered, CourseInfo::getName));
+    assertEquals(ContainerUtil.newArrayList("Adaptive Python", "Introduction to Python", "format2"),
+                 ContainerUtil.map(filtered, CourseInfo::getName));
+  }
+
+  @Test
+  public void placeholderSerialization() throws IOException {
+    final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().
+      registerTypeAdapter(AnswerPlaceholder.class, new StudySerializationUtils.Json.StepicAnswerPlaceholderAdapter()).create();
+    AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
+    answerPlaceholder.setOffset(1);
+    answerPlaceholder.setLength(10);
+    AnswerPlaceholderSubtaskInfo info1 = createSubtaskInfo("type here", "answer1", ContainerUtil.list("hint 1", "hint 2"));
+    AnswerPlaceholderSubtaskInfo info2 = createSubtaskInfo("type here1", "answer2", ContainerUtil.list("hint 11", "hint 22"));
+    answerPlaceholder.setSubtaskInfos(ContainerUtil.newHashMap(ContainerUtil.list(0, 1), ContainerUtil.list(info1, info2)));
+    final String placeholderSerialization = gson.toJson(answerPlaceholder);
+    String expected = FileUtil.loadFile(new File(getTestDataPath(), "placeholder.json"));
+    assertEquals(expected, placeholderSerialization);
+
+  }
+
+  private static AnswerPlaceholderSubtaskInfo createSubtaskInfo(String placeholderText, String possibleAnswer, List<String> hints) {
+    AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
+    info.setPlaceholderText(placeholderText);
+    info.setPossibleAnswer(possibleAnswer);
+    info.setHints(hints);
+    info.setNeedInsertText(true);
+    return info;
   }
 
   @NotNull