package com.jetbrains.env.python;

import com.google.common.collect.ImmutableSet;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.env.python.console.PyConsoleTask;
import com.jetbrains.env.python.debug.PyEnvTestCase;

import java.util.Set;

/**
 * @author traff
 */
public class IPythonConsoleTest extends PyEnvTestCase {
  public void testQuestion() throws Exception {
    runPythonTest(new IPythonTask() {
      @Override
      public void testing() throws Exception {
        exec("import multiprocessing");
        exec("multiprocessing?");
        waitForOutput("Type:", "module");
      }
    });
  }

  public void testParsing() throws Exception {
    runPythonTest(new IPythonTask() {
      @Override
      public void testing() throws Exception {
        waitForReady();
        addTextToEditor("sys?");
        PsiFile psi =
          PsiDocumentManager.getInstance(getProject()).getPsiFile(getConsoleView().getLanguageConsole().getConsoleEditor().getDocument());
        assertFalse(PsiTreeUtil.hasErrorElements(psi));
      }
    });
  }

  public void testParsingNoIPython() throws Exception {
    runPythonTest(new IPythonTask() {
      @Override
      public void testing() throws Exception {
        waitForReady();
        addTextToEditor("sys?");
        PsiFile psi =
          PsiDocumentManager.getInstance(getProject()).getPsiFile(getConsoleView().getLanguageConsole().getConsoleEditor().getDocument());
        //TreeUtil.ensureParsed(psi.getNode());
        assertTrue(PsiTreeUtil.hasErrorElements(psi));
      }

      @Override
      public Set<String> getTags() {
        return ImmutableSet.of("-ipython");
      }
    });
  }

  private static class IPythonTask extends PyConsoleTask {
    @Override
    public Set<String> getTags() {
      return ImmutableSet.of("ipython");
    }
  }
}

@@ -28,16 +28,103 @@
 ython;%0A%0A
+import com.google.common.base.Function;%0Aimport com.google.common.collect.Collections2;%0A
 import c
@@ -206,16 +206,57 @@
 anager;%0A
+import com.intellij.psi.PsiErrorElement;%0A
 import c
@@ -430,16 +430,172 @@
 stCase;%0A
+import org.hamcrest.Matchers;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.junit.Assert;%0A%0Aimport java.util.Collection;%0Aimport java.util.Collections;
 %0Aimport 
@@ -1355,53 +1355,528 @@
     
-assertFalse(PsiTreeUtil.hasErrorElements(psi)
+Assert.assertThat(%22No errors expected%22, getErrors(psi), Matchers.empty());%0A      %7D%0A    %7D);%0A  %7D%0A%0A  @NotNull%0A  private static Collection%3CString%3E getErrors(PsiFile psi) %7B //TODO: NotNull?%0A    if (!PsiTreeUtil.hasErrorElements(psi)) %7B%0A      return Collections.emptyList();%0A    %7D%0A%0A    return Collections2.transform(PsiTreeUtil.findChildrenOfType(psi, PsiErrorElement.class), new Function%3CPsiErrorElement, String%3E() %7B%0A      @Override%0A      public String apply(PsiErrorElement input) %7B%0A        return input.getErrorDescription(
 );%0A 

Test verbosity increased