diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java index cb67fdbbb07..d8f15ceaf4c 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java @@ -104,7 +104,6 @@ public class AST2BaseTest extends BaseTestCase { super(name); } - @Override protected void setUp() throws Exception { sValidateCopy= true; @@ -453,7 +452,7 @@ public class AST2BaseTest extends BaseTestCase { throw new AssertionFailedError("This test must be run as a JUnit plugin test"); return TestSourceReader.getContentsForTest(plugin.getBundle(), "parser", getClass(), getName(), sections); } - + protected static <T> T assertInstance(Object o, Class<T> clazz, Class... cs) { assertNotNull("Expected object of "+clazz.getName()+" but got a null value", o); assertTrue("Expected "+clazz.getName()+" but got "+o.getClass().getName(), clazz.isInstance(o)); @@ -506,7 +505,7 @@ public class AST2BaseTest extends BaseTestCase { } public void assertNoName(String section, int len) { - IASTName name= findName(section,len,false); + IASTName name= findName(section, len); if (name != null) { String selection = section.substring(0, len); fail("Found unexpected \""+selection+"\": " + name.resolveBinding()); @@ -518,7 +517,7 @@ public class AST2BaseTest extends BaseTestCase { * it resolves to the given type of binding. */ public IASTImplicitName assertImplicitName(String section, int len, Class<?> bindingClass) { - IASTName name = findName(section,len,true); + IASTName name = findImplicitName(section, len); final String selection = section.substring(0, len); assertNotNull("did not find \""+selection+"\"", name); @@ -527,10 +526,10 @@ public class AST2BaseTest extends BaseTestCase { IASTImplicitName[] implicits = owner.getImplicitNames(); assertNotNull(implicits); - if(implicits.length > 1) { + if (implicits.length > 1) { boolean found = false; - for(IASTImplicitName n : implicits) { - if(((ASTNode)n).getOffset() == ((ASTNode)name).getOffset()) { + for (IASTImplicitName n : implicits) { + if (((ASTNode) n).getOffset() == ((ASTNode)name).getOffset()) { assertFalse(found); found = true; } @@ -546,23 +545,30 @@ public class AST2BaseTest extends BaseTestCase { } public void assertNoImplicitName(String section, int len) { - IASTName name = findName(section,len,true); + IASTName name = findImplicitName(section, len); final String selection = section.substring(0, len); assertNull("found name \""+selection+"\"", name); } public IASTImplicitName[] getImplicitNames(String section, int len) { - IASTName name = findName(section,len,true); + IASTName name = findImplicitName(section, len); IASTImplicitNameOwner owner = (IASTImplicitNameOwner) name.getParent(); IASTImplicitName[] implicits = owner.getImplicitNames(); return implicits; } - private IASTName findName(String section, int len, boolean implicit) { + public IASTName findName(String section, int len) { final int offset = contents.indexOf(section); assertTrue(offset >= 0); IASTNodeSelector selector = tu.getNodeSelector(null); - return implicit ? selector.findImplicitName(offset, len) : selector.findName(offset, len); + return selector.findName(offset, len); + } + + public IASTName findImplicitName(String section, int len) { + final int offset = contents.indexOf(section); + assertTrue(offset >= 0); + IASTNodeSelector selector = tu.getNodeSelector(null); + return selector.findImplicitName(offset, len); } private String renderProblemID(int i) { @@ -595,7 +601,7 @@ public class AST2BaseTest extends BaseTestCase { } private IBinding binding(String section, int len) { - IASTName name = findName(section, len,false); + IASTName name = findName(section, len); final String selection = section.substring(0, len); assertNotNull("did not find \""+selection+"\"", name); assertEquals(selection, name.getRawSignature()); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/LinkedNamesFinderTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/LinkedNamesFinderTest.java new file mode 100644 index 00000000000..03670e5a033 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/LinkedNamesFinderTest.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2009 Google, Inc and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sergey Prigogin (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.search; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Comparator; + +import junit.framework.AssertionFailedError; +import junit.framework.TestSuite; + +import org.eclipse.jface.text.IRegion; + +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.parser.tests.ast2.AST2BaseTest; +import org.eclipse.cdt.core.testplugin.util.TestSourceReader; +import org.eclipse.cdt.ui.testplugin.CTestPlugin; + +import org.eclipse.cdt.internal.core.parser.ParserException; + +import org.eclipse.cdt.internal.ui.search.LinkedNamesFinder; + +/** + * Tests for LinkedNamesFinder class. + */ +public class LinkedNamesFinderTest extends AST2BaseTest { + private static class RegionComparator implements Comparator<IRegion> { + public int compare(IRegion r1, IRegion r2) { + return r1.getOffset() - r2.getOffset(); + } + } + + static final RegionComparator REGION_COMPARATOR = new RegionComparator(); + + public LinkedNamesFinderTest() { + } + + public LinkedNamesFinderTest(String name) { + super(name); + } + + public static TestSuite suite() { + return suite(LinkedNamesFinderTest.class); + } + + @Override + protected StringBuffer[] getContents(int sections) throws IOException { + CTestPlugin plugin = CTestPlugin.getDefault(); + if (plugin == null) + throw new AssertionFailedError("This test must be run as a JUnit plugin test"); + return TestSourceReader.getContentsForTest(plugin.getBundle(), "ui", getClass(), getName(), sections); + } + + private IRegion[] getLinkedRegions(String code, String name, int len, boolean isCpp) throws ParserException { + BindingAssertionHelper ba= new BindingAssertionHelper(code, isCpp); + IASTName astName = ba.findName(name, len); + IRegion[] regions = LinkedNamesFinder.findByName(ba.getTranslationUnit(), astName); + Arrays.sort(regions, REGION_COMPARATOR); + name = name.substring(0, len); + if (name.charAt(0) == '~') { + name = name.substring(1); + } + for (IRegion region : regions) { + assertEquals(name, code.substring(region.getOffset(), region.getOffset() + region.getLength())); + } + return regions; + } + + private void assertContents(String code, int offset, String expected) { + assertEquals(expected, code.substring(offset, offset + expected.length())); + } + + // class A { + // public: + // void m(int x); + // void m(int x, int y); + // }; + // + // void A::m(int x) {} + // void A::m(int x, int y) {} + public void testMethodParameter() throws Exception { + String code = getAboveComment(); + IRegion[] regions = getLinkedRegions(code, "x);", 1, true); + assertEquals(2, regions.length); + assertContents(code, regions[0].getOffset(), "x)"); + assertContents(code, regions[1].getOffset(), "x) {}"); + } + + // class A { + // public: + // A(); + // A(int x); + // ~A(); + // }; + // + // A::A() {} + // A::A(int x) {} + // A::~A() {} + public void testClass() throws Exception { + String code = getAboveComment(); + IRegion[] regions = getLinkedRegions(code, "A {", 1, true); + assertEquals(10, regions.length); + assertContents(code, regions[0].getOffset(), "A {"); + assertContents(code, regions[1].getOffset(), "A();"); + assertContents(code, regions[2].getOffset(), "A(int x);"); + assertContents(code, regions[3].getOffset() - 1, "~A();"); + assertContents(code, regions[4].getOffset(), "A::A() {}"); + assertContents(code, regions[5].getOffset(), "A() {}"); + assertContents(code, regions[6].getOffset(), "A::A(int x) {}"); + assertContents(code, regions[7].getOffset(), "A(int x) {}"); + assertContents(code, regions[8].getOffset(), "A::~A() {}"); + assertContents(code, regions[9].getOffset() - 1, "~A() {}"); + IRegion[] regions2 = getLinkedRegions(code, "A(int x) {}", 1, true); + assertTrue(Arrays.equals(regions2, regions)); + IRegion[] regions3 = getLinkedRegions(code, "~A();", 2, true); + assertTrue(Arrays.equals(regions3, regions)); + } +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/SearchTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/SearchTestSuite.java index eeccda84627..224bca5141e 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/SearchTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/SearchTestSuite.java @@ -21,5 +21,6 @@ public class SearchTestSuite extends TestSuite { public SearchTestSuite() { super(SearchTestSuite.class.getName()); addTest(BasicSearchTest.suite()); + addTest(LinkedNamesFinderTest.suite()); } }