Search in sources :

Example 71 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqFilterFunctionUnitTest method testFilter_Collection.

@Test
public void testFilter_Collection() {
    final List<String> strs = new LinkedList<String>();
    for (int i = 0; i < 10; i++) {
        strs.add("hello" + i);
    }
    SeqPredicateFunction predicate = new SeqPredicateFunction("eq_temp_1", OperatorType.EQ, new AviatorString("hello1"));
    Map<String, Object> env = new HashMap<String, Object>();
    env.putAll(AviatorEvaluator.FUNC_MAP);
    env.put("eq_temp_1", predicate);
    SeqFilterFunction fun = new SeqFilterFunction();
    AviatorObject result = fun.call(env, AviatorRuntimeJavaType.valueOf(strs), new AviatorJavaType("eq_temp_1"));
    LinkedList list = (LinkedList) result.getValue(null);
    assertEquals(1, list.size());
    for (Object i : list) {
        assertEquals("hello1", i);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 72 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqFilterFunctionUnitTest method testFilter_Array.

@Test
public void testFilter_Array() {
    final String[] strs = new String[10];
    for (int i = 0; i < strs.length; i++) {
        strs[i] = "hello" + i;
    }
    SeqPredicateFunction predicate = new SeqPredicateFunction("eq_temp_1", OperatorType.EQ, new AviatorString("hello1"));
    Map<String, Object> env = new HashMap<String, Object>();
    env.putAll(AviatorEvaluator.FUNC_MAP);
    env.put("eq_temp_1", predicate);
    SeqFilterFunction fun = new SeqFilterFunction();
    AviatorObject result = fun.call(env, AviatorRuntimeJavaType.valueOf(strs), new AviatorJavaType("eq_temp_1"));
    Object[] array = (Object[]) result.getValue(null);
    assertEquals(1, array.length);
    for (Object i : array) {
        assertEquals("hello1", i);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 73 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqIncludeFunctionUnitTest method testInclude_HashSet.

@Test
public void testInclude_HashSet() {
    Set<Integer> a = new HashSet<Integer>();
    a.add(1);
    a.add(-100);
    a.add(null);
    AviatorObject arg1 = AviatorRuntimeJavaType.valueOf(a);
    SeqIncludeFunction fun = new SeqIncludeFunction();
    AviatorObject result = fun.call(null, arg1, AviatorRuntimeJavaType.valueOf(-100));
    assertTrue(result.booleanValue(null));
    // contains null Object
    result = fun.call(null, arg1, AviatorRuntimeJavaType.valueOf(null));
    assertTrue(result.booleanValue(null));
    result = fun.call(null, arg1, AviatorNil.NIL);
    assertTrue(result.booleanValue(null));
    // not match
    result = fun.call(null, arg1, AviatorRuntimeJavaType.valueOf(1000));
    assertFalse(result.booleanValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 74 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqMakePredicateFunctionUnitTest method testMakePredicate_FixedValue.

@Test
public void testMakePredicate_FixedValue() {
    SeqMakePredicateFunFunction fun = new SeqMakePredicateFunFunction("eq", OperatorType.EQ, AviatorRuntimeJavaType.valueOf("hello"));
    Map<String, Object> env = new HashMap<String, Object>();
    AviatorObject predicateName = fun.call(env);
    assertNotNull(predicateName);
    AviatorFunction predicate = (AviatorFunction) predicateName.getValue(env);
    assertNotNull(predicate);
    AviatorObject result = predicate.call(null, AviatorRuntimeJavaType.valueOf("hello"));
    // equals self
    assertTrue(result.booleanValue(null));
    result = predicate.call(null, AviatorRuntimeJavaType.valueOf("he11o"));
    // equals self
    assertFalse(result.booleanValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashMap(java.util.HashMap) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Test(org.junit.Test)

Example 75 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqMapFunctionUnitTest method testMap_Array.

@Test
public void testMap_Array() {
    final String[] strs = new String[10];
    for (int i = 0; i < strs.length; i++) {
        strs[i] = "hello";
    }
    SeqMapFunction fun = new SeqMapFunction();
    AviatorObject result = fun.call(AviatorEvaluator.FUNC_MAP, AviatorRuntimeJavaType.valueOf(strs), new AviatorJavaType("string.length"));
    Object[] array = (Object[]) result.getValue(null);
    for (Object i : array) {
        assertEquals(5, i);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Test(org.junit.Test)

Aggregations

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)110 Test (org.junit.Test)51 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)25 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)22 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)16 HashMap (java.util.HashMap)12 Map (java.util.Map)11 List (java.util.List)10 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)9 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 Env (com.googlecode.aviator.utils.Env)6 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)5 LinkedList (java.util.LinkedList)5 Collection (java.util.Collection)4 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3