]> source.dussan.org Git - jackcess.git/commitdiff
test more functions
authorJames Ahlborn <jtahlborn@yahoo.com>
Tue, 17 Jul 2018 03:48:14 +0000 (03:48 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Tue, 17 Jul 2018 03:48:14 +0000 (03:48 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1182 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java
src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java

index b691d64d0ddca18d9b138cd99b7b1c2fc4e0b04c..f77e0a35cb6c73d2d7d8f2747f6475c09c808cfd 100644 (file)
@@ -90,13 +90,13 @@ public class DefaultFunctions
       }
     }
 
-    protected IllegalStateException invalidFunctionCall(
+    protected EvalException invalidFunctionCall(
         Throwable t, Value[] params)
     {
       String paramStr = Arrays.toString(params);
       String msg = "Invalid function call {" + _name + "(" +
         paramStr.substring(1, paramStr.length() - 1) + ")}";
-      return new IllegalStateException(msg, t);
+      return new EvalException(msg, t);
     }
 
     @Override
index b419f7042064c960f49405c34f3d8a8e33752b86..7831d6e51ee4c46baffb860bebaec011c751374a 100644 (file)
@@ -244,8 +244,8 @@ public class DefaultTextFunctions
       String str = param1.getAsString();
       int strLen = str.length();
       // 1 based offsets
-      int start = Math.max(strLen, params[1].getAsLongInt() - 1);
-      int len = Math.max(
+      int start = Math.min(strLen, params[1].getAsLongInt() - 1);
+      int len = Math.min(
           ((params.length > 2) ? params[2].getAsLongInt() : strLen),
           (strLen - start));
       return BuiltinOperators.toValue(str.substring(start, start + len));
@@ -308,6 +308,8 @@ public class DefaultTextFunctions
       }
       int cmp = (ignoreCase ?
                  s1.compareToIgnoreCase(s2) : s1.compareTo(s2));
+      // stupid java doesn't return 1, -1, 0...
+      cmp = ((cmp < 0) ? -1 : ((cmp > 0) ? 1 : 0));
       return BuiltinOperators.toValue(cmp);
     }
   });
index c95fbaf8f76147f653a290308315c1284ec2393c..39e5da3f99c0fab6f0e5c2ec979113cc90f3966e 100644 (file)
@@ -78,6 +78,23 @@ public class DefaultFunctionsTest extends TestCase
     assertEquals("9786", eval("=CStr(9786)"));
     assertEquals("-42", eval("=CStr(-42)"));
 
+    assertEquals(-1, eval("=IsNull(Null)"));
+    assertEquals(-1, eval("=IsDate(#01/02/2003#)"));
+
+    assertEquals(1, eval("=VarType(Null)"));
+    assertEquals(8, eval("=VarType('blah')"));
+    assertEquals(7, eval("=VarType(#01/02/2003#)"));
+    assertEquals(3, eval("=VarType(42)"));
+    assertEquals(5, eval("=VarType(CDbl(42))"));
+    assertEquals(14, eval("=VarType(42.3)"));
+
+    assertEquals("Null", eval("=TypeName(Null)"));
+    assertEquals("String", eval("=TypeName('blah')"));
+    assertEquals("Date", eval("=TypeName(#01/02/2003#)"));
+    assertEquals("Long", eval("=TypeName(42)"));
+    assertEquals("Double", eval("=TypeName(CDbl(42))"));
+    assertEquals("Decimal", eval("=TypeName(42.3)"));
+
     assertEquals(2, eval("=InStr('AFOOBAR', 'FOO')"));
     assertEquals(2, eval("=InStr('AFOOBAR', 'foo')"));
     assertEquals(2, eval("=InStr(1, 'AFOOBAR', 'foo')"));
@@ -110,11 +127,48 @@ public class DefaultFunctionsTest extends TestCase
     assertEquals("bl", eval("=Left(\"blah\", 2)"));
     assertEquals("", eval("=Left(\"blah\", 0)"));
     assertEquals("blah", eval("=Left(\"blah\", 17)"));
+    assertEquals("la", eval("=Mid(\"blah\", 2, 2)"));
 
     assertEquals("ah", eval("=Right(\"blah\", 2)"));
     assertEquals("", eval("=Right(\"blah\", 0)"));
     assertEquals("blah", eval("=Right(\"blah\", 17)"));
 
+    assertEquals("blah  ", eval("=LTrim(\"  blah  \")"));
+    assertEquals("  blah", eval("=RTrim(\"  blah  \")"));
+    assertEquals("blah", eval("=Trim(\"  blah  \")"));
+    assertEquals("   ", eval("=Space(3)"));
+    assertEquals("ddd", eval("=String(3,'d')"));
+
+    assertEquals(1, eval("=StrComp('FOO', 'bar')"));
+    assertEquals(-1, eval("=StrComp('bar', 'FOO')"));
+    assertEquals(0, eval("=StrComp('FOO', 'foo')"));
+    assertEquals(-1, eval("=StrComp('FOO', 'bar', 0)"));
+    assertEquals(1, eval("=StrComp('bar', 'FOO', 0)"));
+    assertEquals(-1, eval("=StrComp('FOO', 'foo', 0)"));
+
+    assertEquals("halb", eval("=StrReverse('blah')"));
+
+    assertEquals("foo", eval("=Choose(1,'foo','bar','blah')"));
+    assertEquals(null, eval("=Choose(-1,'foo','bar','blah')"));
+    assertEquals("blah", eval("=Choose(3,'foo','bar','blah')"));
+
+    assertEquals(null, eval("=Switch(False,'foo', False, 'bar', False, 'blah')"));
+    assertEquals("bar", eval("=Switch(False,'foo', True, 'bar', True, 'blah')"));
+    assertEquals("blah", eval("=Switch(False,'foo', False, 'bar', True, 'blah')"));
+
+    try {
+      eval("=StrReverse('blah', 1)");
+      fail("EvalException should have been thrown");
+    } catch(EvalException e) {
+      assertTrue(e.getMessage().contains("Invalid function call"));
+    }
+
+    try {
+      eval("=StrReverse()");
+      fail("EvalException should have been thrown");
+    } catch(EvalException e) {
+      assertTrue(e.getMessage().contains("Invalid function call"));
+    }
   }
 
 
index 6c525cc1832dc5fb4455e21ce9e3e531d0251c1c..5f6d9c26c095ef297075b2174d1a01e831344306 100644 (file)
@@ -478,7 +478,6 @@ public class ExpressionatorTest extends TestCase
     doTestEvalFail("=37 In 42", "'In' expression");
     doTestEvalFail("=37 Between 42", "'Between' expression");
     doTestEvalFail("=(3 + 5) Rnd()", "multiple expressions");
-    // doTestEvalFail("=Blah", "");
   }
 
   private static void doTestEvalFail(String exprStr, String msgStr) {
@@ -487,7 +486,6 @@ public class ExpressionatorTest extends TestCase
       fail("ParseException should have been thrown");
     } catch(ParseException pe) {
       // success
-      System.out.println("FOO " + pe);
       assertTrue(pe.getMessage().contains(msgStr));
     }
   }