use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.
the class AviatorJavaTypeUnitTest method testCompareDate.
@Test
public void testCompareDate() {
Map<String, Object> env = new HashMap<String, Object>();
final Date date = new Date();
String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(date);
env.put("date", date);
env.put("dateStr", dateStr);
assertEquals(0, new AviatorJavaType("date").innerCompare(new AviatorJavaType("date"), env));
assertEquals(0, new AviatorJavaType("date").innerCompare(new AviatorString(dateStr), env));
assertEquals(1, new AviatorJavaType("date").innerCompare(new AviatorString("1990-03-21 04:56:30:0"), env));
assertEquals(-1, new AviatorJavaType("date").innerCompare(new AviatorString("2200-03-21 04:56:30:0"), env));
assertEquals(0, new AviatorJavaType("date").innerCompare(new AviatorJavaType("dateStr"), env));
try {
new AviatorJavaType("date").innerCompare(AviatorNumber.valueOf(191), env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
new AviatorJavaType("date").innerCompare(new AviatorString("2200-03 21 04:56:30:0"), env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
new AviatorJavaType("date").innerCompare(AviatorBoolean.TRUE, env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
}
use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.
the class AviatorJavaTypeUnitTest method testNeg.
@Test
public void testNeg() {
AviatorJavaType intType = new AviatorJavaType("intType");
AviatorJavaType boolType = new AviatorJavaType("boolType");
AviatorJavaType stringType = new AviatorJavaType("stringType");
AviatorJavaType charType = new AviatorJavaType("charType");
AviatorJavaType dateType = new AviatorJavaType("dateType");
Map<String, Object> env = new HashMap<String, Object>();
env.put("intType", 3);
env.put("boolType", Boolean.FALSE);
env.put("stringType", "hello");
env.put("charType", 'c');
env.put("dateType", new Date());
assertEquals(-3L, intType.neg(env).getValue(env));
try {
boolType.neg(env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
stringType.neg(env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
charType.neg(env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
dateType.neg(env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
}
use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.
the class SeqPredicateFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, AviatorObject arg1) {
if (this.propertyName != null) {
String propertyNameStr = this.propertyName.stringValue(env);
Object target = arg1.getValue(env);
try {
Object property = Reflector.getProperty(target, propertyNameStr);
arg1 = AviatorRuntimeJavaType.valueOf(property);
} catch (NoSuchPropertyException e) {
throw new IllegalArgumentException("Fail to get property <" + propertyNameStr + "> from <" + arg1.desc(env) + ">", e);
}
}
switch(this.opType) {
case EQ:
return arg1.compare(this.value, env) == 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
case NEQ:
return arg1.compare(this.value, env) != 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
case LT:
return arg1.compare(this.value, env) < 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
case LE:
return arg1.compare(this.value, env) <= 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
case GE:
return arg1.compare(this.value, env) >= 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
case GT:
return arg1.compare(this.value, env) > 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
default:
throw new ExpressionRuntimeException(getName() + " is not a relation operator");
}
}
use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.
the class GrammarUnitTest method testTernaryOperator.
/*
* 测试三元表达式
*/
@Test
public void testTernaryOperator() {
Map<String, Object> env = new HashMap<String, Object>();
int i = 0;
float f = 3.14f;
String email = "killme2008@gmail.com";
char ch = 'a';
boolean t = true;
env.put("i", i);
env.put("f", f);
env.put("email", email);
env.put("ch", ch);
env.put("t", t);
assertEquals(1, this.instance.execute("2>1?1:0"));
assertEquals(0, this.instance.execute("2<1?1:0"));
assertEquals(f, (Float) this.instance.execute("false?i:f", env), 0.001);
assertEquals(i, this.instance.execute("true?i:f", env));
assertEquals("killme2008", this.instance.execute("email=~/([\\w0-9]+)@\\w+\\.\\w+/ ? $1:'unknow'", env));
assertEquals(f, (Float) this.instance.execute("ch!='a'?i:f", env), 0.001);
assertEquals(i, this.instance.execute("ch=='a'?i:f", env));
assertEquals(email, this.instance.execute("t?email:ch", env));
// 多层嵌套
assertEquals(ch, this.instance.execute("t? i>0? f:ch : email", env));
assertEquals(email, this.instance.execute("!t? i>0? f:ch : f>3?email:ch", env));
// 使用括号
assertEquals(email, this.instance.execute("!t? (i>0? f:ch) :( f>3?email:ch)", env));
// 测试错误情况
try {
this.instance.execute("f?1:0", env);
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
this.instance.execute("'hello'?1:0");
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
this.instance.execute("/test/?1:0");
Assert.fail();
} catch (ExpressionRuntimeException e) {
}
try {
this.instance.execute("!t? (i>0? f:ch) : f>3?email:ch)", env);
Assert.fail();
} catch (ExpressionSyntaxErrorException e) {
}
try {
this.instance.execute("!t? (i>0? f:ch : (f>3?email:ch)", env);
Assert.fail();
} catch (ExpressionSyntaxErrorException e) {
}
}
Aggregations