Search in sources :

Example 21 with AviatorJavaType

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

the class MathAbsFunctionUnitTest method testCall.

@Test
public void testCall() {
    Env env = TestUtils.getTestEnv();
    assertEquals(3, this.function.call(env, AviatorNumber.valueOf(-3)).getValue(null));
    assertEquals(3.9, this.function.call(env, AviatorNumber.valueOf(-3.9)).getValue(null));
    assertEquals(400, this.function.call(env, AviatorNumber.valueOf(400)).getValue(null));
    assertEquals(new BigInteger("300000000000000000000000000000000"), this.function.call(env, AviatorNumber.valueOf(new BigInteger("-300000000000000000000000000000000"))).getValue(null));
    assertEquals(new BigDecimal("300000000000000000000000000000000"), this.function.call(env, AviatorNumber.valueOf(new BigDecimal("-300000000000000000000000000000000.0000002223333"))).getValue(null));
    assertEquals(400, this.function.call(env, AviatorNumber.valueOf(400)).getValue(null));
    env.put("a", 300);
    env.put("b", -3.14);
    env.put("c", new BigInteger("-300000000000000000000000000000000"));
    env.put("d", new BigDecimal("-300000000000000000000000000000000.0000002223333"));
    assertEquals(300, this.function.call(env, new AviatorJavaType("a")).getValue(null));
    assertEquals(3.14, this.function.call(env, new AviatorJavaType("b")).getValue(null));
    assertEquals(new BigInteger("300000000000000000000000000000000"), this.function.call(env, new AviatorJavaType("c")).getValue(null));
    assertEquals(new BigDecimal("300000000000000000000000000000000"), this.function.call(env, new AviatorJavaType("d")).getValue(null));
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) BigInteger(java.math.BigInteger) Env(com.googlecode.aviator.utils.Env) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 22 with AviatorJavaType

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

the class SeqReduceFunctionUnitTest method testReduce_Collection.

@Test
public void testReduce_Collection() {
    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("+"), AviatorRuntimeJavaType.valueOf(0));
    assertNotNull(result);
    assertEquals(45, result.getValue(null));
}
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 23 with AviatorJavaType

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

the class SeqReduceFunctionUnitTest method testReduce_Map.

@Test
public void testReduce_Map() {
    Integer[] a = new Integer[10];
    for (int i = 0; i < a.length; i++) {
        a[i] = i;
    }
    SeqReduceFunction fun = new SeqReduceFunction();
    AviatorObject result = fun.call(this.env, AviatorRuntimeJavaType.valueOf(a), new AviatorJavaType("+"), AviatorRuntimeJavaType.valueOf(0));
    assertNotNull(result);
    assertEquals(45, result.getValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Test(org.junit.Test)

Example 24 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType in project graphql-calculator by graphql-calculator.

the class ListContain method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject listName, AviatorObject fieldName) {
    String fieldNameValue = ((AviatorJavaType) fieldName).getName();
    String listNameValue = ((AviatorJavaType) listName).getName();
    if (!env.containsKey(listNameValue) || !env.containsKey(fieldNameValue)) {
        return AviatorBoolean.FALSE;
    }
    Collection collection = (Collection) env.get(listNameValue);
    return AviatorBoolean.valueOf(collection.contains(env.get(fieldNameValue)));
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Collection(java.util.Collection)

Example 25 with AviatorJavaType

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

the class IsAFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object obj = arg1.getValue(env);
    if (obj == null) {
        return AviatorBoolean.FALSE;
    }
    if (arg2.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Invalid class type: " + arg2.desc(env));
    }
    try {
        Class<?> clazz = null;
        final String name = ((AviatorJavaType) arg2).getName();
        if (TypeUtils.PRIMITIVE_TYPES.containsKey(name)) {
            clazz = TypeUtils.PRIMITIVE_TYPES.get(name);
        } else {
            clazz = ((Env) env).resolveClassSymbol(name, false);
        }
        return AviatorBoolean.valueOf(clazz.isInstance(obj));
    } catch (ClassNotFoundException e) {
        throw Reflector.sneakyThrow(e);
    }
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)45 Test (org.junit.Test)29 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)25 HashMap (java.util.HashMap)16 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)11 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)10 Env (com.googlecode.aviator.utils.Env)10 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)6 LinkedList (java.util.LinkedList)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)2 BinaryFunction (com.googlecode.aviator.runtime.function.system.BinaryFunction)2 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 CodeGenerator (com.googlecode.aviator.code.CodeGenerator)1