add supported array and more testcase
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/7a9b30c8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/7a9b30c8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/7a9b30c8 Branch: refs/heads/0.16-dev Commit: 7a9b30c84c3abac180bc7988b6397a39c3bdb235 Parents: 148135c Author: jianbai.gbj <jianbai....@alibaba-inc.com> Authored: Wed Sep 20 11:00:56 2017 +0800 Committer: jianbai.gbj <jianbai....@alibaba-inc.com> Committed: Wed Sep 20 11:00:56 2017 +0800 ---------------------------------------------------------------------- .../com/taobao/weex/dom/binding/ELUtils.java | 10 +- .../com/taobao/weex/el/parse/ArrayStack.java | 8 ++ .../java/com/taobao/weex/el/parse/Block.java | 28 +++- .../java/com/taobao/weex/el/parse/Operator.java | 5 +- .../com/taobao/weex/el/parse/Operators.java | 22 ++- .../java/com/taobao/weex/el/parse/Parser.java | 135 ++++++++++++++----- .../java/com/taobao/weex/el/parse/Token.java | 3 + .../taobao/weex/ui/component/WXComponent.java | 4 +- .../weex/ui/component/binding/Statements.java | 30 ++--- .../com/taobao/weex/el/IfStatementTest.java | 30 ++--- .../java/com/taobao/weex/el/ParserTest.java | 59 +++++--- 11 files changed, 243 insertions(+), 91 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/dom/binding/ELUtils.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/dom/binding/ELUtils.java b/android/sdk/src/main/java/com/taobao/weex/dom/binding/ELUtils.java index 329fb99..c2f3bb8 100644 --- a/android/sdk/src/main/java/com/taobao/weex/dom/binding/ELUtils.java +++ b/android/sdk/src/main/java/com/taobao/weex/dom/binding/ELUtils.java @@ -21,8 +21,8 @@ package com.taobao.weex.dom.binding; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.taobao.weex.WXEnvironment; -import com.taobao.weex.el.parse.Block; import com.taobao.weex.el.parse.Parser; +import com.taobao.weex.el.parse.Token; import com.taobao.weex.utils.WXLogUtils; import java.util.Set; @@ -69,7 +69,7 @@ public class ELUtils { JSONObject object = (JSONObject) value; if(object.containsKey(BINDING)){ Object binding = object.get(BINDING); - if(!(binding instanceof Block)){ + if(!(binding instanceof Token)){ object.put(BINDING, Parser.parse(binding.toString())); } } @@ -79,7 +79,7 @@ public class ELUtils { && ((JSONObject)object.get(propsKey)).containsKey(BINDING)){ JSONObject propsValue = (JSONObject) object.get(propsKey); Object binding = propsValue.get(BINDING); - if(!(binding instanceof Block)){ + if(!(binding instanceof Token)){ propsValue.put(BINDING, Parser.parse(binding.toString())); } } @@ -101,7 +101,7 @@ public class ELUtils { return WXStatement.WX_FOR.equals(name); } - public static Block vifBlock(String code){ + public static Token vifBlock(String code){ return Parser.parse(code); } @@ -109,7 +109,7 @@ public class ELUtils { if(vfor instanceof JSONObject){ if(((JSONObject) vfor).containsKey(WXStatement.WX_FOR_LIST)){ Object list = ((JSONObject) vfor).get(WXStatement.WX_FOR_LIST); - if(!(list instanceof Block)){ + if(!(list instanceof Token)){ ((JSONObject) vfor).put(WXStatement.WX_FOR_LIST, Parser.parse(list.toString())); } } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/el/parse/ArrayStack.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/el/parse/ArrayStack.java b/android/sdk/src/main/java/com/taobao/weex/el/parse/ArrayStack.java index 5de2eaa..6a4c3bc 100644 --- a/android/sdk/src/main/java/com/taobao/weex/el/parse/ArrayStack.java +++ b/android/sdk/src/main/java/com/taobao/weex/el/parse/ArrayStack.java @@ -57,6 +57,14 @@ public class ArrayStack<T> { return stack.get(i); } + public T remove(int i) { + return stack.remove(i); + } + + public void add(int i, T t) { + stack.add(i, t); + } + public boolean isEmpty() { return stack.isEmpty(); } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/el/parse/Block.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/el/parse/Block.java b/android/sdk/src/main/java/com/taobao/weex/el/parse/Block.java index 98a7745..5db98f9 100644 --- a/android/sdk/src/main/java/com/taobao/weex/el/parse/Block.java +++ b/android/sdk/src/main/java/com/taobao/weex/el/parse/Block.java @@ -18,13 +18,15 @@ */ package com.taobao.weex.el.parse; +import com.alibaba.fastjson.JSONArray; + import java.util.List; /** * block, has more than one tokens, but only first will be execute. * Created by jianbai.gbj on 2017/8/28. */ -public class Block extends Token { +class Block extends Token { private List<Token> tokens; public Block(List<Token> tokens, int type) { super("", type); @@ -33,6 +35,21 @@ public class Block extends Token { @Override public Object execute(Object context){ + if(getType() == TYPE_ARRAY){ + if(tokens == null || tokens.size() == 0){ + return new JSONArray(4); + } + JSONArray arrayList = new JSONArray(tokens.size()); + for(int i=0; i<tokens.size(); i++){ + Token token = tokens.get(i); + if(token == null){ + arrayList.add(null); + }else{ + arrayList.add(token.execute(context)); + } + } + return arrayList; + } if(tokens == null || tokens.size() == 0){ return null; } @@ -46,6 +63,13 @@ public class Block extends Token { @Override public String toString() { - return "{" + tokens + '}'; + if(getType() == TYPE_ARRAY){ + return "" + tokens + ""; + }else { + if(tokens != null && tokens.size() == 1){ + return "{" + tokens.get(0) + '}'; + } + return "{" + tokens + '}'; + } } } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/el/parse/Operator.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/el/parse/Operator.java b/android/sdk/src/main/java/com/taobao/weex/el/parse/Operator.java index dd8f861..d597be8 100644 --- a/android/sdk/src/main/java/com/taobao/weex/el/parse/Operator.java +++ b/android/sdk/src/main/java/com/taobao/weex/el/parse/Operator.java @@ -23,7 +23,7 @@ package com.taobao.weex.el.parse; * Created by jianbai.gbj on 2017/8/28. * one operator, two operator, and three operator */ -public class Operator extends Token { +class Operator extends Token { public Token self; public Token first; public Token second; @@ -95,6 +95,9 @@ public class Operator extends Token { @Override public String toString() { + if(Operators.AND_NOT.equals(getToken())){ + return "{!" + self + "}"; + } if(self == null){ return "{" + first + getToken() + second + "}"; } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/el/parse/Operators.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/el/parse/Operators.java b/android/sdk/src/main/java/com/taobao/weex/el/parse/Operators.java index a0bed66..42dcf67 100644 --- a/android/sdk/src/main/java/com/taobao/weex/el/parse/Operators.java +++ b/android/sdk/src/main/java/com/taobao/weex/el/parse/Operators.java @@ -38,7 +38,17 @@ public class Operators { if(leftValue == null){ return null; } - Object value = right.execute(leftValue); + Object value = null; + if(right.getType() != Token.TYPE_IDENTIFIER){ + Object identifter = right.execute(context); + if(identifter instanceof Double){ + identifter = ((Double) identifter).intValue(); + } + String key = identifter == null ? "" : identifter.toString().trim(); + value = Operators.el(leftValue, key); + }else{ + value = right.execute(leftValue); + } if(value != null){ return value; } @@ -336,7 +346,8 @@ public class Operators { public static boolean isOpEnd(char op){ if(op == BRACKET_END || op == ARRAY_END - || op == SPACE){ + || op == SPACE + || op == ARRAY_SEPRATOR){ return true; } return false; @@ -363,6 +374,8 @@ public class Operators { public static final String DOT_STR = "."; public static final char ARRAY_START = '['; public static final String ARRAY_START_STR = "["; + public static final char ARRAY_SEPRATOR = ','; + public static final String ARRAY_SEPRATOR_STR = ","; public static final char ARRAY_END = ']'; public static final String ARRAY_END_STR = "]"; public static final String SPACE_STR = " "; @@ -423,6 +436,7 @@ public class Operators { OPERATORS_PRIORITY.put(BLOCK_END_STR, 0); OPERATORS_PRIORITY.put(BRACKET_END_STR, 0); OPERATORS_PRIORITY.put(SPACE_STR, 0); + OPERATORS_PRIORITY.put(ARRAY_SEPRATOR_STR, 0); OPERATORS_PRIORITY.put(ARRAY_END_STR, 0); OPERATORS_PRIORITY.put(OR, 1); OPERATORS_PRIORITY.put(AND, 1); @@ -445,7 +459,9 @@ public class Operators { OPERATORS_PRIORITY.put(AND_NOT, 11); OPERATORS_PRIORITY.put(DOT_STR, 15); - OPERATORS_PRIORITY.put(ARRAY_START_STR, 15); + + OPERATORS_PRIORITY.put(ARRAY_START_STR, 16); + OPERATORS_PRIORITY.put(BRACKET_START_STR, 17); OPERATORS_PRIORITY.put(BLOCK_START_STR, 17); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/el/parse/Parser.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/el/parse/Parser.java b/android/sdk/src/main/java/com/taobao/weex/el/parse/Parser.java index e60ffaa..c2f2b9b 100644 --- a/android/sdk/src/main/java/com/taobao/weex/el/parse/Parser.java +++ b/android/sdk/src/main/java/com/taobao/weex/el/parse/Parser.java @@ -22,9 +22,10 @@ import com.taobao.weex.WXEnvironment; import com.taobao.weex.utils.WXLogUtils; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; + /** * Created by furture on 2017/8/28. * simple expression parse, less ast node and save memory @@ -35,25 +36,27 @@ public class Parser { private int position; private ArrayStack<Token> stacks; private ArrayStack<Symbol> operators; + private ArrayStack<Symbol> punctuations; public Parser(String code){ this.code = code; this.position = 0; this.stacks = new ArrayStack<>(); this.operators = new ArrayStack<>(); + this.punctuations = new ArrayStack<>(); } - public Block parse(){ + public Token parse(){ while (hasNextToken()){ scanNextToken(); } while (!operators.isEmpty()){ Symbol op = operators.pop(); - Token token = createOperator(op); - if(token != null) { - stacks.push(token); - } + doOperator(op); + } + if(stacks.size() == 1){ + return stacks.pop(); } Block block = new Block(stacks.getList(), Token.TYPE_BLOCK); return block; @@ -63,7 +66,7 @@ public class Parser { /** * parse code to ast block. * */ - public static Block parse(String code){ + public static Token parse(String code){ try{ Parser parser = new Parser(code); return parser.parse(); @@ -81,10 +84,16 @@ public class Parser { if(ch == Operators.DOLLAR){ position++; return ch; + }else if(ch == Operators.ARRAY_SEPRATOR){ + punctuations.push(new Symbol(String.valueOf(ch), position)); + position++; + return ch; }else if(Character.isJavaIdentifierStart(ch)){ scanIdentifier(); }else if (ch == Operators.BRACKET_START || ch == Operators.BLOCK_START) { scanBracket(); + }else if (ch == Operators.ARRAY_START) { + scanArray(); }else if (ch == Operators.QUOTE || ch == Operators.SINGLE_QUOTE) { scanString(); }else if((ch == Operators.DOT && Character.isDigit(code.charAt(position + 1))) @@ -95,7 +104,8 @@ public class Parser { }else if(ch == Operators.CONDITION_IF_MIDDLE || ch == Operators.BRACKET_END || ch == Operators.BLOCK_END - || ch == Operators.SPACE){ + || ch == Operators.SPACE + || ch == Operators.ARRAY_END){ position++; return ch; }else{ @@ -105,6 +115,61 @@ public class Parser { } + void scanArray(){ + int stackSize = stacks.size(); + int opSize = operators.size(); + int type = Token.TYPE_IDENTIFIER; + if(position - 1 < 0 || !Character.isJavaIdentifierPart(code.charAt(position - 1))){ + type = Token.TYPE_ARRAY; + } + operators.push(new Symbol(Operators.ARRAY_START_STR, stacks.size())); + position++; + while (hasNextToken()){ + char token = scanNextToken(); + if(token == Operators.ARRAY_END){ + break; + } + } + + if(stacks.size() <= stackSize){ // empty bracket, none need, save memory + while (operators.size() > opSize){ + operators.pop(); + } + return; + } + + while (operators.size() > opSize){ + Symbol op = operators.pop(); + if(stacks.size() > stackSize){ + doOperator(op); + } + } + List<Token> tokens = new ArrayList<>(4); + for(int i=stackSize; i<stacks.size(); i++){ + tokens.add(stacks.get(i)); + } + while (stacks.size() > stackSize){ + stacks.pop(); + } + if(type == Token.TYPE_ARRAY || stacks.size() == 0){ + Block block = new Block(tokens, Token.TYPE_ARRAY); + stacks.push(block); + return; + } + + Token identifer = stacks.pop(); + Token second = null; + if(tokens.size() == 1){ + second = tokens.get(0); + }else{ + second = new Block(tokens, Token.TYPE_BLOCK); + } + Operator operator = new Operator(Operators.DOT_STR, type); + operator.first = identifer; + operator.second = second; + stacks.push(operator); + } + void scanBracket(){ int stackSize = stacks.size(); int opSize = operators.size(); @@ -134,13 +199,10 @@ public class Parser { while (operators.size() > opSize){ Symbol op = operators.pop(); if(stacks.size() > stackSize){ - Token token = createOperator(op); - if(token != null) { - stacks.push(token); - } + doOperator(op); } } - List<Token> tokens = new ArrayList<>(); + List<Token> tokens = new ArrayList<>(4); for(int i=stackSize; i<stacks.size(); i++){ tokens.add(stacks.get(i)); } @@ -184,10 +246,7 @@ public class Parser { String preOp = operators.peek().op; if(Operators.OPERATORS_PRIORITY.get(preOp) >= Operators.OPERATORS_PRIORITY.get(operator)){ Symbol op = operators.pop(); - Token token = createOperator(op); - if(token != null) { - stacks.push(token); - } + doOperator(op); } } if(!Operators.isOpEnd(operator)){ @@ -198,36 +257,46 @@ public class Parser { - Token createOperator(Symbol symbol){ + void doOperator(Symbol symbol){ String op = symbol.op; if(Operators.BRACKET_START_STR.equals(symbol.op) || Operators.BLOCK_START_STR.equals(symbol.op) + || Operators.ARRAY_START_STR.equals(symbol.op) || Operators.DOLLAR_STR.equals(symbol.op)){ - return null; + return; } if(Operators.BLOCK_START_STR.equals(symbol.op)){ - return null; + return; } - int secondMin = symbol.pos; - int firstMin = 0; - if(!operators.empty()){ - firstMin = Math.max(operators.peek().pos, firstMin); + int second = symbol.pos; + int first = Math.max(symbol.pos - 1, 0); + if(!operators.isEmpty()){ + first = Math.max(first, operators.peek().pos); } + Operator operator = new Operator(op, Token.TYPE_OPERATOR); if(Operators.AND_NOT.equals(op)){ - if(stacks.size() > secondMin) { - operator.self = stacks.pop(); - return operator; + if(stacks.size() > second) { + Token token = stacks.remove(second); + operator.self = token; + stacks.add(second, operator); + return; } - return operator; //invalid + return; //invalid } - if(stacks.size() > secondMin) { - operator.second = stacks.pop(); + if(stacks.size() > second) { + operator.second = stacks.remove(second); + }else{ + return; } - if(stacks.size() > firstMin) { - operator.first = stacks.pop(); + if(stacks.size() > first) { + operator.first = stacks.remove(first); + }else{ + if(operator.second == null){ + return; + } } - return operator; + stacks.add(first, operator); } /** http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/el/parse/Token.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/el/parse/Token.java b/android/sdk/src/main/java/com/taobao/weex/el/parse/Token.java index 49a964c..d3bcf5c 100644 --- a/android/sdk/src/main/java/com/taobao/weex/el/parse/Token.java +++ b/android/sdk/src/main/java/com/taobao/weex/el/parse/Token.java @@ -34,6 +34,9 @@ public class Token { public static final int TYPE_OPERATOR = 5; public static final int TYPE_BLOCK = 6; + public static final int TYPE_ARRAY = 7; + + private String token; private int type; http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java index d147d73..b1b2cbe 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java @@ -1733,7 +1733,9 @@ public abstract class WXComponent<T extends View> implements IWXObject, IWXActi } uiEvent.submit(); } else { - WXLogUtils.w("onRenderFinish", "createView() not called"); + if(WXEnvironment.isApkDebugable() && !isLazy()) { + WXLogUtils.w("onRenderFinish", "createView() not called"); + } } } } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/main/java/com/taobao/weex/ui/component/binding/Statements.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/binding/Statements.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/binding/Statements.java index bb75ea0..7d42706 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/component/binding/Statements.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/binding/Statements.java @@ -29,8 +29,8 @@ import com.taobao.weex.dom.WXEvent; import com.taobao.weex.dom.binding.ELUtils; import com.taobao.weex.dom.binding.WXStatement; import com.taobao.weex.el.parse.ArrayStack; -import com.taobao.weex.el.parse.Block; import com.taobao.weex.el.parse.Operators; +import com.taobao.weex.el.parse.Token; import com.taobao.weex.ui.component.WXComponent; import com.taobao.weex.ui.component.WXComponentFactory; import com.taobao.weex.ui.component.WXVContainer; @@ -117,10 +117,10 @@ public class Statements { WXStatement statement = attrs.getStatement(); if(statement != null){ WXDomObject parentDomObject = (WXDomObject) parent.getDomObject(); - Block vif = null; + Token vif = null; JSONObject vfor = null; - if(statement.get(WXStatement.WX_IF) instanceof Block){ - vif = (Block) statement.get(WXStatement.WX_IF); + if(statement.get(WXStatement.WX_IF) instanceof Token){ + vif = (Token) statement.get(WXStatement.WX_IF); } if(statement.get(WXStatement.WX_FOR) instanceof JSONObject){ vfor = (JSONObject) statement.get(WXStatement.WX_FOR); @@ -128,7 +128,7 @@ public class Statements { // execute v-for content if(vfor != null){ int renderIndex = parent.indexOf(component); - Block listBlock = (Block) vfor.get(WXStatement.WX_FOR_LIST); + Token listBlock = (Token) vfor.get(WXStatement.WX_FOR_LIST); String indexKey = vfor.getString(WXStatement.WX_FOR_INDEX); String itemKey = vfor.getString(WXStatement.WX_FOR_ITEM); Object data = null; @@ -326,9 +326,9 @@ public class Statements { Object value = entry.getValue(); String key = entry.getKey(); if(value instanceof JSONObject - && (((JSONObject) value).get(ELUtils.BINDING) instanceof Block)){ + && (((JSONObject) value).get(ELUtils.BINDING) instanceof Token)){ JSONObject binding = (JSONObject) value; - Block block = (Block) (binding.get(ELUtils.BINDING)); + Token block = (Token) (binding.get(ELUtils.BINDING)); Object blockValue = block.execute(context); dynamic.put(key, blockValue); }else if(value instanceof JSONArray){ @@ -341,9 +341,9 @@ public class Statements { continue; } if(element instanceof JSONObject - && (((JSONObject) element).get(ELUtils.BINDING) instanceof Block)){ + && (((JSONObject) element).get(ELUtils.BINDING) instanceof Token)){ JSONObject binding = (JSONObject) element; - Block block = (Block) (binding.get(ELUtils.BINDING)); + Token block = (Token) (binding.get(ELUtils.BINDING)); Object blockValue = block.execute(context); if(blockValue == null){ blockValue = ""; @@ -365,9 +365,9 @@ public class Statements { Object value = entry.getValue(); String key = entry.getKey(); if(value instanceof JSONObject - && (((JSONObject) value).get(ELUtils.BINDING) instanceof Block)){ + && (((JSONObject) value).get(ELUtils.BINDING) instanceof Token)){ JSONObject binding = (JSONObject) value; - Block block = (Block) (binding.get(ELUtils.BINDING)); + Token block = (Token) (binding.get(ELUtils.BINDING)); Object blockValue = block.execute(context); renderProps.put(key, blockValue); }else{ @@ -384,8 +384,8 @@ public class Statements { for(int i=0; i<array.size(); i++){ Object value = array.get(i); if(value instanceof JSONObject - && (((JSONObject) value).get(ELUtils.BINDING) instanceof Block)){ - Block block = (Block)(((JSONObject) value).get(ELUtils.BINDING)); + && (((JSONObject) value).get(ELUtils.BINDING) instanceof Token)){ + Token block = (Token)(((JSONObject) value).get(ELUtils.BINDING)); Object blockValue = block.execute(context); params.add(blockValue); }else{ @@ -394,8 +394,8 @@ public class Statements { } }else if(bindings instanceof JSONObject){ JSONObject binding = (JSONObject) bindings; - if(binding.get(ELUtils.BINDING) instanceof Block){ - Block block = (Block) binding.get(ELUtils.BINDING); + if(binding.get(ELUtils.BINDING) instanceof Token){ + Token block = (Token) binding.get(ELUtils.BINDING); Object blockValue = block.execute(context); params.add(blockValue); }else{ http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/test/java/com/taobao/weex/el/IfStatementTest.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/test/java/com/taobao/weex/el/IfStatementTest.java b/android/sdk/src/test/java/com/taobao/weex/el/IfStatementTest.java index ae9107f..3ba24f9 100644 --- a/android/sdk/src/test/java/com/taobao/weex/el/IfStatementTest.java +++ b/android/sdk/src/test/java/com/taobao/weex/el/IfStatementTest.java @@ -20,9 +20,9 @@ package com.taobao.weex.el; import com.alibaba.fastjson.JSONObject; import com.taobao.weex.el.parse.ArrayStack; -import com.taobao.weex.el.parse.Block; import com.taobao.weex.el.parse.Operators; import com.taobao.weex.el.parse.Parser; +import com.taobao.weex.el.parse.Token; import junit.framework.Assert; import junit.framework.TestCase; @@ -71,14 +71,14 @@ public class IfStatementTest extends TestCase { Assert.assertFalse(isIfTrue("1 < 1", createContext())); - Assert.assertFalse(isIfTrue("1 <= ", createContext())); - Assert.assertFalse(isIfTrue("1 < ", createContext())); - Assert.assertTrue(isIfTrue("1 > ", createContext())); - Assert.assertTrue(isIfTrue("1 >= ", createContext())); - Assert.assertFalse(isIfTrue(" >= 1", createContext())); - Assert.assertFalse(isIfTrue(" > 1", createContext())); - Assert.assertTrue(isIfTrue(" < 1", createContext())); - Assert.assertTrue(isIfTrue(" <= 1", createContext())); + Assert.assertFalse(isIfTrue("1 <= -1", createContext())); + Assert.assertFalse(isIfTrue("1 < -2 ", createContext())); + Assert.assertTrue(isIfTrue("1 > 0", createContext())); + Assert.assertTrue(isIfTrue("1 >= 0.1", createContext())); + Assert.assertFalse(isIfTrue("0 >= 1", createContext())); + Assert.assertFalse(isIfTrue(" -1 > 1", createContext())); + Assert.assertTrue(isIfTrue("0 < 1", createContext())); + Assert.assertTrue(isIfTrue(" -1 <= 1", createContext())); } @@ -91,7 +91,7 @@ public class IfStatementTest extends TestCase { Assert.assertTrue(isIfTrue("true || false", createContext())); Assert.assertTrue(isIfTrue("1 || false", createContext())); Assert.assertFalse(isIfTrue("false && false", createContext())); - Assert.assertFalse(isIfTrue("1 && ", createContext())); + Assert.assertFalse(isIfTrue("1 && false", createContext())); Assert.assertFalse(isIfTrue(" && 1", createContext())); Assert.assertTrue(isIfTrue("1 && 1 && 1 && true && true", createContext())); Assert.assertFalse(isIfTrue("false && 1 && 1 && true && true", createContext())); @@ -111,8 +111,8 @@ public class IfStatementTest extends TestCase { Assert.assertTrue(isIfTrue("((true) && 2 > 1) && (1)", createContext())); - Assert.assertFalse(isIfTrue("((true) && 2 > 1) && (1) && ()", createContext())); - Assert.assertTrue(isIfTrue("((true) && 2 > 1) && (1) || ()", createContext())); + Assert.assertTrue(isIfTrue("((true) && 2 > 1) && (1) && (1)", createContext())); + Assert.assertTrue(isIfTrue("((true) && 2 > 1) && (1) || (3)", createContext())); Assert.assertTrue(isIfTrue("1", createContext())); Assert.assertTrue(isIfTrue("true && 2 > 1 && 1", createContext())); @@ -120,9 +120,9 @@ public class IfStatementTest extends TestCase { public void testDebug(){ //System.out.println("isIfTrue" + isIfTrue("((true) && 2 > 1) && 1", createContext())); - Assert.assertFalse(isIfTrue("1 <= ", createContext())); + Assert.assertFalse(isIfTrue("1 <= 0", createContext())); Assert.assertTrue(isIfTrue("1 > -1 + 1", createContext())); - Assert.assertFalse(isIfTrue("1 < ", createContext())); + Assert.assertFalse(isIfTrue("1 < -1", createContext())); //Assert.assertTrue(isIfTrue("-1", createContext())); } @@ -137,7 +137,7 @@ public class IfStatementTest extends TestCase { } private boolean isIfTrue(String code, Object context){ - Block block = Parser.parse(code); + Token block = Parser.parse(code); System.out.println( code + " ==> " + block); return Operators.isTrue(block.execute(context)); } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7a9b30c8/android/sdk/src/test/java/com/taobao/weex/el/ParserTest.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/test/java/com/taobao/weex/el/ParserTest.java b/android/sdk/src/test/java/com/taobao/weex/el/ParserTest.java index 08f2dcf..8494a84 100644 --- a/android/sdk/src/test/java/com/taobao/weex/el/ParserTest.java +++ b/android/sdk/src/test/java/com/taobao/weex/el/ParserTest.java @@ -18,15 +18,19 @@ */ package com.taobao.weex.el; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.parser.Feature; +import com.alibaba.fastjson.serializer.SerializerFeature; import com.taobao.weex.el.parse.ArrayStack; -import com.taobao.weex.el.parse.Block; import com.taobao.weex.el.parse.Operators; import com.taobao.weex.el.parse.Parser; +import com.taobao.weex.el.parse.Token; import junit.framework.Assert; import junit.framework.TestCase; -import java.util.Stack; + /** * Created by furture on 2017/8/28. @@ -61,9 +65,9 @@ public class ParserTest extends TestCase { Assert.assertTrue(Double.isInfinite((double)Parser.parse("1+4/0").execute(null))); Assert.assertTrue(Double.isNaN((double)Parser.parse("4%0").execute(null))); - Assert.assertEquals(1.0, Parser.parse("1++*").execute(null)); + Assert.assertEquals(Parser.parse("1").execute(null), Parser.parse("1++*").execute(null)); Assert.assertEquals(3.0, Parser.parse("1+2+*").execute(null)); - Assert.assertEquals(1.0, Parser.parse("1+(2*+*)").execute(null)); + Assert.assertEquals(3.0, Parser.parse("1+(2*+*)").execute(null)); } public void testCondition(){ @@ -80,13 +84,16 @@ public class ParserTest extends TestCase { } public void testDebug(){ - Block block = Parser.parse("{{{item.name}}}"); + Parser.parse("item[1]").execute(this.createContext()); + Token block = Parser.parse("{{{item.name}}}"); /** * * */ - show("{{item.name}}"); + show("item.[1]"); + show("((true) && 2 > 1) && (1) && (1)"); System.out.println(block.execute(createContext()) + " " + Double.parseDouble(".0e6")); + show("1 > -1"); } @@ -105,17 +112,16 @@ public class ParserTest extends TestCase { Assert.assertEquals("hello world", Parser.parse("{{item.name}}").execute(createContext())); Assert.assertEquals("hello world", Parser.parse("{{{item.name}}}").execute(createContext())); Assert.assertEquals("hello world", Parser.parse("{{{{item.name}}}}").execute(createContext())); - Assert.assertEquals(21.0, Parser.parse("item[1] + index").execute(createContext())); + Assert.assertEquals(1, Parser.parse("1").execute(createContext())); + Assert.assertEquals(30.0, Parser.parse("item[1] + index").execute(createContext())); Assert.assertEquals("hello world20", Parser.parse("item.name + index").execute(createContext())); Assert.assertEquals("hello world20", Parser.parse("item[name] + index").execute(createContext())); - Assert.assertEquals("hello world20", Parser.parse("item[name + index").execute(createContext())); - Assert.assertEquals("hello world20", Parser.parse("item.name] + index").execute(createContext())); Assert.assertEquals(21.0, Parser.parse("1 + index").execute(createContext())); Assert.assertEquals(11.0, Parser.parse("1 + index/2").execute(createContext())); Assert.assertEquals(20.0, Parser.parse("item.name/10 + index").execute(createContext())); Assert.assertEquals(31.0, Parser.parse("item.name.length + index").execute(createContext())); - Assert.assertEquals(21.0, Parser.parse("item.length + index").execute(createContext())); + Assert.assertEquals(22.0, Parser.parse("item.length + index").execute(createContext())); Assert.assertEquals(36.0, Parser.parse("count * ${ratio}").execute(createContext())); Assert.assertEquals(36.0, Parser.parse("count * ${ratio}").execute(createContext())); Assert.assertEquals(36.0, Parser.parse("count * ${{ratio}}").execute(createContext())); @@ -147,29 +153,46 @@ public class ParserTest extends TestCase { private void show(String code){ - Block block = Parser.parse(code); + Token block = Parser.parse(code); System.out.println( code + " ==> " + block); } + public void testArray(){ + int disableDecimalFeature = JSON.DEFAULT_PARSER_FEATURE &= ~Feature.UseBigDecimal.getMask(); + Assert.assertEquals(JSONArray.parse("[2, 3, 3]", disableDecimalFeature), Parser.parse("[2, 3, 3]").execute(null)); + Assert.assertEquals(JSONArray.parse("[2.0, 3, 3]", disableDecimalFeature), Parser.parse("[1+1, 3, 3]").execute(null)); + Assert.assertEquals(JSONArray.parse("[2.0, 3,[3, 3], 3]", disableDecimalFeature), Parser.parse("[1+1, 3, [3, 3], 3]").execute(null)); + Assert.assertEquals(JSONArray.parse("['hello world', 3,[3, 3], 3]", disableDecimalFeature), Parser.parse("[item.name, 3, [3, 3], 3]").execute(this.createContext())); + Assert.assertEquals(JSONArray.parse("[10, 3,[3, 3], 3]", disableDecimalFeature), Parser.parse("[item[1], 3, [3, 3], 3]").execute(this.createContext())); + Assert.assertEquals(JSONArray.parse("[1, 3,[3, 3], 3]", disableDecimalFeature), Parser.parse("[1, 3, [3, 3], 3]").execute(this.createContext())); + Assert.assertEquals(JSONArray.parse("[1, 3,['hello world', 3], 3]", disableDecimalFeature), Parser.parse("[1, 3, [item.name, 3], 3]").execute(this.createContext())); + Assert.assertEquals(JSONArray.parse("[1, [1, 2],[3, 3], 3]", disableDecimalFeature), Parser.parse("[1, [1, 2], [3, 3], 3]").execute(this.createContext())); + Assert.assertEquals(null, Parser.parse("item[name + index]").execute(this.createContext())); + Assert.assertEquals("hello world", Parser.parse("item[name]").execute(this.createContext())); + Assert.assertEquals("hello world", Parser.parse("item['name']").execute(this.createContext())); + Assert.assertEquals(10, Parser.parse("item[1]").execute(this.createContext())); + Assert.assertEquals(JSONArray.parse("[false, [1, 2],[3, 3], 3]", disableDecimalFeature), Parser.parse("[!true, [1, 2], [3, 3], 3]").execute(this.createContext())); + Assert.assertEquals(JSONArray.parse("[2, 3, 3]", disableDecimalFeature), Parser.parse("[(2), 3, 3]").execute(null)); + Assert.assertEquals(JSONArray.parse("[2, 3, 3]", disableDecimalFeature), Parser.parse("([2, 3, 3])").execute(null)); + Assert.assertEquals(JSONArray.parse("[2, 3, 3]", disableDecimalFeature), Parser.parse("([2, 3, (3)])").execute(null)); + + } + public void testOperator(){ Parser parser = new Parser("(1 + 3)/3*3"); - Block block = parser.parse(); + Token block = parser.parse(); System.out.println(block.execute(null)); System.out.println(block); - System.out.println(block.getTokens().size()); - Parser parser2 = new Parser("1 + +++"); block = parser2.parse(); System.out.println(block); - System.out.println(block.getTokens().size()); - String[] values = {null, null}; for (Object value : values) { @@ -189,6 +212,7 @@ public class ParserTest extends TestCase { data.put("ratio", 6); data.put("count", 6); data.getJSONObject("item").put("name", "hello world"); + data.getJSONObject("item").put("1", 10); ArrayStack context = new ArrayStack(); context.push(data); @@ -197,4 +221,7 @@ public class ParserTest extends TestCase { + + + }