Evaluator { public EvalEntry eval(String stmt) throws Exception { try { return (EvalEntry) latteEngine.eval(stmt, latteEngine.getBindings(ScriptContext.ENGINE_SCOPE), new Config() .setScannerType(scannerType).setVarNamePrefix(varNameBase).setEval(true)); } catch (ScriptException e) { throw (Exception) e.getCause(); } } Evaluator(ClassPathLoader classPathLoader);  Evaluator(String varNameBase, ClassPathLoader classPathLoader); void setScannerType(int type); void put(String name, Object var); EvalEntry eval(String stmt);  }
@Test public void testSimpleCode() throws Exception { assertEquals(1, evaluator.eval("1").result); }
@Test public void testPrimitiveCast() throws Exception { Evaluator e = evaluator; assertEquals((short) 1, e.eval("int_short:int=1\nint_short as short").result); assertEquals((byte) 1, e.eval("int_byte:int=1\nint_byte as byte").result); assertEquals('a', e.eval("int_char:int=97\nint_char as char").result); assertEquals((long) 1, e.eval("int_long:int=1\nint_long as long").result); assertEquals((float) 1, e.eval("int_float:int=1\nint_float as float").result); assertEquals((double) 1, e.eval("int_double:int=1\nint_double as double").result); assertEquals(true, e.eval("int_bool1:int=1\nint_bool1 as bool").result); assertEquals(false, e.eval("int_bool2:int=0\nint_bool2 as bool").result); assertEquals(1, e.eval("short_int:short=1\nshort_int as int").result); assertEquals((byte) 1, e.eval("short_byte:short=1\nshort_byte as byte").result); assertEquals('a', e.eval("short_char:short=97\nshort_char as char").result); assertEquals((long) 1, e.eval("short_long:short=1\nshort_long as long").result); assertEquals((float) 1, e.eval("short_float:short=1\nshort_float as float").result); assertEquals((double) 1, e.eval("short_double:short=1\nshort_double as double").result); assertEquals(true, e.eval("short_bool1:short=1\nshort_bool1 as bool").result); assertEquals(false, e.eval("short_bool2:short=0\nshort_bool2 as bool").result); assertEquals(1, e.eval("byte_int:byte=1\nbyte_int as int").result); assertEquals((short) 1, e.eval("byte_short:byte=1\nbyte_short as short").result); assertEquals('a', e.eval("byte_char:byte=97\nbyte_char as char").result); assertEquals((long) 1, e.eval("byte_long:byte=1\nbyte_long as long").result); assertEquals((float) 1, e.eval("byte_float:byte=1\nbyte_float as float").result); assertEquals((double) 1, e.eval("byte_double:byte=1\nbyte_double as double").result); assertEquals(true, e.eval("byte_bool1:byte=1\nbyte_bool1 as bool").result); assertEquals(false, e.eval("byte_bool2:byte=0\nbyte_bool2 as bool").result); assertEquals(97, e.eval("char_int:char='a'\nchar_int as int").result); assertEquals((short) 97, e.eval("char_short:char='a'\nchar_short as short").result); assertEquals((byte) 97, e.eval("char_byte:char='a'\nchar_byte as byte").result); assertEquals((long) 97, e.eval("char_long:char='a'\nchar_long as long").result); assertEquals((float) 97, e.eval("char_float:char='a'\nchar_float as float").result); assertEquals((double) 97, e.eval("char_double:char='a'\nchar_double as double").result); assertEquals(true, e.eval("char_bool:char='a'\nchar_bool as bool").result); assertEquals(1, e.eval("long_int:long=1\nlong_int as int").result); assertEquals((short) 1, e.eval("long_short:long=1\nlong_short as short").result); assertEquals((byte) 1, e.eval("long_byte:long=1\nlong_byte as byte").result); assertEquals('a', e.eval("long_char:long=97\nlong_char as char").result); assertEquals((float) 1, e.eval("long_float:long=1\nlong_float as float").result); assertEquals((double) 1, e.eval("long_double:long=1\nlong_double as double").result); assertEquals(true, e.eval("long_bool1:long=1\nlong_bool1 as bool").result); assertEquals(false, e.eval("long_bool2:long=0\nlong_bool2 as bool").result); assertEquals(1, e.eval("float_int:float=1\nfloat_int as int").result); assertEquals((short) 1, e.eval("float_short:float=1\nfloat_short as short").result); assertEquals((byte) 1, e.eval("float_byte:float=1\nfloat_byte as byte").result); assertEquals('a', e.eval("float_char:float=97\nfloat_char as char").result); assertEquals((long) 1, e.eval("float_long:float=1\nfloat_long as long").result); assertEquals((double) 1, e.eval("float_double:float=1\nfloat_double as double").result); assertEquals(true, e.eval("float_bool1:float=1\nfloat_bool1 as bool").result); assertEquals(false, e.eval("float_bool2:float=0\nfloat_bool2 as bool").result); assertEquals(1, e.eval("double_int:double=1\ndouble_int as int").result); assertEquals((short) 1, e.eval("double_short:double=1\ndouble_short as short").result); assertEquals((byte) 1, e.eval("double_byte:double=1\ndouble_byte as byte").result); assertEquals('a', e.eval("double_char:double=97\ndouble_char as char").result); assertEquals((long) 1, e.eval("double_long:double=1\ndouble_long as long").result); assertEquals((float) 1, e.eval("double_float:double=1\ndouble_float as float").result); assertEquals(true, e.eval("double_bool1:double=1\ndouble_bool1 as bool").result); assertEquals(false, e.eval("double_bool2:double=0\ndouble_bool2 as bool").result); }
@Test public void testObject() throws Exception { evaluator.eval("" + "object O"); Object o = evaluator.eval("O").result; assertEquals("O", o.getClass().getName()); }
@Test public void testSimpleExpression() throws Exception { assertEquals(2, evaluator.eval("1+1").result); }
@Test public void testEvaluateTwice() throws Exception { assertEquals(1, evaluator.eval("1").result); assertEquals(2, evaluator.eval("1+1").result); }
@Test public void testEvaluateThreeTimes() throws Exception { assertEquals(1, evaluator.eval("1").result); assertEquals(2, evaluator.eval("1+1").result); assertEquals(100, evaluator.eval("10*10").result); }
@Test public void testEvaluateVariableDef() throws Exception { EvalEntry entry = evaluator.eval("i=10*10"); assertEquals("i", entry.name); assertEquals(100, entry.result); }
@Test public void testEvaluateFieldSet() throws Exception { EvalEntry entry = evaluator.eval("i=10*10"); assertEquals("i", entry.name); assertEquals(100, entry.result); assertEquals(2, evaluator.eval("i=2").result); assertEquals(2, evaluator.eval("i").result); }
@Test public void testEvaluateStmt() throws Exception { EvalEntry entry = evaluator.eval("method()=1"); assertNull(entry.name); Object o = entry.result; Method m = o.getClass().getDeclaredMethod("method"); assertEquals(1, m.invoke(o)); }
@Test public void testEvaluateMethod() throws Exception { evaluator.eval("method()=1"); assertEquals(1, evaluator.eval("method()").result); }
@Test public void testEvaluateClass() throws Exception { @SuppressWarnings("unchecked") java.util.List<Class<?>> list = (java.util.List<Class<?>>) evaluator.eval("class User").result; assertEquals(1, list.size()); assertEquals("User", list.get(0).getName()); }
@Test public void testEvaluateClassAndStmt() throws Exception { Object o = evaluator.eval("" + "class User\n" + "User()").result; assertEquals("User", o.getClass().getName()); }