@SuppressWarnings("unchecked") public static <T> T evaluate(String exp) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, null); return (T) ex.get(null); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model); return (T) ex.get(model); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model, Map<String, UserFunction> functions) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model, functions); return (T) ex.get(model); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model); return (T) ex.get(model); }
@Override public void run() { EvalTools.prepare(s); } }, ClassNotFoundException.class, "Can not find class 'AtomicInteger'");
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, null); return (T) ex.get(null); }
@SuppressWarnings("unchecked") public static <T> T evaluate(String exp, Map<String, Object> model, Map<String, UserFunction> functions) { // System.out.println("evaluate: " + exp + "\t" + model); Expression ex = prepare(exp, model, functions); return (T) ex.get(model); }
public WebApplication loadConfig(String path) { String text = resourcesTools.getResourceAsString(path); if (text != null) { System.out.println("load config from: " + path); EvalTools.prepare(text).get(config); } return this; }
@Test public void test_imports_2() { String s = "import " + AtomicInteger.class.getCanonicalName() + ";\n" + "i = new AtomicInteger(1)"; Expression expression = EvalTools.prepare(s); Map model = new HashMap(); expression.get(model); Assert.assertEquals(1, model.size()); Assert.assertEquals(1, ((AtomicInteger) model.get("i")).get()); }
@Test public void testComment_1() { Map<String, Object> model = new HashMap<String, Object>(); String s = "a = 1\n//a=2"; Assert.assertEquals(1, EvalTools.prepare(s).get(model)); }
@Test public void test_9() { String s = "a.b {\n" + "foo = 'bar'\n" + "}"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("bar", ((Map) ((Map) config.get("a")).get("b")).get("foo")); }
@Test public void test_4() { String s = "a { b { c = 'value'} }"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(1, config.size()); Assert.assertEquals("value", ((Map) ((Map) config.get("a")).get("b")).get("c")); }
@Test public void test_closures_2() { String s = "a = 1\n" + "s = \"|${a}|\"\n" + "c = {a}\n" + "a=2"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(Integer.valueOf(2), config.get("a", 0)); Assert.assertEquals("|1|", config.get("s").toString()); }
@Test public void test_closure() { String s = "" + "closure = {'bar'}\n" + "foo = closure()" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("bar", config.get("foo")); }
@Test public void testClone() throws Exception { String exp = "1+\"ololo\".substring(2)"; Expression eh = EvalTools.prepare(exp); Object ob1 = eh.get(null); Object ob2 = eh.get(null); assertTrue(ob1 == ob2); eh = eh.clone(); Object ob3 = eh.get(null); assertTrue(ob1 != ob3); assertTrue(ob1.equals(ob3)); }
@Test public void test_bind__should_not_fail() { String s = "" + "a.l = 1\n" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); TestClass v = config.config("a").bind(TestClass.class); Assert.assertEquals(1L, v.l); }
@Test public void testVariable() throws Exception { Expression eh; eh = EvalTools.prepare("a=2"); Variable a = new Variable("a", 0); eh.setVariable(a); eh.get(); Assert.assertEquals(2, a.get()); }
@Test public void test_1() { String s = "key = 'value'"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(1, config.size()); Assert.assertEquals("value", config.get("key")); }
@Test public void test_comment_4() { String s = "b = 'this // is not a comment'\n" + "c = '/*also just a string*/'"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("this // is not a comment", config.get("b", "")); Assert.assertEquals("/*also just a string*/", config.get("c", "")); }
@Test public void test_imports() { String s = "import com.wizzardo.tools.evaluation.ConfigTest\n" + "a = new ConfigTest.TestImport()\n" + "a.i = 1" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertTrue(config.get("a") instanceof TestImport); Assert.assertEquals(1, config.get("a", new TestImport()).i); }