From: James Ahlborn Date: Wed, 6 Sep 2017 01:53:52 +0000 (+0000) Subject: add some initial default function unit tests; fix some parse bugs X-Git-Tag: jackcess-2.2.0~24^2~33 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=392dfba3ef4ec2151ba9ac6772a3361d7b4e65f3;p=jackcess.git add some initial default function unit tests; fix some parse bugs git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1114 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java index 9abe334..706f42c 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java @@ -239,7 +239,7 @@ public class DefaultFunctions return BuiltinOperators.ZERO_VAL; } long lv = param1.getAsLong(); - return BuiltinOperators.toValue(Long.toHexString(lv)); + return BuiltinOperators.toValue(Long.toHexString(lv).toUpperCase()); } }); @@ -533,7 +533,7 @@ public class DefaultFunctions } }); - public static final Function LTRIM = registerFunc(new Func1NullIsNull("LTrim") { + public static final Function LTRIM = registerStringFunc(new Func1NullIsNull("LTrim") { @Override protected Value eval1(EvalContext ctx, Value param1) { String str = param1.getAsString(); @@ -541,7 +541,7 @@ public class DefaultFunctions } }); - public static final Function RTRIM = registerFunc(new Func1NullIsNull("RTrim") { + public static final Function RTRIM = registerStringFunc(new Func1NullIsNull("RTrim") { @Override protected Value eval1(EvalContext ctx, Value param1) { String str = param1.getAsString(); @@ -549,7 +549,7 @@ public class DefaultFunctions } }); - public static final Function TRIM = registerFunc(new Func1NullIsNull("Trim") { + public static final Function TRIM = registerStringFunc(new Func1NullIsNull("Trim") { @Override protected Value eval1(EvalContext ctx, Value param1) { String str = param1.getAsString(); @@ -597,7 +597,7 @@ public class DefaultFunctions } }); - public static final Function STRREVERSE = registerStringFunc(new Func1("StrReverse") { + public static final Function STRREVERSE = registerFunc(new Func1("StrReverse") { @Override protected Value eval1(EvalContext ctx, Value param1) { String str = param1.getAsString(); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/ExpressionTokenizer.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/ExpressionTokenizer.java index 596b3f0..463f147 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/ExpressionTokenizer.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/ExpressionTokenizer.java @@ -120,27 +120,16 @@ class ExpressionTokenizer case IS_COMP_FLAG: - switch(exprType) { - case DEFAULT_VALUE: - - // special case - if((c == EQUALS_CHAR) && (buf.prevPos() == 0)) { - // a leading equals sign indicates how a default value should be - // evaluated - tokens.add(new Token(TokenType.OP, String.valueOf(c))); - continue; - } - // def values can't have cond at top level - throw new IllegalArgumentException( - exprType + " cannot have top-level conditional " + buf); - - case FIELD_VALIDATOR: - case RECORD_VALIDATOR: - - tokens.add(new Token(TokenType.OP, parseCompOp(c, buf))); - break; + // special case for default values + if((exprType == Type.DEFAULT_VALUE) && (c == EQUALS_CHAR) && + (buf.prevPos() == 0)) { + // a leading equals sign indicates how a default value should be + // evaluated + tokens.add(new Token(TokenType.OP, String.valueOf(c))); + continue; } - + + tokens.add(new Token(TokenType.OP, parseCompOp(c, buf))); break; case IS_DELIM_FLAG: diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java new file mode 100644 index 0000000..0e2e85e --- /dev/null +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -0,0 +1,54 @@ +/* +Copyright (c) 2016 James Ahlborn + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package com.healthmarketscience.jackcess.impl.expr; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.healthmarketscience.jackcess.DatabaseBuilder; +import com.healthmarketscience.jackcess.TestUtil; +import com.healthmarketscience.jackcess.expr.EvalContext; +import com.healthmarketscience.jackcess.expr.Expression; +import com.healthmarketscience.jackcess.expr.Function; +import com.healthmarketscience.jackcess.expr.TemporalConfig; +import com.healthmarketscience.jackcess.expr.Value; +import junit.framework.TestCase; +import static com.healthmarketscience.jackcess.impl.expr.ExpressionatorTest.eval; + +/** + * + * @author James Ahlborn + */ +public class DefaultFunctionsTest extends TestCase +{ + + public DefaultFunctionsTest(String name) { + super(name); + } + + public void testFuncs() throws Exception + { + assertEquals("foo", eval("=IIf(10 > 1, \"foo\", \"bar\")")); + assertEquals("bar", eval("=IIf(10 < 1, \"foo\", \"bar\")")); + assertEquals(102L, eval("=Asc(\"foo\")")); + assertEquals(9786L, eval("=AscW(\"\u263A\")")); + assertEquals("f", eval("=Chr(102)")); + assertEquals("\u263A", eval("=ChrW(9786)")); + assertEquals("263A", eval("=Hex(9786)")); + } + +} diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java index 9bc7f49..b9b9b22 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java @@ -307,7 +307,7 @@ public class ExpressionatorTest extends TestCase assertEquals(cleanStr, expr.toString()); } - private static Object eval(String exprStr) { + static Object eval(String exprStr) { Expression expr = Expressionator.parse( Expressionator.Type.DEFAULT_VALUE, exprStr, new TestParseContext()); return expr.eval(new TestEvalContext(null)); @@ -342,7 +342,7 @@ public class ExpressionatorTest extends TestCase } public Function getExpressionFunction(String name) { - return null; + return DefaultFunctions.getFunction(name); } }