1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Added OneSourceMultipleHeadersTestCase class.

This commit is contained in:
Sergey Prigogin 2012-10-16 14:20:23 -07:00
parent d5f793f01d
commit d763578079
4 changed files with 198 additions and 46 deletions

View file

@ -273,7 +273,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
} }
} }
interface ITestStrategy { protected interface ITestStrategy {
IIndex getIndex(); IIndex getIndex();
void setUp() throws Exception; void setUp() throws Exception;
void tearDown() throws Exception; void tearDown() throws Exception;
@ -467,7 +467,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
* to put the contents of the section to. To request the AST of a file, put an asterisk after * to put the contents of the section to. To request the AST of a file, put an asterisk after
* the file name. * the file name.
*/ */
class SinglePDOMTestNamedFilesStrategy implements ITestStrategy { protected class SinglePDOMTestNamedFilesStrategy implements ITestStrategy {
private IIndex index; private IIndex index;
private ICProject cproject; private ICProject cproject;
private StringBuilder[] testData; private StringBuilder[] testData;

View file

@ -240,7 +240,7 @@ public class BaseTestCase extends TestCase {
* in the log should fail the test. If the logged number of non-OK status objects * in the log should fail the test. If the logged number of non-OK status objects
* differs from the last value passed, the test is failed. If this method is not called * differs from the last value passed, the test is failed. If this method is not called
* at all, the expected number defaults to zero. * at all, the expected number defaults to zero.
* @param value * @param count the expected number of logged error and warning messages
*/ */
public void setExpectedNumberOfLoggedNonOKStatusObjects(int count) { public void setExpectedNumberOfLoggedNonOKStatusObjects(int count) {
fExpectedLoggedNonOK= count; fExpectedLoggedNonOK= count;

View file

@ -0,0 +1,113 @@
/*******************************************************************************
* Copyright (c) 2012 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.core.testplugin.util;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.pdom.tests.PDOMPrettyPrinter;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
/**
* Base class for tests that use AST. The files in the test project are created from the comments
* preceding the test case. The test project will contain a single source file called source.cpp or
* source.c, depending on whether the project is for C++ or C, and zero or more header files called
* header1.h, header2.h, etc. The AST is created for the source file only and can be obtained
* by calling getAst().
*/
public class OneSourceMultipleHeadersTestCase extends BaseTestCase {
private static final boolean DEBUG = false;
private final TestSourceReader testSourceReader;
private final boolean cpp;
private IIndex index;
private ICProject cproject;
private StringBuilder[] testData;
private IASTTranslationUnit ast;
public OneSourceMultipleHeadersTestCase(TestSourceReader testSourceReader, boolean cpp) {
this(null, testSourceReader, cpp);
}
public OneSourceMultipleHeadersTestCase(String name, TestSourceReader testSourceReader,
boolean cpp) {
super(name);
this.testSourceReader = testSourceReader;
this.cpp = cpp;
}
protected ICProject getCProject() {
return cproject;
}
protected IIndex getIndex() {
return index;
}
protected StringBuilder[] getTestData() {
return testData;
}
protected IASTTranslationUnit getAst() {
return ast;
}
public String getAstSource() {
return testData[testData.length - 1].toString();
}
@Override
protected void setUp() throws Exception {
cproject = cpp ?
CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) :
CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER);
testData = testSourceReader.getContentsForTest(getName());
if (testData.length > 0) {
for (int i = 0; i < testData.length - 1; i++) {
String filename = String.format("header%d.h", i + 1);
IFile file = TestSourceReader.createFile(cproject.getProject(), new Path(filename), testData[i].toString());
CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
}
}
IFile cppfile= TestSourceReader.createFile(cproject.getProject(), new Path("source.c" + (cpp ? "pp" : "")), getAstSource());
waitForIndexer(cproject);
if (DEBUG) {
System.out.println("Project PDOM: " + getName());
((PDOM) CCoreInternals.getPDOMManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter());
}
index= CCorePlugin.getIndexManager().getIndex(cproject);
index.acquireReadLock();
ast = TestSourceReader.createIndexBasedAST(index, cproject, cppfile);
}
@Override
protected void tearDown() throws Exception {
if (index != null) {
index.releaseReadLock();
}
if (cproject != null) {
cproject.getProject().delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
}
}
}

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2010 Wind River Systems, Inc. and others. * Copyright (c) 2006, 2012 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Markus Schorn - initial API and implementation * Markus Schorn - initial API and implementation
* Andrew Ferguson (Symbian) * Andrew Ferguson (Symbian)
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.testplugin.util; package org.eclipse.cdt.core.testplugin.util;
@ -57,23 +58,61 @@ import org.osgi.framework.Bundle;
* Utilities for reading test source code from plug-in .java sources * Utilities for reading test source code from plug-in .java sources
*/ */
public class TestSourceReader { public class TestSourceReader {
private final Bundle bundle;
private final String srcRoot;
private final Class clazz;
private final int numSections;
/**
* @param bundle the bundle containing the source, if {@code null} can try to load using
* classpath (source folder has to be in the classpath for this to work)
* @param srcRoot the directory inside the bundle containing the packages
* @param clazz the name of the class containing the test
*/
public TestSourceReader(Bundle bundle, String srcRoot, Class clazz) {
this(bundle, srcRoot, clazz, 0);
}
/**
* @param bundle the bundle containing the source, if {@code null} can try to load using
* classpath (source folder has to be in the classpath for this to work)
* @param srcRoot the directory inside the bundle containing the packages
* @param clazz the name of the class containing the test
* @param numSections the number of comment sections preceding the named test to return.
* Pass zero to get all available sections.
*/
public TestSourceReader(Bundle bundle, String srcRoot, Class clazz, int numSections) {
this.bundle = bundle;
this.srcRoot = srcRoot;
this.clazz = clazz;
this.numSections = numSections;
}
public StringBuilder[] getContentsForTest(final String testName) throws IOException {
return getContentsForTest(bundle, srcRoot, clazz, testName, numSections);
}
public String readTaggedComment(String tag) throws IOException {
return readTaggedComment(bundle, tag, clazz, tag);
}
/** /**
* Returns an array of StringBuilder objects for each comment section found preceding the named * Returns an array of StringBuilder objects for each comment section found preceding the named
* test in the source code. * test in the source code.
* *
* @param bundle the bundle containing the source, if null can try to load using classpath * @param bundle the bundle containing the source, if {@code null} can try to load using
* (source folder has to be in the classpath for this to work) * classpath (source folder has to be in the classpath for this to work)
* @param srcRoot the directory inside the bundle containing the packages * @param srcRoot the directory inside the bundle containing the packages
* @param clazz the name of the class containing the test * @param clazz the name of the class containing the test
* @param testName the name of the test * @param testName the name of the test
* @param sections the number of comment sections preceding the named test to return. Pass zero * @param numSections the number of comment sections preceding the named test to return.
* to get all available sections. * Pass zero to get all available sections.
* @return an array of StringBuilder objects for each comment section found preceding the named * @return an array of StringBuilder objects for each comment section found preceding the named
* test in the source code. * test in the source code.
* @throws IOException * @throws IOException
*/ */
public static StringBuilder[] getContentsForTest(Bundle bundle, String srcRoot, Class clazz, public static StringBuilder[] getContentsForTest(Bundle bundle, String srcRoot, Class clazz,
final String testName, int sections) throws IOException { final String testName, int numSections) throws IOException {
// Walk up the class inheritance chain until we find the test method. // Walk up the class inheritance chain until we find the test method.
try { try {
while (clazz.getMethod(testName).getDeclaringClass() != clazz) { while (clazz.getMethod(testName).getDeclaringClass() != clazz) {
@ -120,7 +159,7 @@ public class TestSourceReader {
} else { } else {
if (!line.startsWith("@") && content.length() > 0) { if (!line.startsWith("@") && content.length() > 0) {
contents.add(content); contents.add(content);
if (sections > 0 && contents.size() == sections + 1) if (numSections > 0 && contents.size() == numSections + 1)
contents.remove(0); contents.remove(0);
content = new StringBuilder(); content = new StringBuilder();
} }