Search in sources :

Example 36 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType 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 37 with AviatorJavaType

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

the class StringIndexOfFunctionUnitTest method testCall.

@Test
public void testCall() {
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("s1", "hello");
    env.put("s2", "llo");
    env.put("ch", 'l');
    assertEquals(1L, this.function.call(null, new AviatorString("hello"), new AviatorString("ell")).getValue(null));
    assertEquals(2L, this.function.call(env, new AviatorString("hello"), new AviatorJavaType("s2")).getValue(env));
    assertEquals(2L, this.function.call(env, new AviatorString("hello"), new AviatorJavaType("ch")).getValue(env));
    assertEquals(2L, this.function.call(env, new AviatorJavaType("s1"), new AviatorJavaType("s2")).getValue(env));
    assertEquals(2L, this.function.call(env, new AviatorJavaType("s1"), new AviatorJavaType("ch")).getValue(env));
    assertEquals(2L, this.function.call(env, new AviatorJavaType("s1"), new AviatorString("llo")).getValue(env));
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 38 with AviatorJavaType

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

the class StringLengthFunctionUnitTest method testCall.

@Test
public void testCall() {
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("s1", "hello");
    env.put("s2", "hello world");
    assertEquals(5, function.call(null, new AviatorString("hello")).getValue(null));
    assertEquals(11, function.call(null, new AviatorString("hello world")).getValue(null));
    assertEquals(5, function.call(env, new AviatorJavaType("s1")).getValue(null));
    assertEquals(11, function.call(env, new AviatorJavaType("s2")).getValue(null));
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 39 with AviatorJavaType

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

the class MathCosFunctionUnitTest method testCall.

@Test
public void testCall() {
    assertEquals(Math.cos(30), this.function.call(null, AviatorNumber.valueOf(30)).getValue(null));
    assertEquals(Math.cos(1020.999), this.function.call(null, AviatorNumber.valueOf(1020.999)).getValue(null));
    assertEquals(Math.cos(400), this.function.call(null, AviatorNumber.valueOf(400)).getValue(null));
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("a", 10000);
    env.put("b", 9.0);
    assertEquals(Math.cos(10000), this.function.call(env, new AviatorJavaType("a")).getValue(null));
    assertEquals(Math.cos(9.0), this.function.call(env, new AviatorJavaType("b")).getValue(null));
}
Also used : HashMap(java.util.HashMap) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Test(org.junit.Test)

Example 40 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType 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

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