source("TestHarness.bsh");

import bsh.*;

if ( Capabilities.classExists("bsh.ExternalNameSpace") )
{

Interpreter interpreter = new Interpreter();
Map myMap = new HashMap();
ens = new ExternalNameSpace( interpreter.getNameSpace(), "Test", myMap );
interpreter.setNameSpace( ens );

interpreter.eval("foo=42");
assert( myMap.get("foo") instanceof Integer );
assert( myMap.get("foo") == 42 );

// primitives get mapped back to prims inside the interpreter
interpreter.eval("int fooint=42");
assert( interpreter.eval("fooint.getClass()") == bsh.Primitive.class );
assert( myMap.get("fooint") instanceof Integer );

interpreter.eval("Integer fooInt=42");
assert( interpreter.eval("fooInt.getClass()") == Integer.class );
assert( myMap.get("fooint") instanceof Integer );

// make sure vars propogate from child namespace properly
interpreter.eval("run() { if (foo!=42) throw new RuntimeException(); foo=55; }");
interpreter.eval("run()");
assert( interpreter.eval("foo") == 55 );
assert( myMap.get("foo") == 55 );

interpreter.eval("flag=true");
assert( myMap.get("flag") instanceof Boolean );
assert( myMap.get("flag").booleanValue() == true );
interpreter.eval("flag=false");
assert( myMap.get("flag").booleanValue() == false );

interpreter.eval("int bar = 43");
// it's a primitive to bsh
assert( interpreter.eval("bar.getClass() == bsh.Primitive.class") == true );
// it's an Integer to the map
assert( myMap.get("bar") instanceof Integer );
assert( myMap.get("bar") == 43 );
// check again
assert( interpreter.eval("bar.getClass() == bsh.Primitive.class") == true );

assert( interpreter.get("e1") == null );

myMap.put( "e2", new Integer(44) );

assert( interpreter.get("e2") == 44 );

myMap.put( "d1", new Date() );

assert( interpreter.get("d1") instanceof Date );

myMap.put( "d1", new Double(49.9) );

assert( interpreter.get("d1") instanceof Double );

interpreter.eval("print(this.variables)");

// set var in bsh then remove it in external map
interpreter.eval("x1=42");
assert( interpreter.get("x1") == 42 );
myMap.remove("x1");
assert( interpreter.get("x1") == null ); // interpreter maps VOID to null

} else
warning("No ExternalNameSpace to test...");

complete();
