|
7 | 7 | * %% |
8 | 8 | * Redistribution and use in source and binary forms, with or without |
9 | 9 | * modification, are permitted provided that the following conditions are met: |
10 | | - * |
| 10 | + * |
11 | 11 | * 1. Redistributions of source code must retain the above copyright notice, |
12 | 12 | * this list of conditions and the following disclaimer. |
13 | 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
14 | 14 | * this list of conditions and the following disclaimer in the documentation |
15 | 15 | * and/or other materials provided with the distribution. |
16 | | - * |
| 16 | + * |
17 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
30 | 30 |
|
31 | 31 | package org.scijava.plugins.scripting.scala; |
32 | 32 |
|
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 | | - |
40 | 33 | import org.junit.Test; |
41 | 34 | import org.scijava.Context; |
42 | 35 | import org.scijava.script.ScriptLanguage; |
43 | 36 | import org.scijava.script.ScriptService; |
44 | 37 |
|
| 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 | + |
45 | 47 | /** |
46 | 48 | * Scala unit tests. |
47 | 49 | * |
|
50 | 52 | */ |
51 | 53 | public class ScalaTest { |
52 | 54 |
|
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 | + } |
57 | 72 |
|
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); |
61 | 77 |
|
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()); |
65 | 84 |
|
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 | + } |
70 | 90 | } |
0 commit comments