Skip to content

Commit e18f986

Browse files
committed
added test for numeric values
1 parent 165fdf6 commit e18f986

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/main/java/org/julia/scripting/JuliaEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public Object eval(String script, ScriptContext context) throws ScriptException
3636
String formatStr;
3737
// TODO: add direct write of values without text transformations
3838
if (entry.getValue() instanceof Number) {
39-
formatStr = "%s = %s";
39+
formatStr = "%s = %s;\n";
4040
} else {
41-
formatStr = "%s = \"%s\"";
41+
formatStr = "%s = \"%s\";\n";
4242
}
4343
builder.append(String.format(formatStr, entry.getKey(), entry.getValue()));
4444
});

src/test/java/Julia4JScriptingTest.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import static org.junit.jupiter.api.Assertions.assertTrue;
1313

1414
/**
15-
* Test of jsr223 Julia binding
16-
*
15+
* Test of jsr223 Julia binding
16+
* <p>
1717
* Created by rss on 25/08/2018
1818
*/
1919
@SuppressWarnings("WeakerAccess")
@@ -56,23 +56,31 @@ public void testBasic() throws ScriptException {
5656

5757
@Test
5858
public void testReturnvalue() throws ScriptException {
59-
ScriptEngineManager manager = new ScriptEngineManager();
60-
ScriptEngine engine = manager.getEngineByName("julia");
59+
ScriptEngineManager manager = new ScriptEngineManager();
60+
ScriptEngine engine = manager.getEngineByName("julia");
61+
62+
engine.put("x", "hello");
63+
// print global variable "x"
64+
engine.eval("println(x);");
65+
// the above line prints "hello"
66+
67+
// Now, pass a different script context
68+
ScriptContext newContext = new SimpleScriptContext();
69+
Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
6170

62-
engine.put("x", "hello");
63-
// print global variable "x"
64-
engine.eval("println(x);");
65-
// the above line prints "hello"
71+
// add new variable "x" to the new engineScope
72+
engineScope.put("x", "world");
6673

67-
// Now, pass a different script context
68-
ScriptContext newContext = new SimpleScriptContext();
69-
Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
74+
// execute the same script - but this time pass a different script context
75+
engine.eval("println(x);", newContext);
76+
// the above line prints "world"
7077

71-
// add new variable "x" to the new engineScope
72-
engineScope.put("x", "world");
78+
// Add some numeric values
79+
engineScope.put("a", 2);
80+
engineScope.put("b", 10);
81+
engine.eval("for i in 1:5 println(\"Value: $(a * b * i)\"); end", newContext);
7382

74-
// execute the same script - but this time pass a different script context
75-
engine.eval("println(x);", newContext);
76-
// the above line prints "world"
83+
Object result = engine.eval("a + b", newContext);
84+
assertEquals(12, Julia4J.jl_unbox_int64((SWIGTYPE_p_jl_value_t) result));
7785
}
7886
}

0 commit comments

Comments
 (0)