/*
 * Copyright (c) 2013, Regents of the University of California
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package edu.uci.python.test;

import java.io.*;
import java.nio.file.*;

import static org.junit.Assert.*;
import edu.uci.python.builtins.*;
import edu.uci.python.parser.*;
import edu.uci.python.runtime.*;
import edu.uci.python.runtime.source.*;
import edu.uci.python.shell.*;

public class PythonTests {

    public static void assertPrints(String expected, String source) {
        final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream(byteArray);

        String path = "../../graal/edu.uci.python.test/src/tests";
        InputStream stream = new ByteArrayInputStream(source.getBytes());
        SourceManager sourceManager = new SourceManager(path, "(test)", stream);
        RunScript.runScript(new String[0], getContext(sourceManager, printStream, System.err));
        String result = byteArray.toString().replaceAll("\r\n", "\n");
        assertEquals(expected, result);
    }

    public static String parseTest(String source) {
        final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream(byteArray);

        String path = "../../graal/edu.uci.python.test/src/tests";
        InputStream stream = new ByteArrayInputStream(source.getBytes());
        SourceManager sourceManager = new SourceManager(path, "(test)", stream);
        new CustomConsole().parseFile(getContext(sourceManager, printStream, System.err));
        return byteArray.toString().replaceAll("\r\n", "\n");
    }

    public static void assertError(String expected, String source) {
        final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream(byteArray);
        String error = "no error!";

        try {
            String path = "../../graal/edu.uci.python.test/src/tests";
            InputStream stream = new ByteArrayInputStream(source.getBytes());
            SourceManager sourceManager = new SourceManager(path, "(test)", stream);
            RunScript.runTrowableScript(new String[0], getContext(sourceManager, System.out, printStream));
        } catch (Throwable err) {
            error = err.toString();
        }

        assertEquals(expected, error);
    }

    public static void assertPrints(String expected, Path scriptName) {
        final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream(byteArray);

        Path scriptFile = findTestPath(scriptName);
        InputStream scriptStream = loadTestScript(scriptFile);

        SourceManager sourceManager = new SourceManager(scriptFile.toString(), scriptName.toString(), scriptStream);
        RunScript.runScript(new String[0], getContext(sourceManager, printStream, System.err));
        String result = byteArray.toString().replaceAll("\r\n", "\n");
        assertEquals(expected, result);
    }

    public static String parseTest(Path scriptName) {
        final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream(byteArray);

        Path scriptFile = findTestPath(scriptName);
        InputStream scriptStream = loadTestScript(scriptFile);

        SourceManager sourceManager = new SourceManager(scriptFile.toString(), scriptName.toString(), scriptStream);
        new CustomConsole().parseFile(getContext(sourceManager, printStream, System.err));
        return byteArray.toString().replaceAll("\r\n", "\n");
    }

    private static InputStream loadTestScript(Path scriptFile) {
        try {
            return Files.newInputStream(scriptFile);
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }

    private static Path findTestPath(Path scriptName) {
        String path;

        if (Files.isDirectory(Paths.get("graal/edu.uci.python.test/src/tests"))) {
            path = "graal/edu.uci.python.test/src/tests";
        } else if (Files.isDirectory(Paths.get("../../graal/edu.uci.python.test/src/tests"))) {
            path = "../../graal/edu.uci.python.test/src/tests";
        } else {
            throw new RuntimeException("Unable to locate edu.uci.python.test/src/tests/");
        }

        return Paths.get(path).resolve(scriptName.toString());
    }

    public static PythonContext getContext() {
        PythonOptions opts = new PythonOptions();
        PythonContext context = new PythonContext(opts, new PythonDefaultBuiltinsLookup(), new PythonParserImpl(), null);
        return context;
    }

    public static PythonContext getContext(SourceManager sm, PrintStream stdout, PrintStream stderr) {
        PythonOptions opts = new PythonOptions();
        opts.setStandardOut(stdout);
        opts.setStandardErr(stderr);

        PythonContext context = new PythonContext(opts, new PythonDefaultBuiltinsLookup(), new PythonParserImpl(), sm);
        return context;
    }
}

@@ -3910,1327 +3910,37 @@
 File
- = findTestPath(scriptName);%0A        InputStream scriptStream = loadTestScript(scriptFile);%0A%0A        SourceManager sourceManager = new SourceManager(scriptFile.toString(), scriptName.toString(), scriptStream);%0A        RunScript.runScript(new String%5B0%5D, getContext(sourceManager, printStream, System.err));%0A        String result = byteArray.toString().replaceAll(%22%5Cr%5Cn%22, %22%5Cn%22);%0A        assertEquals(expected, result);%0A    %7D%0A%0A    public static String parseTest(Path scriptName) %7B%0A        final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();%0A        final PrintStream printStream = new PrintStream(byteArray);%0A%0A        Path scriptFile = findTestPath(scriptName);%0A        InputStream scriptStream = loadTestScript(scriptFile);%0A%0A        SourceManager sourceManager = new SourceManager(scriptFile.toString(), scriptName.toString(), scriptStream);%0A        new CustomConsole().parseFile(getContext(sourceManager, printStream, System.err));%0A        return byteArray.toString().replaceAll(%22%5Cr%5Cn%22, %22%5Cn%22);%0A    %7D%0A%0A    private static InputStream loadTestScript(Path scriptFile) %7B%0A        try %7B%0A            return Files.newInputStream(scriptFile);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException();%0A        %7D%0A    %7D%0A%0A    private static Path findTestPath(Path scriptName) %7B%0A        String path;%0A
+;%0A        String path = null;
 %0A   
@@ -4069,32 +4069,105 @@
 est/src/tests%22;%0A
+            scriptFile = Paths.get(path).resolve(scriptName.toString());%0A
         %7D else i
@@ -4302,32 +4302,105 @@
 est/src/tests%22;%0A
+            scriptFile = Paths.get(path).resolve(scriptName.toString());%0A
         %7D else %7B
@@ -4514,60 +4514,499 @@
     
-return Paths.get
+InputStream scriptStream;%0A        try %7B%0A            scriptStream = Files.newInputStream(scriptFile);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException();%0A        %7D%0A        SourceManager sourceManager = new SourceManager
 (path
-).resolve(scriptName.toString()
+, scriptName.toString(), scriptStream);%0A        RunScript.runScript(new String%5B0%5D, getContext(sourceManager, printStream, System.err));%0A        String result = byteArray.toString().replaceAll(%22%5Cr%5Cn%22, %22%5Cn%22);%0A        assertEquals(expected, result
 );%0A 

Fix import test broken by previous commit