Skip to content

Commit

Permalink
This PR fixes the issue of flush() (#1358)
Browse files Browse the repository at this point in the history
Fixing the issue #1356
  • Loading branch information
nmondal authored Jul 25, 2023
1 parent 59fd380 commit 76fc05e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/org/mozilla/javascript/engine/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public static void print(Context cx, Scriptable thisObj, Object[] args, Function
self.stdout.write(ScriptRuntime.toString(arg));
}
self.stdout.write('\n');
// ref bug https://github.com/mozilla/rhino/issues/1356
self.stdout.flush();
}

private static Builtins getSelf(Scriptable scope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringWriter;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.*;
import org.mozilla.javascript.engine.RhinoScriptEngineFactory;

public class BuiltinsTest {
Expand All @@ -35,6 +35,27 @@ public void printStdout() throws ScriptException {
engine.eval("print('Hello, World!');");
}

@Test
public void printStdoutAndCheckItPrints() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream original = System.out;
PrintStream modified = new PrintStream(bos, false);
System.setOut(modified);
// Now Get A SimpleContext
ScriptContext sc = new SimpleScriptContext();
try {
// this was a broken test
engine.eval("print('Hello, World!');", sc);
// this has been hard work https://github.com/mozilla/rhino/issues/1356
Assert.assertEquals("Hello, World!\n", bos.toString());
} finally {
// revert the sys out
System.setOut(original);
modified.close();
bos.close();
}
}

@Test
public void printWriter() throws ScriptException {
StringWriter sw = new StringWriter();
Expand Down

0 comments on commit 76fc05e

Please sign in to comment.