Search in sources :

Example 31 with AviatorString

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

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

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

the class OperatorFunctionTest method testCustomBinOperatorFunction.

@Test
public void testCustomBinOperatorFunction() {
    try {
        assertEquals("hello world", this.instance.execute("'hello' & ' world'"));
        fail();
    } catch (Exception e) {
    }
    this.instance.addOpFunction(OperatorType.BIT_AND, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            return new AviatorString(arg1.getValue(env).toString() + arg2.getValue(env).toString());
        }

        @Override
        public String getName() {
            return "&";
        }
    });
    assertEquals("43", this.instance.exec("a&3", 4));
    assertEquals("43", this.instance.exec("4&3", 4));
    assertEquals("hello world", this.instance.execute("'hello' & ' world'"));
    assertEquals("hello world", this.instance.exec("a&' world'", "hello"));
    assertEquals("hello3 world", this.instance.exec("a & 3 & ' world'", "hello"));
    Map<String, Object> env = new HashMap<>();
    env.put("list", Arrays.asList(1, 2, 3));
    assertEquals("123", this.instance.execute("reduce(list, &, '')", env));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) 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) Test(org.junit.Test)

Aggregations

AviatorString (com.googlecode.aviator.runtime.type.AviatorString)33 Test (org.junit.Test)19 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)14 HashMap (java.util.HashMap)11 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)3 Date (java.util.Date)3 List (java.util.List)3 Map (java.util.Map)3 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)2 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)2 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)2 PrintStream (java.io.PrintStream)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 ID (cn.devezhao.persist4j.engine.ID)1 AviatorEvaluator (com.googlecode.aviator.AviatorEvaluator)1 AviatorNil (com.googlecode.aviator.runtime.type.AviatorNil)1 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)1