use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class FunctionTest method testOverloadLogicOperator.
@Test
public void testOverloadLogicOperator() {
// instance.setOption(Options.TRACE_EVAL, true);
this.instance.addOpFunction(OperatorType.AND, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
return arg1.add(arg2, env);
}
@Override
public String getName() {
return "&&";
}
});
this.instance.addOpFunction(OperatorType.OR, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
return arg1.sub(arg2, env);
}
@Override
public String getName() {
return "||";
}
});
assertEquals(3, this.instance.execute("1 && 2"));
assertEquals(6, this.instance.execute("1 && 2 && 3"));
assertEquals(0, this.instance.execute("1 && 2 || 3"));
assertEquals(-4, this.instance.execute("1 || 2 || 3"));
this.instance.removeOpFunction(OperatorType.AND);
this.instance.removeOpFunction(OperatorType.OR);
}
use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class GrammarUnitTest method testReturnNullCustomFunction.
@Test
public void testReturnNullCustomFunction() {
this.instance.addFunction(new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
try {
return AviatorLong.valueOf(FunctionUtils.getJavaObject(arg1, env));
} catch (Exception e) {
return null;
}
}
@Override
public String getName() {
return "SafeCastLong";
}
});
assertNull(this.instance.execute("SafeCastLong('a')"));
}
use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class OperatorFunctionTest method testCustomUnaryOperatorFunction.
@Test
public void testCustomUnaryOperatorFunction() {
try {
assertEquals("3", this.instance.exec("!3"));
fail();
} catch (Exception e) {
}
this.instance.addOpFunction(OperatorType.NOT, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
return new AviatorString(arg1.getValue(env).toString());
}
@Override
public String getName() {
return "!";
}
});
assertEquals("3", this.instance.exec("!3"));
assertEquals("4", this.instance.exec("!a", 4));
assertEquals("3.2", this.instance.exec("!3.2"));
}
use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class AviatorEvaluatorInstanceUnitTest method testIssue245.
@Test
public void testIssue245() {
// test EQ
this.instance.addOpFunction(OperatorType.EQ, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
if (!arg1.getValue(env).equals(arg2.getValue(env))) {
return AviatorBoolean.TRUE;
}
return AviatorBoolean.FALSE;
}
@Override
public String getName() {
return OperatorType.EQ.getToken();
}
});
assertFalse((boolean) this.instance.execute("1==1"));
assertTrue((boolean) this.instance.execute("0==1"));
assertFalse((boolean) this.instance.execute("a==b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
assertTrue((boolean) this.instance.execute("a==b", AviatorEvaluator.newEnv("a", 1, "b", 0)));
// test Assignment
assertEquals(1, this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
assertEquals(2, this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 2)));
this.instance.addOpFunction(OperatorType.ASSIGNMENT, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
int c = arg1.compare(arg2, env);
return AviatorBoolean.valueOf(c == 0);
}
@Override
public String getName() {
return OperatorType.ASSIGNMENT.getToken();
}
});
assertTrue((boolean) this.instance.execute("1=1"));
assertFalse((boolean) this.instance.execute("0=1"));
assertTrue((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 0)));
assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 2)));
assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", "hello", "b", "world")));
assertTrue((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", "hello", "b", "hello")));
}
use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class CustomDivideExample method main.
public static void main(final String[] args) {
AviatorEvaluator.getInstance().addOpFunction(OperatorType.DIV, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
if (arg1.getAviatorType() == AviatorType.Long && arg2.getAviatorType() == AviatorType.Long) {
// If arg1 and arg2 are all long type.
// Cast arg2 into double and divided by arg1.
double d = FunctionUtils.getNumberValue(arg1, env).longValue() / FunctionUtils.getNumberValue(arg2, env).doubleValue();
return AviatorDouble.valueOf(d);
} else {
// Otherwise, call aviatorscript's div function.
return arg1.div(arg2, env);
}
}
@Override
public String getName() {
return OperatorType.DIV.getToken();
}
});
System.out.println(AviatorEvaluator.execute("1/2"));
}
Aggregations