]> source.dussan.org Git - jackcess.git/commitdiff
add some initial default function unit tests; fix some parse bugs
authorJames Ahlborn <jtahlborn@yahoo.com>
Wed, 6 Sep 2017 01:53:52 +0000 (01:53 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Wed, 6 Sep 2017 01:53:52 +0000 (01:53 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1114 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java
src/main/java/com/healthmarketscience/jackcess/impl/expr/ExpressionTokenizer.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java [new file with mode: 0644]
src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java

index 9abe33421d469064b28ccbb78c2d42a948888237..706f42ca77e383a2e0395bb02b993bb35e40474e 100644 (file)
@@ -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();
index 596b3f0eb80310b78113e464cc75e920e6eba03a..463f1474f7a16c3b7f36ccad61d2f1e4eeafcf14 100644 (file)
@@ -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 (file)
index 0000000..0e2e85e
--- /dev/null
@@ -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)"));
+  }
+
+}
index 9bc7f4982f345dc36fe962f9e4227f40a6517806..b9b9b22d62f5a1ff08b550c76fb570000dc71b38 100644 (file)
@@ -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);
     }
   }