@Test public void testEval() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "eval_parsing.drl" ); assertEquals( "org.drools.compiler", pkg.getName() ); assertEquals( 1, pkg.getRules().size() ); final RuleDescr rule1 = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 1, rule1.getLhs().getDescrs().size() ); }
@Test public void testEscapedStrings() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "escaped-string.drl" ); assertNotNull( rule ); assertEquals( "test_Quotes", rule.getName() ); final String expected = "String s = \"\\\"\\n\\t\\\\\";"; assertEqualsIgnoreWhitespace( expected, (String) rule.getConsequence() ); }
@Test public void testCheckOrDescr() throws Exception { final String text = "rule X when Person( eval( age == 25 ) || ( eval( name.equals( \"bob\" ) ) && eval( age == 30 ) ) ) then end"; PatternDescr pattern = (PatternDescr) ((RuleDescr) parse( "rule", text )).getLhs().getDescrs().get( 0 ); assertEquals( 1, pattern.getDescrs().size() ); assertEquals( pattern.getConstraint().getClass(), AndDescr.class ); assertEquals( ExprConstraintDescr.class, pattern.getConstraint().getDescrs().get( 0 ).getClass() ); }
@Test public void testRuleTwoLines() throws Exception { final String text = "rule \"another test\" salience 10 when eval( true ) then System.out.println(1);\n end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertEquals( "another test", rule.getName() ); assertEquals( "System.out.println(1);\n ", rule.getConsequence() ); }
@Test public void testDialect() throws Exception { final String source = "dialect 'mvel'"; PackageDescr pkg = (PackageDescr) parse( "compilationUnit", source ); AttributeDescr attr = (AttributeDescr) pkg.getAttributes().get( 0 ); assertEquals( "dialect", attr.getName() ); assertEquals( "mvel", attr.getValue() ); }
@Test public void testConstraintOrConnective() throws Exception { final String text = "rule X when Person( age < 42 || location==\"atlanta\") then end"; PatternDescr pattern = (PatternDescr) ((RuleDescr) parse( "rule", text )).getLhs().getDescrs().get( 0 ); assertEquals( 1, pattern.getDescrs().size() ); ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get( 0 ); assertEquals( "age < 42 || location==\"atlanta\"", fcd.getExpression() ); }
@Test public void testMemberof() throws Exception { final String text = "rule X when Country( $cities : city )\nPerson( city memberOf $cities )\n then end"; AndDescr descrs = ((RuleDescr) parse( "rule", text )).getLhs(); assertEquals( 2, descrs.getDescrs().size() ); PatternDescr pat = (PatternDescr) descrs.getDescrs().get( 1 ); ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get( 0 ); assertEquals( "city memberOf $cities", fieldConstr.getExpression() ); }
@Test public void testRestrictions() throws Exception { final String text = "rule X when Foo( bar > 1 || == 1 ) then end\n"; AndDescr descrs = (AndDescr) ((RuleDescr) parse( "rule", text )).getLhs(); assertEquals( 1, descrs.getDescrs().size() ); PatternDescr pat = (PatternDescr) descrs.getDescrs().get( 0 ); ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get( 0 ); assertEquals( "bar > 1 || == 1", fieldConstr.getExpression() ); }
@Test public void testFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "from.drl" ); assertFalse( parser.getErrorMessages().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "using_from", rule.getName() ); assertEquals( 9, rule.getLhs().getDescrs().size() ); }
@Test public void testTypeWithMetaData() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "type_with_meta.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations(); assertEquals( 3, declarations.size() ); }
@Test public void testQualifiedClassname() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "qualified_classname.drl" ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); final PatternDescr p = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); assertEquals( "com.cheeseco.Cheese", p.getObjectType() ); }
@Test public void testQuotedStringNameRule() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "quoted_string_name_rule.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "quoted string name", rule.getName() ); assertNotNull( rule.getLhs() ); assertEquals( "", ((String) rule.getConsequence()).trim() ); }
@Test public void testChunkWithParensAndQuotedString() throws Exception { String input = "( fnord( \"cheese\" ) )"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "fnord( \"cheese\" )", returnData ); }
@Test public void testChunkWithParens() throws Exception { String input = "(fnord())"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "fnord()", returnData ); }
@Test public void testComment() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "comment.drl" ); assertNotNull( pkg ); assertEquals( "foo.bar", pkg.getName() ); }
@Test public void testPackageWithError() throws Exception { final String source = "package 12 foo.bar.baz"; final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( true, new StringReader( source ) ); assertTrue( parser.hasErrors() ); assertEquals( "foo.bar.baz", pkg.getName() ); }
@Test public void testPackage() throws Exception { final String source = "package foo.bar.baz"; final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( new StringReader( source ) ); assertFalse( parser.hasErrors() ); assertEquals( "foo.bar.baz", pkg.getName() ); }
@Test public void testPackageWithError2() throws Exception { final String source = "package 12 12312 231"; final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( true, new StringReader( source ) ); assertTrue( parser.hasErrors() ); assertEquals( "", pkg.getName() ); }
@Test public void testRuleParseLhsWithStringQuotes() throws Exception { final String text = "rule X when Person( location==\"atlanta\\\"\") then end\n"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); AndDescr lhs = rule.getLhs(); ExprConstraintDescr constr = (ExprConstraintDescr) ((PatternDescr) lhs.getDescrs().get( 0 )).getDescrs().get( 0 ); assertEquals( "location==\"atlanta\\\"\"", constr.getText() ); }
@Test public void testEmptyRule() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "empty_rule.drl" ); assertNotNull( rule ); assertEquals( "empty", rule.getName() ); assertNotNull( rule.getLhs() ); assertNotNull( rule.getConsequence() ); }