This commit is contained in:
Athijegannathan Sundararajan 2013-07-29 21:56:40 +05:30
commit 820cebb6aa
2 changed files with 44 additions and 2 deletions

View File

@ -39,12 +39,20 @@ Object.defineProperty(this, "__noSuchProperty__", {
}
});
function print(str) {
function print() {
var writer = context.getWriter();
if (! (writer instanceof java.io.PrintWriter)) {
writer = new java.io.PrintWriter(writer);
}
writer.println(String(str));
var buf = new java.lang.StringBuilder();
for (var i = 0; i < arguments.length; i++) {
if (i != 0) {
buf.append(' ');
}
buf.append(String(arguments[i]));
}
writer.println(buf.toString());
}
/**

View File

@ -1220,4 +1220,38 @@ public class ScriptEngineTest {
Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]");
Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]");
}
@Test
// check that print function prints arg followed by newline char
public void printTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final StringWriter sw = new StringWriter();
e.getContext().setWriter(sw);
try {
e.eval("print('hello')");
} catch (final Throwable t) {
t.printStackTrace();
fail(t.getMessage());
}
assertEquals(sw.toString(), "hello\n");
}
@Test
// check that print prints all arguments (more than one)
public void printManyTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final StringWriter sw = new StringWriter();
e.getContext().setWriter(sw);
try {
e.eval("print(34, true, 'hello')");
} catch (final Throwable t) {
t.printStackTrace();
fail(t.getMessage());
}
assertEquals(sw.toString(), "34 true hello\n");
}
}