Permalink
Browse files

Moving test support for direct ANTLR test inside the JAR so we can

eliminate the public/internal test dichotomy.
  • Loading branch information...
1 parent 83fb5d2 commit bacae90214bee567cf6d76538f73f65540f9af67 @mmastrac committed Apr 12, 2011
@@ -0,0 +1,43 @@
+package net.stencilproject.template;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.TokenStream;
+import org.antlr.runtime.tree.CommonTree;
+
+/**
+ * For test support only.
+ */
+class InternalParserTestSupport {
+ public static String testParse(String input) {
+ final ANTLRStringStream stringStream = new ANTLRStringStream(input);
+ final Exception[] stashed = new Exception[1];
+
+ TemplateLexer lexer = new GatingTemplateLexer(stringStream) {
+ @Override
+ public void reportError(RecognitionException e) {
+ stashed[0] = e;
+ throw new RuntimeException(e);
+ };
+ };
+ TokenStream stream = new CommonTokenStream(lexer);
+
+ TemplateParser parser = new TemplateParser(stream) {
+ @Override
+ public void reportError(RecognitionException e) {
+ stashed[0] = e;
+ throw new RuntimeException(e);
+ };
+ };
+
+ if (stashed[0] != null) {
+ throw new RuntimeException(stashed[0]);
+ }
+ try {
+ return ((CommonTree) parser.template().tree).toStringTree();
+ } catch (RecognitionException e1) {
+ throw new RuntimeException(e1);
+ }
+ }
+}
@@ -1,105 +1,65 @@
package net.stencilproject.template;
-import net.stencilproject.template.GatingTemplateLexer;
-import net.stencilproject.template.TemplateLexer;
-import net.stencilproject.template.TemplateParser;
-
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.RecognitionException;
-import org.antlr.runtime.TokenStream;
-import org.antlr.runtime.tree.CommonTree;
import org.junit.Test;
+/**
+ * Smoketests the ANTLR grammar. Doesn't currently assert on contents (should
+ * though).
+ */
public class TestAntlrGrammar {
@Test
- public void testGrammar() throws RecognitionException {
+ public void testGrammar() {
final String input = "yo test!";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
+ System.out.println(InternalParserTestSupport.testParse(input));
}
@Test
- public void testGrammar2() throws RecognitionException {
+ public void testGrammar2() {
final String input = "yo test{{ 1 }}";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
+ System.out.println(InternalParserTestSupport.testParse(input));
}
@Test
- public void testGrammar3() throws RecognitionException {
+ public void testGrammar3() {
final String input = "yo test{# 1 #}";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
+ System.out.println(InternalParserTestSupport.testParse(input));
}
@Test
- public void testGrammar4() throws RecognitionException {
+ public void testGrammar4() {
final String input = "yo test{% 1 %}hey";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
+ System.out.println(InternalParserTestSupport.testParse(input));
}
@Test
- public void testGrammar5() throws RecognitionException {
+ public void testGrammar5() {
final String input = "yo test{{% 1 %}}hey";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
+ System.out.println(InternalParserTestSupport.testParse(input));
}
@Test
- public void testGrammar6() throws RecognitionException {
+ public void testGrammar6() {
final String input = "yo test{{{ 1 }}}hey";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
+ System.out.println(InternalParserTestSupport.testParse(input));
}
@Test
- public void testGrammar7() throws RecognitionException {
+ public void testGrammar7() {
final String input = "yo test{%%%raw this is totally raw{{}} %%%}hey";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
+ System.out.println(InternalParserTestSupport.testParse(input));
}
@Test
- public void testGrammar8() throws RecognitionException {
+ public void testGrammar8() {
final String input = "{{1}} {{{2}}} {{{{3}}}} {%%%raw {{4}}%%%}{{5}}";
- CommonTree tree = parse(input);
- System.out.println(tree.toStringTree());
- }
-
- private CommonTree parse(final String input) throws RecognitionException {
- final ANTLRStringStream stringStream = new ANTLRStringStream(input);
- final Exception[] stashed = new Exception[1];
-
- TemplateLexer lexer = new GatingTemplateLexer(stringStream) {
- @Override
- public void reportError(RecognitionException e) {
- stashed[0] = e;
- throw new RuntimeException(e);
- };
- };
- TokenStream stream = new CommonTokenStream(lexer);
-
- TemplateParser parser = new TemplateParser(stream) {
- @Override
- public void reportError(RecognitionException e) {
- stashed[0] = e;
- throw new RuntimeException(e);
- };
- };
-
- if (stashed[0] != null) {
- throw new RuntimeException(stashed[0]);
- }
- return (CommonTree) parser.template().tree;
+ System.out.println(InternalParserTestSupport.testParse(input));
}
}

0 comments on commit bacae90

Please sign in to comment.