/** * Copyright 2011 Oliver Buchtala * * This file is part of ndogen. * * ndogen is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ndogen is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ndogen. If not, see <http://www.gnu.org/licenses/>. */ package org.ndogen; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.LinkedList; import java.util.List; import org.antlr.runtime.ANTLRStringStream; import org.antlr.runtime.CharStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.Parser; import org.antlr.runtime.Token; import org.antlr.runtime.TokenStream; import org.ndogen.antlr.DebugCharStream; import org.ndogen.antlr.NdogenLexer; import org.ndogen.markdown.MarkdownScanner; public class TestUtils { public static Fixture getFixture(String baseDir, String fixtureName) { Fixture fixture = new Fixture(baseDir+"/"+fixtureName); return fixture; } public static String getScannerOutput(Class<? extends NdogenLexer> lexerClass, Fixture fixture) { CharStream lexerInput = getInputStream(fixture); NdogenLexer lexer = createLexer(lexerClass, lexerInput); List<Token> tokens = getTokens(lexer); LinkedList<String> _tokens = new LinkedList<String>(); for (Token t : tokens) { _tokens.add(lexer.getTokenName(t.getType())); } return join(_tokens, " "); } public static <T extends Parser> T createParser(Fixture fixture, Class<? extends NdogenLexer> lexerClass, Class<T> parserClass) { CharStream lexerInput = getInputStream(fixture); NdogenLexer lexer = createLexer(lexerClass, lexerInput); T parser = createParser(parserClass, lexer); return parser; } public static List<Token> getTokens(Lexer lexer) { LinkedList<Token> tokens = new LinkedList<Token>(); while(true) { Token t = lexer.nextToken(); if(t.getType() == MarkdownScanner.EOF) { break; } tokens.add(t); } return tokens; } public static String join(List<String> vals, String delim) { StringBuilder sb = new StringBuilder(); if(vals.isEmpty()) return ""; for (String s : vals) { sb.append(delim + s); } return sb.substring(delim.length()); } private static NdogenLexer createLexer( Class<? extends NdogenLexer> lexerClass, CharStream lexerInput) { NdogenLexer lexer; try { Constructor<? extends NdogenLexer> ctor = lexerClass.getConstructor(CharStream.class); lexer = (NdogenLexer) ctor.newInstance(lexerInput); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } return lexer; } private static <T extends Parser> T createParser( Class<T> parserClass, Lexer lexer) { T parser; try { CommonTokenStream tokens = new CommonTokenStream(lexer); Constructor<T> ctor = parserClass.getConstructor(TokenStream.class); parser = (T) ctor.newInstance(tokens); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } return parser; } private static CharStream getInputStream(Fixture fixture) { String input = fixture.getInput(); CharStream lexerInput; lexerInput = new ANTLRStringStream(input); lexerInput = new DebugCharStream(lexerInput); return lexerInput; } }