001: package org.codehaus.groovy.tck;
002:
003: import java.io.Reader;
004: import java.io.StringReader;
005: import java.io.BufferedReader;
006: import java.io.IOException;
007: import java.util.ArrayList;
008: import junit.framework.TestResult; // Jsr parser
009: // @todo - refactor pulling generic parser interface up
010: import org.codehaus.groovy.antlr.parser.GroovyLexer;
011: import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
012:
013: // codehaus reference implementation usage
014: // @todo - remove classic references from the TCK
015: import org.codehaus.groovy.control.CompilerConfiguration;
016: import groovy.lang.GroovyShell;
017: import antlr.RecognitionException;
018:
019: /** Helper methods for generated TCK test case using new JSR parser and classic groovy AST and evaluation */
020: public class ClassicGroovyTestGeneratorHelper implements
021: TestGeneratorHelper {
022:
023: /** evaluate the source text against the classic AST with the JSR parser implementation*/
024: public Object evaluate(String theSrcText, String testName)
025: throws Exception {
026: parse(theSrcText, testName); // fail early with a direct message if possible')
027: GroovyShell groovy = new GroovyShell(
028: new CompilerConfiguration());
029: return groovy.run(theSrcText, "main", new ArrayList());
030: }
031:
032: /** run the JSR parser implementation over the supplied source text*/
033: public void parse(String theSrcText, String testName)
034: throws Exception {
035: System.out.println("-------------------------------");
036: System.out.println(" " + testName);
037: System.out.println("-------------------------------");
038: try {
039: Reader reader = new BufferedReader(new StringReader(
040: theSrcText));
041: GroovyRecognizer recognizer = GroovyRecognizer.make(reader);
042: recognizer.compilationUnit();
043: System.out.println(decorateWithLineNumbers(theSrcText));
044:
045: } catch (RecognitionException parseException) {
046: System.out.println(decorateWithLineNumbersAndErrorMessage(
047: theSrcText, parseException));
048: throw parseException;
049: }
050: System.out.println("-------------------------------");
051:
052: }
053:
054: private String decorateWithLineNumbersAndErrorMessage(
055: String theSrcText, RecognitionException parseException) {
056: try {
057: BufferedReader reader = new BufferedReader(
058: new StringReader(theSrcText));
059: String line = null;
060: StringBuffer numberedSrcTextBuffer = new StringBuffer();
061: int lineNum = 1;
062: while ((line = reader.readLine()) != null) {
063: numberedSrcTextBuffer.append(lineNum);
064: numberedSrcTextBuffer.append("\t");
065: numberedSrcTextBuffer.append(line);
066: numberedSrcTextBuffer.append(lineSep);
067:
068: if (parseException != null) {
069: if (lineNum == parseException.getLine()) {
070: StringBuffer padding = new StringBuffer("\t");
071: for (int col = 1; col < parseException
072: .getColumn(); col++) {
073: padding.append(" ");
074: }
075: numberedSrcTextBuffer.append(padding);
076: numberedSrcTextBuffer.append("^");
077: numberedSrcTextBuffer.append(lineSep);
078: numberedSrcTextBuffer.append("ERROR:");
079: numberedSrcTextBuffer.append(lineSep);
080: numberedSrcTextBuffer.append(parseException
081: .getMessage());
082: numberedSrcTextBuffer.append(lineSep);
083: numberedSrcTextBuffer.append(lineSep);
084: }
085: }
086:
087: lineNum++;
088:
089: }
090: theSrcText = numberedSrcTextBuffer.toString();
091: } catch (IOException e) {
092: //ignore
093: }
094: return theSrcText;
095: }
096:
097: private String decorateWithLineNumbers(String theSrcText) {
098: return decorateWithLineNumbersAndErrorMessage(theSrcText, null);
099: }
100:
101: protected String lineSep = System.getProperty("line.separator");
102: }
|