mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Added test framework for fast tests
This commit is contained in:
parent
85d6bff98e
commit
c663103b44
4 changed files with 196 additions and 7 deletions
|
@ -0,0 +1,191 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2009,2010 QNX Software Systems
|
||||||
|
* 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:
|
||||||
|
* QNX Software Systems (Alena Laskavaia) - initial API and implementation
|
||||||
|
*******************************************************************************/
|
||||||
|
package org.eclipse.cdt.codan.core.test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.codan.core.CodanRuntime;
|
||||||
|
import org.eclipse.cdt.codan.core.model.IChecker;
|
||||||
|
import org.eclipse.cdt.codan.core.model.IProblemLocation;
|
||||||
|
import org.eclipse.cdt.codan.core.model.IProblemReporter;
|
||||||
|
import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
import org.eclipse.cdt.core.dom.parser.ISourceCodeParser;
|
||||||
|
import org.eclipse.cdt.core.dom.parser.c.ANSICParserExtensionConfiguration;
|
||||||
|
import org.eclipse.cdt.core.dom.parser.c.GCCParserExtensionConfiguration;
|
||||||
|
import org.eclipse.cdt.core.dom.parser.c.ICParserExtensionConfiguration;
|
||||||
|
import org.eclipse.cdt.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
|
||||||
|
import org.eclipse.cdt.core.dom.parser.cpp.GPPParserExtensionConfiguration;
|
||||||
|
import org.eclipse.cdt.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
|
||||||
|
import org.eclipse.cdt.core.parser.FileContent;
|
||||||
|
import org.eclipse.cdt.core.parser.IScanner;
|
||||||
|
import org.eclipse.cdt.core.parser.IScannerInfo;
|
||||||
|
import org.eclipse.cdt.core.parser.NullLogService;
|
||||||
|
import org.eclipse.cdt.core.parser.ParserLanguage;
|
||||||
|
import org.eclipse.cdt.core.parser.ParserMode;
|
||||||
|
import org.eclipse.cdt.core.parser.ScannerInfo;
|
||||||
|
import org.eclipse.cdt.core.parser.tests.ast2.AST2BaseTest;
|
||||||
|
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||||
|
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
|
||||||
|
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
|
||||||
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
|
||||||
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: add description
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("restriction")
|
||||||
|
public abstract class CodanFastCxxAstTestCase extends TestCase {
|
||||||
|
IASTTranslationUnit tu;
|
||||||
|
|
||||||
|
protected String getAboveComment() {
|
||||||
|
return getContents(1)[0].toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected StringBuffer[] getContents(int sections) {
|
||||||
|
try {
|
||||||
|
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
|
||||||
|
return TestSourceReader.getContentsForTest(plugin == null ? null
|
||||||
|
: plugin.getBundle(), "src", getClass(), getName(), //$NON-NLS-1$
|
||||||
|
sections);
|
||||||
|
} catch (IOException e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCpp() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
private static final NullLogService NULL_LOG = new NullLogService();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public IASTTranslationUnit parse(String code) {
|
||||||
|
return parse(code, isCpp() ? ParserLanguage.CPP : ParserLanguage.C,
|
||||||
|
true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("restriction")
|
||||||
|
protected IASTTranslationUnit parse(String code, ParserLanguage lang,
|
||||||
|
boolean gcc) {
|
||||||
|
FileContent codeReader = FileContent.create("code.c", //$NON-NLS-1$
|
||||||
|
code.toCharArray());
|
||||||
|
IScannerInfo scannerInfo = new ScannerInfo();
|
||||||
|
IScanner scanner = AST2BaseTest.createScanner(codeReader, lang,
|
||||||
|
ParserMode.COMPLETE_PARSE, scannerInfo);
|
||||||
|
ISourceCodeParser parser2 = null;
|
||||||
|
if (lang == ParserLanguage.CPP) {
|
||||||
|
ICPPParserExtensionConfiguration config = null;
|
||||||
|
if (gcc)
|
||||||
|
config = new GPPParserExtensionConfiguration();
|
||||||
|
else
|
||||||
|
config = new ANSICPPParserExtensionConfiguration();
|
||||||
|
parser2 = new GNUCPPSourceParser(scanner,
|
||||||
|
ParserMode.COMPLETE_PARSE, NULL_LOG, config);
|
||||||
|
} else {
|
||||||
|
ICParserExtensionConfiguration config = null;
|
||||||
|
if (gcc)
|
||||||
|
config = new GCCParserExtensionConfiguration();
|
||||||
|
else
|
||||||
|
config = new ANSICParserExtensionConfiguration();
|
||||||
|
parser2 = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE,
|
||||||
|
NULL_LOG, config);
|
||||||
|
}
|
||||||
|
IASTTranslationUnit tu = parser2.parse();
|
||||||
|
if (parser2.encounteredError() && !hasCodeErrors())
|
||||||
|
fail("PARSE FAILURE"); //$NON-NLS-1$
|
||||||
|
if (!hasCodeErrors()) {
|
||||||
|
if (lang == ParserLanguage.C) {
|
||||||
|
IASTProblem[] problems = CVisitor.getProblems(tu);
|
||||||
|
assertEquals(problems.length, 0);
|
||||||
|
} else if (lang == ParserLanguage.CPP) {
|
||||||
|
IASTProblem[] problems = CPPVisitor.getProblems(tu);
|
||||||
|
assertEquals(problems.length, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tu;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override if any of code that test tried to parse has errors, otherwise
|
||||||
|
* parse method would assert
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected boolean hasCodeErrors() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProblemInstance {
|
||||||
|
String id;
|
||||||
|
IProblemLocation loc;
|
||||||
|
Object[] args;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param id
|
||||||
|
* @param loc
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public ProblemInstance(String id, IProblemLocation loc, Object[] args) {
|
||||||
|
this.id = id;
|
||||||
|
this.loc = loc;
|
||||||
|
this.args = args;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private ArrayList<ProblemInstance> codanproblems = new ArrayList<CodanFastCxxAstTestCase.ProblemInstance>();
|
||||||
|
|
||||||
|
void runCodan(String code) {
|
||||||
|
tu = parse(code);
|
||||||
|
runCodan(tu);
|
||||||
|
}
|
||||||
|
|
||||||
|
void runCodan(IASTTranslationUnit tu) {
|
||||||
|
IProblemReporter problemReporter = CodanRuntime.getInstance()
|
||||||
|
.getProblemReporter();
|
||||||
|
CodanRuntime.getInstance().setProblemReporter(new IProblemReporter() {
|
||||||
|
public void reportProblem(String problemId, IProblemLocation loc,
|
||||||
|
Object... args) {
|
||||||
|
codanproblems.add(new ProblemInstance(problemId, loc, args));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
IChecker checker = getChecker();
|
||||||
|
((IRunnableInEditorChecker) checker).processModel(tu);
|
||||||
|
} finally {
|
||||||
|
CodanRuntime.getInstance().setProblemReporter(problemReporter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract IChecker getChecker();
|
||||||
|
|
||||||
|
protected int line2offset(int linePar, String code) throws IOException {
|
||||||
|
byte[] bytes = code.getBytes();
|
||||||
|
int line = 1;
|
||||||
|
for (int j = 0; j < bytes.length; j++) {
|
||||||
|
byte c = bytes[j];
|
||||||
|
if (line >= linePar)
|
||||||
|
return j;
|
||||||
|
if (c == '\n')
|
||||||
|
line++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ package org.eclipse.cdt.codan.core.test;
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.codan.core.cxx.CxxAstUtilsTest;
|
||||||
import org.eclipse.cdt.codan.core.param.BasicProblemPreferenceTest;
|
import org.eclipse.cdt.codan.core.param.BasicProblemPreferenceTest;
|
||||||
import org.eclipse.cdt.codan.core.param.ListProblemPreferenceTest;
|
import org.eclipse.cdt.codan.core.param.ListProblemPreferenceTest;
|
||||||
import org.eclipse.cdt.codan.core.param.MapProblemPreferenceTest;
|
import org.eclipse.cdt.codan.core.param.MapProblemPreferenceTest;
|
||||||
|
@ -39,6 +40,7 @@ public class CodanFastTestSuite extends TestSuite {
|
||||||
suite.addTestSuite(BasicProblemPreferenceTest.class);
|
suite.addTestSuite(BasicProblemPreferenceTest.class);
|
||||||
suite.addTestSuite(ListProblemPreferenceTest.class);
|
suite.addTestSuite(ListProblemPreferenceTest.class);
|
||||||
suite.addTestSuite(MapProblemPreferenceTest.class);
|
suite.addTestSuite(MapProblemPreferenceTest.class);
|
||||||
|
suite.addTestSuite(CxxAstUtilsTest.class);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,6 @@ import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import junit.framework.AssertionFailedError;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.core.CCorePlugin;
|
import org.eclipse.cdt.core.CCorePlugin;
|
||||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
@ -65,6 +63,7 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
removeLeftOverProjects();
|
removeLeftOverProjects();
|
||||||
|
@ -72,6 +71,7 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile();
|
tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void tearDown() throws CoreException {
|
public void tearDown() throws CoreException {
|
||||||
if (cproject != null) {
|
if (cproject != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -120,7 +120,6 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
}, null);
|
}, null);
|
||||||
mj.join();
|
mj.join();
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
mj.dispose();
|
mj.dispose();
|
||||||
}
|
}
|
||||||
|
@ -175,9 +174,6 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
protected StringBuffer[] getContents(int sections) {
|
protected StringBuffer[] getContents(int sections) {
|
||||||
try {
|
try {
|
||||||
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
|
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
|
||||||
if (plugin == null)
|
|
||||||
throw new AssertionFailedError(
|
|
||||||
"This test must be run as a JUnit plugin test");
|
|
||||||
return TestSourceReader.getContentsForTest(plugin.getBundle(),
|
return TestSourceReader.getContentsForTest(plugin.getBundle(),
|
||||||
"src", getClass(), getName(), sections);
|
"src", getClass(), getName(), sections);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class TestUtils {
|
||||||
srcRoot + "/" + classFile);
|
srcRoot + "/" + classFile);
|
||||||
st = resource.openStream();
|
st = resource.openStream();
|
||||||
} else {
|
} else {
|
||||||
st = clazz.getResourceAsStream(classFile);
|
st = clazz.getResourceAsStream("/" + classFile);
|
||||||
}
|
}
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue