Skip to content

Commit

Permalink
add errorList demo
Browse files Browse the repository at this point in the history
  • Loading branch information
baoxingjie committed Jan 9, 2018
1 parent e15a478 commit 4b33710
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 背景介绍

由阿里的电商业务规则、表达式(布尔组合)、特殊数学公式计算(高精度)、语法分析、脚本二次定制等强需求而设计的一门动态脚本引擎解析工具。
在阿里集团有很强的影响力,同时为了自身不断优化、发扬开源贡献精神,于2009年开源
在阿里集团有很强的影响力,同时为了自身不断优化、发扬开源贡献精神,于2012年开源

## 依赖和调用说明

Expand Down
52 changes: 52 additions & 0 deletions src/test/java/com/ql/util/express/test/ErrorListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ql.util.express.test;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import com.ql.util.express.InstructionSet;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class ErrorListTest {

public boolean isVIP(String nick)
{
return nick.contains("vip_");
}
public Integer getUserLevel(String nick)
{
if(nick.contains("vip_")){
return 3;
}
return 2;
}

@Test
public void testErrorList() throws Exception {
ExpressRunner runner = new ExpressRunner(false, false);
runner.addFunctionOfServiceMethod("isVIP", new ErrorListTest(),
"isVIP", new Class[]{String.class},"$1不是vip");
runner.addFunctionOfServiceMethod("getUserLevel", new ErrorListTest(),
"getUserLevel", new Class[]{String.class},"");
runner.addOperatorWithAlias("大于",">","用户等级不够");

runner.addOperatorWithAlias("是否VIP","isVIP","亲爱的$1,你还不是VIP用户");
testExample(runner,"isVIP('vip_11111')");
testExample(runner,"isVIP('common_11111')");

testExample(runner,"getUserLevel('vip_11111') 大于 2");
testExample(runner,"getUserLevel('common_11111') 大于 2");
}

public void testExample(ExpressRunner runner,String express) throws Exception {
IExpressContext<String, Object> context = new DefaultContext<String, Object>();
List<String> errorList = new ArrayList<String>();
Object r = runner.execute(express,context,errorList,false,false,null);
System.out.println(r);
for(int i=0;i<errorList.size();i++){
System.out.println(errorList.get(i));
}
}
}

0 comments on commit 4b33710

Please sign in to comment.