Search in sources :

Example 91 with AviatorObject

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

the class SeqReduceFunctionUnitTest method testReduce_IllegalArguments.

@Test(expected = IllegalArgumentException.class)
public void testReduce_IllegalArguments() {
    LinkedHashSet<Integer> a = new LinkedHashSet<Integer>();
    for (int i = 0; i < 10; i++) {
        a.add(i);
    }
    SeqReduceFunction fun = new SeqReduceFunction();
    AviatorObject result = fun.call(this.env, AviatorRuntimeJavaType.valueOf(a), new AviatorJavaType("+"));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Test(org.junit.Test)

Example 92 with AviatorObject

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

the class SeqSortFunctionUnitTest method testSort_Array.

@Test
public void testSort_Array() {
    Integer[] a = new Integer[10];
    int index = 0;
    for (int i = 9; i >= 0; i--) {
        a[index++] = i;
    }
    SeqSortFunction fun = new SeqSortFunction();
    AviatorObject result = fun.call(null, AviatorRuntimeJavaType.valueOf(a));
    index = 0;
    Integer[] dup = (Integer[]) result.getValue(null);
    assertFalse(Arrays.equals(a, dup));
    for (Integer i : dup) {
        assertEquals(i, index++);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Test(org.junit.Test)

Example 93 with AviatorObject

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

the class SeqSortFunctionUnitTest method testSort_HashSet.

@Test(expected = IllegalArgumentException.class)
public void testSort_HashSet() {
    Set<Integer> a = new HashSet<Integer>();
    int index = 0;
    for (int i = 9; i >= 0; i--) {
        a.add(i);
    }
    SeqSortFunction fun = new SeqSortFunction();
    AviatorObject result = fun.call(null, AviatorRuntimeJavaType.valueOf(a));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 94 with AviatorObject

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

the class SeqSortFunctionUnitTest method testSort_List.

@Test
public void testSort_List() {
    List<Integer> a = new ArrayList<Integer>();
    int index = 0;
    for (int i = 9; i >= 0; i--) {
        a.add(i);
    }
    SeqSortFunction fun = new SeqSortFunction();
    AviatorObject result = fun.call(null, AviatorRuntimeJavaType.valueOf(a));
    index = 0;
    List<Integer> dup = (List<Integer>) result.getValue(null);
    assertFalse(a.equals(dup));
    System.out.println(a);
    System.out.println(dup);
    for (Integer i : dup) {
        assertEquals(i, index++);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 95 with AviatorObject

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

the class SeqArrayFunction method variadicCall.

@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
    if (args == null || args.length == 0) {
        throw new IllegalArgumentException("Missing arguments for seq.array");
    }
    AviatorObject clazzVar = args[0];
    if (clazzVar == null || clazzVar.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Invalid class:" + (clazzVar == null ? "null" : clazzVar.desc(env)));
    }
    try {
        String name = ((AviatorJavaType) clazzVar).getName();
        Class<?> clazz = null;
        if (TypeUtils.PRIMITIVE_TYPES.containsKey(name)) {
            clazz = TypeUtils.PRIMITIVE_TYPES.get(name);
        } else {
            assert (env instanceof Env);
            clazz = ((Env) env).resolveClassSymbol(name);
        }
        Object ret = Array.newInstance(clazz, args.length - 1);
        for (int i = 1; i < args.length; i++) {
            ArrayUtils.set(ret, i - 1, Reflector.boxArg(clazz, args[i].getValue(env)));
        }
        return AviatorRuntimeJavaType.valueOf(ret);
    } catch (Throwable t) {
        throw Reflector.sneakyThrow(t);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Env(com.googlecode.aviator.utils.Env)

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