Search in sources :

Example 1 with ScriptModule

use of org.scijava.script.ScriptModule in project imagej-ops by imagej.

the class ReadmeExampleTest method testReadmesExample.

@Test
public void testReadmesExample() throws Exception {
    // extract the example script
    final File readme = new File("README.md");
    final String contents = new String(FileUtils.readFile(readme), "UTF-8");
    final String telltale = String.format("```python%n");
    final int begin = contents.indexOf(telltale) + telltale.length();
    assertTrue(begin > telltale.length());
    assertTrue(contents.indexOf(telltale, begin) < 0);
    final int end = contents.indexOf(String.format("```%n"), begin);
    assertTrue(end > 0);
    final String snippet = contents.substring(begin, end);
    assertTrue(snippet.startsWith("# @ImageJ ij"));
    final Context context = new Context();
    final ScriptService script = context.getService(ScriptService.class);
    // create mock ImageJ gateway
    script.addAlias("ImageJ", Mock.class);
    final ScriptModule module = script.run("op-example.py", snippet, true).get();
    assertNotNull(module);
    module.run();
    final Mock ij = context.getService(Mock.class);
    assertEquals(3, ij.images.size());
    assertEquals(11.906, ij.getPixel("sinusoid", 50, 50), 1e-3);
    assertEquals(100, ij.getPixel("gradient", 50, 50), 1e-3);
    assertEquals(111.906, ij.getPixel("composite", 50, 50), 1e-3);
}
Also used : Context(org.scijava.Context) ScriptService(org.scijava.script.ScriptService) ScriptModule(org.scijava.script.ScriptModule) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)1 Test (org.junit.Test)1 Context (org.scijava.Context)1 ScriptModule (org.scijava.script.ScriptModule)1 ScriptService (org.scijava.script.ScriptService)1