mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
added framework for quick fix tests and example
This commit is contained in:
parent
eea0205ab1
commit
2d7b307308
7 changed files with 216 additions and 2 deletions
|
@ -11,7 +11,13 @@ Require-Bundle: org.eclipse.core.runtime,
|
||||||
org.eclipse.cdt.core.tests,
|
org.eclipse.cdt.core.tests,
|
||||||
org.eclipse.cdt.codan.core;bundle-version="1.0.0",
|
org.eclipse.cdt.codan.core;bundle-version="1.0.0",
|
||||||
org.eclipse.cdt.codan.core.cxx;bundle-version="1.0.0",
|
org.eclipse.cdt.codan.core.cxx;bundle-version="1.0.0",
|
||||||
org.junit
|
org.eclipse.cdt.ui,
|
||||||
|
org.eclipse.jface;bundle-version="3.6.0",
|
||||||
|
org.eclipse.jface.text;bundle-version="3.6.0",
|
||||||
|
org.junit,
|
||||||
|
org.eclipse.cdt.codan.checkers.ui;bundle-version="1.0.0",
|
||||||
|
org.eclipse.cdt.codan.ui;bundle-version="1.1.0",
|
||||||
|
org.eclipse.ui.ide;bundle-version="3.6.0"
|
||||||
Bundle-ActivationPolicy: lazy
|
Bundle-ActivationPolicy: lazy
|
||||||
Bundle-RequiredExecutionEnvironment: J2SE-1.5
|
Bundle-RequiredExecutionEnvironment: J2SE-1.5
|
||||||
Bundle-Vendor: %Bundle-Vendor
|
Bundle-Vendor: %Bundle-Vendor
|
||||||
|
|
|
@ -19,6 +19,7 @@ import org.eclipse.cdt.codan.core.internal.checkers.CatchByReferenceTest;
|
||||||
import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest;
|
import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest;
|
||||||
import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerTest;
|
import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerTest;
|
||||||
import org.eclipse.cdt.codan.core.internal.checkers.SuggestedParenthesisCheckerTest;
|
import org.eclipse.cdt.codan.core.internal.checkers.SuggestedParenthesisCheckerTest;
|
||||||
|
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
|
||||||
|
|
||||||
public class AutomatedIntegrationSuite extends TestSuite {
|
public class AutomatedIntegrationSuite extends TestSuite {
|
||||||
public AutomatedIntegrationSuite() {
|
public AutomatedIntegrationSuite() {
|
||||||
|
@ -38,12 +39,16 @@ public class AutomatedIntegrationSuite extends TestSuite {
|
||||||
|
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
final AutomatedIntegrationSuite suite = new AutomatedIntegrationSuite();
|
final AutomatedIntegrationSuite suite = new AutomatedIntegrationSuite();
|
||||||
|
// checkers
|
||||||
suite.addTestSuite(StatementHasNoEffectCheckerTest.class);
|
suite.addTestSuite(StatementHasNoEffectCheckerTest.class);
|
||||||
suite.addTestSuite(SuggestedParenthesisCheckerTest.class);
|
suite.addTestSuite(SuggestedParenthesisCheckerTest.class);
|
||||||
suite.addTestSuite(ReturnCheckerTest.class);
|
suite.addTestSuite(ReturnCheckerTest.class);
|
||||||
suite.addTestSuite(CatchByReferenceTest.class);
|
suite.addTestSuite(CatchByReferenceTest.class);
|
||||||
suite.addTestSuite(AssignmentInConditionCheckerTest.class);
|
suite.addTestSuite(AssignmentInConditionCheckerTest.class);
|
||||||
|
// framework
|
||||||
suite.addTest(CodanFastTestSuite.suite());
|
suite.addTest(CodanFastTestSuite.suite());
|
||||||
|
// quick fixes
|
||||||
|
suite.addTestSuite(SuggestedParenthesisQuickFixTest.class);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ import org.eclipse.core.runtime.NullProgressMonitor;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CheckerTestCase extends CodanTestCase {
|
public class CheckerTestCase extends CodanTestCase {
|
||||||
private IMarker[] markers;
|
protected IMarker[] markers;
|
||||||
|
|
||||||
public IMarker checkErrorLine(int i) {
|
public IMarker checkErrorLine(int i) {
|
||||||
return checkErrorLine(currentFile, i);
|
return checkErrorLine(currentFile, i);
|
||||||
|
|
|
@ -18,10 +18,13 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
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.CModelException;
|
||||||
|
import org.eclipse.cdt.core.model.ICElement;
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
||||||
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
|
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
|
||||||
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||||
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.resources.IProject;
|
import org.eclipse.core.resources.IProject;
|
||||||
import org.eclipse.core.resources.IResource;
|
import org.eclipse.core.resources.IResource;
|
||||||
import org.eclipse.core.resources.IWorkspace;
|
import org.eclipse.core.resources.IWorkspace;
|
||||||
|
@ -30,6 +33,7 @@ import org.eclipse.core.resources.ResourcesPlugin;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||||
|
import org.eclipse.core.runtime.Path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: add description
|
* TODO: add description
|
||||||
|
@ -39,6 +43,8 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
protected File tmpDir;
|
protected File tmpDir;
|
||||||
protected ICProject cproject;
|
protected ICProject cproject;
|
||||||
protected File currentFile;
|
protected File currentFile;
|
||||||
|
protected ICElement currentCElem;
|
||||||
|
protected IFile currentIFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -183,6 +189,18 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public File loadcode(String code, boolean cpp) {
|
public File loadcode(String code, boolean cpp) {
|
||||||
|
String fileKey = "@file:"; //$NON-NLS-1$
|
||||||
|
int indf = code.indexOf(fileKey);
|
||||||
|
if (indf >= 0) {
|
||||||
|
int sep = code.indexOf('\n');
|
||||||
|
if (sep != -1) {
|
||||||
|
String line = code.substring(0, sep);
|
||||||
|
code = code.substring(sep + 1);
|
||||||
|
String fileName = line.substring(indf + fileKey.length())
|
||||||
|
.trim();
|
||||||
|
return loadcode(code, new File(tmpDir, fileName));
|
||||||
|
}
|
||||||
|
}
|
||||||
String ext = cpp ? ".cpp" : ".c"; //$NON-NLS-1$ //$NON-NLS-2$
|
String ext = cpp ? ".cpp" : ".c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
File testFile = null;
|
File testFile = null;
|
||||||
try {
|
try {
|
||||||
|
@ -211,10 +229,16 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
// hmm
|
// hmm
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
currentCElem = cproject
|
||||||
|
.findElement(new Path(currentFile.toString()));
|
||||||
|
currentIFile = (IFile) currentCElem.getResource();
|
||||||
return testFile;
|
return testFile;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
fail("Cannot save test: " + testFile + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
|
fail("Cannot save test: " + testFile + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
return null;
|
return null;
|
||||||
|
} catch (CModelException e) {
|
||||||
|
fail("Cannot find file: " + testFile + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,17 @@ public class TestUtils {
|
||||||
return testFile;
|
return testFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String loadFile(InputStream st) throws IOException {
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(st));
|
||||||
|
String buffer;
|
||||||
|
StringBuffer result = new StringBuffer();
|
||||||
|
while ((buffer = br.readLine()) != null) {
|
||||||
|
result.append(buffer);
|
||||||
|
}
|
||||||
|
st.close();
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param clazz
|
* @param clazz
|
||||||
* @return
|
* @return
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.internal.checkers.ui.quickfix;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
|
||||||
|
import org.eclipse.cdt.codan.core.test.TestUtils;
|
||||||
|
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
||||||
|
import org.eclipse.cdt.core.model.CModelException;
|
||||||
|
import org.eclipse.cdt.internal.ui.util.EditorUtility;
|
||||||
|
import org.eclipse.core.resources.IMarker;
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
import org.eclipse.jface.text.TextSelection;
|
||||||
|
import org.eclipse.jface.viewers.ISelection;
|
||||||
|
import org.eclipse.ui.PartInitException;
|
||||||
|
import org.eclipse.ui.PlatformUI;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: add description
|
||||||
|
*/
|
||||||
|
public abstract class QuickFixTestCase extends CheckerTestCase {
|
||||||
|
AbstractCodanCMarkerResolution quickFix;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
quickFix = createQuickFix();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected abstract AbstractCodanCMarkerResolution createQuickFix();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param code
|
||||||
|
* @param string
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected ISelection textSelection(String code, String string) {
|
||||||
|
return new TextSelection(code.indexOf(string), string.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
* @throws CModelException
|
||||||
|
* @throws PartInitException
|
||||||
|
* @throws IOException
|
||||||
|
* @throws CoreException
|
||||||
|
*/
|
||||||
|
public String runQuickFixOneFile() {
|
||||||
|
// need to load before running codan because otherwise marker is lost when doing quick fix 8[]
|
||||||
|
try {
|
||||||
|
EditorUtility.openInEditor(currentIFile);
|
||||||
|
runCodan();
|
||||||
|
doRunQuickFix();
|
||||||
|
String result = TestUtils.loadFile(currentIFile.getContents());
|
||||||
|
return result;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail(e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void doRunQuickFix() {
|
||||||
|
for (int i = 0; i < markers.length; i++) {
|
||||||
|
IMarker marker = markers[i];
|
||||||
|
quickFix.run(marker);
|
||||||
|
}
|
||||||
|
PlatformUI.getWorkbench().saveAllEditors(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param result
|
||||||
|
* @param expected
|
||||||
|
*/
|
||||||
|
public void assertContainedIn(String expected, String result) {
|
||||||
|
assertTrue(
|
||||||
|
"Text <" + expected + "> not found in <" + result + ">", result.contains(expected)); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,74 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.internal.checkers.ui.quickfix;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.codan.core.test.TestUtils;
|
||||||
|
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
||||||
|
import org.eclipse.cdt.internal.ui.util.EditorUtility;
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for quick fix for suggested parenthesis
|
||||||
|
*/
|
||||||
|
public class SuggestedParenthesisQuickFixTest extends QuickFixTestCase {
|
||||||
|
@SuppressWarnings("restriction")
|
||||||
|
@Override
|
||||||
|
public AbstractCodanCMarkerResolution createQuickFix() {
|
||||||
|
return new SuggestedParenthesisQuickFix();
|
||||||
|
}
|
||||||
|
|
||||||
|
// main() {
|
||||||
|
// int a=1,b=3;
|
||||||
|
// if (b+a && a>b || b-a) b--; // error here
|
||||||
|
// }
|
||||||
|
public void testSimple() throws IOException, CoreException {
|
||||||
|
loadcode(getAboveComment());
|
||||||
|
String result = runQuickFixOneFile();
|
||||||
|
assertContainedIn("(b+a && a>b)", result); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
// @file:header.h
|
||||||
|
// int foo();
|
||||||
|
/* ---- */
|
||||||
|
// @file:main.c
|
||||||
|
// #include "header.h"
|
||||||
|
// main() {
|
||||||
|
// foo();
|
||||||
|
// }
|
||||||
|
/*
|
||||||
|
* this test is using two files, there was not actually bugs here so
|
||||||
|
* quick fix is not called
|
||||||
|
*/
|
||||||
|
public void test2FilesExample() throws FileNotFoundException, IOException {
|
||||||
|
StringBuffer[] code = getContents(2);
|
||||||
|
File f1 = loadcode(code[0].toString());
|
||||||
|
File f2 = loadcode(code[1].toString());
|
||||||
|
// lets pretend marker is found in main.c but fixes go in both files,
|
||||||
|
// to check do something like this
|
||||||
|
try {
|
||||||
|
EditorUtility.openInEditor(f2);
|
||||||
|
runCodan();
|
||||||
|
doRunQuickFix();
|
||||||
|
String result_main = TestUtils.loadFile(new FileInputStream(f2));
|
||||||
|
String result_header = TestUtils.loadFile(new FileInputStream(f1));
|
||||||
|
assertContainedIn("foo", result_main); //$NON-NLS-1$
|
||||||
|
assertContainedIn("foo", result_header); //$NON-NLS-1$
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue