Search in sources :

Example 11 with AviatorJavaType

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

the class PrintFunctionUnitTest method testCall_WithTwoArgument.

@Test
public void testCall_WithTwoArgument() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("out", out);
    this.fun.call(env, new AviatorJavaType("out"), new AviatorString("hello"));
    out.flush();
    out.close();
    byte[] data = out.toByteArray();
    assertEquals("hello", new String(data));
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 12 with AviatorJavaType

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

the class SeqFilterFunctionUnitTest method testFilter_map.

@Test
public void testFilter_map() {
    Map<Integer, String> data = new HashMap<>();
    for (int i = 0; i < 5; i++) {
        data.put(i, "a" + i);
    }
    Map<String, Object> env = new HashMap<>();
    SeqPredicateFunction predicate = new SeqPredicateFunction("eq_temp_1", OperatorType.EQ, new AviatorString("a1"), new AviatorString("value"));
    env.putAll(AviatorEvaluator.FUNC_MAP);
    env.put("eq_temp_1", predicate);
    SeqFilterFunction fun = new SeqFilterFunction();
    AviatorObject result = fun.call(env, AviatorRuntimeJavaType.valueOf(data), new AviatorJavaType("eq_temp_1"));
    Map map = ((Map) result.getValue(null));
    assertEquals(1, map.size());
    for (Object value : map.values()) {
        assertEquals("a1", value.toString());
    }
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with AviatorJavaType

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

the class SeqReduceFunctionUnitTest method testReduce_String.

@Test
public void testReduce_String() {
    SeqReduceFunction fun = new SeqReduceFunction();
    AviatorObject result = fun.call(AviatorEvaluator.FUNC_MAP, AviatorRuntimeJavaType.valueOf("hello"), new AviatorJavaType("+"), AviatorRuntimeJavaType.valueOf(0));
    assertEquals("0hello", result.getValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Test(org.junit.Test)

Example 14 with AviatorJavaType

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

the class MathLog10FunctionUnitTest method testCall.

@Test
public void testCall() {
    assertEquals(2.0, this.function.call(null, AviatorNumber.valueOf(100)).getValue(null));
    assertEquals(3.0, this.function.call(null, AviatorNumber.valueOf(1000)).getValue(null));
    assertEquals(2.60, 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(4.0, this.function.call(env, new AviatorJavaType("a")).getValue(null));
    assertEquals(0.9542, 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 15 with AviatorJavaType

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

the class MathPowFunctionUnitTest method testCall.

@Test
public void testCall() {
    assertEquals(Math.pow(3, 2), this.function.call(null, AviatorNumber.valueOf(3), AviatorNumber.valueOf(2)).getValue(null));
    assertEquals(Math.pow(10.9, 4.0), this.function.call(null, AviatorNumber.valueOf(10.9), AviatorNumber.valueOf(4.0)).getValue(null));
    assertEquals(Math.pow(20, -3), this.function.call(null, AviatorNumber.valueOf(20), AviatorNumber.valueOf(-3)).getValue(null));
    assertEquals(new BigInteger("1000000000000000000000000000000000000").pow(3), this.function.call(null, AviatorNumber.valueOf(new BigInteger("1000000000000000000000000000000000000")), AviatorNumber.valueOf(3)).getValue(null));
    assertEquals(new BigDecimal("1000000000000000000000000000000000000.000022222").pow(3, RuntimeUtils.getMathContext(null)), this.function.call(null, AviatorNumber.valueOf(new BigDecimal("1000000000000000000000000000000000000.000022222")), AviatorNumber.valueOf(3)).getValue(null));
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("a", 3.0);
    env.put("b", 9.0);
    assertEquals(Math.pow(3.0, 9.0), this.function.call(env, new AviatorJavaType("a"), new AviatorJavaType("b")).getValue(null));
    assertEquals(Math.pow(9.0, 4), this.function.call(env, new AviatorJavaType("b"), AviatorNumber.valueOf(4)).getValue(null));
    assertEquals(Math.pow(-2.3, 3.0), this.function.call(env, AviatorNumber.valueOf(-2.3), new AviatorJavaType("a")).getValue(null));
}
Also used : HashMap(java.util.HashMap) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) BigInteger(java.math.BigInteger) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

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