Skip to content

Commit 01b5db4

Browse files
committed
More tests: add testLocals()
1 parent a8f2de0 commit 01b5db4

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

src/test/java/org/scijava/plugins/scripting/scala/ScalaTest.java

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
* %%
88
* Redistribution and use in source and binary forms, with or without
99
* modification, are permitted provided that the following conditions are met:
10-
*
10+
*
1111
* 1. Redistributions of source code must retain the above copyright notice,
1212
* this list of conditions and the following disclaimer.
1313
* 2. Redistributions in binary form must reproduce the above copyright notice,
1414
* this list of conditions and the following disclaimer in the documentation
1515
* and/or other materials provided with the distribution.
16-
*
16+
*
1717
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1818
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1919
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -30,18 +30,20 @@
3030

3131
package org.scijava.plugins.scripting.scala;
3232

33-
import static org.junit.Assert.assertEquals;
34-
35-
import java.io.StringWriter;
36-
37-
import javax.script.ScriptEngine;
38-
import javax.script.SimpleScriptContext;
39-
4033
import org.junit.Test;
4134
import org.scijava.Context;
4235
import org.scijava.script.ScriptLanguage;
4336
import org.scijava.script.ScriptService;
4437

38+
import javax.script.Bindings;
39+
import javax.script.ScriptContext;
40+
import javax.script.ScriptEngine;
41+
import javax.script.SimpleScriptContext;
42+
import java.io.StringWriter;
43+
44+
import static org.junit.Assert.assertEquals;
45+
import static org.junit.Assert.assertNull;
46+
4547
/**
4648
* Scala unit tests.
4749
*
@@ -50,21 +52,39 @@
5052
*/
5153
public class ScalaTest {
5254

53-
@Test
54-
public void testBasic() throws Exception {
55-
final Context context = new Context(ScriptService.class);
56-
final ScriptService scriptService = context.getService(ScriptService.class);
55+
@Test
56+
public void testBasic() throws Exception {
57+
final Context context = new Context(ScriptService.class);
58+
final ScriptService scriptService = context.getService(ScriptService.class);
59+
60+
final ScriptLanguage language =
61+
scriptService.getLanguageByExtension("scala");
62+
final ScriptEngine engine = language.getScriptEngine();
63+
64+
final SimpleScriptContext ssc = new SimpleScriptContext();
65+
final StringWriter writer = new StringWriter();
66+
ssc.setWriter(writer);
67+
68+
final String script = "print(\"3\");";
69+
engine.eval(script, ssc);
70+
assertEquals("3", writer.toString());
71+
}
5772

58-
final ScriptLanguage language =
59-
scriptService.getLanguageByExtension("scala");
60-
final ScriptEngine engine = language.getScriptEngine();
73+
@Test
74+
public void testLocals() throws Exception {
75+
try (final Context context = new Context(ScriptService.class)) {
76+
final ScriptService scriptService = context.getService(ScriptService.class);
6177

62-
final SimpleScriptContext ssc = new SimpleScriptContext();
63-
final StringWriter writer = new StringWriter();
64-
ssc.setWriter(writer);
78+
final ScriptLanguage language = scriptService.getLanguageByExtension("scala");
79+
final ScriptEngine engine = language.getScriptEngine();
80+
assertEquals("org.scijava.plugins.scripting.scala.ScalaScriptEngine", engine.getClass().getName());
81+
engine.put("hello", 17);
82+
assertEquals("17", engine.eval("hello").toString());
83+
assertEquals("17", engine.get("hello").toString());
6584

66-
final String script = "print(\"3\");";
67-
engine.eval(script, ssc);
68-
assertEquals("3", writer.toString());
69-
}
85+
final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
86+
bindings.clear();
87+
assertNull(engine.get("hello"));
88+
}
89+
}
7090
}

0 commit comments

Comments
 (0)