use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class FunctionTest method testGetVariableNamesConcurrently.
@Test
public void testGetVariableNamesConcurrently() throws Exception {
final Expression exp = this.instance.compile("{let a = 1; let b = b + 1; p(a+b);} c-a");
int threads = 30;
final CyclicBarrier barrier = new CyclicBarrier(threads + 1);
final AtomicInteger c = new AtomicInteger(0);
for (int i = 0; i < threads; i++) {
new Thread() {
@Override
public void run() {
try {
barrier.await();
List<String> vars = exp.getVariableNames();
assertEquals(3, vars.size());
assertEquals("b", vars.get(0));
assertEquals("c", vars.get(1));
assertEquals("a", vars.get(2));
c.incrementAndGet();
barrier.await();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}.start();
}
barrier.await();
barrier.await();
assertEquals(threads, c.get());
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class GrammarUnitTest method testStringInterpolation.
@Test
public void testStringInterpolation() {
assertEquals("aviator", this.instance.execute("let name='aviator'; '#{name}'"));
assertEquals("hello,aviator", this.instance.execute("let name='aviator'; 'hello,#{name}'"));
assertEquals("hello,aviator,great", this.instance.execute("let name='aviator'; 'hello,#{name},great'"));
assertEquals("3", this.instance.execute("'#{1+2}'"));
assertEquals("good,3", this.instance.execute("'good,#{1+2}'"));
String name = "aviator";
Expression exp = this.instance.compile(" a ? '#{name}': 'hello,#{name},great'");
assertEquals("aviator", exp.execute(exp.newEnv("a", true, "name", name)));
assertEquals("hello,aviator,great", exp.execute(exp.newEnv("a", false, "name", name)));
assertEquals("aviator", exp.execute(exp.newEnv("a", true, "name", name)));
assertEquals("hello,aviator,great", exp.execute(exp.newEnv("a", false, "name", name)));
assertEquals("cool", exp.execute(exp.newEnv("a", true, "name", "cool")));
assertEquals("hello,cool,great", exp.execute(exp.newEnv("a", false, "name", "cool")));
// String Interpolation in string.
exp = this.instance.compile("'hello,#{a+b+\"good,#{c*d}\"}'");
assertEquals("hello,3good,12", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("hello,3good,12", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("hello,11good,56", exp.execute(exp.newEnv("a", 5, "b", 6, "c", 7, "d", 8)));
exp = this.instance.compile("'hello,#{a+b+\"good,#{let c=100; c*d}\"}'");
assertEquals("hello,3good,400", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("hello,3good,400", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("hello,3good,9900", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 99)));
exp = this.instance.compile("'#{a+b/c}'");
assertEquals("1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
exp = this.instance.compile("'#{a+b/c}' + d");
assertEquals("14", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("14", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("36", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
assertEquals("36", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
exp = this.instance.compile("d + '#{a+b/c}'");
assertEquals("41", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("41", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("63", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
assertEquals("63", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
exp = this.instance.compile("d + '#{a+b/c}' + a");
assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
exp = this.instance.compile("'#{d}#{a+b/c}#{a}'");
assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("411", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
assertEquals("633", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
exp = this.instance.compile("'#{d},a+b/c=#{a+b/c}#{a}'");
assertEquals("4,a+b/c=11", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("4,a+b/c=11", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("6,a+b/c=33", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
assertEquals("6,a+b/c=33", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
exp = this.instance.compile("'d=#{d},a+b/c=#{a+b/c},a=#{a}'");
assertEquals("d=4,a+b/c=1,a=1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("d=4,a+b/c=1,a=1", exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3, "d", 4)));
assertEquals("d=6,a+b/c=3,a=3", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
assertEquals("d=6,a+b/c=3,a=3", exp.execute(exp.newEnv("a", 3, "b", 4, "c", 5, "d", 6)));
assertEquals("#{name}", this.instance.execute("'\\#{name}'"));
assertEquals("a#{name}", this.instance.execute("'a\\#{name}'"));
assertEquals("a#{name}b3", this.instance.execute("'a\\#{name}b#{1+2}'"));
assertEquals("\\#{name}", this.instance.execute("'\\\\\\#{name}'"));
assertEquals("#{name3", this.instance.execute("'\\#{name#{1+2}'"));
try {
this.instance.execute("'#{1+2'");
fail();
} catch (CompileExpressionErrorException e) {
e.printStackTrace();
}
try {
this.instance.execute("'#{1+*2'");
fail();
} catch (CompileExpressionErrorException e) {
e.printStackTrace();
}
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class ASMCodeGeneratorUnitTest method testOnConstant_Pattern.
@Test
public void testOnConstant_Pattern() throws Exception {
this.codeGenerator.onConstant(new PatternToken("[a-z_A-Z]+", 0, 0));
Expression exp = this.codeGenerator.getResult(true);
Object result = exp.execute();
assertTrue(result instanceof Pattern);
assertEquals("[a-z_A-Z]+", result.toString());
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class ASMCodeGeneratorUnitTest method testOnConstant_Long.
@Test
public void testOnConstant_Long() throws Exception {
this.codeGenerator.onConstant(new NumberToken(3L, "3"));
Expression exp = this.codeGenerator.getResult(true);
Object result = exp.execute();
assertEquals(3L, result);
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class ASMCodeGeneratorUnitTest method testOnConstant_Variable.
@Test
public void testOnConstant_Variable() throws Exception {
this.codeGenerator.onConstant(new Variable("a", 0, 0));
Expression exp = this.codeGenerator.getResult(true);
HashMap<String, Object> env = new HashMap<String, Object>();
long now = System.currentTimeMillis();
env.put("a", now);
Object result = exp.execute(env);
assertEquals(now, result);
}
Aggregations