mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-22 22:22:11 +02:00
A unit test for LinkedNamesFinder class.
This commit is contained in:
parent
c7bf0c7e89
commit
934e4908a5
3 changed files with 145 additions and 12 deletions
|
@ -104,7 +104,6 @@ public class AST2BaseTest extends BaseTestCase {
|
|||
super(name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
sValidateCopy= true;
|
||||
|
@ -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());
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -21,5 +21,6 @@ public class SearchTestSuite extends TestSuite {
|
|||
public SearchTestSuite() {
|
||||
super(SearchTestSuite.class.getName());
|
||||
addTest(BasicSearchTest.suite());
|
||||
addTest(LinkedNamesFinderTest.suite());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue