|
12 | 12 | import static org.junit.jupiter.api.Assertions.assertTrue; |
13 | 13 |
|
14 | 14 | /** |
15 | | - * Test of jsr223 Julia binding |
16 | | - * |
| 15 | + * Test of jsr223 Julia binding |
| 16 | + * <p> |
17 | 17 | * Created by rss on 25/08/2018 |
18 | 18 | */ |
19 | 19 | @SuppressWarnings("WeakerAccess") |
@@ -56,23 +56,31 @@ public void testBasic() throws ScriptException { |
56 | 56 |
|
57 | 57 | @Test |
58 | 58 | 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); |
61 | 70 |
|
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"); |
66 | 73 |
|
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" |
70 | 77 |
|
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); |
73 | 82 |
|
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)); |
77 | 85 | } |
78 | 86 | } |
0 commit comments