Skip to content
Permalink
Browse files

test: Add more tests

  • Loading branch information...
rhwood committed Sep 18, 2019
1 parent 90c3776 commit 70f9340e39b17da4321c7b1d0b5d8259f53f9ea9
@@ -1,7 +1,10 @@
package jmri.script;

import jmri.InstanceManager;
import jmri.TurnoutManager;
import jmri.profile.NullProfile;
import jmri.profile.ProfileManager;
import jmri.util.FileUtil;
import jmri.util.JUnitAppender;
import jmri.util.JUnitUtil;

@@ -16,10 +19,12 @@
import java.lang.reflect.Field;
import java.util.HashMap;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

import org.junit.After;
import org.junit.Before;
@@ -28,12 +33,56 @@

/**
*
* @author Paul Bender Copyright (C) 2017
* @author Paul Bender Copyright 2017
* @author Randall Wood Copyright 2019
*/
public class JmriScriptEngineManagerTest {

private JmriScriptEngineManager jsem;

@Test
public void testEval_String_ScriptEngine() throws ScriptException {
Object result = null;
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
assertNull(result);
assertNull(manager.getBySystemName("IT1"));
ScriptEngine engine = jsem.getEngine(JmriScriptEngineManager.PYTHON);
result = jsem.eval("turnouts.provideTurnout(\"1\")", engine);
assertNotNull(result);
assertNotNull(manager.getBySystemName("IT1"));
assertEquals(manager.getBySystemName("IT1"), result);
}

@Test
public void testEval_File() throws IOException, ScriptException {
Object result = null;
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
assertNull(result);
assertNull(manager.getBySystemName("IT1"));
JUnitUtil.resetProfileManager(new NullProfile(new File("java/test/jmri/script/exec-file-profile")));
result = jsem.eval(FileUtil.getFile("profile:turnout.py"));
assertNotNull(result);
assertNotNull(manager.getBySystemName("IT1"));
assertEquals(InstanceManager.getDefault(TurnoutManager.class).getBySystemName("IT1"), result);
}

@Test
public void testEval_File_Bindings() throws IOException, ScriptException {
// first test that test binding is not in default bindings
// to ensure later part of tests are not obscured by existing binding
jsem.getDefaultContext().getBindings(ScriptContext.GLOBAL_SCOPE).forEach((name, value) -> assertNotEquals("profiles", name));
// and now test
JUnitUtil.resetProfileManager(new NullProfile(new File("java/test/jmri/script/exec-file-profile")));
Bindings bindings = new SimpleBindings();
bindings.put("profiles", ProfileManager.getDefault());
Object result = null;
ProfileManager manager = ProfileManager.getDefault();
assertNull(result);
result = jsem.eval(FileUtil.getFile("profile:profile.py"), bindings);
assertNotNull(result);
assertEquals(Integer.valueOf(manager.getAutoStartActiveProfileTimeout()), result);
}

@Test
public void testGetDefault() {
assertEquals("getDefault ==s InstanceManager instance", InstanceManager.getDefault(JmriScriptEngineManager.class), JmriScriptEngineManager.getDefault());
@@ -227,6 +276,9 @@ public void setUp() {
// ensure no bindings are null in tests
JUnitUtil.initDebugPowerManager();
JUnitUtil.initDebugCommandStation();
// create an object for test scripts
JUnitUtil.initInternalTurnoutManager();
// create the tested object
jsem = new JmriScriptEngineManager();
}

@@ -0,0 +1,8 @@
# The result of the last method in the script is returned by the Java
# ScriptEngine.eval(...) methods, so this Jython script is calling a single
# non-void method of an object provided in the bindings for the ScriptEngine.
#
# Note that the bindings are not necessarily the default bindings a JMRI
# application provides to ScriptEngines.

profiles.getAutoStartActiveProfileTimeout()
@@ -0,0 +1,8 @@
# The result of the last method in the script is returned by the Java
# ScriptEngine.eval(...) methods, so this Jython script is calling a single
# non-void method of an object provided in the bindings for the ScriptEngine.
#
# Note that the bindings are not necessarily the default bindings a JMRI
# application provides to ScriptEngines.

turnouts.provideTurnout("1")

0 comments on commit 70f9340

Please sign in to comment.
You can’t perform that action at this time.