@Test public void testLineNumberIncludingCommentsInRHS() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "test_CommentLineNumbersInConsequence.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final String rhs = (String) ((RuleDescr) pkg.getRules().get( 0 )).getConsequence(); String expected = "\\s*//woot$\\s*first$\\s*$\\s*//$\\s*$\\s*/\\* lala$\\s*$\\s*\\*/$\\s*second$\\s*"; assertTrue( Pattern.compile( expected, Pattern.DOTALL | Pattern.MULTILINE ).matcher( rhs ).matches() ); }
@Test public void testRuleWithoutEnd() throws Exception { String source = "rule \"Invalid customer id\" \n" + " when \n" + " o: Order( ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n"; parse( "compilationUnit", source ); assertTrue( parser.hasErrors() ); }
@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 testKeywordCollisions() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "eol_funny_business.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertEquals( 1, pkg.getRules().size() ); }
@Test public void testInvalidSyntax_Catches() throws Exception { DRLParser parser = parseResource("invalid_syntax.drl"); parser.compilationUnit(); assertTrue(parser.hasErrors()); }
@Test public void testNotBindindShouldBarf() throws Exception { final DRLParser parser = parseResource( "not_with_binding_error.drl" ); parser.compilationUnit(); assertTrue(parser.hasErrors()); }
@Test public void testPackageGarbage() throws Exception { DRLParser parser = parseResource( "package_garbage.drl" ); parser.compilationUnit(); assertTrue(parser.hasErrors()); }
@Test public void testLexicalError() throws Exception { DRLParser parser = parseResource( "lex_error.drl" ); parser.compilationUnit(); assertTrue( parser.hasErrors() ); }
@Test public void testTempleteError() throws Exception { DRLParser parser = parseResource( "template_test_error.drl" ); parser.compilationUnit(); assertTrue(parser.hasErrors()); }
@Test public void testNPEOnParser() throws Exception { final DRLParser parser = parseResource( "npe_on_parser.drl" ); parser.compilationUnit(); assertTrue( "Parser should have raised errors", parser.hasErrors() ); assertEquals( 1, parser.getErrors().size() ); assertTrue( parser.getErrors().get( 0 ).getErrorCode().equals( "ERR 102" ) ); }
@Test public void testMultipleErrors() throws Exception { DRLParser parser = parseResource( "multiple_errors.drl" ); parser.compilationUnit(); assertTrue(parser.hasErrors()); assertEquals(2, parser.getErrors().size()); }
@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 testEvalWithSemicolon() throws Exception { DRLParser parser = parseResource( "eval_with_semicolon.drl" ); parser.compilationUnit(); assertTrue( parser.hasErrors() ); assertEquals(1, parser.getErrorMessages().size()); assertEquals("ERR 102", parser.getErrors().get(0).getErrorCode()); }
@Test public void testErrorMessageForMisplacedParenthesis() throws Exception { final DRLParser parser = parseResource( "misplaced_parenthesis.drl" ); parser.compilationUnit(); assertTrue( "Parser should have raised errors", parser.hasErrors() ); assertEquals( 1, parser.getErrors().size() ); assertEquals("ERR 102", parser.getErrors().get(0).getErrorCode()); }
@Test public void testCommaMisuse() throws Exception { final DRLParser parser = parseResource( "comma_misuse.drl" ); try { parser.compilationUnit(); assertTrue( "Parser should have raised errors", parser.hasErrors() ); } catch ( NullPointerException npe ) { fail( "Should not raise NPE" ); } }
@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 testSimpleMethodCallWithFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "test_SimpleMethodCallWithFrom.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final FromDescr from = (FromDescr) pattern.getSource(); final MVELExprDescr method = (MVELExprDescr) from.getDataSource(); assertEquals( "something.doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )", method.getExpression() ); }
@Test public void testSimpleFunctionCallWithFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "test_SimpleFunctionCallWithFrom.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final FromDescr from = (FromDescr) pattern.getSource(); final MVELExprDescr func = (MVELExprDescr) from.getDataSource(); assertEquals( "doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )", func.getExpression() ); }
@Test public void testSimpleAccessorAndArgWithFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "test_SimpleAccessorArgWithFrom.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final FromDescr from = (FromDescr) pattern.getSource(); final MVELExprDescr accessor = (MVELExprDescr) from.getDataSource(); assertEquals( "something.doIt[\"key\"]", accessor.getExpression() ); }
@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() ); }