Skip to content

Commit

Permalink
[incubator-kie-drools-5792] [new-parser] improve drools-drl-parser-te…
Browse files Browse the repository at this point in the history
…sts (#5855)

* [incubator-kie-drools-5854] [new-parser] improve drools-drl-parser-testsa to test with old and new parsers
- Old and new parser coverage using 2 surefire test executions
- Fixed Descr common property issues to keep backward compatibility
- A few test cases remaining without backward compatibility ("Backward Compatibility Notes" left) because the old parser seems to be wrong.
- A few expr test cases remaining without backward compatibility ("Backward Compatibility Notes" left). Error code/message don't seem to be important. Also the new parser ones are better.

* Update drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/LexerHelper.java

Co-authored-by: Jiří Locker <[email protected]>

* - removed unused import

---------

Co-authored-by: Jiří Locker <[email protected]>
  • Loading branch information
tkobayas and yurloc authored Apr 23, 2024
1 parent f148383 commit 025ff72
Show file tree
Hide file tree
Showing 14 changed files with 745 additions and 307 deletions.
38 changes: 38 additions & 0 deletions drools-drl/drools-drl-parser-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,42 @@
</dependency>
</dependencies>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<!-- override default-test for new parser -->
<id>default-test</id>
<configuration>
<systemPropertyVariables>
<drools.drl.antlr4.parser.enabled>true</drools.drl.antlr4.parser.enabled>
</systemPropertyVariables>
</configuration>
</execution>
<execution>
<id>old-parser-test</id>
<configuration>
<systemPropertyVariables>
<drools.drl.antlr4.parser.enabled>false</drools.drl.antlr4.parser.enabled>
</systemPropertyVariables>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.drools.drl.ast.descr.ConstraintConnectiveDescr;
import org.drools.drl.ast.descr.RelationalExprDescr;
import org.drools.drl.parser.DrlExprParser;
import org.drools.drl.parser.DrlExprParserFactory;
import org.drools.drl.parser.DrlParser;
import org.drools.drl.parser.DroolsParserException;
import org.drools.drl.parser.impl.Operator;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -49,17 +51,17 @@ public class DRLExprParserTest {
DrlExprParser parser;

@BeforeEach
public void setUp() throws Exception {
this.parser = new Drl6ExprParserAntlr4(LanguageLevelOption.DRL6);
public void setUp() {
this.parser = DrlExprParserFactory.getDrlExprParser(LanguageLevelOption.DRL6);
}

@AfterEach
public void tearDown() throws Exception {
public void tearDown() {
this.parser = null;
}

@Test
public void testSimpleExpression() throws Exception {
public void testSimpleExpression() {
String source = "a > b";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();
Expand Down Expand Up @@ -387,13 +389,26 @@ public void testMismatchedInput() {
assertThat(parser.hasErrors()).isTrue();
assertThat(parser.getErrors()).hasSize(1);
DroolsParserException exception = parser.getErrors().get(0);
assertThat(exception.getErrorCode()).isEqualTo("ERR 102");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(1);
assertThat(exception.getOffset()).isEqualTo(1);
assertThat(exception.getMessage())
.startsWithIgnoringCase("[ERR 102] Line 1:1 mismatched input '<EOF>' expecting ")
.contains("TIME_INTERVAL", "DRL_STRING_LITERAL", "?/", "boolean", "byte", "char", "double", "float", "int", "long", "new", "short", "super", "DECIMAL_LITERAL", "HEX_LITERAL", "FLOAT_LITERAL", "BOOL_LITERAL", "STRING_LITERAL", "null", "(", "[", ".", "<", "!", "~", "++", "--", "+", "-", "*", "/", "IDENTIFIER");

// Backward Compatibility Notes:
// Antlr4 gives a different error code/message from antlr3 for this case.
// Backward compatibility doesn't seem to be required in this case.
if (DrlParser.ANTLR4_PARSER_ENABLED) {
assertThat(exception.getErrorCode()).isEqualTo("ERR 102");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(1);
assertThat(exception.getOffset()).isEqualTo(1);
assertThat(exception.getMessage())
.startsWithIgnoringCase("[ERR 102] Line 1:1 mismatched input '<EOF>' expecting ")
.contains("TIME_INTERVAL", "DRL_STRING_LITERAL", "?/", "boolean", "byte", "char", "double", "float", "int", "long", "new", "short", "super", "DECIMAL_LITERAL", "HEX_LITERAL", "FLOAT_LITERAL", "BOOL_LITERAL", "STRING_LITERAL", "null", "(", "[", ".", "<", "!", "~", "++", "--", "+", "-", "*", "/", "IDENTIFIER");
} else {
assertThat(exception.getErrorCode()).isEqualTo("ERR 101");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(1);
assertThat(exception.getOffset()).isEqualTo(1);
assertThat(exception.getMessage())
.isEqualToIgnoringCase("[ERR 101] Line 1:1 no viable alternative at input '<eof>'");
}
}

@Test
Expand All @@ -403,26 +418,48 @@ public void testExtraneousInput() {
assertThat(parser.hasErrors()).isTrue();
assertThat(parser.getErrors()).hasSize(1);
DroolsParserException exception = parser.getErrors().get(0);
assertThat(exception.getErrorCode()).isEqualTo("ERR 109");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(3);
assertThat(exception.getOffset()).isEqualTo(3);
assertThat(exception.getMessage())
.startsWithIgnoringCase("[ERR 109] Line 1:3 extraneous input ';' expecting ")
.contains("TIME_INTERVAL", "DRL_STRING_LITERAL", "?/", "boolean", "byte", "char", "double", "float", "int", "long", "new", "short", "super", "DECIMAL_LITERAL", "HEX_LITERAL", "FLOAT_LITERAL", "BOOL_LITERAL", "STRING_LITERAL", "null", "(", "[", ".", "<", "!", "~", "++", "--", "+", "-", "*", "/", "IDENTIFIER");

// Backward Compatibility Notes:
// Antlr4 gives a different error code/message from antlr3 for this case.
// Backward compatibility doesn't seem to be required in this case.
if (DrlParser.ANTLR4_PARSER_ENABLED) {
assertThat(exception.getErrorCode()).isEqualTo("ERR 109");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(3);
assertThat(exception.getOffset()).isEqualTo(3);
assertThat(exception.getMessage())
.startsWithIgnoringCase("[ERR 109] Line 1:3 extraneous input ';' expecting ")
.contains("TIME_INTERVAL", "DRL_STRING_LITERAL", "?/", "boolean", "byte", "char", "double", "float", "int", "long", "new", "short", "super", "DECIMAL_LITERAL", "HEX_LITERAL", "FLOAT_LITERAL", "BOOL_LITERAL", "STRING_LITERAL", "null", "(", "[", ".", "<", "!", "~", "++", "--", "+", "-", "*", "/", "IDENTIFIER");
} else {
assertThat(exception.getErrorCode()).isEqualTo("ERR 101");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(3);
assertThat(exception.getOffset()).isEqualTo(3);
assertThat(exception.getMessage())
.isEqualToIgnoringCase("[ERR 101] Line 1:3 no viable alternative at input ';'");
}
}

@Test
public void testNoViableAlt() {
String source = "x.int";
parser.parse(source);
assertThat(parser.hasErrors()).isTrue();
assertThat(parser.getErrors()).hasSize(1);
DroolsParserException exception = parser.getErrors().get(0);
assertThat(exception.getErrorCode()).isEqualTo("ERR 101");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(2);
assertThat(exception.getOffset()).isEqualTo(2);
assertThat(exception.getMessage())
.isEqualToIgnoringCase("[ERR 101] Line 1:2 no viable alternative at input '.int'");

// Backward Compatibility Notes:
// Old expr parser (DRL6Expressions) allows this expression because it's too tolerant (fail at runtime anyway).
// Backward compatibility doesn't seem to be required in this case. (But we may align with the old tolerant behavior.)
if (DrlParser.ANTLR4_PARSER_ENABLED) {
assertThat(parser.hasErrors()).isTrue();
assertThat(parser.getErrors()).hasSize(1);
DroolsParserException exception = parser.getErrors().get(0);
assertThat(exception.getErrorCode()).isEqualTo("ERR 101");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(2);
assertThat(exception.getOffset()).isEqualTo(2);
assertThat(exception.getMessage())
.isEqualToIgnoringCase("[ERR 101] Line 1:2 no viable alternative at input '.int'");
} else {
assertThat(parser.hasErrors()).isFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import static org.drools.drl.parser.antlr4.DRLParserHelper.createDrlParser;
import static org.drools.drl.parser.antlr4.DRLParserHelper.parse;

/**
* This test class is specific to new antlr4 parser.
*/
class DRLParserTest {

private static final String drl =
Expand Down
Loading

0 comments on commit 025ff72

Please sign in to comment.