Skip to content

Commit

Permalink
Enhance test/grammar coverage. label, consequenceLocation, test refac… (
Browse files Browse the repository at this point in the history
#15)

* Enhance test/grammar coverage. label, consequenceLocation, test refactoring, assertj best practice

* - better test method names
  • Loading branch information
tkobayas authored and rgdoliveira committed Oct 24, 2024
1 parent 108e0f2 commit 79afbdd
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ globaldef : DRL_GLOBAL type drlIdentifier SEMI? ;

// rule := RULE stringId (EXTENDS stringId)? annotation* attributes? lhs? rhs END

ruledef : DRL_RULE name=stringId (EXTENDS stringId)? drlAnnotation* attributes? DRL_WHEN lhs DRL_THEN rhs DRL_END ;
ruledef : DRL_RULE name=stringId (EXTENDS stringId)? drlAnnotation* attributes? lhs rhs DRL_END ;

lhs : lhsExpression? ;
lhs : DRL_WHEN lhsExpression? ;
lhsExpression : lhsOr+ ;
lhsOr : LPAREN DRL_OR lhsAnd+ RPAREN | lhsAnd (DRL_OR lhsAnd)* ;
lhsAnd : LPAREN DRL_AND lhsUnary+ RPAREN | lhsUnary (DRL_AND lhsUnary)* ;
Expand Down Expand Up @@ -260,7 +260,9 @@ lhsExists : DRL_EXISTS lhsPatternBind ;
*/
lhsNot : DRL_NOT lhsPatternBind ;

rhs : drlRhsBlockStatement* ;
rhs : DRL_THEN consequence ;

consequence : drlRhsBlockStatement* ;

stringId : ( IDENTIFIER | DRL_STRING_LITERAL ) ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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.AndDescr;
import org.drools.drl.ast.descr.AnnotationDescr;
import org.drools.drl.ast.descr.AttributeDescr;
import org.drools.drl.ast.descr.BaseDescr;
Expand Down Expand Up @@ -115,7 +116,6 @@ public Object visitFunctiondef(DRLParser.FunctiondefContext ctx) {
@Override
public Object visitRuledef(DRLParser.RuledefContext ctx) {
currentRule = new RuleDescr(safeStripStringDelimiters(ctx.name.getText()));
currentRule.setConsequence(ParserStringUtils.getTextPreservingWhitespace(ctx.rhs()));
packageDescr.addRule(currentRule);

Object result = super.visitRuledef(ctx);
Expand All @@ -137,7 +137,8 @@ public Object visitLhs(DRLParser.LhsContext ctx) {
public Object visitLhsPatternBind(DRLParser.LhsPatternBindContext ctx) {
if (ctx.lhsPattern().size() == 1) {
Object result = super.visitLhsPatternBind(ctx);
PatternDescr patternDescr = (PatternDescr) currentConstructStack.peek().getDescrs().get(0);
ConditionalElementDescr parentDescr = currentConstructStack.peek();
PatternDescr patternDescr = (PatternDescr) parentDescr.getDescrs().get(parentDescr.getDescrs().size() - 1);
if (ctx.label() != null) {
patternDescr.setIdentifier(ctx.label().IDENTIFIER().getText());
}
Expand Down Expand Up @@ -175,7 +176,7 @@ public Object visitLhsPattern(DRLParser.LhsPatternContext ctx) {
currentPattern.setSource(from);
}
Object result = super.visitLhsPattern(ctx);
currentConstructStack.peek().addDescr(currentPattern);
currentConstructStack.peek().addDescr(currentPattern);
currentPattern = null;
return result;
}
Expand All @@ -184,7 +185,12 @@ public Object visitLhsPattern(DRLParser.LhsPatternContext ctx) {
public Object visitConstraint(DRLParser.ConstraintContext ctx) {
Object constraint = super.visitConstraint(ctx);
if (constraint != null) {
ExprConstraintDescr constr = new ExprConstraintDescr(constraint.toString());
String constraintString = constraint.toString();
DRLParser.LabelContext label = ctx.label();
if (label != null) {
constraintString = label.getText() + constraintString;
}
ExprConstraintDescr constr = new ExprConstraintDescr(constraintString);
constr.setType(ExprConstraintDescr.Type.NAMED);
currentPattern.addConstraint(constr);
}
Expand Down Expand Up @@ -292,6 +298,13 @@ public Object visitLhsOr(DRLParser.LhsOrContext ctx) {
}
}

@Override
public Object visitRhs(DRLParser.RhsContext ctx) {
currentRule.setConsequenceLocation(ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine()); // location of "then"
currentRule.setConsequence(ParserStringUtils.getTextPreservingWhitespace(ctx.consequence()));
return super.visitChildren(ctx);
}

public PackageDescr getPackageDescr() {
return packageDescr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DRLParserTest {
"end\n";

@Test
void testParse() {
void parse_basicRule() {
PackageDescr packageDescr = parse(drl);
assertThat(packageDescr.getName()).isEqualTo("org.test");

Expand Down Expand Up @@ -72,7 +72,7 @@ void testParse() {
}

@Test
void testComputeTokenIndex() {
void computeTokenIndex_basicRule() {
DRLParser parser = createDrlParser(drl);
parser.compilationUnit();

Expand Down
Loading

0 comments on commit 79afbdd

Please sign in to comment.