package com.jetbrains.python;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.fixtures.LightMarkedTestCase;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.controlflow.ControlFlow;
import com.jetbrains.python.psi.controlflow.Instruction;

import java.io.File;

/**
 * @author oleg
 */
public class PyControlFlowBuilderTest extends LightMarkedTestCase {

  public String getTestDataPath() {
    return PythonTestUtil.getTestDataPath() + "/psi/controlflow/";
  }

  private void doTest() throws Exception {
    final String testName = getTestName(false).toLowerCase();
    configureByFile(testName + ".py");
    final StringBuffer buffer = new StringBuffer();
    final ControlFlow flow = ((PyFile)myFile).getControlFlow();
    final Instruction[] instructions = flow.getInstructions();
    for (Instruction instruction : instructions) {
      buffer.append(instruction).append("\n");
    }
    final String fullPath = getTestDataPath() + testName + ".txt";
    final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(fullPath.replace(File.separatorChar, '/'));
    final String fileText = StringUtil.convertLineSeparators(VfsUtil.loadText(vFile), "\n");
    assertEquals(fileText.trim(), buffer.toString().trim());
  }
  
  public void testFile() throws Exception {
    doTest();
  }
  
  public void testIf() throws Exception {
    doTest();
  }

  public void testFor() throws Exception {
    doTest();
  }

  public void testWhile() throws Exception {
    doTest();
  }

  public void testBreak() throws Exception {
    doTest();
  }

  public void testContinue() throws Exception {
    doTest();
  }

  public void testReturn() throws Exception {
    doTest();
  }

}

@@ -303,24 +303,68 @@
 psi.PyFile;%0A
+import com.jetbrains.python.psi.PyFunction;%0A
 import com.j
@@ -486,16 +486,44 @@
 io.File;
+%0Aimport java.io.IOException;
 %0A%0A/**%0A *
@@ -865,61 +865,8 @@
 %22);%0A
-    final StringBuffer buffer = new StringBuffer();%0A 
    f
@@ -938,958 +938,1480 @@
 nal 
-Instruction%5B%5D instructions = flow.getInstructions();%0A    for (Instruction instruction : instructions) %7B%0A      buffer.append(instruction).append(%22%5Cn%22);%0A    %7D%0A    final String fullPath = getTestDataPath() + testName + %22.txt%22;%0A    final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(fullPath.replace(File.separatorChar, '/'));%0A    final String fileText = StringUtil.convertLineSeparators(VfsUtil.loadText(vFile), %22%5Cn%22);%0A    assertEquals(fileText.trim(), buffer.toString().trim());%0A  %7D%0A  %0A  public void testFile() throws Exception %7B%0A    doTest();%0A  %7D%0A  %0A  public void testIf() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testFor() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testWhile() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testBreak() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testContinue() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testReturn() throws Exception %7B%0A    doTest();%0A  %7D%0A
+String fullPath = getTestDataPath() + testName + %22.txt%22;%0A    check(fullPath, flow);%0A   %7D%0A  %0A  public void testFile() throws Exception %7B%0A    doTest();%0A  %7D%0A  %0A  public void testIf() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testFor() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testWhile() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testBreak() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testContinue() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testReturn() throws Exception %7B%0A    doTest();%0A  %7D%0A%0A  public void testFunction() throws Exception %7B%0A    final String testName = getTestName(false).toLowerCase();%0A    configureByFile(testName + %22.py%22);%0A    final String fullPath = getTestDataPath() + testName + %22.txt%22;%0A    final ControlFlow flow = ((PyFunction)((PyFile)myFile).getStatements().get(0)).getControlFlow();%0A    check(fullPath, flow);%0A  %7D%0A%0A  private void check(final String fullPath, final ControlFlow flow) throws IOException %7B%0A    final StringBuffer buffer = new StringBuffer();%0A    final Instruction%5B%5D instructions = flow.getInstructions();%0A    for (Instruction instruction : instructions) %7B%0A      buffer.append(instruction).append(%22%5Cn%22);%0A    %7D%0A    final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(fullPath.replace(File.separatorChar, '/'));%0A    final String fileText = StringUtil.convertLineSeparators(VfsUtil.loadText(vFile), %22%5Cn%22);%0A    assertEquals(fileText.trim(), buffer.toString().trim());%0A  %7D
 %0A%7D%0A

Support for named parameters in control flow