Search in sources :

Example 26 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class AviatorPatternUnitTest method testMatchString.

@Test
public void testMatchString() {
    Env env = TestUtils.getTestEnv();
    AviatorPattern p1 = new AviatorPattern("[a-zA-Z]+");
    assertTrue((Boolean) p1.match(new AviatorString("hello"), env).getValue(env));
    assertFalse((Boolean) p1.match(new AviatorString("hello world"), env).getValue(env));
}
Also used : Env(com.googlecode.aviator.utils.Env) Test(org.junit.Test)

Example 27 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class AviatorPatternUnitTest method testPatternGroup.

@Test
public void testPatternGroup() {
    // 
    AviatorPattern p1 = new AviatorPattern("-\\d+\\.\\d+");
    Env env = TestUtils.getTestEnv();
    p1.match(new AviatorString("-3.4"), env);
    assertEquals(1, env.size());
    assertEquals("-3.4", env.get("$0"));
    p1 = new AviatorPattern("^(-?\\d+)(\\.\\d+)?$");
    env.clear();
    p1.match(new AviatorString("-3.4"), env);
    assertEquals(3, env.size());
    assertEquals("-3.4", env.get("$0"));
    assertEquals("-3", env.get("$1"));
    assertEquals(".4", env.get("$2"));
    // Disable putting capturing groups into env
    try {
        AviatorEvaluator.setOption(Options.PUT_CAPTURING_GROUPS_INTO_ENV, false);
        env.clear();
        assertTrue(p1.match(new AviatorString("-3.4"), env).booleanValue(env));
        assertEquals(0, env.size());
    } finally {
        AviatorEvaluator.setOption(Options.PUT_CAPTURING_GROUPS_INTO_ENV, true);
    }
}
Also used : Env(com.googlecode.aviator.utils.Env) Test(org.junit.Test)

Example 28 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class TestScripts method testUseStatement.

@Test
public void testUseStatement() {
    Env env = (Env) testScript("use1.av", "Long", Long.class);
    assertNotNull(env);
    List<String> symbols = env.getImportedSymbols();
    assertNotNull(symbols);
    assertEquals(2, symbols.size());
    assertTrue(symbols.contains("com.googlecode.aviator.runtime.type.AviatorObject"));
    assertTrue(symbols.contains("java.util.List"));
    testScript("use2.av");
    testScript("use3.av");
    testScript("use4.av");
    try {
        testScript("use5.av");
        fail();
    } catch (ExpressionSyntaxErrorException e) {
        assertTrue(e.getMessage().contains("expect variable name or * to use at"));
    }
}
Also used : Env(com.googlecode.aviator.utils.Env) ExpressionSyntaxErrorException(com.googlecode.aviator.exception.ExpressionSyntaxErrorException) Test(org.junit.Test)

Aggregations

Env (com.googlecode.aviator.utils.Env)28 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)10 Test (org.junit.Test)8 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)6 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 LRUMap (com.googlecode.aviator.utils.LRUMap)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Import (com.googlecode.aviator.annotation.Import)1 InterpretContext (com.googlecode.aviator.code.interpreter.InterpretContext)1 LoadIR (com.googlecode.aviator.code.interpreter.ir.LoadIR)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)1 ClassMethodFunction (com.googlecode.aviator.runtime.function.ClassMethodFunction)1