Search in sources :

Example 1 with Expression

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());
}
Also used : Expression(com.googlecode.aviator.Expression) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) CompareNotSupportedException(com.googlecode.aviator.exception.CompareNotSupportedException) ExpressionSyntaxErrorException(com.googlecode.aviator.exception.ExpressionSyntaxErrorException) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 2 with Expression

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();
    }
}
Also used : Expression(com.googlecode.aviator.Expression) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 3 with Expression

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());
}
Also used : Pattern(java.util.regex.Pattern) PatternToken(com.googlecode.aviator.lexer.token.PatternToken) Expression(com.googlecode.aviator.Expression) Test(org.junit.Test)

Example 4 with Expression

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);
}
Also used : Expression(com.googlecode.aviator.Expression) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) Test(org.junit.Test)

Example 5 with Expression

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);
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable) Expression(com.googlecode.aviator.Expression) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Expression (com.googlecode.aviator.Expression)31 Test (org.junit.Test)16 HashMap (java.util.HashMap)8 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)3 Map (java.util.Map)3 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)2 StringToken (com.googlecode.aviator.lexer.token.StringToken)2 CollectRep (com.usthe.common.entity.message.CollectRep)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AviatorEvaluator (com.googlecode.aviator.AviatorEvaluator)1 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 BaseExpression (com.googlecode.aviator.BaseExpression)1 LiteralExpression (com.googlecode.aviator.LiteralExpression)1 CompareNotSupportedException (com.googlecode.aviator.exception.CompareNotSupportedException)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 DelegateToken (com.googlecode.aviator.lexer.token.DelegateToken)1