From 18a4998e71e239bba1712109f456dbea80067867 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Tue, 17 Jul 2018 03:48:14 +0000 Subject: [PATCH] test more functions git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1182 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/impl/expr/DefaultFunctions.java | 4 +- .../impl/expr/DefaultTextFunctions.java | 6 ++- .../impl/expr/DefaultFunctionsTest.java | 54 +++++++++++++++++++ .../impl/expr/ExpressionatorTest.java | 2 - 4 files changed, 60 insertions(+), 6 deletions(-) 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 b691d64..f77e0a3 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java @@ -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 diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java index b419f70..7831d6e 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java @@ -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); } }); diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java index c95fbaf..39e5da3 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -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")); + } } 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 6c525cc..5f6d9c2 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java @@ -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)); } } -- 2.39.5