Search in sources :

Example 6 with Context

use of org.scijava.Context 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

Context (org.scijava.Context)6 File (java.io.File)2 Before (org.junit.Before)2 Test (org.junit.Test)2 CommandService (org.scijava.command.CommandService)2 ArrayList (java.util.ArrayList)1 OMEROCredentials (net.imagej.omero.OMEROCredentials)1 OMEROService (net.imagej.omero.OMEROService)1 OpService (net.imagej.ops.OpService)1 OpEvaluator (net.imagej.ops.eval.OpEvaluator)1 ByteType (net.imglib2.type.numeric.integer.ByteType)1 UnsignedByteType (net.imglib2.type.numeric.integer.UnsignedByteType)1 AppService (org.scijava.app.AppService)1 Command (org.scijava.command.Command)1 MenuService (org.scijava.menu.MenuService)1 EvaluatorConsole (org.scijava.parse.eval.EvaluatorConsole)1 PluginIndex (org.scijava.plugin.PluginIndex)1 ScriptModule (org.scijava.script.ScriptModule)1 ScriptService (org.scijava.script.ScriptService)1 Service (org.scijava.service.Service)1