From 841023653876054e9ba28cd4f5f4fc9142ae847e Mon Sep 17 00:00:00 2001 From: Markus Spann Date: Fri, 1 Mar 2024 07:47:45 +0100 Subject: [PATCH] Unit test parameterization --- .../impl/expr/DefaultFunctionsTest.java | 95 +++++++++++-------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/src/test/java/io/github/spannm/jackcess/impl/expr/DefaultFunctionsTest.java b/src/test/java/io/github/spannm/jackcess/impl/expr/DefaultFunctionsTest.java index 5ca58c0..5846cc9 100644 --- a/src/test/java/io/github/spannm/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/io/github/spannm/jackcess/impl/expr/DefaultFunctionsTest.java @@ -159,7 +159,7 @@ void testFuncsQuoting() { } @ParameterizedTest(name = "[{index}] {0} --> {1}") - @CsvSource(delimiter = ';', quoteCharacter = '\"', value = { + @CsvSource(delimiter = ';', value = { "Asc(\"foo\"); 102", "AscW(\"☺\"); 9786", "CBool(\"1\"); -1", @@ -231,7 +231,7 @@ void testFuncsInt(String exprStr, int expected) { } @ParameterizedTest(name = "[{index}] {0}") - @CsvSource(delimiter = ';', quoteCharacter = '\"', value = { + @CsvSource(delimiter = ';', value = { "Str(Null)", "InStr(3, Null, 'FOO')", "InStrRev(Null, 'FOO', 3)", @@ -243,7 +243,7 @@ void testFuncsNull(String exprStr) { } @ParameterizedTest(name = "[{index}] {0} --> {1}") - @CsvSource(delimiter = ';', quoteCharacter = '\"', value = { + @CsvSource(delimiter = ';', value = { "CDbl(\"57.12345\"); 57.12345", "Val(' 1615 198th Street N.E.'); 1615198d", "Val(' &HFFFFwhatever'); -1d", @@ -260,7 +260,7 @@ void testFuncsDouble(String exprStr, double expected) { } @ParameterizedTest(name = "[{index}] {0} --> {1}") - @CsvSource(delimiter = ';', quoteCharacter = '\"', value = { + @CsvSource(delimiter = ';', value = { "CCur(\"57.12346\"); 57.1235", "CDec(\"57.123456789\"); 57.123456789" }) @@ -269,7 +269,7 @@ void testFuncsBigDecimal(String exprStr, BigDecimal expected) { } @ParameterizedTest(name = "[{index}] {0} --> {1}") - @CsvSource(delimiter = ';', quoteCharacter = '\"', value = { + @CsvSource(delimiter = ';', value = { "CSng(\"57.12345\"); 57.12345" }) void testFuncsFloat(String exprStr, String expected) { @@ -582,9 +582,9 @@ void testCustomFormat() { } @ParameterizedTest(name = "[{index}] {0} --> {1}") - @CsvSource(delimiter = ';', quoteCharacter = '\"', value = { - "Abs(1); 1", - "Abs(-1); 1", + @CsvSource(delimiter = ';', value = { + "Abs(1) ; 1", + "Abs(-1) ; 1", "Abs(-1.1); 1.1" }) void testNumberFuncsInt(String exprStr, double expected) { @@ -592,7 +592,7 @@ void testNumberFuncsInt(String exprStr, double expected) { assertEquals(expected, result.doubleValue()); } - void testNumberFuncsTrig(String exprStr, String expected) { + void testNumberFuncsTrig() { Map> map = Map.of( "Atan(0.2)", () -> Math.atan(0.2), "Sin(0.2)", () -> Math.sin(0.2), @@ -603,36 +603,51 @@ void testNumberFuncsTrig(String exprStr, String expected) { }); } - void testNumberFuncsRest(String exprStr, String expected) { - - assertEquals(Math.exp(0.2), eval("=Exp(0.2)")); - assertEquals(Math.log(0.2), eval("=Log(0.2)")); - assertEquals(Math.sqrt(4.3), eval("=Sqr(4.3)")); - - assertEquals(3, eval("=Fix(3.5)")); - assertEquals(4, eval("=Fix(4)")); - assertEquals(-3, eval("=Fix(-3.5)")); - assertEquals(-4, eval("=Fix(-4)")); - - assertEquals(1, eval("=Sgn(3.5)")); - assertEquals(1, eval("=Sgn(4)")); - assertEquals(-1, eval("=Sgn(-3.5)")); - assertEquals(-1, eval("=Sgn(-4)")); - - assertEquals(3, eval("=Int(3.5)")); - assertEquals(4, eval("=Int(4)")); - assertEquals(-4, eval("=Int(-3.5)")); - assertEquals(-4, eval("=Int(-4)")); + void testNumberFuncsMath() { + Map> map = Map.of( + "Exp(0.2)", () -> Math.exp(0.2), + "Log(0.2)", () -> Math.log(0.2), + "Sqr(4.3)", () -> Math.sqrt(4.3)); + map.entrySet().forEach(e -> { + assertEquals(e.getKey(), e.getValue().get()); + }); + } - assertEquals(toBD(4), eval("=Round(3.7)")); - assertEquals(4, eval("=Round(4)")); - assertEquals(toBD(-4), eval("=Round(-3.7)")); - assertEquals(-4, eval("=Round(-4)")); + @ParameterizedTest(name = "[{index}] {0} --> {1}") + @CsvSource(delimiter = ';', value = { + "Fix(3.5) ; 3", + "Fix(4) ; 4", + "Fix(-3.5); -3", + "Fix(-4) ; -4", + + "Sgn(3.5) ; 1", + "Sgn(4) ; 1", + "Sgn(-3.5); -1", + "Sgn(-4) ; -1", + + "Int(3.5) ; 3", + "Int(4) ; 4", + "Int(-3.5); -4", + "Int(-4) ; -4 " + }) + void testNumberFuncsInt2(String exprStr, int expected) { + assertEquals(expected, eval('=' + exprStr)); + } - assertEquals(toBD(3.73), eval("=Round(3.7345, 2)")); - assertEquals(4, eval("=Round(4, 2)")); - assertEquals(toBD(-3.73), eval("=Round(-3.7345, 2)")); - assertEquals(-4, eval("=Round(-4, 2)")); + @ParameterizedTest(name = "[{index}] {0} --> {1}") + @CsvSource(delimiter = ';', value = { + "Round(3.7) ; 4; true", + "Round(4) ; 4; false", + "Round(-3.7); -4; true", + "Round(-4) ; -4; false", + + "Round(3.7345, 2) ; 3.73; true", + "Round(4, 2) ; 4; false", + "Round(-3.7345, 2); -3.73; true", + "Round(-4, 2) ; -4; false" + }) + void testNumberFuncsRound(String exprStr, Double expected, boolean bd) { + assertEquals(bd ? toBD(expected) : expected.intValue(), eval('=' + exprStr)); } @Test @@ -668,8 +683,8 @@ void testDateFuncs() { assertEquals("Tuesday", eval("=WeekdayName(1,False,3)")); assertEquals("Thu", eval("=WeekdayName(3,True,3)")); - assertTrue(((String) eval("=CStr(Date())")).matches("\\d{1,2}/\\d{1,2}/\\d{4}")); - assertTrue(((String) eval("=CStr(Time())")).matches("\\d{1,2}:\\d{2}:\\d{2} (AM|PM)")); + assertTrue(((String) eval("=CStr(Date())")).matches("[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}")); + assertTrue(((String) eval("=CStr(Time())")).matches("[0-9]{1,2}:[0-9]{2}:[0-9]{2} (AM|PM)")); assertEquals("3:57:34 AM", eval("=CStr(TimeSerial(3,57,34))")); assertEquals("3:57:34 PM", eval("=CStr(TimeSerial(15,57,34))")); @@ -789,7 +804,7 @@ void testDateFuncs() { } @ParameterizedTest(name = "[{index}] {0} --> {1}") - @CsvSource(delimiter = ';', quoteCharacter = '\"', value = { + @CsvSource(delimiter = ';', value = { "CStr(NPer(0.12/12,-100,-1000)); -9.57859403981306", "CStr(NPer(0.12/12,-100,-1000,0,1)); -9.48809500550578", "CStr(NPer(0.12/12,-100,-1000,10000)); 60.0821228537616",