Skip to content

Commit

Permalink
Enhance test/grammar coverage. dialect, invalid lhs (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas authored and rgdoliveira committed Oct 24, 2024
1 parent 65e2257 commit 4632ed2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 17 deletions.
5 changes: 5 additions & 0 deletions drools-drl/drools-drl10-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<artifactId>drools-drl-ast</artifactId>
<version>${version.drools-drl-ast}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-util</artifactId>
<version>${version.drools-drl-ast}</version>
</dependency>

<!-- External dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ options { tokenVocab=DRLLexer; }
import JavaParser;


compilationUnit : packagedef? unitdef? importdef* globaldef* ruledef* ;
compilationUnit : packagedef? unitdef? importdef* attributes* globaldef* ruledef* ;

packagedef : PACKAGE name=qualifiedName SEMI? ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.drools.drl.ast.descr.*;
import org.drools.drl.ast.descr.AnnotationDescr;
import org.drools.drl.ast.descr.AttributeDescr;
import org.drools.drl.ast.descr.BaseDescr;
import org.drools.drl.ast.descr.ConditionalElementDescr;
import org.drools.drl.ast.descr.ExistsDescr;
import org.drools.drl.ast.descr.ExprConstraintDescr;
import org.drools.drl.ast.descr.FromDescr;
import org.drools.drl.ast.descr.FunctionImportDescr;
import org.drools.drl.ast.descr.GlobalDescr;
import org.drools.drl.ast.descr.ImportDescr;
import org.drools.drl.ast.descr.MVELExprDescr;
import org.drools.drl.ast.descr.NotDescr;
import org.drools.drl.ast.descr.OrDescr;
import org.drools.drl.ast.descr.PackageDescr;
import org.drools.drl.ast.descr.PatternDescr;
import org.drools.drl.ast.descr.RuleDescr;
import org.drools.drl.ast.descr.UnitDescr;

import static org.drools.parser.DRLParserHelper.getTextWithoutErrorNode;
import static org.drools.parser.StringUtils.safeStripStringDelimiters;
import static org.drools.parser.ParserStringUtils.safeStripStringDelimiters;
import static org.drools.util.StringUtils.unescapeJava;

public class DRLVisitorImpl extends DRLParserBaseVisitor<Object> {

Expand Down Expand Up @@ -197,9 +214,15 @@ public Object visitDrlAnnotation(DRLParser.DrlAnnotationContext ctx) {
public Object visitAttribute(DRLParser.AttributeContext ctx) {
AttributeDescr attributeDescr = new AttributeDescr(ctx.getChild(0).getText());
if (ctx.getChildCount() > 1) {
attributeDescr.setValue(ctx.getChild(1).getText());
// TODO : will likely split visitAttribute methods using labels (e.g. #stringAttribute)
String value = unescapeJava(safeStripStringDelimiters(ctx.getChild(1).getText()));
attributeDescr.setValue(value);
}
if (currentRule != null) {
currentRule.addAttribute(attributeDescr);
} else {
packageDescr.addAttribute(attributeDescr);
}
currentRule.addAttribute(attributeDescr);
return super.visitAttribute(ctx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
/**
* will be merged in drools-util
*/
public class StringUtils {
public class ParserStringUtils {

private StringUtils() {
private ParserStringUtils() {
}

public static String safeStripStringDelimiters(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import junit.framework.TestCase;
import org.assertj.core.api.Assertions;
import org.drools.drl.ast.descr.AndDescr;
import org.drools.drl.ast.descr.AttributeDescr;
import org.drools.drl.ast.descr.BaseDescr;
import org.drools.drl.ast.descr.ExprConstraintDescr;
import org.drools.drl.ast.descr.FromDescr;
Expand Down Expand Up @@ -47,7 +48,7 @@ protected void tearDown() throws Exception {
super.tearDown();
}

private PackageDescr parseResource(final String filename) throws Exception {
private String readResource(final String filename) throws Exception {
Path path = Paths.get(getClass().getResource(filename).toURI());
final StringBuilder sb = new StringBuilder();
try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
Expand All @@ -57,7 +58,7 @@ private PackageDescr parseResource(final String filename) throws Exception {
} catch (IOException e) {
e.printStackTrace();
}
return parser.parse(sb.toString());
return sb.toString();
}

@Test
Expand Down Expand Up @@ -210,26 +211,27 @@ public void testGlobalWithComplexType() throws Exception {

@Test
public void testGlobalWithOrWithoutSemi() throws Exception {
PackageDescr pack = parseResource("globals.drl");
String source = readResource("globals.drl");
PackageDescr pkg = parser.parse(source);

assertEquals(1,
pack.getRules().size());
pkg.getRules().size());

final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1,
rule.getLhs().getDescrs().size());

assertEquals(1,
pack.getImports().size());
pkg.getImports().size());
assertEquals(2,
pack.getGlobals().size());
pkg.getGlobals().size());

final GlobalDescr foo = (GlobalDescr) pack.getGlobals().get(0);
final GlobalDescr foo = (GlobalDescr) pkg.getGlobals().get(0);
assertEquals("java.lang.String",
foo.getType());
assertEquals("foo",
foo.getIdentifier());
final GlobalDescr bar = (GlobalDescr) pack.getGlobals().get(1);
final GlobalDescr bar = (GlobalDescr) pkg.getGlobals().get(1);
assertEquals("java.lang.Integer",
bar.getType());
assertEquals("bar",
Expand All @@ -238,7 +240,8 @@ public void testGlobalWithOrWithoutSemi() throws Exception {

@Test
public void testFunctionImportWithNotExist() throws Exception {
PackageDescr pkg = (PackageDescr) parseResource("test_FunctionImport.drl");
String source = readResource("test_FunctionImport.drl");
PackageDescr pkg = parser.parse(source);

assertEquals(2,
pkg.getFunctionImports().size());
Expand Down Expand Up @@ -458,4 +461,37 @@ public void testStringEscapes() throws Exception {
assertEquals( "type matches \"\\..*\\\\.\"",
constraint.getDescrs().get( 0 ).toString() );
}

@Test
public void testDialect() throws Exception {
final String source = "dialect 'mvel'";
PackageDescr pkg = parser.parse(source);
AttributeDescr attr = (AttributeDescr) pkg.getAttributes().get(0);
assertEquals("dialect",
attr.getName());
assertEquals("mvel",
attr.getValue());
}

@Test
public void testDialect2() throws Exception {
final String source = "dialect \"mvel\"";
PackageDescr pkg = parser.parse(source);
AttributeDescr attr = pkg.getAttributes().get(0);
assertEquals("dialect",
attr.getName());
assertEquals("mvel",
attr.getValue());
}

@Test
public void testEmptyRuleWithoutWhen() throws Exception {
String source = readResource("empty_rule.drl"); // without WHEN
PackageDescr pkg = parser.parse(source);

assertTrue(parser.getErrors().toString(),
parser.hasErrors());

// Note : RuleParserTest.testEmptyRule allows this DRL, but I think is doesn't make sense to pass this DRL
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


rule empty

then

end

0 comments on commit 4632ed2

Please sign in to comment.