@Test public void testVisitPatternMultipleWithLabels() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))"); final List<String> elements = new ArrayList<String>(); wiz.visit( t, "(%a:A %b:B)", new TreeWizard.ContextVisitor() { @Override public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels) { elements.add( adaptor.getText(t) + "@" + (parent != null ? adaptor.getText(parent) : "nil") + "[" + childIndex + "]" + labels.get("a") + "&" + labels.get("b")); } }); String found = elements.toString(); String expecting = "[foo@A[2]foo&bar, big@D[0]big&dog]"; assertEquals(expecting, found); }
@Test public void testVisitPattern() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C (A B) D)"); final List<Object> elements = new ArrayList<Object>(); wiz.visit( t, "(A B)", new TreeWizard.Visitor() { @Override public void visit(Object t) { elements.add(t); } }); String found = elements.toString(); String expecting = "[A]"; // shouldn't match overall root, just (A B) assertEquals(expecting, found); }
@Test public void testRepeatsVisit2() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)"); final List<Object> elements = new ArrayList<Object>(); wiz.visit( t, wiz.getTokenType("A"), new TreeWizard.Visitor() { @Override public void visit(Object t) { elements.add(t); } }); String found = elements.toString(); String expecting = "[A, A]"; assertEquals(expecting, found); }
@Test public void testVisitPatternMultiple() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C (A B) (D (A B)))"); final List<String> elements = new ArrayList<String>(); wiz.visit( t, "(A B)", new TreeWizard.ContextVisitor() { @Override public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels) { elements.add( adaptor.getText(t) + "@" + (parent != null ? adaptor.getText(parent) : "nil") + "[" + childIndex + "]"); } }); String found = elements.toString(); String expecting = "[A@A[2], A@D[0]]"; // shouldn't match overall root, just (A B) assertEquals(expecting, found); }
@Test public void testRepeatsVisitWithNullParentAndContext() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)"); final List<String> elements = new ArrayList<String>(); wiz.visit( t, wiz.getTokenType("A"), new TreeWizard.ContextVisitor() { @Override public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels) { elements.add( adaptor.getText(t) + "@" + (parent != null ? adaptor.getText(parent) : "nil") + "[" + childIndex + "]"); } }); String found = elements.toString(); String expecting = "[A@nil[0], A@A[1]]"; assertEquals(expecting, found); }