File: Issue2007Test.java

package info (click to toggle)
jython 2.7.3%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 62,820 kB
  • sloc: python: 641,384; java: 306,981; xml: 2,066; sh: 514; ansic: 126; makefile: 77
file content (51 lines) | stat: -rw-r--r-- 1,569 bytes parent folder | download
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
package javatests;

import org.junit.Test;
import org.python.core.PyStringMap;
import org.python.core.PySystemState;
import org.python.util.InteractiveInterpreter;

import java.io.StringWriter;

import static org.junit.Assert.*;

public class Issue2007Test {

    /**
     * Verify that {@code print_function} works in scripts. This was always the case.
     */
    @Test
    public void testPrintFunctionInScript() {
        CapturingInterpreter interp = new CapturingInterpreter();
        interp.exec("from __future__ import print_function\n" + "print(1, 2, 3)\n");
        assertEquals("1 2 3\n", interp.getOutput());
    }

    /**
     * Verify that print_function works when passing individual statements to an interactive
     * interpreter. If issue 2007 is present, this will fail as the second statement will print
     * {@code '(1, 2, 3)'} instead of {@code '1 2 3'}.
     */
    @Test
    public void testPrintFunctionInInteractive() {
        CapturingInterpreter interp = new CapturingInterpreter();
        interp.runsource("from __future__ import print_function");
        interp.runsource("print(1, 2, 3)");
        assertEquals("1 2 3\n", interp.getOutput());
    }

    private static class CapturingInterpreter extends InteractiveInterpreter {

        private final StringWriter writer = new StringWriter();

        CapturingInterpreter() {
            super(new PyStringMap(), new PySystemState());
            setOut(writer);
        }

        String getOutput() {
            close();
            return writer.toString();
        }
    }
}