From 68719b8df713e0b649bc2e8ab90500e93ec056bd Mon Sep 17 00:00:00 2001 From: parrt Date: Fri, 12 Feb 2010 17:46:33 -0800 Subject: [PATCH] got test harness going [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6691] --- tool/test/org/antlr/v4/test/BaseTest.java | 10 ++- .../antlr/v4/test/TestAttributeChecks.java | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 tool/test/org/antlr/v4/test/TestAttributeChecks.java diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index ec99e5698..5e2a46e9c 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -426,9 +426,13 @@ public abstract class BaseTest { ErrorManager.setErrorListener(equeue); try { String[] lines = input.split("\n"); - int lastSpace = lines[0].lastIndexOf(' '); - int semi = lines[0].lastIndexOf(';'); - String fileName = lines[0].substring(lastSpace+1, semi)+".g"; + String fileName = ""; + int grIndex = lines[0].indexOf("grammar"); + if ( grIndex>=0 ) { + int space = lines[0].indexOf(' ', grIndex); + int semi = lines[0].lastIndexOf(';'); + fileName = lines[0].substring(space+1, semi)+".g"; + } Grammar g = new Grammar(fileName, input); g.loadImportedGrammars(); SemanticPipeline sem = new SemanticPipeline(); diff --git a/tool/test/org/antlr/v4/test/TestAttributeChecks.java b/tool/test/org/antlr/v4/test/TestAttributeChecks.java new file mode 100644 index 000000000..da74abbd4 --- /dev/null +++ b/tool/test/org/antlr/v4/test/TestAttributeChecks.java @@ -0,0 +1,69 @@ +package org.antlr.v4.test; + +import org.antlr.runtime.RecognitionException; +import org.junit.Test; +import org.stringtemplate.v4.ST; + +/** */ +public class TestAttributeChecks extends BaseTest { + String attributeTemplate = + "parser grammar A;\n"+ + "@members {}\n" + + "a[int x] returns [int y]\n" + + "@init {}\n" + + " : {}\n" + + " ;\n" + + " finally {}\n" + + "b[int d] returns [int e]\n" + + " : {}\n" + + " ;\n" + + "c : ;"; + + String scopeTemplate = + "parser grammar A;\n"+ + "@members {\n" + + "}\n" + + "scope S { int i; }\n" + + "a[int x] returns [int y]\n" + + "scope { int z; }\n" + + "scope S;\n" + + "@init {}\n" + + " : {}\n" + + " ;\n" + + " finally {}\n" + + "b[int d] returns [int e]\n" + + "scope { int f; }\n" + + " : {}\n" + + " ;\n" + + "c : ;"; + + String[] membersChecks = { + "$a.y", "error(29): A.g:2:12: unknown attribute reference a in $a.y", + }; + + String[] initChecks = { + "$a.y", "error(29): A.g:4:9: unknown attribute reference a in $a.y", + }; + + @Test public void testMembersActions() throws RecognitionException { + for (int i = 0; i < membersChecks.length; i+=2) { + String m = membersChecks[i]; + String expected = membersChecks[i+1]; + ST st = new ST(attributeTemplate); + st.add("members", m); + String grammar = st.render(); + testErrors(new String[] {grammar, expected}); + } + } + + @Test public void testInitActions() throws RecognitionException { + for (int i = 0; i < initChecks.length; i+=2) { + String init = initChecks[i]; + String expected = initChecks[i+1]; + ST st = new ST(attributeTemplate); + st.add("init", init); + String grammar = st.render(); + testErrors(new String[] {grammar, expected}); + } + } +}