All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tests.java.javatests.Issue2007Test Maven / Gradle / Ivy

Go to download

Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.

There is a newer version: 2.7.4
Show newest version
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();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy