mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-24 17:35:35 +02:00
Added test for problem argument
This commit is contained in:
parent
dc1e7535ae
commit
f4f2bfd6ee
3 changed files with 47 additions and 95 deletions
|
@ -1,89 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2009 Alena Laskavaia
|
|
||||||
* 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:
|
|
||||||
* Alena Laskavaia - initial API and implementation
|
|
||||||
* Felipe Martinez - ExpressionRequiredInReturnCheckerTest implementation
|
|
||||||
*******************************************************************************/
|
|
||||||
package org.eclipse.cdt.codan.core.internal.checkers;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for {@see ExpressionRequiredInReturnCheckerTest} class
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ExpressionRequiredInReturnCheckerTest extends CheckerTestCase {
|
|
||||||
|
|
||||||
|
|
||||||
// dummy() {
|
|
||||||
// return; // error here on line 2
|
|
||||||
// }
|
|
||||||
public void testDummyFunction() {
|
|
||||||
loadCodeAndRun(getAboveComment());
|
|
||||||
checkNoErrors(); // because return type if not defined, usually people don't care
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// void void_function(void) {
|
|
||||||
// return; // no error here
|
|
||||||
// }
|
|
||||||
public void testVoidFunction() {
|
|
||||||
loadCodeAndRun(getAboveComment());
|
|
||||||
checkNoErrors();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// int integer_return_function(void) {
|
|
||||||
// if (global) {
|
|
||||||
// if (global == 100) {
|
|
||||||
// return; // error here on line 4
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
public void testBasicTypeFunction() {
|
|
||||||
loadCodeAndRun(getAboveComment());
|
|
||||||
checkErrorLine(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// struct My_Struct {
|
|
||||||
// int a;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// struct My_Struct struct_return_function(void) {
|
|
||||||
// return; // error here on line 6
|
|
||||||
// }
|
|
||||||
public void testUserDefinedFunction() {
|
|
||||||
loadCodeAndRun(getAboveComment());
|
|
||||||
checkErrorLine(6);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// typedef unsigned int uint8_t;
|
|
||||||
//
|
|
||||||
// uint8_t return_typedef(void) {
|
|
||||||
// return; // error here on line 4
|
|
||||||
// }
|
|
||||||
public void testTypedefReturnFunction() {
|
|
||||||
loadCodeAndRun(getAboveComment());
|
|
||||||
checkErrorLine(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// typedef unsigned int uint8_t;
|
|
||||||
//
|
|
||||||
// uint8_t (*return_fp_no_typedef(void))(void)
|
|
||||||
// {
|
|
||||||
// return; // error here on line 5
|
|
||||||
// }
|
|
||||||
public void testFunctionPointerReturnFunction() {
|
|
||||||
loadCodeAndRun(getAboveComment());
|
|
||||||
checkErrorLine(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -16,6 +16,7 @@ import java.io.IOException;
|
||||||
import org.eclipse.cdt.codan.core.param.IProblemPreference;
|
import org.eclipse.cdt.codan.core.param.IProblemPreference;
|
||||||
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
|
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
|
||||||
import org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectChecker;
|
import org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectChecker;
|
||||||
|
import org.eclipse.core.resources.IMarker;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for {@see StatementHasNoEffectChecker} class
|
* Test for {@see StatementHasNoEffectChecker} class
|
||||||
|
@ -152,6 +153,17 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
||||||
checkErrorLine(4);
|
checkErrorLine(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#define FUNC(a) a
|
||||||
|
// main() {
|
||||||
|
// int x;
|
||||||
|
// FUNC(x); // error
|
||||||
|
// }
|
||||||
|
public void testMessageInMacro() throws IOException {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
IMarker m = checkErrorLine(4);
|
||||||
|
assertMessageMatch("'FUNC(x)'", m); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
//#define FUNC(a) a
|
//#define FUNC(a) a
|
||||||
// main() {
|
// main() {
|
||||||
// int a;
|
// int a;
|
||||||
|
@ -166,4 +178,14 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
||||||
loadCodeAndRun(getAboveComment());
|
loadCodeAndRun(getAboveComment());
|
||||||
checkNoErrors();
|
checkNoErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// main() {
|
||||||
|
// int a;
|
||||||
|
// +a; // error here on line 3
|
||||||
|
// }
|
||||||
|
public void testMessage() throws IOException {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
IMarker m = checkErrorLine(3);
|
||||||
|
assertMessageMatch("'\\+a'", m); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,22 +29,24 @@ import org.eclipse.core.runtime.NullProgressMonitor;
|
||||||
public class CheckerTestCase extends CodanTestCase {
|
public class CheckerTestCase extends CodanTestCase {
|
||||||
private IMarker[] markers;
|
private IMarker[] markers;
|
||||||
|
|
||||||
public void checkErrorLine(int i) {
|
public IMarker checkErrorLine(int i) {
|
||||||
checkErrorLine(currentFile, i);
|
return checkErrorLine(currentFile, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param expectedLine
|
* @param expectedLine
|
||||||
* - line
|
* - line
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
public void checkErrorLine(File file, int expectedLine) {
|
public IMarker checkErrorLine(File file, int expectedLine) {
|
||||||
assertTrue(markers != null);
|
assertTrue(markers != null);
|
||||||
assertTrue("No problems found but should", markers.length > 0); //$NON-NLS-1$
|
assertTrue("No problems found but should", markers.length > 0); //$NON-NLS-1$
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
Integer line = null;
|
Integer line = null;
|
||||||
String mfile = null;
|
String mfile = null;
|
||||||
|
IMarker m = null;
|
||||||
for (int j = 0; j < markers.length; j++) {
|
for (int j = 0; j < markers.length; j++) {
|
||||||
IMarker m = markers[j];
|
m = markers[j];
|
||||||
Object pos;
|
Object pos;
|
||||||
try {
|
try {
|
||||||
line = (Integer) m.getAttribute(IMarker.LINE_NUMBER);
|
line = (Integer) m.getAttribute(IMarker.LINE_NUMBER);
|
||||||
|
@ -70,6 +72,8 @@ public class CheckerTestCase extends CodanTestCase {
|
||||||
if (file != null)
|
if (file != null)
|
||||||
assertEquals(file.getName(), mfile);
|
assertEquals(file.getName(), mfile);
|
||||||
assertTrue(found);
|
assertTrue(found);
|
||||||
|
assertNotNull(m);
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkNoErrors() {
|
public void checkNoErrors() {
|
||||||
|
@ -123,12 +127,27 @@ public class CheckerTestCase extends CodanTestCase {
|
||||||
* @param paramId
|
* @param paramId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected IProblemPreference getPreference(String problemId,
|
protected IProblemPreference getPreference(String problemId, String paramId) {
|
||||||
String paramId) {
|
|
||||||
IProblem problem = CodanRuntime.getInstance().getChechersRegistry()
|
IProblem problem = CodanRuntime.getInstance().getChechersRegistry()
|
||||||
.getWorkspaceProfile().findProblem(problemId);
|
.getWorkspaceProfile().findProblem(problemId);
|
||||||
IProblemPreference pref = ((MapProblemPreference) problem
|
IProblemPreference pref = ((MapProblemPreference) problem
|
||||||
.getPreference()).getChildDescriptor(paramId);
|
.getPreference()).getChildDescriptor(paramId);
|
||||||
return pref;
|
return pref;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string
|
||||||
|
* @param m
|
||||||
|
*/
|
||||||
|
public void assertMessageMatch(String pattern, IMarker m) {
|
||||||
|
try {
|
||||||
|
String attribute = (String) m.getAttribute(IMarker.MESSAGE);
|
||||||
|
if (attribute.matches(pattern)) {
|
||||||
|
fail("Expected " + attribute + " to match with /" + pattern //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
+ "/"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
} catch (CoreException e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue