use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class TestScripts method testLib.
public Object testLib(final String name, final Object... args) {
try {
System.out.println("Testing lib " + name + " with args: " + Arrays.toString(args));
final String file = TestScripts.class.getResource("/lib/test_" + name + ".av").getFile();
this.instance.validate(IoModule.slurp(file));
Expression exp = this.instance.compileScript(file, true);
return exp.execute(AviatorEvaluator.newEnv(args));
} catch (Throwable t) {
Reflector.sneakyThrow(t);
}
return null;
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class FunctionTest method testGetVariableNamesComplex.
@Test
public void testGetVariableNamesComplex() {
// lambda
Expression exp = this.instance.compile("a = 1; add = lambda(x) -> x + a end; add(b)");
List<String> vars = exp.getVariableNames();
System.out.println(vars);
assertEquals(1, vars.size());
assertEquals("b", vars.get(0));
// lambda closure over
exp = this.instance.compile("add = lambda(x) -> x + a end; add(b)");
vars = exp.getVariableNames();
assertEquals(2, vars.size());
assertEquals("a", vars.get(0));
assertEquals("b", vars.get(1));
// if.. else
exp = this.instance.compile("b=2; if(a > 1) { a + b } elsif( a > 10) { return a + c; } else { return 10; }");
vars = exp.getVariableNames();
assertEquals(2, vars.size());
assertEquals("a", vars.get(0));
assertEquals("c", vars.get(1));
// for..loop
exp = this.instance.compile("let list = seq.list(1, 2, 3); for x in list { sum = sum + x }; return sum;");
vars = exp.getVariableNames();
assertEquals(1, vars.size());
assertEquals("sum", vars.get(0));
// let statement in block
exp = this.instance.compile("{let a = 1; let b = b + 1; p(a+b);} c-a");
vars = exp.getVariableNames();
assertEquals(3, vars.size());
assertEquals("b", vars.get(0));
assertEquals("c", vars.get(1));
assertEquals("a", vars.get(2));
// redfine variable
exp = this.instance.compile("{let a = 1; let b = 2; let b = b + 1; p(a+b);} c-a");
vars = exp.getVariableNames();
assertEquals(2, vars.size());
assertEquals("c", vars.get(0));
assertEquals("a", vars.get(1));
// high-order function
exp = this.instance.compile("map(list, lambda(v) -> v + u end)");
vars = exp.getVariableNames();
assertEquals(2, vars.size());
assertEquals("list", vars.get(0));
assertEquals("u", vars.get(1));
// a complex script
exp = this.instance.compile("let n = 0; let index =0 ; " + "for i in a {" + "let t = string_to_date(i, 'yyyyMMdd'); " + "if t == nil { continue; } " + "let m = date.month(b, i); " + "if c[index] == '03' && m <= 12 {" + " n = n + 1; " + "}" + "index = index + 1;" + " } return n;");
vars = exp.getVariableNames();
assertEquals(3, vars.size());
assertEquals("a", vars.get(0));
assertEquals("b", vars.get(1));
assertEquals("c", vars.get(2));
exp = this.instance.compile("a = seq.map(); add = lambda() -> a.b + a.c end; add()");
vars = exp.getVariableNames();
assertEquals(0, vars.size());
vars = exp.getVariableFullNames();
assertEquals(2, vars.size());
assertEquals("a.b", vars.get(0));
assertEquals("a.c", vars.get(1));
exp = this.instance.compile("a = seq.map(); a.c = 2; add = lambda() -> a.b + a.c end; add()");
vars = exp.getVariableNames();
assertEquals(0, vars.size());
vars = exp.getVariableFullNames();
assertEquals(1, vars.size());
assertEquals("a.b", vars.get(0));
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class FunctionTest method testIssue431.
@Test
public void testIssue431() {
Expression expr = this.instance.compile("{\n" + " let itemv1 = seq.get(info.value,index);\n" + " print( itemv1.name )\n" + "}");
List<String> vars = expr.getVariableNames();
assertEquals(2, vars.size());
assertEquals("info", vars.get(0));
assertEquals("index", vars.get(1));
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class FunctionTest method testGetVariableNames.
@Test
public void testGetVariableNames() {
Expression expression = this.instance.compile("b+a", true);
assertNotNull(expression);
List<String> vars = expression.getVariableNames();
assertNotNull(vars);
assertEquals(2, vars.size());
assertTrue(vars.contains("a"));
assertTrue(vars.contains("b"));
assertEquals("b", vars.get(0));
assertEquals("a", vars.get(1));
expression = this.instance.compile("b==a || d>3 || e+c*d/2 <= 1000", true);
assertNotNull(expression);
vars = expression.getVariableNames();
assertNotNull(vars);
assertEquals(5, vars.size());
assertTrue(vars.contains("a"));
assertTrue(vars.contains("b"));
assertTrue(vars.contains("c"));
assertTrue(vars.contains("d"));
assertTrue(vars.contains("e"));
assertEquals("b", vars.get(0));
assertEquals("a", vars.get(1));
assertEquals("d", vars.get(2));
assertEquals("e", vars.get(3));
assertEquals("c", vars.get(4));
// Test map or list as variable
expression = this.instance.compile("map.a>10 && list[0][1]<3 && bean.c == bean.x || bean.d == y", true);
assertNotNull(expression);
vars = expression.getVariableNames();
assertEquals(4, vars.size());
assertTrue(vars.contains("map"));
assertTrue(vars.contains("list"));
assertTrue(vars.contains("bean"));
assertTrue(vars.contains("y"));
assertEquals("map", vars.get(0));
assertEquals("list", vars.get(1));
assertEquals("bean", vars.get(2));
assertEquals("y", vars.get(3));
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class GrammarUnitTest method testIssue186.
@Test
public void testIssue186() {
Expression exp = this.instance.compile("1==1");
assertTrue(exp.getVariableFullNames().isEmpty());
assertTrue(exp.getVariableNames().isEmpty());
exp = this.instance.compile("a!=nil");
List<String> vars = exp.getVariableFullNames();
assertEquals(1, vars.size());
assertTrue(vars.contains("a"));
vars = exp.getVariableNames();
assertEquals(1, vars.size());
assertTrue(vars.contains("a"));
}
Aggregations