* Test of get method, of class BiVariableMap.
*/
@Test
public void testGet_Object() {
logger1.info("get");
ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.TRANSIENT);
BiVariableMap instance = container.getVarMap();
ArrayList<String> argv_values = new ArrayList<String>();
argv_values.add("spring"); argv_values.add("fall");
container.put("ARGV", argv_values);
ArrayList<String> const_values = new ArrayList<String>();
const_values.add("summer"); const_values.add("winter");
container.put("SEASON", const_values);
ArrayList<String> gvar_values = new ArrayList<String>();
gvar_values.add("baseball"); gvar_values.add("soccer"); gvar_values.add("ski");
container.put("$sports", gvar_values);
ArrayList<String> ivar_values = new ArrayList<String>();
ivar_values.add("snow"); ivar_values.add("sleet"); ivar_values.add("drizzle");
container.put("@weather", ivar_values);
ArrayList<String> cvar_values = new ArrayList<String>();
cvar_values.add("cirrus"); cvar_values.add("stratus"); cvar_values.add("cumulus");
container.put("@@clouds", cvar_values);
ArrayList<String> lvar_values = new ArrayList<String>();
lvar_values.add("cypress"); lvar_values.add("hemlock"); lvar_values.add("spruce");
container.put("trees", lvar_values);
assertEquals(argv_values, instance.get("ARGV"));
assertEquals(const_values, instance.get("SEASON"));
assertEquals(gvar_values, instance.get("$sports"));
assertEquals(ivar_values, instance.get("@weather"));
assertEquals(cvar_values, instance.get("@@clouds"));
assertEquals(lvar_values, instance.get("trees"));
String script =
"class Forecast\n" +
" SEASON = 'halloween'\n" +
" @@clouds = ['contrail', 'snow laden clouds']\n" +
" attr_accessor :weather, :temp\n" +
" def initialize(weather, temp)\n" +
" @weather, @temp = weather, temp\n" +
" end\n" +
" def temp\n" +
" @temp\n" +
" end\n" +
" def cloud_names\n" +
" @@clouds\n" +
" end\n" +
"end";
container.runScriptlet(script);
Object klazz = container.get("Forecast");
assertEquals("Forecast", ((RubyClass)klazz).getName());
Object receiver = container.callMethod(klazz, "new", "blizzard", "6F");
assertEquals("blizzard", container.get(receiver, "@weather"));
assertEquals("6F", container.callMethod(receiver, "temp"));
assertEquals("halloween", container.get(receiver, "SEASON"));
ArrayList<String> expResult = new ArrayList<String>();
expResult.add("contrail"); expResult.add("snow laden clouds");
//assertEquals(expResult, container.get(receiver, "@@clouds")); // why this fails?
container.put(receiver, "@temp", "-5F");
assertEquals("-5F", container.callMethod(receiver, "temp"));
container.put(receiver, "SEASON", "colored leaves");
container.put(receiver, "@@clouds", "thunder clouds");
// need runScriptlet/callMethod to inject a new value
container.runScriptlet("a=1");
assertEquals("colored leaves", container.get(receiver, "SEASON"));
assertEquals("thunder clouds", container.callMethod(receiver, "cloud_names"));
assertEquals("thunder clouds", container.get(receiver, "@@clouds")); // this passes
expResult.clear();
expResult.add("cirrus"); expResult.add("stratus"); expResult.add("cumulus");
assertEquals(expResult, container.get("@@clouds"));
// gvar should be receiver insensitive
expResult.clear();
expResult.add("baseball"); expResult.add("soccer"); expResult.add("ski");
assertEquals(expResult, container.get(receiver, "$sports"));
container.put(receiver, "$team", "tigers");
assertEquals("tigers", container.get("$team"));
container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.PERSISTENT);
instance = container.getVarMap();
container.put("trees", lvar_values);
container.runScriptlet(script);
klazz = container.get("Forecast");
receiver = container.callMethod(klazz, "new", "blizzard", "6F");
expResult.clear();
expResult.add("cypress"); expResult.add("hemlock"); expResult.add("spruce");
assertEquals(expResult, container.get("trees"));
}