From efd39c546f580f740f18734d55b251cd90362f1c Mon Sep 17 00:00:00 2001 From: Andrus Adamchik Date: Sat, 29 Jul 2023 12:37:53 -0400 Subject: [PATCH] Expressions - immutable parameter binding, pruning unbound conditions #602 * flattenning parameterized tests for readability and flexibility --- .../io/agrest/exp/parser/ExpEqualTest.java | 75 ++++++++--------- .../java/io/agrest/exp/parser/ExpInTest.java | 81 +++++++++---------- 2 files changed, 72 insertions(+), 84 deletions(-) diff --git a/agrest-engine/src/test/java/io/agrest/exp/parser/ExpEqualTest.java b/agrest-engine/src/test/java/io/agrest/exp/parser/ExpEqualTest.java index c6e9721bd..611e249a8 100644 --- a/agrest-engine/src/test/java/io/agrest/exp/parser/ExpEqualTest.java +++ b/agrest-engine/src/test/java/io/agrest/exp/parser/ExpEqualTest.java @@ -4,52 +4,43 @@ import io.agrest.exp.AgExpression; import io.agrest.protocol.Exp; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.provider.Arguments; - -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; - -class ExpEqualTest extends AbstractExpTest { - - @Override - ExpTestVisitor provideVisitor() { - return new ExpTestVisitor(ExpEqual.class); - } - - @Override - Stream parseExp() { - return Stream.of( - "a=b", - "a = b", - "a = b", - "a == b", - "$a = $b", - "1 = 2", - "1 = 2.2", - "1 = TRUE", - "'1' = '2'", - "null = c", - "a = currentDate()" - ); +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; + +public class ExpEqualTest { + + @ParameterizedTest + @ValueSource(strings = {"a=b", + "a = b", + "a = b", + "a == b", + "$a = $b", + "1 = 2", + "1 = 2.2", + "1 = TRUE", + "'1' = '2'", + "null = c", + "a = currentDate()"}) + public void parse(String expString) { + assertEquals(ExpEqual.class, Exp.parse(expString).getClass()); } - @Override - Stream parseExpThrows() { - return Stream.of( - Arguments.of("=", AgException.class), - Arguments.of("a =", AgException.class), - Arguments.of("= b", AgException.class) - ); + @ParameterizedTest + @CsvSource({ + "a=b,(a) = (b)", + "a = b,(a) = (b)" + }) + public void parsedToString(String expString, String expected) { + assertEquals(expected, Exp.parse(expString).toString()); } - @Override - Stream stringifyRaw() { - return Stream.of( - Arguments.of("a=b", "(a) = (b)"), - Arguments.of("a = b", "(a) = (b)") - ); + @ParameterizedTest + @ValueSource(strings = {"=", "a =", "= b"}) + public void parseInvalidGrammar(String expString) { + assertThrows(AgException.class, () -> Exp.parse(expString)); } @Test diff --git a/agrest-engine/src/test/java/io/agrest/exp/parser/ExpInTest.java b/agrest-engine/src/test/java/io/agrest/exp/parser/ExpInTest.java index 29a504e85..cdacedf86 100644 --- a/agrest-engine/src/test/java/io/agrest/exp/parser/ExpInTest.java +++ b/agrest-engine/src/test/java/io/agrest/exp/parser/ExpInTest.java @@ -4,57 +4,54 @@ import io.agrest.exp.AgExpression; import io.agrest.protocol.Exp; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; import java.util.List; -import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.*; -public class ExpInTest extends AbstractExpTest { +public class ExpInTest { - @Override - ExpTestVisitor provideVisitor() { - return new ExpTestVisitor(ExpIn.class); + @ParameterizedTest + @ValueSource(strings = { + "a in('b','c')", + "a in ('b', 'c')", + "a in ('b')", + "a in ('b', 'c')", + "a in ($b, $c)", + "a in (1, 2)", + "a in (1, 2.2)", + "a in (1, TRUE)", + "a in ('1', '2')", + "a in $b"}) + public void parse(String expString) { + assertEquals(ExpIn.class, Exp.parse(expString).getClass()); } - @Override - Stream parseExp() { - return Stream.of( - "a in('b','c')", - "a in ('b', 'c')", - "a in ('b')", - "a in ('b', 'c')", - "a in ($b, $c)", - "a in (1, 2)", - "a in (1, 2.2)", - "a in (1, TRUE)", - "a in ('1', '2')", - "a in $b" - ); + @ParameterizedTest + @CsvSource(delimiterString = "|", value = { + "a in('b','c')|a in ('b', 'c')", + "a in ('b', 'c')|a in ('b', 'c')", + "a in ('b', 'c', 'd')|a in ('b', 'c', 'd')", + "a in $b|a in $b" + }) + public void parsedToString(String expString, String expected) { + assertEquals(expected, Exp.parse(expString).toString()); } - @Override - Stream parseExpThrows() { - return Stream.of( - Arguments.of("a in", AgException.class), - Arguments.of("a in ()", AgException.class), - Arguments.of("a in ('b',)", AgException.class), - Arguments.of("a in (null, 'c')", AgException.class), - Arguments.of("a in (, 'c')", AgException.class), - Arguments.of("a IN ('b', 'c')", AgException.class) - ); - } - - @Override - Stream stringifyRaw() { - return Stream.of( - Arguments.of("a in('b','c')", "a in ('b', 'c')"), - Arguments.of("a in ('b', 'c')", "a in ('b', 'c')"), - Arguments.of("a in ('b', 'c', 'd')", "a in ('b', 'c', 'd')"), - Arguments.of("a in $b", "a in $b") - ); + @ParameterizedTest + // TODO: null should be a valid scalar in the list + @ValueSource(strings = { + "a in", + "a in ()", + "a in ('b',)", + "a in (null, 'c')", + "a in (, 'c')", + "a IN ('b', 'c')"}) + public void parseInvalidGrammar(String expString) { + assertThrows(AgException.class, () -> Exp.parse(expString)); } @Test