mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
227967: add test cases
This commit is contained in:
parent
4db6ea5e78
commit
0efd6a1877
4 changed files with 286 additions and 8 deletions
|
@ -415,11 +415,14 @@ public class AST2BaseTest extends BaseTestCase {
|
|||
CTestPlugin.getDefault().getBundle(), "parser", getClass(), getName(), sections);
|
||||
}
|
||||
|
||||
protected static void assertInstance(Object o, Class... cs) {
|
||||
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));
|
||||
for(Class c : cs) {
|
||||
assertNotNull("Expected object of "+c.getName()+" but got a null value", o);
|
||||
assertTrue("Expected "+c.getName()+" but got "+o.getClass().getName(), c.isInstance(o));
|
||||
}
|
||||
return clazz.cast(o);
|
||||
}
|
||||
|
||||
protected static void assertField(IBinding binding, String fieldName, String ownerName) throws DOMException {
|
||||
|
@ -456,7 +459,9 @@ public class AST2BaseTest extends BaseTestCase {
|
|||
IBinding binding= binding(section, len);
|
||||
assertTrue("ProblemBinding for name: "+section, !(binding instanceof IProblemBinding));
|
||||
assertInstance(binding, type);
|
||||
assertInstance(binding, cs);
|
||||
for(Class c : cs) {
|
||||
assertInstance(binding, c);
|
||||
}
|
||||
return type.cast(binding);
|
||||
}
|
||||
|
||||
|
|
|
@ -1299,6 +1299,130 @@ public class AST2TemplateTests extends AST2BaseTest {
|
|||
assertSame( ((ICPPSpecialization)f2).getSpecializedBinding(), f1 );
|
||||
}
|
||||
|
||||
// template<typename T>
|
||||
// class A {};
|
||||
//
|
||||
// class B {};
|
||||
//
|
||||
// template<>
|
||||
// class A<B> {};
|
||||
//
|
||||
// class C {};
|
||||
//
|
||||
// A<B> ab;
|
||||
// A<C> ac;
|
||||
public void testEnclosingScopes_a() throws Exception {
|
||||
BindingAssertionHelper ba= new BindingAssertionHelper(getAboveComment(), true);
|
||||
|
||||
ICPPSpecialization b0= ba.assertNonProblem("A<B>", 4, ICPPSpecialization.class, ICPPClassType.class);
|
||||
ICPPTemplateInstance b1= ba.assertNonProblem("A<C>", 4, ICPPTemplateInstance.class, ICPPClassType.class);
|
||||
|
||||
ICPPClassType sc0= assertInstance(b0.getSpecializedBinding(), ICPPClassType.class);
|
||||
ICPPClassType sc1= assertInstance(b1.getSpecializedBinding(), ICPPClassType.class);
|
||||
assertTrue(sc0.isSameType(sc1));
|
||||
|
||||
assertInstance(b0, ICPPSpecialization.class);
|
||||
assertInstance(b1, ICPPTemplateInstance.class);
|
||||
|
||||
assertInstance(b0.getScope(), ICPPTemplateScope.class);
|
||||
|
||||
IScope ts0= ((ICPPClassType) b0.getSpecializedBinding()).getScope();
|
||||
IScope ts1= ((ICPPClassType) b1.getSpecializedBinding()).getScope();
|
||||
|
||||
assertInstance(ts0, ICPPTemplateScope.class);
|
||||
|
||||
assertSame(ts0, ts1);
|
||||
assertNotSame(ts0, b0.getScope());
|
||||
assertSame(ts1, b1.getScope()); // a class instance exists in the same scope as the template its defined from
|
||||
}
|
||||
|
||||
// template<typename T>
|
||||
// class A {
|
||||
// public:
|
||||
// class B {};
|
||||
// };
|
||||
//
|
||||
// class C {}; class D {};
|
||||
//
|
||||
// template<>
|
||||
// class A<C> {
|
||||
// public:
|
||||
// class B {};
|
||||
// };
|
||||
//
|
||||
// void refs() {
|
||||
// A<C>::B acb;
|
||||
// A<D>::B adb;
|
||||
// }
|
||||
public void testEnclosingScopes_b() throws Exception {
|
||||
BindingAssertionHelper ba= new BindingAssertionHelper(getAboveComment(), true);
|
||||
|
||||
ICPPClassType b0= ba.assertNonProblem("B acb", 1, ICPPClassType.class);
|
||||
ICPPClassType b1= ba.assertNonProblem("B adb", 1, ICPPClassType.class, ICPPSpecialization.class);
|
||||
ICPPClassType b2= ba.assertNonProblem("A<C>", 4, ICPPClassType.class, ICPPSpecialization.class);
|
||||
ICPPClassType b3= ba.assertNonProblem("A {", 1, ICPPClassType.class, ICPPTemplateDefinition.class);
|
||||
ICPPClassType b4= ba.assertNonProblem("B {}", 1, ICPPClassType.class);
|
||||
|
||||
assertFalse(b0 instanceof ICPPSpecialization);
|
||||
|
||||
assertSame(b0.getScope(), b2.getCompositeScope());
|
||||
ICPPClassScope cs1= assertInstance(b1.getScope(), ICPPClassScope.class);
|
||||
assertInstance(cs1.getClassType(), ICPPTemplateInstance.class);
|
||||
assertSame(b4.getScope(), b3.getCompositeScope());
|
||||
}
|
||||
|
||||
// class A {};
|
||||
//
|
||||
// template<typename T>
|
||||
// class X {
|
||||
// public:
|
||||
// class Y {
|
||||
// public:
|
||||
// class Z {};
|
||||
// };
|
||||
// };
|
||||
//
|
||||
// X<A>::Y::Z xayz;
|
||||
public void testEnclosingScopes_c() throws Exception {
|
||||
BindingAssertionHelper ba= new BindingAssertionHelper(getAboveComment(), true);
|
||||
|
||||
ICPPClassType b0= ba.assertNonProblem("Y::Z x", 1, ICPPClassType.class);
|
||||
ICPPClassType b1= ba.assertNonProblem("Z xayz", 1, ICPPClassType.class);
|
||||
|
||||
ICPPClassScope cs0= assertInstance(b0.getScope(), ICPPClassScope.class);
|
||||
assertInstance(cs0.getClassType(), ICPPSpecialization.class);
|
||||
|
||||
ICPPClassScope cs1= assertInstance(b1.getScope(), ICPPClassScope.class);
|
||||
assertInstance(cs1.getClassType(), ICPPSpecialization.class);
|
||||
}
|
||||
|
||||
// class A {}; class B {};
|
||||
//
|
||||
// template<typename T1, typename T2>
|
||||
// class X {};
|
||||
//
|
||||
// template<typename T3>
|
||||
// class X<T3, A> {
|
||||
// public:
|
||||
// class N {};
|
||||
// };
|
||||
//
|
||||
// X<B,A>::N n;
|
||||
public void testEnclosingScopes_d() throws Exception {
|
||||
BindingAssertionHelper ba= new BindingAssertionHelper(getAboveComment(), true);
|
||||
|
||||
ICPPClassType b0= ba.assertNonProblem("N n", 1, ICPPClassType.class);
|
||||
ICPPClassType b1= ba.assertNonProblem("N {", 1, ICPPClassType.class);
|
||||
|
||||
ICPPClassScope s0= assertInstance(b0.getScope(), ICPPClassScope.class);
|
||||
assertInstance(s0.getClassType(), ICPPTemplateInstance.class);
|
||||
|
||||
ICPPClassScope s1= assertInstance(b1.getScope(), ICPPClassScope.class);
|
||||
assertInstance(s1.getClassType(), ICPPTemplateDefinition.class);
|
||||
|
||||
ICPPTemplateScope s2= assertInstance(s1.getClassType().getScope(), ICPPTemplateScope.class);
|
||||
}
|
||||
|
||||
// template<class T> struct A {
|
||||
// void f(T);
|
||||
// template<class X> void g(T,X);
|
||||
|
|
|
@ -92,13 +92,13 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
|
|||
* <li> The binding associated with the name is null or a problem binding
|
||||
* <li> The binding is not an instance of the specified class
|
||||
* </ul>
|
||||
* @param clazz an expected class type or interface that the binding should extend/implement
|
||||
* @param section the code fragment to search for in the AST. The first occurrence of an identical section is used.
|
||||
* @param len the length of the specified section to use as a name. This can also be useful for distinguishing between
|
||||
* template names, and template ids.
|
||||
* @param clazz an expected class type or interface that the binding should extend/implement
|
||||
* @return the associated name's binding
|
||||
*/
|
||||
protected <T> T getBindingFromASTName(Class<T> clazz, String section, int len) {
|
||||
protected <T> T getBindingFromASTName(String section, int len, Class<T> clazz, Class ... cs) {
|
||||
IASTName name= findName(section, len);
|
||||
assertNotNull("name not found for \""+section+"\"", name);
|
||||
assertEquals(section.substring(0, len), name.getRawSignature());
|
||||
|
@ -106,7 +106,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
|
|||
IBinding binding = name.resolveBinding();
|
||||
assertNotNull("No binding for "+name.getRawSignature(), binding);
|
||||
assertFalse("Binding is a ProblemBinding for name "+name.getRawSignature(), IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass()));
|
||||
assertInstance(binding, clazz);
|
||||
assertInstance(binding, clazz, cs);
|
||||
return clazz.cast(binding);
|
||||
}
|
||||
|
||||
|
@ -114,7 +114,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
|
|||
* @see IndexBindingResolutionTestBase#getBindingFromASTName(Class, String, int)
|
||||
*/
|
||||
protected IBinding getBindingFromASTName(String section, int len) {
|
||||
return getBindingFromASTName(IBinding.class, section, len);
|
||||
return getBindingFromASTName(section, len, IBinding.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -173,9 +173,13 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
|
|||
assertEquals(qn, CPPVisitor.renderQualifiedName(((ICPPClassType)ft.getParameterTypes()[index]).getQualifiedName()));
|
||||
}
|
||||
|
||||
protected static void assertInstance(Object o, Class c) {
|
||||
protected static <T> T assertInstance(Object o, Class<T> clazz, Class ... cs) {
|
||||
assertNotNull(o);
|
||||
assertTrue("Expected "+c.getName()+" but got "+o.getClass().getName(), c.isInstance(o));
|
||||
assertTrue("Expected "+clazz.getName()+" but got "+o.getClass().getName(), clazz.isInstance(o));
|
||||
for(Class c : cs) {
|
||||
assertTrue("Expected "+clazz.getName()+" but got "+o.getClass().getName(), c.isInstance(o));
|
||||
}
|
||||
return clazz.cast(o);
|
||||
}
|
||||
|
||||
protected String readTaggedComment(final String tag) throws IOException {
|
||||
|
|
|
@ -15,15 +15,19 @@ import java.util.List;
|
|||
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
|
||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||
import org.eclipse.cdt.core.dom.ast.IBasicType;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.IFunctionType;
|
||||
import org.eclipse.cdt.core.dom.ast.IParameter;
|
||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
|
||||
|
@ -37,12 +41,17 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateScope;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTemplateParameter;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
|
||||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IndexFilter;
|
||||
import org.eclipse.cdt.core.parser.util.ObjectMap;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassSpecializationScope;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
|
||||
import org.eclipse.cdt.internal.core.index.IIndexScope;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
/**
|
||||
* Tests for exercising resolution of template bindings against IIndex
|
||||
|
@ -1090,4 +1099,140 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa
|
|||
assertEquals(foo1, foo2); assertEquals(foo2, foo3);
|
||||
assertEquals(foo3, foo4);
|
||||
}
|
||||
|
||||
// template<typename T>
|
||||
// class A {};
|
||||
//
|
||||
// class B {};
|
||||
//
|
||||
// template<>
|
||||
// class A<B> {};
|
||||
|
||||
// class C {};
|
||||
//
|
||||
// A<B> ab;
|
||||
// A<C> ac;
|
||||
public void _testEnclosingScopes_a() throws Exception {
|
||||
ICPPSpecialization b0= getBindingFromASTName("A<B>", 4, ICPPSpecialization.class, ICPPClassType.class);
|
||||
ICPPTemplateInstance b1= getBindingFromASTName("A<C>", 4, ICPPTemplateInstance.class, ICPPClassType.class);
|
||||
|
||||
ICPPClassType sc0= assertInstance(b0.getSpecializedBinding(), ICPPClassType.class);
|
||||
ICPPClassType sc1= assertInstance(b1.getSpecializedBinding(), ICPPClassType.class);
|
||||
assertTrue(sc0.isSameType(sc1));
|
||||
|
||||
IIndexScope sc2= assertInstance(sc0.getScope(), IIndexScope.class, ICPPTemplateScope.class);
|
||||
|
||||
assertInstance(b0.getScope(), ICPPTemplateScope.class);
|
||||
|
||||
assertNotSame(sc2, b0.getScope());
|
||||
assertEquals(sc2.getScopeBinding(), ((IIndexScope)b1.getScope()).getScopeBinding());
|
||||
}
|
||||
|
||||
// template<typename T>
|
||||
// class A {
|
||||
// public:
|
||||
// class B {};
|
||||
// };
|
||||
//
|
||||
// class C {}; class D {};
|
||||
//
|
||||
// template<>
|
||||
// class A<C> {
|
||||
// public:
|
||||
// class B {};
|
||||
// };
|
||||
|
||||
// void refs() {
|
||||
// A<C>::B acb;
|
||||
// A<D>::B adb;
|
||||
// }
|
||||
public void testEnclosingScopes_b() throws Exception {
|
||||
ICPPClassType b0= getBindingFromASTName("B acb", 1, ICPPClassType.class);
|
||||
ICPPClassType b1= getBindingFromASTName("B adb", 1, ICPPClassType.class, ICPPSpecialization.class);
|
||||
ICPPClassType b2= getBindingFromASTName("A<C>", 4, ICPPClassType.class, ICPPSpecialization.class);
|
||||
|
||||
ICPPClassType b3= (ICPPClassType) getIndex().findBindings("A".toCharArray(), new IndexFilter() {
|
||||
@Override
|
||||
public boolean acceptBinding(IBinding binding) throws CoreException {
|
||||
return !(binding instanceof ICPPSpecialization);
|
||||
}
|
||||
}, NPM)[0];
|
||||
|
||||
ICPPClassType b4= (ICPPClassType) getIndex().findBindings(new char[][] {"A".toCharArray(), "B".toCharArray()}, new IndexFilter() {
|
||||
@Override
|
||||
public boolean acceptBinding(IBinding binding) throws CoreException {
|
||||
try {
|
||||
return !(binding.getScope() instanceof CPPClassSpecializationScope); //
|
||||
} catch(DOMException de) {
|
||||
CCorePlugin.log(de);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}, NPM)[0];
|
||||
|
||||
assertFalse(b0 instanceof ICPPSpecialization);
|
||||
|
||||
IIndexScope s0= (IIndexScope) b0.getScope(), s4= (IIndexScope) b4.getScope();
|
||||
IScope s1= b1.getScope();
|
||||
|
||||
assertTrue(((IType)s0.getScopeBinding()).isSameType((IType)((IIndexScope)b2.getCompositeScope()).getScopeBinding()));
|
||||
ICPPClassScope cs1= assertInstance(s1, ICPPClassScope.class);
|
||||
assertInstance(cs1.getClassType(), ICPPClassType.class);
|
||||
assertInstance(cs1.getClassType(), ICPPTemplateInstance.class);
|
||||
assertTrue(((IType)s4.getScopeBinding()).isSameType( (IType) ((IIndexScope)b3.getCompositeScope()).getScopeBinding() ));
|
||||
}
|
||||
|
||||
// class A {};
|
||||
//
|
||||
// template<typename T>
|
||||
// class X {
|
||||
// public:
|
||||
// class Y {
|
||||
// public:
|
||||
// class Z {};
|
||||
// };
|
||||
// };
|
||||
|
||||
// X<A>::Y::Z xayz;
|
||||
public void _testEnclosingScopes_c() throws Exception {
|
||||
fakeFailForSingle();
|
||||
|
||||
ICPPClassType b0= getBindingFromASTName("Y::Z x", 1, ICPPClassType.class);
|
||||
ICPPClassType b1= getBindingFromASTName("Z xayz", 1, ICPPClassType.class);
|
||||
|
||||
IScope s0= b0.getScope(), s1= b1.getScope();
|
||||
|
||||
ICPPClassScope cs0= assertInstance(s0, ICPPClassScope.class);
|
||||
assertInstance(cs0.getClassType(), ICPPClassType.class);
|
||||
assertInstance(cs0.getClassType(), ICPPSpecialization.class);
|
||||
|
||||
ICPPClassScope cs1= assertInstance(s1, ICPPClassScope.class);
|
||||
assertInstance(cs1.getClassType(), ICPPClassType.class);
|
||||
assertInstance(cs1.getClassType(), ICPPSpecialization.class);
|
||||
}
|
||||
|
||||
// class A {}; class B {};
|
||||
//
|
||||
// template<typename T1, typename T2>
|
||||
// class X {};
|
||||
//
|
||||
// template<typename T3>
|
||||
// class X<T3, A> {
|
||||
// public:
|
||||
// class N {};
|
||||
// };
|
||||
|
||||
// X<B,A>::N n;
|
||||
public void _testEnclosingScopes_d() throws Exception {
|
||||
ICPPClassType b0= getBindingFromASTName("N n", 1, ICPPClassType.class, ICPPSpecialization.class);
|
||||
ICPPClassType b1= assertInstance(((ICPPSpecialization) b0).getSpecializedBinding(), ICPPClassType.class);
|
||||
|
||||
ICPPClassScope s0= assertInstance(b0.getScope(), ICPPClassScope.class);
|
||||
assertInstance(s0.getClassType(), ICPPTemplateInstance.class);
|
||||
|
||||
ICPPClassScope s1= assertInstance(b1.getScope(), ICPPClassScope.class);
|
||||
assertInstance(s1.getClassType(), ICPPTemplateDefinition.class);
|
||||
|
||||
assertInstance(s1.getClassType().getScope(), ICPPTemplateScope.class);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue