mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-31 21:05:37 +02:00
191679: add some unit tests
This commit is contained in:
parent
a032fbd206
commit
25213bca9f
4 changed files with 141 additions and 0 deletions
|
@ -23,6 +23,7 @@ import java.util.regex.Pattern;
|
|||
import junit.framework.Test;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ILinkage;
|
||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||
|
@ -57,6 +58,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
private static List toDeleteOnTearDown= new ArrayList();
|
||||
private final static String locProject1= "resources/pdomtests/generatePDOMTests/project1";
|
||||
private final static String locProject2= "resources/pdomtests/generatePDOMTests/project2";
|
||||
private final static String locProject3= "resources/pdomtests/generatePDOMTests/project3";
|
||||
private URI baseURI;
|
||||
|
||||
public static Test suite() {
|
||||
|
@ -224,6 +226,37 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI), LanguageManager.getInstance().getPDOMLinkageFactoryMappings());
|
||||
verifyProject2Content(wpdom);
|
||||
}
|
||||
|
||||
public void testExternalExportProjectProvider_CLinkage() throws Exception {
|
||||
File target= File.createTempFile("test", "pdom");
|
||||
|
||||
URL url= FileLocator.find(CTestPlugin.getDefault().getBundle(), new Path(locProject3), null);
|
||||
String baseDir= FileLocator.toFileURL(url).getFile();
|
||||
|
||||
doGenerate(new String[] {
|
||||
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
|
||||
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider5.class.getName(),
|
||||
ExternalExportProjectProvider.OPT_SOURCE, baseDir,
|
||||
ExternalExportProjectProvider.OPT_FRAGMENT_ID, "hello.world"
|
||||
});
|
||||
assertTrue(target.exists());
|
||||
|
||||
IndexFilter CLinkage= new IndexFilter() {
|
||||
public boolean acceptLinkage(ILinkage linkage) {
|
||||
return linkage.getID().equals(ILinkage.C_LINKAGE_ID);
|
||||
}
|
||||
};
|
||||
|
||||
IndexFilter CPPLinkage= new IndexFilter() {
|
||||
public boolean acceptLinkage(ILinkage linkage) {
|
||||
return linkage.getID().equals(ILinkage.CPP_LINKAGE_ID);
|
||||
}
|
||||
};
|
||||
|
||||
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI), LanguageManager.getInstance().getPDOMLinkageFactoryMappings());
|
||||
assertEquals(1, wpdom.findBindings(new char[][] {"foo".toCharArray()}, CLinkage, NPM).length);
|
||||
assertEquals(0, wpdom.findBindings(new char[][] {"foo".toCharArray()}, CPPLinkage, NPM).length);
|
||||
}
|
||||
|
||||
public void verifyProject1Content(WritablePDOM wpdom) throws Exception {
|
||||
wpdom.acquireReadLock();
|
||||
|
@ -314,6 +347,25 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
}
|
||||
public void setApplicationArguments(String[] arguments) {}
|
||||
}
|
||||
|
||||
public static class TestProjectProvider5 implements IExportProjectProvider {
|
||||
public ICProject createProject() throws CoreException {
|
||||
ICProject cproject= CProjectHelper.createCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
|
||||
toDeleteOnTearDown.add(cproject);
|
||||
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), locProject3);
|
||||
return cproject;
|
||||
}
|
||||
public Map getExportProperties() {
|
||||
Map map= new HashMap();
|
||||
map.put(SDK_VERSION, "4.0.1");
|
||||
map.put(IIndexFragment.PROPERTY_FRAGMENT_ID, ACME_SDK_ID);
|
||||
return map;
|
||||
}
|
||||
public IIndexLocationConverter getLocationConverter(ICProject cproject) {
|
||||
return new ResourceContainerRelativeLocationConverter(cproject.getProject());
|
||||
}
|
||||
public void setApplicationArguments(String[] arguments) {}
|
||||
}
|
||||
}
|
||||
|
||||
class MockApplicationContext implements IApplicationContext {
|
||||
|
|
|
@ -15,13 +15,28 @@ import java.io.File;
|
|||
|
||||
import junit.framework.Test;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ILinkage;
|
||||
import org.eclipse.cdt.core.dom.IName;
|
||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||
import org.eclipse.cdt.core.dom.ast.ICompositeType;
|
||||
import org.eclipse.cdt.core.dom.ast.IFunction;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
|
||||
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
|
||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||
import org.eclipse.cdt.core.index.IIndexLocationConverter;
|
||||
import org.eclipse.cdt.core.index.IndexFilter;
|
||||
import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
|
||||
import org.eclipse.cdt.core.language.ProjectLanguageConfiguration;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.core.model.LanguageManager;
|
||||
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
|
||||
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
|
||||
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
||||
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
|
||||
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||
import org.eclipse.cdt.internal.core.CCoreInternals;
|
||||
import org.eclipse.cdt.internal.core.index.IIndexFragment;
|
||||
import org.eclipse.cdt.internal.core.index.IWritableIndexFragment;
|
||||
|
@ -29,8 +44,14 @@ import org.eclipse.cdt.internal.core.pdom.PDOM;
|
|||
import org.eclipse.cdt.internal.core.pdom.PDOMManager;
|
||||
import org.eclipse.cdt.internal.core.pdom.WritablePDOM;
|
||||
import org.eclipse.cdt.internal.core.pdom.db.ChunkCache;
|
||||
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IFolder;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.core.runtime.Platform;
|
||||
import org.eclipse.core.runtime.content.IContentType;
|
||||
|
||||
/**
|
||||
* Tests bugs found in the PDOM
|
||||
|
@ -174,4 +195,57 @@ public class PDOMBugsTest extends BaseTestCase {
|
|||
pdom.acquireWriteLock();
|
||||
pdom.releaseWriteLock();
|
||||
}
|
||||
|
||||
public void _test191679() throws Exception {
|
||||
IProject project= cproject.getProject();
|
||||
IFolder cHeaders= cproject.getProject().getFolder("cHeaders");
|
||||
cHeaders.create(true, true, NPM);
|
||||
LanguageManager lm= LanguageManager.getInstance();
|
||||
|
||||
IFile cHeader= TestSourceReader.createFile(cHeaders, "cHeader.h", "struct S {int a; int b};\nvoid foo(struct S) {}\n");
|
||||
ICProjectDescription pd= CCorePlugin.getDefault().getProjectDescription(project);
|
||||
ICConfigurationDescription cfgd= pd.getDefaultSettingConfiguration();
|
||||
ProjectLanguageConfiguration plc= LanguageManager.getInstance().getLanguageConfiguration(project);
|
||||
plc.addFileMapping(cfgd, cHeader, GCCLanguage.ID);
|
||||
IContentType ct= Platform.getContentTypeManager().getContentType(CCorePlugin.CONTENT_TYPE_CHEADER);
|
||||
lm.storeLanguageMappingConfiguration(project, new IContentType[] {ct});
|
||||
|
||||
IFile cppSource= TestSourceReader.createFile(cHeaders, "cppSource.cpp", "struct S s; void ref() {foo(s);}");
|
||||
|
||||
IndexerPreferences.set(project, IndexerPreferences.KEY_INDEXER_ID, IPDOMManager.ID_FAST_INDEXER);
|
||||
CCorePlugin.getIndexManager().reindex(cproject);
|
||||
CCorePlugin.getIndexManager().joinIndexer(10000, NPM);
|
||||
|
||||
final PDOM pdom= (PDOM) CCoreInternals.getPDOMManager().getPDOM(cproject);
|
||||
pdom.acquireReadLock();
|
||||
try {
|
||||
{ // test reference to 'foo' was resolved correctly
|
||||
IIndexBinding[] ib= pdom.findBindings(new char[][]{"foo".toCharArray()}, IndexFilter.ALL, NPM);
|
||||
assertEquals(1, ib.length);
|
||||
|
||||
assertTrue(ib[0] instanceof IFunction);
|
||||
assertTrue(!(ib[0] instanceof ICPPBinding));
|
||||
|
||||
IName[] nms= pdom.findNames(ib[0], IIndexFragment.FIND_REFERENCES);
|
||||
assertEquals(1, nms.length);
|
||||
assertTrue(nms[0].getFileLocation().getFileName().endsWith(".cpp"));
|
||||
}
|
||||
|
||||
{ // test struct S has resolved to the C linkage
|
||||
IIndexBinding[] ib= pdom.findBindings(new char[][]{{'s'}}, IndexFilter.ALL, NPM);
|
||||
assertEquals(1, ib.length);
|
||||
|
||||
assertTrue(ib[0] instanceof ICPPVariable);
|
||||
ICPPVariable cppv= (ICPPVariable) ib[0];
|
||||
|
||||
IType type= cppv.getType();
|
||||
assertTrue(type instanceof ICompositeType);
|
||||
assertTrue(((ICompositeType) type).getKey() == ICompositeType.k_struct);
|
||||
assertTrue(((ICompositeType) type).getLinkage().getID().equals(ILinkage.C_LINKAGE_ID));
|
||||
}
|
||||
} finally {
|
||||
pdom.releaseReadLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,6 +44,9 @@
|
|||
<ExportProjectProvider
|
||||
class="org.eclipse.cdt.internal.pdom.tests.GeneratePDOMApplicationTest$TestProjectProvider4">
|
||||
</ExportProjectProvider>
|
||||
<ExportProjectProvider
|
||||
class="org.eclipse.cdt.internal.pdom.tests.GeneratePDOMApplicationTest$TestProjectProvider5">
|
||||
</ExportProjectProvider>
|
||||
</extension>
|
||||
|
||||
<extension
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
// this is a C content type header
|
||||
|
||||
void foo() {}
|
||||
|
||||
int a;
|
||||
|
||||
struct S {
|
||||
int c;
|
||||
int d;
|
||||
};
|
||||
|
||||
|
Loading…
Add table
Reference in a new issue