From c663103b44ba2727e52fb02f18572279e612c311 Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Wed, 19 May 2010 02:49:01 +0000 Subject: [PATCH] Added test framework for fast tests --- .../core/test/CodanFastCxxAstTestCase.java | 191 ++++++++++++++++++ .../codan/core/test/CodanFastTestSuite.java | 2 + .../cdt/codan/core/test/CodanTestCase.java | 8 +- .../cdt/codan/core/test/TestUtils.java | 2 +- 4 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastCxxAstTestCase.java diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastCxxAstTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastCxxAstTestCase.java new file mode 100644 index 00000000000..10f221d380e --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastCxxAstTestCase.java @@ -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 codanproblems = new ArrayList(); + + 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; + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java index 84a42fdda1d..bba33cc52bc 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.codan.core.test; import junit.framework.Test; 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.ListProblemPreferenceTest; import org.eclipse.cdt.codan.core.param.MapProblemPreferenceTest; @@ -39,6 +40,7 @@ public class CodanFastTestSuite extends TestSuite { suite.addTestSuite(BasicProblemPreferenceTest.class); suite.addTestSuite(ListProblemPreferenceTest.class); suite.addTestSuite(MapProblemPreferenceTest.class); + suite.addTestSuite(CxxAstUtilsTest.class); return suite; } } diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java index cb82fb3067a..dac61fcecad 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java @@ -16,8 +16,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; -import junit.framework.AssertionFailedError; - import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.IPDOMManager; import org.eclipse.cdt.core.model.ICProject; @@ -65,6 +63,7 @@ public class CodanTestCase extends BaseTestCase { return false; } + @Override public void setUp() throws Exception { super.setUp(); removeLeftOverProjects(); @@ -72,6 +71,7 @@ public class CodanTestCase extends BaseTestCase { tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile(); } + @Override public void tearDown() throws CoreException { if (cproject != null) { try { @@ -120,7 +120,6 @@ public class CodanTestCase extends BaseTestCase { } }, null); mj.join(); - } finally { mj.dispose(); } @@ -175,9 +174,6 @@ public class CodanTestCase extends BaseTestCase { protected StringBuffer[] getContents(int sections) { try { 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(), "src", getClass(), getName(), sections); } catch (IOException e) { diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java index 5085d07c7e2..30a25788a5d 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java @@ -54,7 +54,7 @@ public class TestUtils { srcRoot + "/" + classFile); st = resource.openStream(); } else { - st = clazz.getResourceAsStream(classFile); + st = clazz.getResourceAsStream("/" + classFile); } return st; }