Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScriptService: add getScript(String,String) signature #390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/scijava/script/DefaultScriptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package org.scijava.script;

import java.io.File;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
Expand Down Expand Up @@ -159,6 +160,11 @@ public ScriptInfo getScript(final File scriptFile) {
return getOrCreate(scriptFile);
}

@Override
public ScriptInfo getScript(final String path, final String script) {
return new ScriptInfo(getContext(), path, new StringReader(script));
}

@Override
public Future<ScriptModule> run(final File file, final boolean process,
final Object... inputs)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/scijava/script/ScriptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ default ScriptLanguage getLanguageByName(final String name) {
*/
ScriptInfo getScript(File scriptFile);

/**
* Creates the {@link ScriptInfo} metadata for the provided script.
*
* @param path
* Pseudo-path to the script file. This file does not actually need
* to exist, but rather provides a name for the script with file
* extension.
* @param script
* The script contents.
*/
ScriptInfo getScript(String path, String script);

/**
* Executes the script in the given file.
*
Expand Down
60 changes: 38 additions & 22 deletions src/test/java/org/scijava/script/ScriptServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@

import javax.script.ScriptException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.scijava.Context;
import org.scijava.script.ScriptInfoTest.BindingSizes;
import org.scijava.util.AppUtils;
import org.scijava.util.ColorRGB;
import org.scijava.util.ColorRGBA;
Expand All @@ -53,6 +56,20 @@
*/
public class ScriptServiceTest {

private Context context;
private ScriptService scriptService;

@Before
public void setUp() {
context = new Context(ScriptService.class);
scriptService = context.service(ScriptService.class);
}

@After
public void tearDown() {
context.dispose();
}

/**
* Tests that the "scijava.scripts.path" system property is handled correctly.
*/
Expand All @@ -65,9 +82,6 @@ public void testSystemProperty() {
final String dir2 = root + "to" + slash + "the" + slash + "moon";
System.setProperty("scijava.scripts.path", dir1 + sep + dir2);

final Context context = new Context(ScriptService.class);
final ScriptService scriptService = context.service(ScriptService.class);

final List<File> scriptDirs = scriptService.getScriptDirectories();
assertEquals(3, scriptDirs.size());

Expand All @@ -80,9 +94,6 @@ public void testSystemProperty() {

@Test
public void testBuiltInAliases() throws ScriptException {
final Context ctx = new Context(ScriptService.class);
final ScriptService ss = ctx.service(ScriptService.class);

final Class<?>[] builtIns = { boolean.class, byte.class, char.class,
double.class, float.class, int.class, long.class, short.class,
Boolean.class, Byte.class, Character.class, Double.class, Float.class,
Expand All @@ -91,39 +102,44 @@ public void testBuiltInAliases() throws ScriptException {
String.class };

for (final Class<?> builtIn : builtIns) {
final Class<?> c = ss.lookupClass(builtIn.getSimpleName());
final Class<?> c = scriptService.lookupClass(builtIn.getSimpleName());
assertSame(builtIn, c);
}

ctx.dispose();
}

@Test
public void testArrayAliases() throws ScriptException {
final Context ctx = new Context(ScriptService.class);
final ScriptService ss = ctx.service(ScriptService.class);

final Class<?> pInt2D = ss.lookupClass("int[][]");
final Class<?> pInt2D = scriptService.lookupClass("int[][]");
assertSame(int[][].class, pInt2D);
final Class<?> pInt1D = ss.lookupClass("int[]");
final Class<?> pInt1D = scriptService.lookupClass("int[]");
assertSame(int[].class, pInt1D);
final Class<?> pInt = ss.lookupClass("int");
final Class<?> pInt = scriptService.lookupClass("int");
assertSame(int.class, pInt);

final Class<?> oInt2D = ss.lookupClass("Integer[][]");
final Class<?> oInt2D = scriptService.lookupClass("Integer[][]");
assertSame(Integer[][].class, oInt2D);
final Class<?> oInt1D = ss.lookupClass("Integer[]");
final Class<?> oInt1D = scriptService.lookupClass("Integer[]");
assertSame(Integer[].class, oInt1D);
final Class<?> oInt = ss.lookupClass("Integer");
final Class<?> oInt = scriptService.lookupClass("Integer");
assertSame(Integer.class, oInt);

final Class<?> str2D = ss.lookupClass("String[][]");
final Class<?> str2D = scriptService.lookupClass("String[][]");
assertSame(String[][].class, str2D);
final Class<?> str1D = ss.lookupClass("String[]");
final Class<?> str1D = scriptService.lookupClass("String[]");
assertSame(String[].class, str1D);
final Class<?> str = ss.lookupClass("String");
final Class<?> str = scriptService.lookupClass("String");
assertSame(String.class, str);
}

ctx.dispose();
@Test
public void testGetScript() {
String script = "#@ String name\n" +
"#@output String greeting\n" +
"greeting = \"Hello, \" + name + \"!\"";
// see ScriptInfoTest for the .bsizes ScriptLanguage used here
ScriptInfo scriptInfo = scriptService.getScript(".bsizes", script);
assertEquals(BindingSizes.class, scriptInfo.getLanguage().getClass());
assertEquals("name", scriptInfo.inputs().iterator().next().getName());
assertEquals("greeting", scriptInfo.outputs().iterator().next().getName());
}
}