package test.jycessing;

import static junit.framework.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.JUnitCore;

import jycessing.Runner;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class JycessingTests {

    private static String run(final String testResource) throws Exception {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PrintStream saved = System.out;
        try {
            System.setOut(new PrintStream(baos, true));
            Runner.main(new String[] { "testing/test_resources/test_"
                                       + testResource + ".py" });
            Thread.sleep(500);
            return new String(baos.toByteArray()).replaceAll("\r\n", "\n")
                                                 .replaceAll("\r", "\n");
        } finally {
            System.setOut(saved);
        }
    }

    private static void testImport(final String module) throws Exception {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PrintStream saved = System.out;
        try {
            System.setOut(new PrintStream(baos, true));
            final String testClass = module + "_test";
            final String bogusFileName = "<test " + module + ">";
            final String testText = "import " + module + "\nprint 'OK'";
            Runner.runSketch(new String[] { testClass }, bogusFileName,
                             testText);
            assertEquals("OK\n",
                         new String(baos.toByteArray()).replaceAll("\r\n", "\n")
                                                       .replaceAll("\r", "\n"));
        } finally {
            System.setOut(saved);
        }
    }

    @Test
    public void inherit_str() throws Exception {
        assertEquals("cosmic\n12\n[12, 13]\n", run("inherit_str"));
    }

    @Test
    public void static_size() throws Exception {
        assertEquals("OK\n", run("static_size"));
    }

    @Test
    public void set_builtins() throws Exception {
        assertEquals("128\nset(['banana'])\nissubclass: True\nset(['baz'])\n",
                     run("set"));
    }

    @Test
    public void map_builtins() throws Exception {
        assertEquals("50\n13\n", run("map"));
    }

    @Test
    public void md5() throws Exception {
        assertEquals("bb649c83dd1ea5c9d9dec9a18df0ffe9\n", run("md5"));
    }

    @Test
    public void urllib2() throws Exception {
        testImport("urllib2");
    }

    @Test
    public void urllib() throws Exception {
        testImport("urllib");
    }

    @Test
    public void load_in_initializer() throws Exception {
        assertEquals("OK\n", run("load_in_initializer"));
    }

    @Test
    public void datetime() throws Exception {
        testImport("datetime");
    }

    @Test
    public void calendar() throws Exception {
        testImport("calendar");
    }

    @Test
    public void processing_core() throws Exception {
        assertEquals("[ 1.0, 2.0, 3.0 ]\n<type 'processing.core.PFont'>\n",
                     run("pcore"));
    }

    @Test
    public void pvector() throws Exception {
        assertEquals("OK\n", run("pvector"));
    }

    public static void main(final String[] args) {
        JUnitCore.runClasses(JycessingTests.class);
    }
}

@@ -81,150 +81,150 @@
 ort 
-org.junit.Test;%0Aimport org.junit.runner.JUnitCore;%0A%0Aimport jycessing.Runner;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.PrintStream
+java.io.ByteArrayOutputStream;%0Aimport java.io.PrintStream;%0A%0Aimport jycessing.Runner;%0A%0Aimport org.junit.Test;%0Aimport org.junit.runner.JUnitCore
 ;%0A%0Ap
@@ -607,35 +607,16 @@
         
-                   
 + testRe
@@ -744,37 +744,8 @@
 n%22)%0A
-                             
     
@@ -1405,25 +1405,16 @@
         
-         
 testText
@@ -1445,29 +1445,24 @@
 als(%22OK%5Cn%22,%0A
-     
             
@@ -1529,35 +1529,8 @@
 n%22)%0A
-                           
     
@@ -2051,29 +2051,24 @@
             
-     
 run(%22set%22));
@@ -2975,29 +2975,24 @@
             
-     
 run(%22pcore%22)
@@ -3105,24 +3105,138 @@
 %22));%0A    %7D%0A%0A
+    @Test%0A    public void loadPixels() throws Exception %7B%0A        assertEquals(%22OK%5Cn%22, run(%22loadPixels%22));%0A    %7D%0A%0A
     public s

add regression test for loadPixels()