File: ScriptEngineIOTest.java

package info (click to toggle)
jython 2.5.2-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 72,424 kB
  • sloc: python: 375,154; java: 216,094; xml: 1,413; sh: 330; ansic: 126; perl: 122; makefile: 98
file content (81 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.python.jsr223;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.ScriptEngineFactory;

import junit.framework.TestCase;

import java.io.StringReader;
import java.io.StringWriter;

public class ScriptEngineIOTest extends TestCase
{
    ScriptEngineFactory pythonEngineFactory;
    ScriptEngine pythonEngine;

    public void setUp() throws ScriptException
    {
        pythonEngineFactory = new PyScriptEngineFactory();
        pythonEngine = new PyScriptEngine(pythonEngineFactory);
    }

    public void testEvalString() throws ScriptException
    {
        assertNull(pythonEngine.eval("x = 5"));
        assertEquals(Integer.valueOf(5), pythonEngine.eval("x"));
    }

    public void testReadline() throws ScriptException
    {
        final String testString = "Shazaam Batman!\n";

        pythonEngine.getContext().setReader(new StringReader(testString));

        assertNull(pythonEngine.eval("import sys"));
        assertEquals(testString, pythonEngine.eval("sys.stdin.readline()"));
    }

    public void testReadlines() throws ScriptException
    {
        final String testString = "Holy Smokes Batman!\nBIF!\r\n\nKAPOW!!!\rTHE END.";

        pythonEngine.getContext().setReader(new StringReader(testString));

        assertNull(pythonEngine.eval("import sys"));
        final Object o = pythonEngine.eval("''.join(sys.stdin.readlines())");
        
        assertEquals("Holy Smokes Batman!\nBIF!\n\nKAPOW!!!\nTHE END.\n", o);
    }

    public void testWriter() throws ScriptException
    {
        final StringWriter sw = new StringWriter();

        pythonEngine.getContext().setWriter(sw);

        final String testString = "It is a wonderful world.";

        assertNull(pythonEngine.eval("print '" + testString + "',"));
        assertEquals(testString, sw.toString());
    }

    public void testErrorWriter() throws ScriptException
    {
        final StringWriter stdout = new StringWriter();
        final StringWriter stderr = new StringWriter();

        pythonEngine.getContext().setWriter(stdout);
        pythonEngine.getContext().setErrorWriter(stderr);

        final String testString1 = "It is a wonderful world.";
        final String testString2 = "Stuff happens!";

        assertNull(pythonEngine.eval("import sys"));
        assertNull(pythonEngine.eval("sys.stdout.write('" + testString1 + "')"));
        assertNull(pythonEngine.eval("sys.stderr.write('" + testString2 + "')"));

        assertEquals(testString1, stdout.toString());
        assertEquals(testString2, stderr.toString());
    }
}