public Object execParser( String testRuleName, CharStream charStream ) { try { createParser( charStream ); /** Use Reflection to get rule method from parser */ Method ruleName = null; Object[] params = null; for ( Method method : DRL6Parser.class.getMethods() ) { if ( method.getName().equals( testRuleName ) ) { ruleName = method; Class< ? >[] parameterTypes = method.getParameterTypes(); params = new Object[parameterTypes.length]; } } /** Invoke grammar rule, and get the return value */ Object ruleReturn = ruleName.invoke( parser, params ); if ( parser.hasErrors() ) { System.out.println( parser.getErrorMessages() ); } return ruleReturn; } catch ( Exception e ) { e.printStackTrace(); fail( e.getMessage() ); } return null; }
@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 testChunkWithoutParens() throws Exception { String input = "( foo )"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "foo", returnData ); }
@Test public void testChunkWithRandomCharac5ters() throws Exception { String input = "( %*9dkj)"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "%*9dkj", returnData ); }