package test.jycessing;

import static junit.framework.Assert.assertEquals;

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

import jycessing.Runner;

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

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[] { "test_resources/test_" + testResource
                    + ".py" });
            return 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 set_builtins() throws Exception {
        assertEquals("128\nset(['banana'])\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 {
        assertEquals("OK\n", run("urllib2"));
    }

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

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

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

@@ -557,16 +557,24 @@
 ng%5B%5D %7B %22
+testing/
 test_res
@@ -590,24 +590,11 @@
 st_%22
- + testResource%0A
+%0A  
     
@@ -607,17 +607,30 @@
         
- 
++ testResource
  + %22.py%22
@@ -817,32 +817,826 @@
        %7D%0A    %7D%0A%0A
+    private static void testImport(final String module) throws Exception %7B%0A        final ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        final PrintStream saved = System.out;%0A        try %7B%0A            System.setOut(new PrintStream(baos, true));%0A            final String testClass = module + %22_test%22;%0A            final String bogusFileName = %22%3Ctest %22 + module + %22%3E%22;%0A            final String testText = %22import %22 + module + %22%5Cnprint 'OK'%22;%0A            Runner.runSketch(new String%5B%5D %7B testClass %7D, bogusFileName,%0A                    testText);%0A            assertEquals(%22OK%5Cn%22,%0A                    new String(baos.toByteArray()).replaceAll(%22%5Cr%5Cn%22, %22%5Cn%22)%0A                            .replaceAll(%22%5Cr%22, %22%5Cn%22));%0A        %7D finally %7B%0A            System.setOut(saved);%0A        %7D%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2190,32 +2190,18 @@
     
-assertEquals(%22OK%5Cn%22, run
+testImport
 (%22ur
@@ -2207,17 +2207,16 @@
 rllib2%22)
-)
 ;%0A    %7D%0A
@@ -2282,32 +2282,18 @@
     
-assertEquals(%22OK%5Cn%22, run
+testImport
 (%22ur
@@ -2298,17 +2298,16 @@
 urllib%22)
-)
 ;%0A    %7D%0A
@@ -2366,32 +2366,32 @@
 ows Exception %7B%0A
-
         assertEq
@@ -2435,24 +2435,214 @@
 %22));%0A    %7D%0A%0A
+    @Test%0A    public void datetime() throws Exception %7B%0A        testImport(%22datetime%22);%0A    %7D%0A%0A    @Test%0A    public void calendar() throws Exception %7B%0A        testImport(%22calendar%22);%0A    %7D%0A%0A
     public s

fix old tests; add new tests for std lib regressions